:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(cdp): fall back to hashed identifiers in Google Ads destination (#72560)
posthog[bot]f6684081995c
diff --git a/nodejs/src/cdp/templates/_destinations/google_ads/google.template.test.ts b/nodejs/src/cdp/templates/_destinations/google_ads/google.template.test.tsindex dc35d5364dab..04e6f94c65f9 100644--- a/nodejs/src/cdp/templates/_destinations/google_ads/google.template.test.ts+++ b/nodejs/src/cdp/templates/_destinations/google_ads/google.template.test.ts@@ -59,7 +59,7 @@ describe('google template', () => {expect(response.finished).toEqual(false)expect(response.invocation.queueParameters).toMatchInlineSnapshot(`{- "body": "{"conversions":[{"gclid":"google-id","conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00","conversion_value":"100","currency_code":"USD","order_id":"1234567890"}],"partialFailure":true}",+ "body": "{"conversions":[{"conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00","gclid":"google-id","user_identifiers":[{"hashed_email":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"}],"conversion_value":"100","currency_code":"USD","order_id":"1234567890"}],"partialFailure":true}","headers": {"Authorization": "Bearer access-token","Content-Type": "application/json",@@ -97,7 +97,7 @@ describe('google template', () => {expect(response.finished).toEqual(false)expect(response.invocation.queueParameters).toMatchInlineSnapshot(`{- "body": "{"conversions":[{"gclid":"google-id","conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00"}],"partialFailure":true}",+ "body": "{"conversions":[{"conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00","gclid":"google-id","user_identifiers":[{"hashed_email":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"}]}],"partialFailure":true}","headers": {"Authorization": "Bearer access-token","Content-Type": "application/json",@@ -136,7 +136,7 @@ describe('google template', () => {expect(response.finished).toEqual(false)expect(response.invocation.queueParameters).toMatchInlineSnapshot(`{- "body": "{"conversions":[{"gclid":"google-id","conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00"}],"partialFailure":true}",+ "body": "{"conversions":[{"conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00","gclid":"google-id","user_identifiers":[{"hashed_email":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"}]}],"partialFailure":true}","headers": {"Authorization": "Bearer access-token","Content-Type": "application/json",@@ -159,7 +159,7 @@ describe('google template', () => {)})- it('handles missing gclid', async () => {+ it('falls back to hashed identifiers when gclid is missing', async () => {const response = await tester.invokeMapping('Conversion',{@@ -175,12 +175,60 @@ describe('google template', () => {gclid: null,},},+ }),+ {+ phone: '{person.properties.phone}',+ }+ )++ expect(response.error).toBeUndefined()+ expect(response.finished).toEqual(false)+ expect(response.invocation.queueParameters).toMatchInlineSnapshot(`+ {+ "body": "{"conversions":[{"conversion_action":"customers/1231231234/conversionActions/123456789","conversion_date_time":"2025-01-01 00:00:00+00:00","user_identifiers":[{"hashed_email":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"},{"hashed_phone_number":"422ce82c6fc1724ac878042f7d055653ab5e983d186e616826a72d4384b68af8"}]}],"partialFailure":true}",+ "headers": {+ "Authorization": "Bearer access-token",+ "Content-Type": "application/json",+ "login-customer-id": "5675675678",+ },+ "method": "POST",+ "type": "fetch",+ "url": "https://googleads.googleapis.com/v24/customers/1231231234:uploadClickConversions",+ }+ `)++ const fetchResponse = await tester.invokeFetchResponse(response.invocation, {+ status: 200,+ body: { status: 'OK' },+ })++ expect(fetchResponse.finished).toBe(true)+ expect(fetchResponse.error).toBeUndefined()+ })++ it('skips when neither gclid nor user identifiers are present', async () => {+ const response = await tester.invokeMapping(+ 'Conversion',+ {+ oauth: {+ access_token: 'access-token',+ },+ customerId: '1231231234/5675675678',+ conversionActionId: '123456789',+ },+ createAdDestinationPayload({+ person: {+ properties: {+ gclid: null,+ email: null,+ },+ },}))expect(response.logs.filter((log) => log.level === 'info').map((log) => log.message)).toMatchInlineSnapshot(`[- "Empty \`gclid\`. Skipping...",+ "No \`gclid\` or user identifiers. Skipping...",]`)expect(response.finished).toEqual(true)diff --git a/nodejs/src/cdp/templates/_destinations/google_ads/google.template.ts b/nodejs/src/cdp/templates/_destinations/google_ads/google.template.tsindex fad2fa3d77aa..ef70f6c82a6a 100644--- a/nodejs/src/cdp/templates/_destinations/google_ads/google.template.ts+++ b/nodejs/src/cdp/templates/_destinations/google_ads/google.template.ts@@ -20,10 +20,11 @@ const build_inputs = (): HogFunctionInputSchemaType[] => {key: 'gclid',type: 'string',label: 'Google Click ID (gclid)',- description: 'The Google click ID (gclid) associated with this conversion.',+ description:+ 'The Google click ID (gclid) associated with this conversion. Leave blank to attribute via hashed email/phone instead.',default: '{person.properties.gclid ?? person.properties.$initial_gclid}',secret: false,- required: true,+ required: false,},{key: 'conversionDateTime',@@ -64,6 +65,26 @@ const build_inputs = (): HogFunctionInputSchemaType[] => {secret: false,required: false,},+ {+ key: 'email',+ type: 'string',+ label: 'Email address',+ description:+ 'Email address for enhanced conversions, used when no gclid is available. Sent SHA-256 hashed; leave blank to omit. Normalize (lowercase, trimmed) for best match rates.',+ default: '{person.properties.email}',+ secret: false,+ required: false,+ },+ {+ key: 'phone',+ type: 'string',+ label: 'Phone number',+ description:+ 'Phone number for enhanced conversions, used when no gclid is available. Sent SHA-256 hashed; leave blank to omit. Normalize to E.164 format (e.g. +14255551234) for best match rates.',+ default: '',+ secret: false,+ required: false,+ },]}@@ -78,30 +99,42 @@ export const template: HogFunctionTemplate = {category: ['Advertisement'],code_language: 'hog',code: `-if (empty(inputs.gclid)) {- print('Empty \`gclid\`. Skipping...')- return+let userIdentifiers := []+if (not empty(inputs.email)) {+ userIdentifiers := arrayPushBack(userIdentifiers, {'hashed_email': sha256Hex(lower(trim(inputs.email)))})+}+if (not empty(inputs.phone)) {+ userIdentifiers := arrayPushBack(userIdentifiers, {'hashed_phone_number': sha256Hex(lower(trim(inputs.phone)))})}-let body := {- 'conversions': [- {- 'gclid': inputs.gclid,- 'conversion_action': f'customers/{splitByString('/', inputs.customerId)[1]}/conversionActions/{inputs.conversionActionId}',- 'conversion_date_time': inputs.conversionDateTime- }- ],- 'partialFailure': true+if (empty(inputs.gclid) and empty(userIdentifiers)) {+ print('No \`gclid\` or user identifiers. Skipping...')+ return}+let conversion := {+ 'conversion_action': f'customers/{splitByString('/', inputs.customerId)[1]}/conversionActions/{inputs.conversionActionId}',+ 'conversion_date_time': inputs.conversionDateTime+}+if (not empty(inputs.gclid)) {+ conversion.gclid := inputs.gclid+}+if (not empty(userIdentifiers)) {+ conversion.user_identifiers := userIdentifiers+}if (not empty(inputs.conversionValue)) {- body.conversions[1].conversion_value := inputs.conversionValue+ conversion.conversion_value := inputs.conversionValue}if (not empty(inputs.currencyCode)) {- body.conversions[1].currency_code := inputs.currencyCode+ conversion.currency_code := inputs.currencyCode}if (not empty(inputs.orderId)) {- body.conversions[1].order_id := inputs.orderId+ conversion.order_id := inputs.orderId+}++let body := {+ 'conversions': [conversion],+ 'partialFailure': true}let res := fetch(f'https://googleads.googleapis.com/v24/customers/{splitByString('/', inputs.customerId)[1]}:uploadClickConversions', {