Skip to content

Commit fc58970

Browse files
STRATCONN-6743 - [Facebook CAPI] - refactor for pageView Actions (#3720)
* [Facebook CAPI] - refactor for pageView Actions * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> * adding additional test coverage --------- Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent 777bcda commit fc58970

12 files changed

Lines changed: 3128 additions & 139 deletions

File tree

packages/destination-actions/src/destinations/facebook-conversions-api/__tests__/pageView.test.ts

Lines changed: 158 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import nock from 'nock'
22
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
33
import Destination from '../index'
44
import { API_VERSION } from '../constants'
5+
import { FEATURE_FLAG_PAGE_VIEW } from '../shared/constants'
56

67
const testDestination = createTestIntegration(Destination)
78
const settings = {
@@ -15,71 +16,95 @@ const settingsWithTestEventCode = {
1516
token: process.env.TOKEN
1617
}
1718

19+
const testCases = [
20+
{ name: 'flag off', features: undefined },
21+
{ name: 'flag on', features: { [FEATURE_FLAG_PAGE_VIEW]: true } }
22+
]
23+
1824
describe('FacebookConversionsApi', () => {
19-
describe('PageView', () => {
20-
it('should handle a basic event', async () => {
21-
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})
25+
describe.each(testCases)('PageView ($name)', ({ features }) => {
26+
describe('PageView', () => {
27+
it('should handle a basic event', async () => {
28+
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})
29+
30+
const event = createTestEvent({
31+
type: 'page',
32+
userId: 'abc123',
33+
timestamp: '1631210020',
34+
messageId: 'test',
35+
properties: {
36+
timestamp: 1631210000,
37+
action_source: 'email',
38+
39+
}
40+
})
2241

23-
const event = createTestEvent({
24-
type: 'page',
25-
userId: 'abc123',
26-
timestamp: '1631210020',
27-
messageId: 'test',
28-
properties: {
29-
timestamp: 1631210000,
30-
action_source: 'email',
31-
32-
}
33-
})
42+
const responses = await testDestination.testAction('pageView', {
43+
event,
44+
settings,
45+
features,
46+
useDefaultMappings: true,
47+
mapping: { action_source: { '@path': '$.properties.action_source' } }
48+
})
3449

35-
const responses = await testDestination.testAction('pageView', {
36-
event,
37-
settings,
38-
useDefaultMappings: true,
39-
mapping: { action_source: { '@path': '$.properties.action_source' } }
50+
expect(responses.length).toBe(1)
51+
expect(responses[0].status).toBe(201)
52+
53+
expect(JSON.parse(responses[0].options.body as string)).toEqual({
54+
data: [
55+
{
56+
event_name: 'PageView',
57+
event_time: '1631210020',
58+
action_source: 'email',
59+
event_source_url: 'https://segment.com/academy/',
60+
event_id: 'test',
61+
user_data: {
62+
external_id: [
63+
'6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090'
64+
],
65+
client_ip_address: '8.8.8.8',
66+
client_user_agent:
67+
'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'
68+
}
69+
}
70+
]
71+
})
4072
})
4173

42-
expect(responses.length).toBe(1)
43-
expect(responses[0].status).toBe(201)
44-
45-
expect(responses[0].options.body).toMatchInlineSnapshot(
46-
`"{\\"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\\"}}]}"`
47-
)
48-
})
49-
50-
it('should throw an error when action_source is website and no client_user_agent', async () => {
51-
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})
74+
it('should throw an error when action_source is website and no client_user_agent', async () => {
75+
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})
5276

53-
const event = createTestEvent({
54-
type: 'page',
55-
userId: 'abc123',
56-
properties: {
57-
timestamp: 1631210000,
58-
action_source: 'website',
59-
60-
}
61-
})
77+
const event = createTestEvent({
78+
type: 'page',
79+
userId: 'abc123',
80+
properties: {
81+
timestamp: 1631210000,
82+
action_source: 'website',
83+
84+
}
85+
})
6286

63-
await expect(
64-
testDestination.testAction('pageView', {
65-
event,
66-
settings,
67-
mapping: {
68-
action_source: {
69-
'@path': '$.properties.action_source'
70-
},
71-
event_time: {
72-
'@path': '$.properties.timestamp'
73-
},
74-
user_data: {
75-
email: {
76-
'@path': '$.properties.email'
87+
await expect(
88+
testDestination.testAction('pageView', {
89+
event,
90+
settings,
91+
features,
92+
mapping: {
93+
action_source: {
94+
'@path': '$.properties.action_source'
95+
},
96+
event_time: {
97+
'@path': '$.properties.timestamp'
98+
},
99+
user_data: {
100+
email: {
101+
'@path': '$.properties.email'
102+
}
77103
}
78104
}
79-
}
80-
})
81-
).rejects.toThrowError('If action source is "Website" then client_user_agent must be defined')
82-
})
105+
})
106+
).rejects.toThrowError('If action source is "Website" then client_user_agent must be defined')
107+
})
83108

84109
it('should handle default mappings', async () => {
85110
nock(`https://graph.facebook.com/v${API_VERSION}/${settings.pixelId}`).post(`/events`).reply(201, {})
@@ -100,6 +125,7 @@ describe('FacebookConversionsApi', () => {
100125
const responses = await testDestination.testAction('pageView', {
101126
event,
102127
settings,
128+
features,
103129
useDefaultMappings: true,
104130
mapping: {
105131
action_source: { '@path': '$.properties.action_source' },
@@ -120,9 +146,22 @@ describe('FacebookConversionsApi', () => {
120146
expect(responses.length).toBe(1)
121147
expect(responses[0].status).toBe(201)
122148

123-
expect(responses[0].options.body).toMatchInlineSnapshot(
124-
`"{\\"data\\":[{\\"event_name\\":\\"PageView\\",\\"event_time\\":\\"1631210020\\",\\"action_source\\":\\"email\\",\\"event_source_url\\":\\"https://segment.com/academy/\\",\\"event_id\\":\\"test\\",\\"user_data\\":{\\"em\\":\\"eeaf810ee0e3cef3307089f22c3804f54c79eed19ef29bf70df864b43862c380\\",\\"partner_id\\":\\"faf12efasdfasdf1edasdasdfadf=\\",\\"partner_name\\":\\"liveramp\\"}}]}"`
125-
)
149+
expect(JSON.parse(responses[0].options.body as string)).toEqual({
150+
data: [
151+
{
152+
event_name: "PageView",
153+
event_time: "1631210020",
154+
action_source: "email",
155+
event_source_url: "https://segment.com/academy/",
156+
event_id: "test",
157+
user_data: {
158+
em: "eeaf810ee0e3cef3307089f22c3804f54c79eed19ef29bf70df864b43862c380",
159+
partner_id: "faf12efasdfasdf1edasdasdfadf=",
160+
partner_name: "liveramp"
161+
}
162+
}
163+
]
164+
})
126165
})
127166

128167
it('should throw an error if no user_data keys are included', async () => {
@@ -141,6 +180,7 @@ describe('FacebookConversionsApi', () => {
141180
testDestination.testAction('pageView', {
142181
event,
143182
settings,
183+
features,
144184
mapping: {
145185
currency: {
146186
'@path': '$.properties.currency'
@@ -180,16 +220,33 @@ describe('FacebookConversionsApi', () => {
180220
const responses = await testDestination.testAction('pageView', {
181221
event,
182222
settings: settingsWithTestEventCode,
223+
features,
183224
useDefaultMappings: true,
184225
mapping: { action_source: { '@path': '$.properties.action_source' } }
185226
})
186227

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

190-
expect(responses[0].options.body).toMatchInlineSnapshot(
191-
`"{\\"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\\":\\"1234567890\\"}"`
192-
)
231+
expect(JSON.parse(responses[0].options.body as string)).toEqual({
232+
data: [
233+
{
234+
event_name: "PageView",
235+
event_time: "1631210020",
236+
action_source: "email",
237+
event_source_url: "https://segment.com/academy/",
238+
event_id: "test",
239+
user_data: {
240+
external_id: [
241+
"6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090"
242+
],
243+
client_ip_address: "8.8.8.8",
244+
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"
245+
}
246+
}
247+
],
248+
test_event_code: "1234567890"
249+
})
193250
})
194251

195252
it('should send test_event_code if present in the mapping', async () => {
@@ -211,6 +268,7 @@ describe('FacebookConversionsApi', () => {
211268
const responses = await testDestination.testAction('pageView', {
212269
event,
213270
settings: settingsWithTestEventCode,
271+
features,
214272
useDefaultMappings: true,
215273
mapping: {
216274
action_source: { '@path': '$.properties.action_source' },
@@ -223,9 +281,25 @@ describe('FacebookConversionsApi', () => {
223281
expect(responses.length).toBe(1)
224282
expect(responses[0].status).toBe(201)
225283

226-
expect(responses[0].options.body).toMatchInlineSnapshot(
227-
`"{\\"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\\"}"`
228-
)
284+
expect(JSON.parse(responses[0].options.body as string)).toEqual({
285+
data: [
286+
{
287+
event_name: "PageView",
288+
event_time: "1631210020",
289+
action_source: "email",
290+
event_source_url: "https://segment.com/academy/",
291+
event_id: "test",
292+
user_data: {
293+
external_id: [
294+
"6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090"
295+
],
296+
client_ip_address: "8.8.8.8",
297+
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"
298+
}
299+
}
300+
],
301+
test_event_code: "2345678901"
302+
})
229303
})
230304

231305
it('should handle a basic event with multiple external Ids', async () => {
@@ -247,6 +321,7 @@ describe('FacebookConversionsApi', () => {
247321
const responses = await testDestination.testAction('pageView', {
248322
event,
249323
settings,
324+
features,
250325
useDefaultMappings: true,
251326
mapping: {
252327
action_source: { '@path': '$.properties.action_source' },
@@ -261,9 +336,24 @@ describe('FacebookConversionsApi', () => {
261336
expect(responses.length).toBe(1)
262337
expect(responses[0].status).toBe(201)
263338

264-
expect(responses[0].options.body).toMatchInlineSnapshot(
265-
`"{\\"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\\",\\"f0a72890897acefdb2c6c8c06134339a73cc6205833ca38dba6f9fdc94b60596\\"]}}]}"`
266-
)
339+
expect(JSON.parse(responses[0].options.body as string)).toEqual({
340+
data: [
341+
{
342+
event_name: "PageView",
343+
event_time: "1631210020",
344+
action_source: "email",
345+
event_source_url: "https://segment.com/academy/",
346+
event_id: "test",
347+
user_data: {
348+
external_id: [
349+
"6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090",
350+
"f0a72890897acefdb2c6c8c06134339a73cc6205833ca38dba6f9fdc94b60596"
351+
]
352+
}
353+
}
354+
]
355+
})
267356
})
268357
})
358+
})
269359
})

0 commit comments

Comments
 (0)