@segment/analytics-browser-actions-facebook-conversions-api-web 1.11.1-staging-49cc16573.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,7 +29,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
29
29
  }
30
30
 
31
31
  describe('send - Standard Events', () => {
32
- it('should send Purchase event with required fields', () => {
32
+ it('should send Purchase event with required fields', async () => {
33
33
  const payload = {
34
34
  event_config: {
35
35
  event_name: 'Purchase',
@@ -40,22 +40,23 @@ describe('Facebook Conversions API Web - Send Functions', () => {
40
40
  currency: 'USD'
41
41
  }
42
42
 
43
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
43
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
44
44
 
45
45
  expect(mockFbq).toHaveBeenCalledWith(
46
46
  'trackSingle',
47
47
  'test-pixel-123',
48
48
  'Purchase',
49
- expect.objectContaining({
49
+ {
50
+ partner_agent: 'segment',
50
51
  content_ids: ['product-123'],
51
52
  value: 99.99,
52
53
  currency: 'USD'
53
- }),
54
+ },
54
55
  undefined
55
56
  )
56
57
  })
57
58
 
58
- it('should send AddToCart event with contents', () => {
59
+ it('should send AddToCart event with contents', async () => {
59
60
  const payload = {
60
61
  event_config: {
61
62
  event_name: 'AddToCart',
@@ -72,22 +73,23 @@ describe('Facebook Conversions API Web - Send Functions', () => {
72
73
  currency: 'USD'
73
74
  }
74
75
 
75
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
76
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
76
77
 
77
78
  expect(mockFbq).toHaveBeenCalledWith(
78
79
  'trackSingle',
79
80
  'test-pixel-123',
80
81
  'AddToCart',
81
- expect.objectContaining({
82
+ {
83
+ partner_agent: 'segment',
82
84
  contents: [{ id: 'product-123', quantity: 2, item_price: 49.99 }],
83
85
  value: 99.98,
84
86
  currency: 'USD'
85
- }),
87
+ },
86
88
  undefined
87
89
  )
88
90
  })
89
91
 
90
- it('should send ViewContent event', () => {
92
+ it('should send ViewContent event', async () => {
91
93
  const payload = {
92
94
  event_config: {
93
95
  event_name: 'ViewContent',
@@ -99,23 +101,24 @@ describe('Facebook Conversions API Web - Send Functions', () => {
99
101
  value: 199.99
100
102
  }
101
103
 
102
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
104
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
103
105
 
104
106
  expect(mockFbq).toHaveBeenCalledWith(
105
107
  'trackSingle',
106
108
  'test-pixel-123',
107
109
  'ViewContent',
108
- expect.objectContaining({
110
+ {
111
+ partner_agent: 'segment',
109
112
  content_ids: ['product-456'],
110
113
  content_name: 'Test Product',
111
114
  content_category: 'Electronics',
112
115
  value: 199.99
113
- }),
116
+ },
114
117
  undefined
115
118
  )
116
119
  })
117
120
 
118
- it('should send PageView event', () => {
121
+ it('should send PageView event', async () => {
119
122
  const payload = {
120
123
  event_config: {
121
124
  event_name: 'PageView',
@@ -123,14 +126,14 @@ describe('Facebook Conversions API Web - Send Functions', () => {
123
126
  }
124
127
  }
125
128
 
126
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
129
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
127
130
 
128
- expect(mockFbq).toHaveBeenCalledWith('trackSingle', 'test-pixel-123', 'PageView', {}, undefined)
131
+ expect(mockFbq).toHaveBeenCalledWith('trackSingle', 'test-pixel-123', 'PageView', { partner_agent: 'segment' }, undefined)
129
132
  })
130
133
  })
131
134
 
132
135
  describe('send - Custom Events', () => {
133
- it('should send custom event with custom event name', () => {
136
+ it('should send custom event with custom event name', async () => {
134
137
  const payload = {
135
138
  event_config: {
136
139
  event_name: 'CustomEvent',
@@ -144,26 +147,27 @@ describe('Facebook Conversions API Web - Send Functions', () => {
144
147
  }
145
148
  }
146
149
 
147
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
150
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
148
151
 
149
152
  expect(mockFbq).toHaveBeenCalledWith(
150
153
  'trackSingleCustom',
151
154
  'test-pixel-123',
152
155
  'MyCustomEvent',
153
- expect.objectContaining({
156
+ {
157
+ partner_agent: 'segment',
154
158
  value: 50.0,
155
159
  custom_data: {
156
160
  custom_field_1: 'value1',
157
161
  custom_field_2: 'value2'
158
162
  }
159
- }),
163
+ },
160
164
  undefined
161
165
  )
162
166
  })
163
167
  })
164
168
 
165
169
  describe('send - Validation', () => {
166
- it('should warn if AddToCart is missing both content_ids and contents', () => {
170
+ it('should warn if AddToCart is missing both content_ids and contents', async () => {
167
171
  const payload = {
168
172
  event_config: {
169
173
  event_name: 'AddToCart',
@@ -172,7 +176,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
172
176
  value: 99.99
173
177
  }
174
178
 
175
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
179
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
176
180
 
177
181
  expect(consoleWarnSpy).toHaveBeenCalledWith(
178
182
  expect.stringContaining('At least one of content_ids or contents is required for the AddToCart event')
@@ -180,7 +184,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
180
184
  expect(mockFbq).not.toHaveBeenCalled()
181
185
  })
182
186
 
183
- it('should warn if Purchase is missing both content_ids and contents', () => {
187
+ it('should warn if Purchase is missing both content_ids and contents', async () => {
184
188
  const payload = {
185
189
  event_config: {
186
190
  event_name: 'Purchase',
@@ -190,7 +194,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
190
194
  currency: 'USD'
191
195
  }
192
196
 
193
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
197
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
194
198
 
195
199
  expect(consoleWarnSpy).toHaveBeenCalledWith(
196
200
  expect.stringContaining('At least one of content_ids or contents is required for the Purchase event')
@@ -198,7 +202,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
198
202
  expect(mockFbq).not.toHaveBeenCalled()
199
203
  })
200
204
 
201
- it('should warn if ViewContent is missing both content_ids and contents', () => {
205
+ it('should warn if ViewContent is missing both content_ids and contents', async () => {
202
206
  const payload = {
203
207
  event_config: {
204
208
  event_name: 'ViewContent',
@@ -206,7 +210,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
206
210
  }
207
211
  }
208
212
 
209
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
213
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
210
214
 
211
215
  expect(consoleWarnSpy).toHaveBeenCalledWith(
212
216
  expect.stringContaining('At least one of content_ids or contents is required for the ViewContent event')
@@ -214,7 +218,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
214
218
  expect(mockFbq).not.toHaveBeenCalled()
215
219
  })
216
220
 
217
- it('should not warn if AddToCart has content_ids', () => {
221
+ it('should not warn if AddToCart has content_ids', async () => {
218
222
  const payload = {
219
223
  event_config: {
220
224
  event_name: 'AddToCart',
@@ -224,13 +228,13 @@ describe('Facebook Conversions API Web - Send Functions', () => {
224
228
  value: 99.99
225
229
  }
226
230
 
227
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
231
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
228
232
 
229
233
  expect(consoleWarnSpy).not.toHaveBeenCalled()
230
234
  expect(mockFbq).toHaveBeenCalled()
231
235
  })
232
236
 
233
- it('should not warn if AddToCart has contents', () => {
237
+ it('should not warn if AddToCart has contents', async () => {
234
238
  const payload = {
235
239
  event_config: {
236
240
  event_name: 'AddToCart',
@@ -240,7 +244,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
240
244
  value: 99.99
241
245
  }
242
246
 
243
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
247
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
244
248
 
245
249
  expect(consoleWarnSpy).not.toHaveBeenCalled()
246
250
  expect(mockFbq).toHaveBeenCalled()
@@ -248,7 +252,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
248
252
  })
249
253
 
250
254
  describe('send - Event Options', () => {
251
- it('should include eventID when provided', () => {
255
+ it('should include eventID when provided', async () => {
252
256
  const payload = {
253
257
  event_config: {
254
258
  event_name: 'Purchase',
@@ -259,20 +263,20 @@ describe('Facebook Conversions API Web - Send Functions', () => {
259
263
  eventID: 'unique-event-id-123'
260
264
  }
261
265
 
262
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
266
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
263
267
 
264
268
  expect(mockFbq).toHaveBeenCalledWith(
265
269
  'trackSingle',
266
270
  'test-pixel-123',
267
271
  'Purchase',
268
272
  expect.any(Object),
269
- expect.objectContaining({
273
+ {
270
274
  eventID: 'unique-event-id-123'
271
- })
275
+ }
272
276
  )
273
277
  })
274
278
 
275
- it('should include eventSourceUrl when provided', () => {
279
+ it('should include eventSourceUrl when provided', async () => {
276
280
  const payload = {
277
281
  event_config: {
278
282
  event_name: 'Purchase',
@@ -283,20 +287,20 @@ describe('Facebook Conversions API Web - Send Functions', () => {
283
287
  eventSourceUrl: 'https://example.com/checkout'
284
288
  }
285
289
 
286
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
290
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
287
291
 
288
292
  expect(mockFbq).toHaveBeenCalledWith(
289
293
  'trackSingle',
290
294
  'test-pixel-123',
291
295
  'Purchase',
292
296
  expect.any(Object),
293
- expect.objectContaining({
297
+ {
294
298
  eventSourceUrl: 'https://example.com/checkout'
295
- })
299
+ }
296
300
  )
297
301
  })
298
302
 
299
- it('should include both eventID and eventSourceUrl when provided', () => {
303
+ it('should include both eventID and eventSourceUrl when provided', async () => {
300
304
  const payload = {
301
305
  event_config: {
302
306
  event_name: 'Purchase',
@@ -308,431 +312,23 @@ describe('Facebook Conversions API Web - Send Functions', () => {
308
312
  eventSourceUrl: 'https://example.com/checkout'
309
313
  }
310
314
 
311
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
315
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
312
316
 
313
317
  expect(mockFbq).toHaveBeenCalledWith(
314
318
  'trackSingle',
315
319
  'test-pixel-123',
316
320
  'Purchase',
317
321
  expect.any(Object),
318
- expect.objectContaining({
322
+ {
319
323
  eventID: 'unique-event-id-123',
320
324
  eventSourceUrl: 'https://example.com/checkout'
321
- })
322
- )
323
- })
324
- })
325
-
326
- describe('send - User Data Formatting', () => {
327
- it('should format userData with email', () => {
328
- mockAnalytics.storage.get.mockReturnValue('0')
329
-
330
- const payload = {
331
- event_config: {
332
- event_name: 'Purchase',
333
- show_fields: false
334
- },
335
- content_ids: ['product-123'],
336
- value: 99.99,
337
- userData: {
338
- em: 'TEST@EXAMPLE.COM'
339
325
  }
340
- }
341
-
342
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
343
-
344
- // Should have called init with formatted user data
345
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ em: 'test@example.com' }))
346
- // Should have stored user data
347
- expect(mockAnalytics.storage.set).toHaveBeenCalledWith(
348
- 'fb_user_data',
349
- expect.stringContaining('test@example.com')
350
326
  )
351
327
  })
352
-
353
- it('should format userData with phone number', () => {
354
- mockAnalytics.storage.get.mockReturnValue('0')
355
-
356
- const payload = {
357
- event_config: {
358
- event_name: 'Purchase',
359
- show_fields: false
360
- },
361
- content_ids: ['product-123'],
362
- value: 99.99,
363
- userData: {
364
- ph: '(555) 123-4567'
365
- }
366
- }
367
-
368
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
369
-
370
- // Phone should be cleaned of non-numeric characters
371
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ ph: '5551234567' }))
372
- })
373
-
374
- it('should format userData with first and last name', () => {
375
- mockAnalytics.storage.get.mockReturnValue('0')
376
-
377
- const payload = {
378
- event_config: {
379
- event_name: 'Purchase',
380
- show_fields: false
381
- },
382
- content_ids: ['product-123'],
383
- value: 99.99,
384
- userData: {
385
- fn: ' JOHN ',
386
- ln: ' DOE '
387
- }
388
- }
389
-
390
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
391
-
392
- // Names should be lowercased and trimmed
393
- expect(mockFbq).toHaveBeenCalledWith(
394
- 'init',
395
- 'test-pixel-123',
396
- expect.objectContaining({ fn: 'john', ln: 'doe' })
397
- )
398
- })
399
-
400
- it('should format userData with gender', () => {
401
- mockAnalytics.storage.get.mockReturnValue('0')
402
-
403
- const payload = {
404
- event_config: {
405
- event_name: 'Purchase',
406
- show_fields: false
407
- },
408
- content_ids: ['product-123'],
409
- value: 99.99,
410
- userData: {
411
- ge: 'm'
412
- }
413
- }
414
-
415
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
416
-
417
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ ge: 'm' }))
418
- })
419
-
420
- it('should format userData with date of birth', () => {
421
- mockAnalytics.storage.get.mockReturnValue('0')
422
-
423
- const payload = {
424
- event_config: {
425
- event_name: 'Purchase',
426
- show_fields: false
427
- },
428
- content_ids: ['product-123'],
429
- value: 99.99,
430
- userData: {
431
- db: '1990-05-15T00:00:00.000Z'
432
- }
433
- }
434
-
435
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
436
-
437
- // Date should be formatted as YYYYMMDD
438
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ db: '19900515' }))
439
- })
440
-
441
- it('should format userData with city', () => {
442
- mockAnalytics.storage.get.mockReturnValue('0')
443
-
444
- const payload = {
445
- event_config: {
446
- event_name: 'Purchase',
447
- show_fields: false
448
- },
449
- content_ids: ['product-123'],
450
- value: 99.99,
451
- userData: {
452
- ct: ' New York '
453
- }
454
- }
455
-
456
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
457
-
458
- // City should be lowercased with spaces removed
459
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ ct: 'newyork' }))
460
- })
461
-
462
- it('should format userData with US state - full name to code', () => {
463
- mockAnalytics.storage.get.mockReturnValue('0')
464
-
465
- const payload = {
466
- event_config: {
467
- event_name: 'Purchase',
468
- show_fields: false
469
- },
470
- content_ids: ['product-123'],
471
- value: 99.99,
472
- userData: {
473
- st: 'California'
474
- }
475
- }
476
-
477
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
478
-
479
- // State should be converted to 2-letter code
480
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ st: 'ca' }))
481
- })
482
-
483
- it('should format userData with US state - already 2-letter code', () => {
484
- mockAnalytics.storage.get.mockReturnValue('0')
485
-
486
- const payload = {
487
- event_config: {
488
- event_name: 'Purchase',
489
- show_fields: false
490
- },
491
- content_ids: ['product-123'],
492
- value: 99.99,
493
- userData: {
494
- st: 'NY'
495
- }
496
- }
497
-
498
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
499
-
500
- // State code should be lowercased
501
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ st: 'ny' }))
502
- })
503
-
504
- it('should format userData with country - full name to code', () => {
505
- mockAnalytics.storage.get.mockReturnValue('0')
506
-
507
- const payload = {
508
- event_config: {
509
- event_name: 'Purchase',
510
- show_fields: false
511
- },
512
- content_ids: ['product-123'],
513
- value: 99.99,
514
- userData: {
515
- country: 'United States'
516
- }
517
- }
518
-
519
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
520
-
521
- // Country should be converted to 2-letter code
522
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ country: 'us' }))
523
- })
524
-
525
- it('should format userData with country - already 2-letter code', () => {
526
- mockAnalytics.storage.get.mockReturnValue('0')
527
-
528
- const payload = {
529
- event_config: {
530
- event_name: 'Purchase',
531
- show_fields: false
532
- },
533
- content_ids: ['product-123'],
534
- value: 99.99,
535
- userData: {
536
- country: 'GB'
537
- }
538
- }
539
-
540
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
541
-
542
- // Country code should be lowercased
543
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ country: 'gb' }))
544
- })
545
-
546
- it('should format userData with zip code', () => {
547
- mockAnalytics.storage.get.mockReturnValue('0')
548
-
549
- const payload = {
550
- event_config: {
551
- event_name: 'Purchase',
552
- show_fields: false
553
- },
554
- content_ids: ['product-123'],
555
- value: 99.99,
556
- userData: {
557
- zp: ' 94102 '
558
- }
559
- }
560
-
561
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
562
-
563
- // Zip should be trimmed
564
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ zp: '94102' }))
565
- })
566
-
567
- it('should format userData with external_id', () => {
568
- mockAnalytics.storage.get.mockReturnValue('0')
569
-
570
- const payload = {
571
- event_config: {
572
- event_name: 'Purchase',
573
- show_fields: false
574
- },
575
- content_ids: ['product-123'],
576
- value: 99.99,
577
- userData: {
578
- external_id: ' user-123 '
579
- }
580
- }
581
-
582
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
583
-
584
- // External ID should be trimmed
585
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ external_id: 'user-123' }))
586
- })
587
-
588
- it('should format userData with fbp and fbc cookies', () => {
589
- mockAnalytics.storage.get.mockReturnValue('0')
590
-
591
- const payload = {
592
- event_config: {
593
- event_name: 'Purchase',
594
- show_fields: false
595
- },
596
- content_ids: ['product-123'],
597
- value: 99.99,
598
- userData: {
599
- fbp: ' fb.1.1234567890.1234567890 ',
600
- fbc: ' fb.1.1234567890.AbCdEf123 '
601
- }
602
- }
603
-
604
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
605
-
606
- // FBP and FBC should be trimmed
607
- expect(mockFbq).toHaveBeenCalledWith(
608
- 'init',
609
- 'test-pixel-123',
610
- expect.objectContaining({
611
- fbp: 'fb.1.1234567890.1234567890',
612
- fbc: 'fb.1.1234567890.AbCdEf123'
613
- })
614
- )
615
- })
616
-
617
- it('should format userData with all fields combined', () => {
618
- mockAnalytics.storage.get.mockReturnValue('0')
619
-
620
- const payload = {
621
- event_config: {
622
- event_name: 'Purchase',
623
- show_fields: false
624
- },
625
- content_ids: ['product-123'],
626
- value: 99.99,
627
- userData: {
628
- external_id: 'user-123',
629
- em: 'test@example.com',
630
- ph: '5551234567',
631
- fn: 'John',
632
- ln: 'Doe',
633
- ge: 'm',
634
- db: '1990-05-15T00:00:00.000Z',
635
- ct: 'San Francisco',
636
- st: 'California',
637
- zp: '94102',
638
- country: 'United States',
639
- fbp: 'fb.1.1234567890.1234567890',
640
- fbc: 'fb.1.1234567890.AbCdEf123'
641
- }
642
- }
643
-
644
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
645
-
646
- expect(mockFbq).toHaveBeenCalledWith(
647
- 'init',
648
- 'test-pixel-123',
649
- expect.objectContaining({
650
- external_id: 'user-123',
651
- em: 'test@example.com',
652
- ph: '5551234567',
653
- fn: 'john',
654
- ln: 'doe',
655
- ge: 'm',
656
- db: '19900515',
657
- ct: 'sanfrancisco',
658
- st: 'ca',
659
- zp: '94102',
660
- country: 'us',
661
- fbp: 'fb.1.1234567890.1234567890',
662
- fbc: 'fb.1.1234567890.AbCdEf123'
663
- })
664
- )
665
- })
666
-
667
- it('should not send userData init when init count is at max', () => {
668
- mockAnalytics.storage.get.mockReturnValue('2')
669
-
670
- const payload = {
671
- event_config: {
672
- event_name: 'Purchase',
673
- show_fields: false
674
- },
675
- content_ids: ['product-123'],
676
- value: 99.99,
677
- userData: {
678
- em: 'test@example.com'
679
- }
680
- }
681
-
682
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
683
-
684
- // Should not call init with user data when count is at max
685
- const initCalls = (mockFbq).mock.calls.filter((call) => call[0] === 'init')
686
- expect(initCalls.length).toBe(0)
687
- })
688
-
689
- it('should skip invalid gender values', () => {
690
- mockAnalytics.storage.get.mockReturnValue('0')
691
-
692
- const payload = {
693
- event_config: {
694
- event_name: 'Purchase',
695
- show_fields: false
696
- },
697
- content_ids: ['product-123'],
698
- value: 99.99,
699
- userData: {
700
- ge: 'invalid'
701
- }
702
- }
703
-
704
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
705
-
706
- // Should not call init if only invalid gender is provided
707
- const initCalls = (mockFbq).mock.calls.filter((call) => call[0] === 'init')
708
- expect(initCalls.length).toBe(0)
709
- })
710
-
711
- it('should skip invalid date of birth', () => {
712
- mockAnalytics.storage.get.mockReturnValue('0')
713
-
714
- const payload = {
715
- event_config: {
716
- event_name: 'Purchase',
717
- show_fields: false
718
- },
719
- content_ids: ['product-123'],
720
- value: 99.99,
721
- userData: {
722
- db: 'invalid-date'
723
- }
724
- }
725
-
726
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
727
-
728
- // Should not call init if only invalid date is provided
729
- const initCalls = (mockFbq).mock.calls.filter((call) => call[0] === 'init')
730
- expect(initCalls.length).toBe(0)
731
- })
732
328
  })
733
329
 
734
330
  describe('send - Event Data Fields', () => {
735
- it('should include all event fields when show_fields is true', () => {
331
+ it('should include all event fields when show_fields is true', async () => {
736
332
  const payload = {
737
333
  event_config: {
738
334
  event_name: 'Purchase',
@@ -748,13 +344,14 @@ describe('Facebook Conversions API Web - Send Functions', () => {
748
344
  delivery_category: 'home_delivery'
749
345
  }
750
346
 
751
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
347
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
752
348
 
753
349
  expect(mockFbq).toHaveBeenCalledWith(
754
350
  'trackSingle',
755
351
  'test-pixel-123',
756
352
  'Purchase',
757
- expect.objectContaining({
353
+ {
354
+ partner_agent: 'segment',
758
355
  content_ids: ['product-123'],
759
356
  content_name: 'Test Product',
760
357
  content_category: 'Electronics',
@@ -763,12 +360,12 @@ describe('Facebook Conversions API Web - Send Functions', () => {
763
360
  currency: 'USD',
764
361
  num_items: 1,
765
362
  delivery_category: 'home_delivery'
766
- }),
363
+ },
767
364
  undefined
768
365
  )
769
366
  })
770
367
 
771
- it('should include predicted_ltv for Subscribe event', () => {
368
+ it('should include predicted_ltv for Subscribe event', async () => {
772
369
  const payload = {
773
370
  event_config: {
774
371
  event_name: 'Subscribe',
@@ -778,21 +375,22 @@ describe('Facebook Conversions API Web - Send Functions', () => {
778
375
  value: 50.0
779
376
  }
780
377
 
781
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
378
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
782
379
 
783
380
  expect(mockFbq).toHaveBeenCalledWith(
784
381
  'trackSingle',
785
382
  'test-pixel-123',
786
383
  'Subscribe',
787
- expect.objectContaining({
384
+ {
385
+ partner_agent: 'segment',
788
386
  predicted_ltv: 500.0,
789
387
  value: 50.0
790
- }),
388
+ },
791
389
  undefined
792
390
  )
793
391
  })
794
392
 
795
- it('should include net_revenue for Purchase event', () => {
393
+ it('should include net_revenue for Purchase event', async () => {
796
394
  const payload = {
797
395
  event_config: {
798
396
  event_name: 'Purchase',
@@ -803,21 +401,23 @@ describe('Facebook Conversions API Web - Send Functions', () => {
803
401
  value: 50.0
804
402
  }
805
403
 
806
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
404
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
807
405
 
808
406
  expect(mockFbq).toHaveBeenCalledWith(
809
407
  'trackSingle',
810
408
  'test-pixel-123',
811
409
  'Purchase',
812
- expect.objectContaining({
410
+ {
411
+ partner_agent: 'segment',
412
+ content_ids: ['product-123'],
813
413
  net_revenue: 450.0,
814
414
  value: 50.0
815
- }),
415
+ },
816
416
  undefined
817
417
  )
818
418
  })
819
419
 
820
- it('should include custom_data', () => {
420
+ it('should include custom_data', async () => {
821
421
  const payload = {
822
422
  event_config: {
823
423
  event_name: 'Purchase',
@@ -832,24 +432,27 @@ describe('Facebook Conversions API Web - Send Functions', () => {
832
432
  }
833
433
  }
834
434
 
835
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
435
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
836
436
 
837
437
  expect(mockFbq).toHaveBeenCalledWith(
838
438
  'trackSingle',
839
439
  'test-pixel-123',
840
440
  'Purchase',
841
- expect.objectContaining({
441
+ {
442
+ partner_agent: 'segment',
443
+ content_ids: ['product-123'],
444
+ value: 99.99,
842
445
  custom_data: {
843
446
  order_id: 'order-789',
844
447
  campaign_id: 'summer-sale',
845
448
  user_tier: 'premium'
846
449
  }
847
- }),
450
+ },
848
451
  undefined
849
452
  )
850
453
  })
851
454
 
852
- it('should not include empty arrays or objects', () => {
455
+ it('should not include empty arrays or objects', async () => {
853
456
  const payload = {
854
457
  event_config: {
855
458
  event_name: 'PageView',
@@ -860,13 +463,13 @@ describe('Facebook Conversions API Web - Send Functions', () => {
860
463
  custom_data: {}
861
464
  }
862
465
 
863
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
466
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
864
467
 
865
468
  const eventData = (mockFbq).mock.calls[0][3]
866
- expect(eventData).toEqual({})
469
+ expect(eventData).toEqual({ partner_agent: 'segment' })
867
470
  })
868
471
 
869
- it('should handle numeric values correctly including zero', () => {
472
+ it('should handle numeric values correctly including zero', async () => {
870
473
  const payload = {
871
474
  event_config: {
872
475
  event_name: 'InitiateCheckout',
@@ -877,16 +480,18 @@ describe('Facebook Conversions API Web - Send Functions', () => {
877
480
  num_items: 0
878
481
  }
879
482
 
880
- send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
483
+ await send(mockFbq, mockClientParamBuilder, payload, defaultSettings, mockAnalytics)
881
484
 
882
485
  expect(mockFbq).toHaveBeenCalledWith(
883
486
  'trackSingle',
884
487
  'test-pixel-123',
885
488
  'InitiateCheckout',
886
- expect.objectContaining({
489
+ {
490
+ partner_agent: 'segment',
491
+ content_ids: ['product-123'],
887
492
  value: 0,
888
493
  num_items: 0
889
- }),
494
+ },
890
495
  undefined
891
496
  )
892
497
  })
@@ -904,7 +509,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
904
509
  }
905
510
  })
906
511
 
907
- it('should use clientParamBuilder to format email when available', () => {
512
+ it('should use clientParamBuilder to format email when available', async () => {
908
513
  mockAnalytics.storage.get.mockReturnValue('0')
909
514
  mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue('hashed_email_value')
910
515
 
@@ -920,14 +525,14 @@ describe('Facebook Conversions API Web - Send Functions', () => {
920
525
  }
921
526
  }
922
527
 
923
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
528
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
924
529
 
925
530
  expect(mockClientParamBuilderInstance.processAndCollectAllParams).toHaveBeenCalled()
926
531
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith('TEST@EXAMPLE.COM', 'email')
927
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ em: 'hashed_email_value' }))
532
+ expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', { em: 'hashed_email_value' })
928
533
  })
929
534
 
930
- it('should use clientParamBuilder to format phone number when available', () => {
535
+ it('should use clientParamBuilder to format phone number when available', async () => {
931
536
  mockAnalytics.storage.get.mockReturnValue('0')
932
537
  mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue('hashed_phone_value')
933
538
 
@@ -943,16 +548,16 @@ describe('Facebook Conversions API Web - Send Functions', () => {
943
548
  }
944
549
  }
945
550
 
946
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
551
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
947
552
 
948
553
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith(
949
554
  '+1 (555) 123-4567',
950
555
  'phone'
951
556
  )
952
- expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', expect.objectContaining({ ph: 'hashed_phone_value' }))
557
+ expect(mockFbq).toHaveBeenCalledWith('init', 'test-pixel-123', { ph: 'hashed_phone_value' })
953
558
  })
954
559
 
955
- it('should use clientParamBuilder to format all PII fields', () => {
560
+ it('should use clientParamBuilder to format all PII fields', async () => {
956
561
  mockAnalytics.storage.get.mockReturnValue('0')
957
562
  mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockImplementation((_, type) => {
958
563
  return `hashed_${type}_value`
@@ -980,7 +585,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
980
585
  }
981
586
  }
982
587
 
983
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
588
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
984
589
 
985
590
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith('test@example.com', 'email')
986
591
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith('5551234567', 'phone')
@@ -997,7 +602,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
997
602
  expect(mockFbq).toHaveBeenCalledWith(
998
603
  'init',
999
604
  'test-pixel-123',
1000
- expect.objectContaining({
605
+ {
1001
606
  em: 'hashed_email_value',
1002
607
  ph: 'hashed_phone_value',
1003
608
  fn: 'hashed_first_name_value',
@@ -1009,14 +614,15 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1009
614
  zp: 'hashed_zip_code_value',
1010
615
  country: 'hashed_country_value',
1011
616
  external_id: 'hashed_external_id_value'
1012
- })
617
+ }
1013
618
  )
1014
619
  })
1015
620
 
1016
- it('should use clientParamBuilder getFbc and getFbp methods', () => {
621
+ it('should use clientParamBuilder getFbc and getFbp methods', async () => {
1017
622
  mockAnalytics.storage.get.mockReturnValue('0')
1018
623
  mockClientParamBuilderInstance.getFbc.mockReturnValue('fb.1.1234567890.ClientParamBuilderFbc')
1019
624
  mockClientParamBuilderInstance.getFbp.mockReturnValue('fb.1.1234567890.ClientParamBuilderFbp')
625
+ mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue('hashed_email')
1020
626
 
1021
627
  const payload = {
1022
628
  event_config: {
@@ -1032,7 +638,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1032
638
  }
1033
639
  }
1034
640
 
1035
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
641
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
1036
642
 
1037
643
  expect(mockClientParamBuilderInstance.processAndCollectAllParams).toHaveBeenCalled()
1038
644
  expect(mockClientParamBuilderInstance.getFbc).toHaveBeenCalled()
@@ -1042,17 +648,19 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1042
648
  expect(mockFbq).toHaveBeenCalledWith(
1043
649
  'init',
1044
650
  'test-pixel-123',
1045
- expect.objectContaining({
651
+ {
652
+ em: 'hashed_email',
1046
653
  fbc: 'fb.1.1234567890.ClientParamBuilderFbc',
1047
654
  fbp: 'fb.1.1234567890.ClientParamBuilderFbp'
1048
- })
655
+ }
1049
656
  )
1050
657
  })
1051
658
 
1052
- it('should use payload fbc/fbp when clientParamBuilder methods return null', () => {
659
+ it('should use payload fbc/fbp when clientParamBuilder methods return null', async () => {
1053
660
  mockAnalytics.storage.get.mockReturnValue('0')
1054
661
  mockClientParamBuilderInstance.getFbc.mockReturnValue(null)
1055
662
  mockClientParamBuilderInstance.getFbp.mockReturnValue(null)
663
+ mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue('hashed_email')
1056
664
 
1057
665
  const payload = {
1058
666
  event_config: {
@@ -1068,7 +676,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1068
676
  }
1069
677
  }
1070
678
 
1071
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
679
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
1072
680
 
1073
681
  expect(mockClientParamBuilderInstance.getFbc).toHaveBeenCalled()
1074
682
  expect(mockClientParamBuilderInstance.getFbp).toHaveBeenCalled()
@@ -1077,14 +685,15 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1077
685
  expect(mockFbq).toHaveBeenCalledWith(
1078
686
  'init',
1079
687
  'test-pixel-123',
1080
- expect.objectContaining({
688
+ {
689
+ em: 'hashed_email',
1081
690
  fbc: 'fb.1.1234567890.PayloadFbc',
1082
691
  fbp: 'fb.1.1234567890.PayloadFbp'
1083
- })
692
+ }
1084
693
  )
1085
694
  })
1086
695
 
1087
- it('should fall back to default formatting when clientParamBuilder returns undefined', () => {
696
+ it('should fall back to default formatting when clientParamBuilder returns undefined', async () => {
1088
697
  mockAnalytics.storage.get.mockReturnValue('0')
1089
698
  mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue(undefined)
1090
699
 
@@ -1101,26 +710,21 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1101
710
  }
1102
711
  }
1103
712
 
1104
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
713
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
1105
714
 
1106
715
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith('TEST@EXAMPLE.COM', 'email')
1107
716
  expect(mockClientParamBuilderInstance.getNormalizedAndHashedPII).toHaveBeenCalledWith('(555) 123-4567', 'phone')
1108
717
 
1109
- // When clientParamBuilder returns undefined, should fall back to default formatting
1110
- expect(mockFbq).toHaveBeenCalledWith(
1111
- 'init',
1112
- 'test-pixel-123',
1113
- expect.objectContaining({
1114
- em: 'test@example.com',
1115
- ph: '5551234567'
1116
- })
1117
- )
718
+ // When clientParamBuilder returns undefined, nothing should be sent (empty userData)
719
+ const initCalls = (mockFbq).mock.calls.filter((call) => call[0] === 'init')
720
+ expect(initCalls.length).toBe(0)
1118
721
  })
1119
722
 
1120
- it('should call processAndCollectAllParams before getting cookie values', () => {
723
+ it('should call processAndCollectAllParams before getting cookie values', async () => {
1121
724
  mockAnalytics.storage.get.mockReturnValue('0')
1122
725
  mockClientParamBuilderInstance.getFbc.mockReturnValue('fb.1.fbc')
1123
726
  mockClientParamBuilderInstance.getFbp.mockReturnValue('fb.1.fbp')
727
+ mockClientParamBuilderInstance.getNormalizedAndHashedPII.mockReturnValue('hashed_email')
1124
728
 
1125
729
  const payload = {
1126
730
  event_config: {
@@ -1134,7 +738,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1134
738
  }
1135
739
  }
1136
740
 
1137
- send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
741
+ await send(mockFbq, mockClientParamBuilderInstance, payload, defaultSettings, mockAnalytics)
1138
742
 
1139
743
  const calls = mockClientParamBuilderInstance.processAndCollectAllParams.mock.invocationCallOrder
1140
744
  const fbcCalls = mockClientParamBuilderInstance.getFbc.mock.invocationCallOrder
@@ -1145,7 +749,7 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1145
749
  expect(calls[0]).toBeLessThan(fbpCalls[0])
1146
750
  })
1147
751
 
1148
- it('should work correctly when clientParamBuilder is undefined', () => {
752
+ it('should work correctly when clientParamBuilder is undefined', async () => {
1149
753
  mockAnalytics.storage.get.mockReturnValue('0')
1150
754
 
1151
755
  const payload = {
@@ -1162,16 +766,16 @@ describe('Facebook Conversions API Web - Send Functions', () => {
1162
766
  }
1163
767
 
1164
768
  // Pass undefined for clientParamBuilder
1165
- send(mockFbq, undefined, payload, defaultSettings, mockAnalytics)
769
+ await send(mockFbq, undefined, payload, defaultSettings, mockAnalytics)
1166
770
 
1167
- // Should use default formatting (lowercase and trim for email, digits only for phone)
771
+ // Should use default formatting (normalize and hash)
1168
772
  expect(mockFbq).toHaveBeenCalledWith(
1169
773
  'init',
1170
774
  'test-pixel-123',
1171
- expect.objectContaining({
1172
- em: 'test@example.com',
1173
- ph: '5551234567'
1174
- })
775
+ {
776
+ em: '973dfe463ec85785f5f95af5ba3906eedb2d931c24e69824a89ea65dba4e813b',
777
+ ph: '3c95277da5fd0da6a1a44ee3fdf56d20af6c6d242695a40e18e6e90dc3c5872c'
778
+ }
1175
779
  )
1176
780
  })
1177
781
  })