Skip to content

: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.

fix(cdp): match microsoft ads conversions on email or phone without a click id (#74250)

Luke Belton8761078299d5
diff --git a/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.test.ts b/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.test.ts
index 920322c402cb..635b54f669bc 100644
--- a/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.test.ts
+++ b/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.test.ts
@@ -1,5 +1,7 @@
import { DateTime, Settings } from 'luxon'
+import { parseJSON } from '~/common/utils/json-parse'
+
import { TemplateTester, createAdDestinationPayload } from '../../test/test-helpers'
import { template } from './microsoft.template'
@@ -27,7 +29,7 @@ describe('microsoft template', () => {
expect(response.finished).toEqual(false)
expect(response.invocation.queueParameters).toMatchInlineSnapshot(`
{
- "body": "{"data":[{"eventType":"custom","eventName":"purchase","eventTime":1735689600,"userData":{"msclkid":"microsoft-id","em":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"},"eventId":"event-id","customData":{"value":100,"currency":"USD"}}]}",
+ "body": "{"data":[{"eventType":"custom","eventName":"purchase","eventTime":1735689600,"userData":{"msclkid":"microsoft-id","em":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3","ph":"422ce82c6fc1724ac878042f7d055653ab5e983d186e616826a72d4384b68af8"},"eventId":"event-id","customData":{"value":100,"currency":"USD"}}]}",
"headers": {
"Authorization": "Bearer api-token",
"Content-Type": "application/json",
@@ -51,7 +53,7 @@ describe('microsoft template', () => {
expect(response.finished).toEqual(false)
expect(response.invocation.queueParameters).toMatchInlineSnapshot(`
{
- "body": "{"data":[{"eventType":"custom","eventName":"purchase","eventTime":1735689600,"userData":{"msclkid":"microsoft-id","em":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3"},"eventId":"event-id"}]}",
+ "body": "{"data":[{"eventType":"custom","eventName":"purchase","eventTime":1735689600,"userData":{"msclkid":"microsoft-id","em":"3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3","ph":"422ce82c6fc1724ac878042f7d055653ab5e983d186e616826a72d4384b68af8"},"eventId":"event-id"}]}",
"headers": {
"Authorization": "Bearer api-token",
"Content-Type": "application/json",
@@ -82,16 +84,39 @@ describe('microsoft template', () => {
`"Error from capi.uet.microsoft.com (status 401): {'error': {'code': 'Unauthorized', 'message': 'You are not authorized to access this resource.'}}"`
)
})
- it('skips when microsoftClickId is missing', async () => {
+ // Any one identifier is enough for Microsoft to attribute, so none of them may gate the send
+ it.each([
+ ['click id', { email: null, phone: null }, { msclkid: 'microsoft-id' }],
+ [
+ 'email',
+ { msclkid: null, phone: null },
+ { em: '3d4eee8538a4bbbe2ef7912f90ee494c1280f74dd7fd81232e58deb9cb9997e3' },
+ ],
+ [
+ 'phone',
+ { msclkid: null, email: null },
+ { ph: '422ce82c6fc1724ac878042f7d055653ab5e983d186e616826a72d4384b68af8' },
+ ],
+ ])('sends the conversion when only %s is available', async (_, personProperties, expectedUserData) => {
+ const response = await tester.invokeMapping(
+ 'Conversion',
+ { tagId: '12345678', apiToken: 'api-token', eventName: 'purchase' },
+ createAdDestinationPayload({ person: { properties: personProperties } })
+ )
+ expect(response.error).toBeUndefined()
+ const body = parseJSON((response.invocation.queueParameters as { body: string }).body)
+ expect(body.data[0].userData).toEqual(expectedUserData)
+ })
+ it('skips when no identifier is available', async () => {
const response = await tester.invokeMapping(
'Conversion',
{ tagId: '12345678', apiToken: 'api-token', eventName: 'purchase' },
- createAdDestinationPayload({ person: { properties: { msclkid: null } } })
+ createAdDestinationPayload({ person: { properties: { msclkid: null, email: null, phone: null } } })
)
expect(response.logs.filter((log) => log.level === 'info').map((log) => log.message)).toMatchInlineSnapshot(
`
[
- "Empty \`microsoftClickId\`. Skipping...",
+ "No \`microsoftClickId\`, \`email\` or \`phone\` to identify the user with. Skipping...",
]
`
)
diff --git a/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.ts b/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.ts
index 1c0a58f5ea04..a5b27f2ddb9f 100644
--- a/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.ts
+++ b/nodejs/src/cdp/templates/_destinations/microsoft_ads/microsoft.template.ts
@@ -19,10 +19,10 @@ const build_inputs = (): HogFunctionInputSchemaType[] => {
type: 'string',
label: 'Microsoft Click ID (msclkid)',
description:
- 'The Microsoft click ID (msclkid) associated with this conversion. Required for click attribution.',
+ 'The Microsoft click ID (msclkid) associated with this conversion. It gives the most accurate click attribution, but conversions can also match on hashed email or phone when no click ID is available.',
default: '{person.properties.msclkid ?? person.properties.$initial_msclkid}',
secret: false,
- required: true,
+ required: false,
},
{
key: 'eventTime',
@@ -77,7 +77,7 @@ const build_inputs = (): HogFunctionInputSchemaType[] => {
label: 'Phone number',
description:
'Phone number for enhanced conversions. Sent SHA-256 hashed; leave blank to omit. Normalize to E.164 format (e.g. +14255551234) for best match rates.',
- default: '',
+ default: '{person.properties.phone}',
secret: false,
required: false,
},
@@ -95,13 +95,9 @@ export const template: HogFunctionTemplate = {
category: ['Advertisement'],
code_language: 'hog',
code: `
-if (empty(inputs.microsoftClickId)) {
- print('Empty \`microsoftClickId\`. Skipping...')
- return
-}
-
-let userData := {
- 'msclkid': inputs.microsoftClickId
+let userData := {}
+if (not empty(inputs.microsoftClickId)) {
+ userData.msclkid := inputs.microsoftClickId
}
if (not empty(inputs.email)) {
userData.em := sha256Hex(lower(trim(inputs.email)))
@@ -109,6 +105,11 @@ if (not empty(inputs.email)) {
if (not empty(inputs.phone)) {
userData.ph := sha256Hex(lower(trim(inputs.phone)))
}
+// Microsoft can only attribute a conversion if it carries at least one identifier
+if (length(keys(userData)) == 0) {
+ print('No \`microsoftClickId\`, \`email\` or \`phone\` to identify the user with. Skipping...')
+ return
+}
let conversion := {
'eventType': 'custom',