Skip to content

Commit

Permalink
Customer Match User List Action (#2109)
Browse files Browse the repository at this point in the history
* New Action + properties

* Create and get audience functions

* perform methods

* Add dev token to requests

* Better error handling

* Add other contact fields

* Fix addressInfo

* Add smartHash function

* Move app_id to audience setting

* Add retlOnMappingSave hook

* Fixes

* Patch browser build fail

* add dynamic field

* Fixes

* Add syncMode

* Fix

* Refresh token before audience calls

* Fix hook output

* Fixes

* Add unit test

* Mess with hook

* Add comment about e164
  • Loading branch information
maryamsharif authored and harsh-joshi99 committed Aug 16, 2024
1 parent 6fe7b38 commit 48b35d8
Show file tree
Hide file tree
Showing 10 changed files with 1,028 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/browser-destinations/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const unobfuscatedOutput = {
mainFields: ['exports', 'module', 'browser', 'main'],
extensions: ['.ts', '.js'],
fallback: {
vm: require.resolve('vm-browserify')
vm: require.resolve('vm-browserify'),
crypto: false
}
},
devServer: {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/hashing-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Checks if value is already hashed with sha256 to avoid double hashing
*/
import * as crypto from 'crypto'

const sha256HashedRegex = /^[a-f0-9]{64}$/i

export function sha256SmartHash(value: string): string {
if (sha256HashedRegex.test(value)) {
return value
}
return crypto.createHash('sha256').update(value).digest('hex')
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
export { get } from './get'
export { omit } from './omit'
export { removeUndefined } from './remove-undefined'
export { sha256SmartHash } from './hashing-utils'
export { time, duration } from './time'

export { realTypeOf, isObject, isArray, isString } from './real-type-of'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import GoogleEnhancedConversions from '../index'
import { API_VERSION } from '../functions'

const testDestination = createTestIntegration(GoogleEnhancedConversions)
const timestamp = new Date('Thu Jun 10 2021 11:08:04 GMT-0700 (Pacific Daylight Time)').toISOString()
const customerId = '1234'

describe('GoogleEnhancedConversions', () => {
describe('userList', () => {
it('sends an event with default mappings', async () => {
const event = createTestEvent({
timestamp,
event: 'Audience Entered',
properties: {
gclid: '54321',
email: 'test@gmail.com',
orderId: '1234',
phone: '1234567890',
firstName: 'Jane',
lastName: 'Doe',
currency: 'USD',
value: '123',
address: {
street: '123 Street SW',
city: 'San Diego',
state: 'CA',
postalCode: '982004'
}
}
})

nock(`https://googleads.googleapis.com/${API_VERSION}/customers/${customerId}/offlineUserDataJobs:create`)
.post(/.*/)
.reply(200, { data: 'offlineDataJob' })

nock(`https://googleads.googleapis.com/${API_VERSION}/offlineDataJob:addOperations`)
.post(/.*/)
.reply(200, { data: 'offlineDataJob' })

nock(`https://googleads.googleapis.com/${API_VERSION}/offlineDataJob:run`)
.post(/.*/)
.reply(200, { data: 'offlineDataJob' })

const responses = await testDestination.testAction('userList', {
event,
mapping: {
ad_user_data_consent_state: 'GRANTED',
ad_personalization_consent_state: 'GRANTED',
external_audience_id: '1234',
retlOnMappingSave: {
outputs: {
id: '1234',
name: 'Test List',
external_id_type: 'CONTACT_INFO'
}
}
},
useDefaultMappings: true,
settings: {
customerId
}
})

expect(responses.length).toEqual(3)
expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"job\\":{\\"type\\":\\"CUSTOMER_MATCH_USER_LIST\\",\\"customerMatchUserListMetadata\\":{\\"userList\\":\\"customers/1234/userLists/1234\\",\\"consent\\":{\\"adUserData\\":\\"GRANTED\\",\\"adPersonalization\\":\\"GRANTED\\"}}}}"`
)
expect(responses[1].options.body).toMatchInlineSnapshot(
`"{\\"operations\\":[{\\"create\\":{\\"userIdentifiers\\":[{\\"hashedEmail\\":\\"87924606b4131a8aceeeae8868531fbb9712aaa07a5d3a756b26ce0f5d6ca674\\"},{\\"hashedPhoneNumber\\":\\"422ce82c6fc1724ac878042f7d055653ab5e983d186e616826a72d4384b68af8\\"},{\\"addressInfo\\":{\\"hashedFirstName\\":\\"4f23798d92708359b734a18172c9c864f1d48044a754115a0d4b843bca3a5332\\",\\"hashedLastName\\":\\"fd53ef835b15485572a6e82cf470dcb41fd218ae5751ab7531c956a2a6bcd3c7\\",\\"countryCode\\":\\"\\",\\"postalCode\\":\\"\\"}}]}}],\\"enable_warnings\\":true}"`
)
})
})
})
Loading

0 comments on commit 48b35d8

Please sign in to comment.