Skip to content

Commit

Permalink
[Facebook Conversions] Adding Test Event Code to all mappings. (#2516)
Browse files Browse the repository at this point in the history
* [Facebook CAPI] Adding Test Event Code to all mappings.

* Adding corresponding unit tests.
  • Loading branch information
seg-leonelsanches authored Nov 12, 2024
1 parent a2ef07c commit 5e76015
Show file tree
Hide file tree
Showing 45 changed files with 1,000 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,65 @@ describe('FacebookConversionsApi', () => {
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
event: 'Product Added',
userId: 'abc123',
timestamp: '1631210000',
properties: {
action_source: 'email',
currency: 'USD',
value: 12.12,
email: '[email protected]',
traits: {
city: 'Gotham',
country: 'United States',
last_name: 'Wayne'
},
test_event_code: '2345678901'
}
})

const responses = await testDestination.testAction('addToCart', {
event,
settings: settings,
mapping: {
currency: {
'@path': '$.properties.currency'
},
value: {
'@path': '$.properties.value'
},
user_data: {
email: {
'@path': '$.properties.email'
}
},
action_source: {
'@path': '$.properties.action_source'
},
event_time: {
'@path': '$.timestamp'
},
custom_data: {
'@path': '$.properties.traits'
},
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"AddToCart\\",\\"event_time\\":\\"1631210000\\",\\"action_source\\":\\"email\\",\\"user_data\\":{\\"em\\":\\"eeaf810ee0e3cef3307089f22c3804f54c79eed19ef29bf70df864b43862c380\\"},\\"custom_data\\":{\\"city\\":\\"Gotham\\",\\"country\\":\\"United States\\",\\"last_name\\":\\"Wayne\\",\\"currency\\":\\"USD\\",\\"value\\":12.12}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})

it('should send app events without anon_id and madId using default mappings correctly', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,66 @@ describe('FacebookConversionsApi', () => {
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
event: 'Product Added',
userId: 'abc123',
timestamp: '1631210000',
properties: {
action_source: 'email',
currency: 'USD',
value: 12.12,
email: '[email protected]',
traits: {
city: 'Gotham',
country: 'United States',
last_name: 'Wayne'
},
test_event_code: '2345678901'
}
})

const responses = await testDestination.testAction('addToCart2', {
event,
settings: settingsWithTestEventCode,
mapping: {
__segment_internal_sync_mode: 'add',
currency: {
'@path': '$.properties.currency'
},
value: {
'@path': '$.properties.value'
},
user_data: {
email: {
'@path': '$.properties.email'
}
},
action_source: {
'@path': '$.properties.action_source'
},
event_time: {
'@path': '$.timestamp'
},
custom_data: {
'@path': '$.properties.traits'
},
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"AddToCart\\",\\"event_time\\":\\"1631210000\\",\\"action_source\\":\\"email\\",\\"user_data\\":{\\"em\\":\\"eeaf810ee0e3cef3307089f22c3804f54c79eed19ef29bf70df864b43862c380\\"},\\"custom_data\\":{\\"city\\":\\"Gotham\\",\\"country\\":\\"United States\\",\\"last_name\\":\\"Wayne\\",\\"currency\\":\\"USD\\",\\"value\\":12.12}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})

it('should send app events using default mappings correctly', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,63 @@ describe('FacebookConversionsApi', () => {
`"{\\"data\\":[{\\"event_name\\":\\"identify\\",\\"event_time\\":\\"2015-02-23T22:28:55.111Z\\",\\"action_source\\":\\"website\\",\\"event_id\\":\\"022bb90c-bbac-11e4-8dfc-aa07a5b093db\\",\\"user_data\\":{\\"external_id\\":[\\"df73b86ff613b9d7008c175ae3c3aa3f2c1ea1674a80cac85274d58048e44127\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\\"},\\"custom_data\\":{\\"action_source\\":\\"website\\",\\"timestamp\\":\\"1633473963\\"}}],\\"test_event_code\\":\\"1234567890\\"}"`
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
anonymousId: '507f191e810c19729de860ea',
context: {
ip: '8.8.8.8',
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'
},
messageId: '022bb90c-bbac-11e4-8dfc-aa07a5b093db',
receivedAt: '2015-02-23T22:28:55.387Z',
sentAt: '2015-02-23T22:28:55.111Z',
timestamp: '2015-02-23T22:28:55.111Z',
traits: {
name: 'Peter Gibbons',
email: '[email protected]',
plan: 'premium',
logins: 5,
address: {
street: '6th St',
city: 'San Francisco',
state: 'CA',
postalCode: '94103',
country: 'USA'
}
},
properties: {
action_source: 'website',
timestamp: '1633473963',
test_event_code: '2345678901'
},
type: 'identify',
userId: '97980cfea0067',
event: 'identify'
})

const responses = await testDestination.testAction('custom', {
event,
settings: settingsWithTestEventCode,
useDefaultMappings: true,
mapping: {
action_source: { '@path': '$.properties.action_source' },
custom_data: { '@path': '$.properties' },
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"identify\\",\\"event_time\\":\\"2015-02-23T22:28:55.111Z\\",\\"action_source\\":\\"website\\",\\"event_id\\":\\"022bb90c-bbac-11e4-8dfc-aa07a5b093db\\",\\"user_data\\":{\\"external_id\\":[\\"df73b86ff613b9d7008c175ae3c3aa3f2c1ea1674a80cac85274d58048e44127\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\\"},\\"custom_data\\":{\\"action_source\\":\\"website\\",\\"timestamp\\":\\"1633473963\\",\\"test_event_code\\":\\"2345678901\\"}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,64 @@ describe('FacebookConversionsApi', () => {
`"{\\"data\\":[{\\"event_name\\":\\"identify\\",\\"event_time\\":\\"2015-02-23T22:28:55.111Z\\",\\"action_source\\":\\"website\\",\\"event_id\\":\\"022bb90c-bbac-11e4-8dfc-aa07a5b093db\\",\\"user_data\\":{\\"external_id\\":[\\"df73b86ff613b9d7008c175ae3c3aa3f2c1ea1674a80cac85274d58048e44127\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\\"},\\"custom_data\\":{\\"action_source\\":\\"website\\",\\"timestamp\\":\\"1633473963\\"}}],\\"test_event_code\\":\\"1234567890\\"}"`
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
anonymousId: '507f191e810c19729de860ea',
context: {
ip: '8.8.8.8',
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'
},
messageId: '022bb90c-bbac-11e4-8dfc-aa07a5b093db',
receivedAt: '2015-02-23T22:28:55.387Z',
sentAt: '2015-02-23T22:28:55.111Z',
timestamp: '2015-02-23T22:28:55.111Z',
traits: {
name: 'Peter Gibbons',
email: '[email protected]',
plan: 'premium',
logins: 5,
address: {
street: '6th St',
city: 'San Francisco',
state: 'CA',
postalCode: '94103',
country: 'USA'
}
},
properties: {
action_source: 'website',
timestamp: '1633473963',
test_event_code: '2345678901'
},
type: 'identify',
userId: '97980cfea0067',
event: 'identify'
})

const responses = await testDestination.testAction('custom2', {
event,
settings: settingsWithTestEventCode,
useDefaultMappings: true,
mapping: {
__segment_internal_sync_mode: 'add',
action_source: { '@path': '$.properties.action_source' },
custom_data: { '@path': '$.properties' },
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"identify\\",\\"event_time\\":\\"2015-02-23T22:28:55.111Z\\",\\"action_source\\":\\"website\\",\\"event_id\\":\\"022bb90c-bbac-11e4-8dfc-aa07a5b093db\\",\\"user_data\\":{\\"external_id\\":[\\"df73b86ff613b9d7008c175ae3c3aa3f2c1ea1674a80cac85274d58048e44127\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36\\"},\\"custom_data\\":{\\"action_source\\":\\"website\\",\\"timestamp\\":\\"1633473963\\",\\"test_event_code\\":\\"2345678901\\"}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,46 @@ describe('FacebookConversionsApi', () => {
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
event: 'Checkout Started',
timestamp: '1631210020',
messageId: 'test',
properties: {
userId: 'testuser1234',
action_source: 'email',
currency: 'USD',
revenue: 12.12,
products: [
{ product_id: '123', quantity: 1, price: 100 },
{ product_id: '345', quantity: 2, price: 50 }
],
test_event_code: '2345678901'
}
})

const responses = await testDestination.testAction('initiateCheckout', {
event,
settings: settingsWithTestEventCode,
useDefaultMappings: true,
mapping: {
action_source: { '@path': '$.properties.action_source' },
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"InitiateCheckout\\",\\"event_time\\":\\"1631210020\\",\\"action_source\\":\\"email\\",\\"event_source_url\\":\\"https://segment.com/academy/\\",\\"event_id\\":\\"test\\",\\"user_data\\":{\\"external_id\\":[\\"831c237928e6212bedaa4451a514ace3174562f6761f6a157a2fe5082b36e2fb\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1\\"},\\"custom_data\\":{\\"currency\\":\\"USD\\",\\"value\\":12.12,\\"contents\\":[{\\"id\\":\\"123\\",\\"quantity\\":1,\\"item_price\\":100},{\\"id\\":\\"345\\",\\"quantity\\":2,\\"item_price\\":50}]}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})

it('should handle basic event mapping with mutiple externalId', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,47 @@ describe('FacebookConversionsApi', () => {
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
event: 'Checkout Started',
timestamp: '1631210020',
messageId: 'test',
properties: {
userId: 'testuser1234',
action_source: 'email',
currency: 'USD',
revenue: 12.12,
products: [
{ product_id: '123', quantity: 1, price: 100 },
{ product_id: '345', quantity: 2, price: 50 }
],
test_event_code: '2345678901'
}
})

const responses = await testDestination.testAction('initiateCheckout2', {
event,
settings: settingsWithTestEventCode,
useDefaultMappings: true,
mapping: {
__segment_internal_sync_mode: 'add',
action_source: { '@path': '$.properties.action_source' },
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"InitiateCheckout\\",\\"event_time\\":\\"1631210020\\",\\"action_source\\":\\"email\\",\\"event_source_url\\":\\"https://segment.com/academy/\\",\\"event_id\\":\\"test\\",\\"user_data\\":{\\"external_id\\":[\\"831c237928e6212bedaa4451a514ace3174562f6761f6a157a2fe5082b36e2fb\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1\\"},\\"custom_data\\":{\\"currency\\":\\"USD\\",\\"value\\":12.12,\\"contents\\":[{\\"id\\":\\"123\\",\\"quantity\\":1,\\"item_price\\":100},{\\"id\\":\\"345\\",\\"quantity\\":2,\\"item_price\\":50}]}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})

it('should handle basic event mapping with mutiple externalId', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@ describe('FacebookConversionsApi', () => {
)
})

it('should send test_event_code if present in the mapping', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

const event = createTestEvent({
type: 'page',
userId: 'abc123',
timestamp: '1631210020',
messageId: 'test',
properties: {
timestamp: 1631210000,
action_source: 'email',
email: '[email protected]',
test_event_code: '2345678901'
}
})

const responses = await testDestination.testAction('pageView', {
event,
settings: settingsWithTestEventCode,
useDefaultMappings: true,
mapping: {
action_source: { '@path': '$.properties.action_source' },
test_event_code: {
'@path': '$.properties.test_event_code'
}
}
})

expect(responses.length).toBe(1)
expect(responses[0].status).toBe(201)

expect(responses[0].options.body).toMatchInlineSnapshot(
`"{\\"data\\":[{\\"event_name\\":\\"PageView\\",\\"event_time\\":\\"1631210020\\",\\"action_source\\":\\"email\\",\\"event_source_url\\":\\"https://segment.com/academy/\\",\\"event_id\\":\\"test\\",\\"user_data\\":{\\"external_id\\":[\\"6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090\\"],\\"client_ip_address\\":\\"8.8.8.8\\",\\"client_user_agent\\":\\"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1\\"}}],\\"test_event_code\\":\\"2345678901\\"}"`
)
})

it('should handle a basic event with multiple external Ids', async () => {
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})

Expand Down
Loading

0 comments on commit 5e76015

Please sign in to comment.