@mparticle/web-google-analytics-4-client-kit 1.0.0 → 1.0.3

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.
package/test/tests.js DELETED
@@ -1,1433 +0,0 @@
1
- /* eslint-disable no-undef*/
2
- describe('Google Analytics 4 Event', function () {
3
- // -------------------DO NOT EDIT ANYTHING BELOW THIS LINE-----------------------
4
- var MessageType = {
5
- SessionStart: 1,
6
- SessionEnd: 2,
7
- PageView: 3,
8
- PageEvent: 4,
9
- CrashReport: 5,
10
- OptOut: 6,
11
- AppStateTransition: 10,
12
- Profile: 14,
13
- Commerce: 16,
14
- },
15
- EventType = {
16
- Unknown: 0,
17
- Navigation: 1,
18
- Location: 2,
19
- Search: 3,
20
- Transaction: 4,
21
- UserContent: 5,
22
- UserPreference: 6,
23
- Social: 7,
24
- Other: 8,
25
- Media: 9,
26
- getName: function () {
27
- return 'blahblah';
28
- },
29
- },
30
- CommerceEventType = {
31
- Unknown: 0,
32
- ProductAddToCart: 10,
33
- ProductRemoveFromCart: 11,
34
- ProductCheckout: 12,
35
- ProductCheckoutOption: 13,
36
- ProductClick: 14,
37
- ProductViewDetail: 15,
38
- ProductPurchase: 16,
39
- ProductRefund: 17,
40
- PromotionView: 18,
41
- PromotionClick: 19,
42
- ProductAddToWishlist: 20,
43
- ProductRemoveFromWishlist: 21,
44
- ProductImpression: 22,
45
- },
46
- ProductActionType = {
47
- Unknown: 0,
48
- AddToCart: 1,
49
- RemoveFromCart: 2,
50
- Checkout: 3,
51
- CheckoutOption: 4,
52
- Click: 5,
53
- ViewDetail: 6,
54
- Purchase: 7,
55
- Refund: 8,
56
- AddToWishlist: 9,
57
- RemoveFromWishlist: 10,
58
- },
59
- PromotionActionType = {
60
- Unknown: 0,
61
- PromotionClick: 1,
62
- PromotionView: 2,
63
- },
64
- IdentityType = {
65
- Other: 0,
66
- CustomerId: 1,
67
- Facebook: 2,
68
- Twitter: 3,
69
- Google: 4,
70
- Microsoft: 5,
71
- Yahoo: 6,
72
- Email: 7,
73
- Alias: 8,
74
- FacebookCustomAudienceId: 9,
75
- },
76
- ReportingService = function () {
77
- var self = this;
78
-
79
- this.id = null;
80
- this.event = null;
81
-
82
- this.cb = function (forwarder, event) {
83
- self.id = forwarder.id;
84
- self.event = event;
85
- };
86
-
87
- this.reset = function () {
88
- this.id = null;
89
- this.event = null;
90
- };
91
- },
92
- reportService = new ReportingService();
93
-
94
- // -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE-----------------------
95
- // -------------------START EDITING BELOW:-----------------------
96
- // -------------------mParticle stubs - Add any additional stubbing to our methods as needed-----------------------
97
- mParticle.Identity = {
98
- getCurrentUser: function () {
99
- return {
100
- getMPID: function () {
101
- return '123';
102
- },
103
- };
104
- },
105
- };
106
- // -------------------START EDITING BELOW:-----------------------
107
- var mockGA4EventForwarder = function () {
108
- var self = this;
109
-
110
- // create properties for each type of event you want tracked, see below for examples
111
- this.trackCustomEventCalled = false;
112
- this.logPurchaseEventCalled = false;
113
- this.initializeCalled = false;
114
-
115
- this.trackCustomName = null;
116
- this.logPurchaseName = null;
117
- this.apiKey = null;
118
- this.appId = null;
119
- this.userId = null;
120
- this.userAttributes = {};
121
- this.userIdField = null;
122
-
123
- this.eventProperties = [];
124
- this.purchaseEventProperties = [];
125
-
126
- // stub your different methods to ensure they are being called properly
127
- this.initialize = function (appId, apiKey) {
128
- self.initializeCalled = true;
129
- self.apiKey = apiKey;
130
- self.appId = appId;
131
- };
132
-
133
- this.stubbedTrackingMethod = function (name, eventProperties) {
134
- self.trackCustomEventCalled = true;
135
- self.trackCustomName = name;
136
- self.eventProperties.push(eventProperties);
137
- // Return true to indicate event should be reported
138
- return true;
139
- };
140
-
141
- this.stubbedUserAttributeSettingMethod = function (userAttributes) {
142
- self.userId = id;
143
- userAttributes = userAttributes || {};
144
- if (Object.keys(userAttributes).length) {
145
- for (var key in userAttributes) {
146
- if (userAttributes[key] === null) {
147
- delete self.userAttributes[key];
148
- } else {
149
- self.userAttributes[key] = userAttributes[key];
150
- }
151
- }
152
- }
153
- };
154
-
155
- this.stubbedUserLoginMethod = function (id) {
156
- self.userId = id;
157
- };
158
- };
159
-
160
- var kitSettings = {
161
- clientKey: '123456',
162
- appId: 'abcde',
163
- externalUserIdentityType: 'customerId',
164
- measurementId: 'testMeasurementId',
165
- };
166
-
167
- describe('initialization', function () {
168
- it('should initialize gtag and the dataLayer', function (done) {
169
- (typeof window.gtag === 'undefined').should.be.true();
170
- (typeof window.dataLayer === 'undefined').should.be.true();
171
- window.mockGA4EventForwarder = new mockGA4EventForwarder();
172
- // Include any specific settings that is required for initializing your SDK here
173
- mParticle.forwarder.init(kitSettings, reportService.cb, true);
174
-
175
- window.gtag.should.be.ok();
176
- window.dataLayer.should.be.ok();
177
-
178
- done();
179
- });
180
- });
181
- describe('forwarder mapping', function () {
182
- beforeEach(function () {
183
- window.dataLayer = [];
184
- window.mockGA4EventForwarder = new mockGA4EventForwarder();
185
- mParticle.forwarder.init(kitSettings, reportService.cb, true, null);
186
- window.gtag = function () {
187
- window.dataLayer.push(Array.prototype.slice.call(arguments));
188
- };
189
- });
190
-
191
- it('should set user attribute', function (done) {
192
- mParticle.forwarder.setUserAttribute('foo', 'bar');
193
- var result = ['set', 'user_properties', { foo: 'bar' }];
194
- window.dataLayer[0].should.eql(result);
195
-
196
- done();
197
- });
198
-
199
- it('should remove user attribute', function (done) {
200
- mParticle.forwarder.removeUserAttribute('bar');
201
- var result = ['set', 'user_properties', { bar: null }];
202
- window.dataLayer[0].should.eql(result);
203
-
204
- done();
205
- });
206
-
207
- describe('ecommerce mapping', function () {
208
- var result;
209
- beforeEach(function () {
210
- result = [
211
- 'event',
212
- 'event_type_to_be_updated',
213
- {
214
- currency: 'USD',
215
- value: 100,
216
- items: [
217
- {
218
- attributes: {
219
- eventMetric1: 'metric2',
220
- journeyType: 'testjourneytype1',
221
- },
222
- coupon_code: 'coupon',
223
- item_brand: 'brand',
224
- item_category: 'category',
225
- item_id: 'iphoneSKU',
226
- item_name: 'iphone',
227
- item_variant: 'variant',
228
- position: 1,
229
- price: 999,
230
- quantity: 1,
231
- total_amount: 999,
232
- },
233
- {
234
- attributes: {
235
- eventMetric1: 'metric1',
236
- journeyType: 'testjourneytype2',
237
- },
238
- coupon_code: 'coupon',
239
- item_brand: 'brand',
240
- item_category: 'category',
241
- item_id: 'iphoneSKU',
242
- item_name: 'iphone',
243
- item_variant: 'variant',
244
- position: 1,
245
- price: 999,
246
- quantity: 1,
247
- total_amount: 999,
248
- },
249
- ],
250
- },
251
- ];
252
- });
253
-
254
- it('should map MP AddToCart commerce event to GA4 add_to_cart event', function (done) {
255
- mParticle.forwarder.process({
256
- CurrencyCode: 'USD',
257
- EventName: 'Test Purchase Event',
258
- EventDataType: MessageType.Commerce,
259
- EventCategory: CommerceEventType.ProductAddToCart,
260
- CustomFlags: { 'GA4.Value': 100 },
261
- ProductAction: {
262
- ProductActionType: ProductActionType.AddToCart,
263
- ProductList: [
264
- {
265
- Attributes: {
266
- eventMetric1: 'metric2',
267
- journeyType: 'testjourneytype1',
268
- },
269
- Brand: 'brand',
270
- Category: 'category',
271
- CouponCode: 'coupon',
272
- Name: 'iphone',
273
- Position: 1,
274
- Price: 999,
275
- Quantity: 1,
276
- Sku: 'iphoneSKU',
277
- TotalAmount: 999,
278
- Variant: 'variant',
279
- },
280
- {
281
- Attributes: {
282
- eventMetric1: 'metric1',
283
- journeyType: 'testjourneytype2',
284
- },
285
- Brand: 'brand',
286
- Category: 'category',
287
- CouponCode: 'coupon',
288
- Name: 'iphone',
289
- Position: 1,
290
- Price: 999,
291
- Quantity: 1,
292
- Sku: 'iphoneSKU',
293
- TotalAmount: 999,
294
- Variant: 'variant',
295
- },
296
- ],
297
- TaxAmount: 40,
298
- ShippingAmount: 10,
299
- CouponCode: 'coupon',
300
- },
301
- });
302
-
303
- result[1] = 'add_to_cart';
304
- window.dataLayer[0].should.eql(result);
305
-
306
- done();
307
- });
308
-
309
- it('should map MP RemoveFromCart commerce event to GA4 remove_from_cart event', function (done) {
310
- mParticle.forwarder.process({
311
- CurrencyCode: 'USD',
312
- EventName: 'Test Purchase Event',
313
- EventDataType: MessageType.Commerce,
314
- EventCategory: CommerceEventType.ProductRemoveFromCart,
315
- CustomFlags: { 'GA4.Value': 100 },
316
- ProductAction: {
317
- ProductActionType: ProductActionType.RemoveFromCart,
318
- ProductList: [
319
- {
320
- Attributes: {
321
- eventMetric1: 'metric2',
322
- journeyType: 'testjourneytype1',
323
- },
324
- Brand: 'brand',
325
- Category: 'category',
326
- CouponCode: 'coupon',
327
- Name: 'iphone',
328
- Position: 1,
329
- Price: 999,
330
- Quantity: 1,
331
- Sku: 'iphoneSKU',
332
- TotalAmount: 999,
333
- Variant: 'variant',
334
- },
335
- {
336
- Attributes: {
337
- eventMetric1: 'metric1',
338
- journeyType: 'testjourneytype2',
339
- },
340
- Brand: 'brand',
341
- Category: 'category',
342
- CouponCode: 'coupon',
343
- Name: 'iphone',
344
- Position: 1,
345
- Price: 999,
346
- Quantity: 1,
347
- Sku: 'iphoneSKU',
348
- TotalAmount: 999,
349
- Variant: 'variant',
350
- },
351
- ],
352
- TotalAmount: 450,
353
- TaxAmount: 40,
354
- ShippingAmount: 10,
355
- CouponCode: 'couponCode',
356
- },
357
- });
358
-
359
- result[1] = 'remove_from_cart';
360
- window.dataLayer[0].should.eql(result);
361
-
362
- done();
363
- });
364
-
365
- it('should map MP Checkout commerce event to GA4 begin_checkout event', function (done) {
366
- mParticle.forwarder.process({
367
- CurrencyCode: 'USD',
368
- EventName: 'Test Purchase Event',
369
- EventDataType: MessageType.Commerce,
370
- EventCategory: CommerceEventType.ProductCheckout,
371
- CustomFlags: { 'GA4.Value': 100 },
372
- ProductAction: {
373
- ProductActionType: ProductActionType.ProductCheckout,
374
- ProductList: [
375
- {
376
- Attributes: {
377
- eventMetric1: 'metric2',
378
- journeyType: 'testjourneytype1',
379
- },
380
- Brand: 'brand',
381
- Category: 'category',
382
- CouponCode: 'coupon',
383
- Name: 'iphone',
384
- Position: 1,
385
- Price: 999,
386
- Quantity: 1,
387
- Sku: 'iphoneSKU',
388
- TotalAmount: 999,
389
- Variant: 'variant',
390
- },
391
- {
392
- Attributes: {
393
- eventMetric1: 'metric1',
394
- journeyType: 'testjourneytype2',
395
- },
396
- Brand: 'brand',
397
- Category: 'category',
398
- CouponCode: 'coupon',
399
- Name: 'iphone',
400
- Position: 1,
401
- Price: 999,
402
- Quantity: 1,
403
- Sku: 'iphoneSKU',
404
- TotalAmount: 999,
405
- Variant: 'variant',
406
- },
407
- ],
408
- TotalAmount: 450,
409
- TaxAmount: 40,
410
- ShippingAmount: 10,
411
- CouponCode: 'couponCode',
412
- },
413
- });
414
-
415
- result[1] = 'begin_checkout';
416
- result[2].coupon = 'couponCode';
417
- window.dataLayer[0].should.eql(result);
418
-
419
- done();
420
- });
421
-
422
- it('should map MP Purchase commerce event to GA4 purchase event', function (done) {
423
- mParticle.forwarder.process({
424
- CurrencyCode: 'USD',
425
- EventName: 'Test Purchase Event',
426
- EventDataType: MessageType.Commerce,
427
- EventCategory: CommerceEventType.ProductPurchase,
428
- ProductAction: {
429
- ProductActionType: ProductActionType.ProductPurchase,
430
- ProductList: [
431
- {
432
- Attributes: {
433
- eventMetric1: 'metric2',
434
- journeyType: 'testjourneytype1',
435
- },
436
- Brand: 'brand',
437
- Category: 'category',
438
- CouponCode: 'coupon',
439
- Name: 'iphone',
440
- Position: 1,
441
- Price: 999,
442
- Quantity: 1,
443
- Sku: 'iphoneSKU',
444
- TotalAmount: 999,
445
- Variant: 'variant',
446
- },
447
- {
448
- Attributes: {
449
- eventMetric1: 'metric1',
450
- journeyType: 'testjourneytype2',
451
- },
452
- Brand: 'brand',
453
- Category: 'category',
454
- CouponCode: 'coupon',
455
- Name: 'iphone',
456
- Position: 1,
457
- Price: 999,
458
- Quantity: 1,
459
- Sku: 'iphoneSKU',
460
- TotalAmount: 999,
461
- Variant: 'variant',
462
- },
463
- ],
464
- Affiliation: 'foo-affiliation-id',
465
- TransactionId: 'foo-transaction-id',
466
- TotalAmount: 450,
467
- TaxAmount: 40,
468
- ShippingAmount: 10,
469
- CouponCode: 'couponCode',
470
- },
471
- });
472
-
473
- result[1] = 'purchase';
474
- result[2].coupon = 'couponCode';
475
- result[2].transaction_id = 'foo-transaction-id';
476
- result[2].affiliation = 'foo-affiliation-id';
477
- result[2].shipping = 10;
478
- result[2].tax = 40;
479
- result[2].value = 450;
480
- window.dataLayer[0].should.eql(result);
481
-
482
- done();
483
- });
484
-
485
- it('should map MP Refund commerce event to GA4 refund event', function (done) {
486
- mParticle.forwarder.process({
487
- CurrencyCode: 'USD',
488
- EventName: 'Test Purchase Event',
489
- EventDataType: MessageType.Commerce,
490
- EventCategory: CommerceEventType.ProductRefund,
491
- ProductAction: {
492
- ProductActionType: ProductActionType.ProductRefund,
493
- ProductList: [
494
- {
495
- Attributes: {
496
- eventMetric1: 'metric2',
497
- journeyType: 'testjourneytype1',
498
- },
499
- Brand: 'brand',
500
- Category: 'category',
501
- CouponCode: 'coupon',
502
- Name: 'iphone',
503
- Position: 1,
504
- Price: 999,
505
- Quantity: 1,
506
- Sku: 'iphoneSKU',
507
- TotalAmount: 999,
508
- Variant: 'variant',
509
- },
510
- {
511
- Attributes: {
512
- eventMetric1: 'metric1',
513
- journeyType: 'testjourneytype2',
514
- },
515
- Brand: 'brand',
516
- Category: 'category',
517
- CouponCode: 'coupon',
518
- Name: 'iphone',
519
- Position: 1,
520
- Price: 999,
521
- Quantity: 1,
522
- Sku: 'iphoneSKU',
523
- TotalAmount: 999,
524
- Variant: 'variant',
525
- },
526
- ],
527
- Affiliation: 'foo-affiliation-id',
528
- TransactionId: 'foo-transaction-id',
529
- TotalAmount: 450,
530
- TaxAmount: 40,
531
- ShippingAmount: 10,
532
- CouponCode: 'couponCode',
533
- },
534
- });
535
-
536
- result[1] = 'refund';
537
- result[2].coupon = 'couponCode';
538
- result[2].transaction_id = 'foo-transaction-id';
539
- result[2].affiliation = 'foo-affiliation-id';
540
- result[2].shipping = 10;
541
- result[2].tax = 40;
542
- result[2].value = 450;
543
- window.dataLayer[0].should.eql(result);
544
-
545
- done();
546
- });
547
-
548
- it('should map MP AddToWishlist commerce event to GA4 add_to_wishlist event', function (done) {
549
- mParticle.forwarder.process({
550
- CurrencyCode: 'USD',
551
- EventName: 'Test Purchase Event',
552
- EventDataType: MessageType.Commerce,
553
- EventCategory: CommerceEventType.ProductAddToWishlist,
554
- ProductAction: {
555
- ProductActionType: ProductActionType.AddToWishlist,
556
- ProductList: [
557
- {
558
- Attributes: {
559
- eventMetric1: 'metric2',
560
- journeyType: 'testjourneytype1',
561
- },
562
- Brand: 'brand',
563
- Category: 'category',
564
- CouponCode: 'coupon',
565
- Name: 'iphone',
566
- Position: 1,
567
- Price: 999,
568
- Quantity: 1,
569
- Sku: 'iphoneSKU',
570
- TotalAmount: 999,
571
- Variant: 'variant',
572
- },
573
- {
574
- Attributes: {
575
- eventMetric1: 'metric1',
576
- journeyType: 'testjourneytype2',
577
- },
578
- Brand: 'brand',
579
- Category: 'category',
580
- CouponCode: 'coupon',
581
- Name: 'iphone',
582
- Position: 1,
583
- Price: 999,
584
- Quantity: 1,
585
- Sku: 'iphoneSKU',
586
- TotalAmount: 999,
587
- Variant: 'variant',
588
- },
589
- ],
590
- TotalAmount: 450,
591
- TaxAmount: 40,
592
- ShippingAmount: 10,
593
- CouponCode: 'couponCode',
594
- },
595
- });
596
-
597
- result[1] = 'add_to_wishlist';
598
- result[2].value = 450;
599
- window.dataLayer[0].should.eql(result);
600
-
601
- done();
602
- });
603
-
604
- it('should map MP ViewDetail commerce event to GA4 view_item event', function (done) {
605
- mParticle.forwarder.process({
606
- CurrencyCode: 'USD',
607
- EventName: 'Test Purchase Event',
608
- EventDataType: MessageType.Commerce,
609
- EventCategory: CommerceEventType.ProductViewDetail,
610
- CustomFlags: { 'GA4.Value': 100 },
611
- ProductAction: {
612
- ProductActionType: ProductActionType.ViewDetail,
613
- ProductList: [
614
- {
615
- Attributes: {
616
- eventMetric1: 'metric2',
617
- journeyType: 'testjourneytype1',
618
- },
619
- Brand: 'brand',
620
- Category: 'category',
621
- CouponCode: 'coupon',
622
- Name: 'iphone',
623
- Position: 1,
624
- Price: 999,
625
- Quantity: 1,
626
- Sku: 'iphoneSKU',
627
- TotalAmount: 999,
628
- Variant: 'variant',
629
- },
630
- {
631
- Attributes: {
632
- eventMetric1: 'metric1',
633
- journeyType: 'testjourneytype2',
634
- },
635
- Brand: 'brand',
636
- Category: 'category',
637
- CouponCode: 'coupon',
638
- Name: 'iphone',
639
- Position: 1,
640
- Price: 999,
641
- Quantity: 1,
642
- Sku: 'iphoneSKU',
643
- TotalAmount: 999,
644
- Variant: 'variant',
645
- },
646
- ],
647
- TaxAmount: 40,
648
- ShippingAmount: 10,
649
- CouponCode: 'couponCode',
650
- },
651
- });
652
-
653
- result[1] = 'view_item';
654
- window.dataLayer[0].should.eql(result);
655
-
656
- done();
657
- });
658
-
659
- it('should map MP ProductImpression commerce event to GA4 view_item_list event', function (done) {
660
- mParticle.forwarder.process({
661
- CurrencyCode: 'USD',
662
- EventName: 'Test Purchase Event',
663
- EventDataType: MessageType.Commerce,
664
- EventCategory: CommerceEventType.ProductImpression,
665
- ProductImpressions: [
666
- {
667
- // TODO: Does this map to the name or id of the impression?
668
- ProductImpressionList: 'Related Products',
669
- ProductList: [
670
- {
671
- Attributes: {
672
- eventMetric1: 'metric2',
673
- journeyType: 'testjourneytype1',
674
- },
675
- Brand: 'brand',
676
- Category: 'category',
677
- CouponCode: 'coupon',
678
- Name: 'iphone',
679
- Position: 1,
680
- Price: 999,
681
- Quantity: 1,
682
- Sku: 'iphoneSKU',
683
- TotalAmount: 999,
684
- Variant: 'variant',
685
- },
686
- {
687
- Attributes: {
688
- eventMetric1: 'metric1',
689
- journeyType: 'testjourneytype2',
690
- },
691
- Brand: 'brand',
692
- Category: 'category',
693
- CouponCode: 'coupon',
694
- Name: 'iphone',
695
- Position: 1,
696
- Price: 999,
697
- Quantity: 1,
698
- Sku: 'iphoneSKU',
699
- TotalAmount: 999,
700
- Variant: 'variant',
701
- },
702
- ],
703
- TaxAmount: 40,
704
- ShippingAmount: 10,
705
- CouponCode: 'couponCode',
706
- },
707
- ],
708
- });
709
-
710
- result = [
711
- 'event',
712
- 'view_item_list',
713
- {
714
- item_list_id: 'Related Products',
715
- item_list_name: 'Related Products',
716
- items: [
717
- {
718
- attributes: {
719
- eventMetric1: 'metric2',
720
- journeyType: 'testjourneytype1',
721
- },
722
- coupon_code: 'coupon',
723
- item_brand: 'brand',
724
- item_category: 'category',
725
- item_id: 'iphoneSKU',
726
- item_name: 'iphone',
727
- item_variant: 'variant',
728
- position: 1,
729
- price: 999,
730
- quantity: 1,
731
- total_amount: 999,
732
- },
733
- {
734
- attributes: {
735
- eventMetric1: 'metric1',
736
- journeyType: 'testjourneytype2',
737
- },
738
- coupon_code: 'coupon',
739
- item_brand: 'brand',
740
- item_category: 'category',
741
- item_id: 'iphoneSKU',
742
- item_name: 'iphone',
743
- item_variant: 'variant',
744
- position: 1,
745
- price: 999,
746
- quantity: 1,
747
- total_amount: 999,
748
- },
749
- ],
750
- },
751
- ];
752
-
753
- window.dataLayer[0].should.eql(result);
754
-
755
- done();
756
- });
757
-
758
- it('should map MP Click commerce event to GA4 select_item event', function (done) {
759
- mParticle.forwarder.process({
760
- CurrencyCode: 'USD',
761
- EventName: 'Test Purchase Event',
762
- EventDataType: MessageType.Commerce,
763
- EventCategory: CommerceEventType.ProductClick,
764
- ProductAction: {
765
- ProductActionType: ProductActionType.Click,
766
- ProductList: [
767
- {
768
- Attributes: {
769
- eventMetric1: 'metric2',
770
- journeyType: 'testjourneytype1',
771
- },
772
- Brand: 'brand',
773
- Category: 'category',
774
- CouponCode: 'coupon',
775
- Name: 'iphone',
776
- Position: 1,
777
- Price: 999,
778
- Quantity: 1,
779
- Sku: 'iphoneSKU',
780
- TotalAmount: 999,
781
- Variant: 'variant',
782
- },
783
- {
784
- Attributes: {
785
- eventMetric1: 'metric1',
786
- journeyType: 'testjourneytype2',
787
- },
788
- Brand: 'brand',
789
- Category: 'category',
790
- CouponCode: 'coupon',
791
- Name: 'iphone',
792
- Position: 1,
793
- Price: 999,
794
- Quantity: 1,
795
- Sku: 'iphoneSKU',
796
- TotalAmount: 999,
797
- Variant: 'variant',
798
- },
799
- ],
800
- TotalAmount: 450,
801
- TaxAmount: 40,
802
- ShippingAmount: 10,
803
- CouponCode: 'couponCode',
804
- },
805
- });
806
-
807
- result[1] = 'select_item';
808
- // select items do not include currency of value, which is by default on the result
809
- delete result[2]['currency'];
810
- delete result[2]['value'];
811
- window.dataLayer[0].should.eql(result);
812
-
813
- done();
814
- });
815
-
816
- it('should map MP PromotionView commerce event to GA4 view_promotion event', function (done) {
817
- mParticle.forwarder.process({
818
- CurrencyCode: 'USD',
819
- EventName: 'Test Promotion Action Event',
820
- EventDataType: MessageType.Commerce,
821
- EventCategory: CommerceEventType.PromotionView,
822
- PromotionAction: {
823
- PromotionActionType: PromotionActionType.PromotionView,
824
- PromotionList: [
825
- {
826
- Id: 'P_12345',
827
- Name: 'Summer Sale Banner',
828
- Creative: 'Summer Sale',
829
- Position: 'featured_app_1',
830
- },
831
- {
832
- Id: 'P_78901',
833
- Name: 'Winter Sale Banner',
834
- Creative: 'Winter Sale',
835
- Position: 'featured_app_2',
836
- },
837
- ],
838
- },
839
- });
840
-
841
- promotionResult1 = [
842
- 'event',
843
- 'view_promotion',
844
- {
845
- promotion_id: 'P_12345',
846
- promotion_name: 'Summer Sale Banner',
847
- creative_name: 'Summer Sale',
848
- creative_slot: 'featured_app_1',
849
- },
850
- ];
851
-
852
- var promotionResult2 = [
853
- 'event',
854
- 'view_promotion',
855
- {
856
- promotion_id: 'P_78901',
857
- promotion_name: 'Winter Sale Banner',
858
- creative_name: 'Winter Sale',
859
- creative_slot: 'featured_app_2',
860
- },
861
- ];
862
-
863
- window.dataLayer[0].should.eql(promotionResult1);
864
- window.dataLayer[1].should.eql(promotionResult2);
865
-
866
- done();
867
- });
868
-
869
- it('should map MP PromotionClick commerce event to GA4 select_promotion event', function (done) {
870
- mParticle.forwarder.process({
871
- CurrencyCode: 'USD',
872
- EventName: 'Test Purchase Event',
873
- EventDataType: MessageType.Commerce,
874
- EventCategory: CommerceEventType.PromotionClick,
875
- PromotionAction: {
876
- PromotionActionType: PromotionActionType.PromotionClick,
877
- PromotionList: [
878
- {
879
- Id: 'P_12345',
880
- Name: 'Summer Sale Banner',
881
- Creative: 'Summer Sale',
882
- Position: 'featured_app_1',
883
- },
884
- {
885
- Id: 'P_78901',
886
- Name: 'Winter Sale Banner',
887
- Creative: 'Winter Sale',
888
- Position: 'featured_app_2',
889
- },
890
- ],
891
- },
892
- });
893
-
894
- promotionResult1 = [
895
- 'event',
896
- 'select_promotion',
897
- {
898
- promotion_id: 'P_12345',
899
- promotion_name: 'Summer Sale Banner',
900
- creative_name: 'Summer Sale',
901
- creative_slot: 'featured_app_1',
902
- },
903
- ];
904
-
905
- var promotionResult2 = [
906
- 'event',
907
- 'select_promotion',
908
- {
909
- promotion_id: 'P_78901',
910
- promotion_name: 'Winter Sale Banner',
911
- creative_name: 'Winter Sale',
912
- creative_slot: 'featured_app_2',
913
- },
914
- ];
915
-
916
- window.dataLayer[0].should.eql(promotionResult1);
917
- window.dataLayer[1].should.eql(promotionResult2);
918
-
919
- done();
920
- });
921
-
922
- it('should map MP CheckoutOption commerce event with add_payment_info custom flag to GA4 add_payment_info event', function (done) {
923
- mParticle.forwarder.process({
924
- CurrencyCode: 'USD',
925
- EventName: 'Test add_payment_info Event',
926
- EventDataType: MessageType.Commerce,
927
- EventCategory: CommerceEventType.ProductCheckoutOption,
928
- CustomFlags: {
929
- 'GA4.CommerceEventType': 'add_payment_info',
930
- 'GA4.PaymentType': 'credit-card',
931
- },
932
- ProductAction: {
933
- ProductActionType: ProductActionType.Click,
934
- ProductList: [
935
- {
936
- Attributes: {
937
- eventMetric1: 'metric2',
938
- journeyType: 'testjourneytype1',
939
- },
940
- Brand: 'brand',
941
- Category: 'category',
942
- CouponCode: 'coupon',
943
- Name: 'iphone',
944
- Position: 1,
945
- Price: 999,
946
- Quantity: 1,
947
- Sku: 'iphoneSKU',
948
- TotalAmount: 999,
949
- Variant: 'variant',
950
- },
951
- {
952
- Attributes: {
953
- eventMetric1: 'metric1',
954
- journeyType: 'testjourneytype2',
955
- },
956
- Brand: 'brand',
957
- Category: 'category',
958
- CouponCode: 'coupon',
959
- Name: 'iphone',
960
- Position: 1,
961
- Price: 999,
962
- Quantity: 1,
963
- Sku: 'iphoneSKU',
964
- TotalAmount: 999,
965
- Variant: 'variant',
966
- },
967
- ],
968
- TotalAmount: 450,
969
- TaxAmount: 40,
970
- ShippingAmount: 10,
971
- CouponCode: 'couponCode',
972
- },
973
- });
974
-
975
- result = [
976
- 'event',
977
- 'add_payment_info',
978
- {
979
- payment_type: 'credit-card',
980
- coupon: 'couponCode',
981
- items: [
982
- {
983
- attributes: {
984
- eventMetric1: 'metric2',
985
- journeyType: 'testjourneytype1',
986
- },
987
- coupon_code: 'coupon',
988
- item_brand: 'brand',
989
- item_category: 'category',
990
- item_id: 'iphoneSKU',
991
- item_name: 'iphone',
992
- item_variant: 'variant',
993
- position: 1,
994
- price: 999,
995
- quantity: 1,
996
- total_amount: 999,
997
- },
998
- {
999
- attributes: {
1000
- eventMetric1: 'metric1',
1001
- journeyType: 'testjourneytype2',
1002
- },
1003
- coupon_code: 'coupon',
1004
- item_brand: 'brand',
1005
- item_category: 'category',
1006
- item_id: 'iphoneSKU',
1007
- item_name: 'iphone',
1008
- item_variant: 'variant',
1009
- position: 1,
1010
- price: 999,
1011
- quantity: 1,
1012
- total_amount: 999,
1013
- },
1014
- ],
1015
- },
1016
- ];
1017
-
1018
- window.dataLayer[0].should.match(result);
1019
-
1020
- done();
1021
- });
1022
-
1023
- it('should map MP CheckoutOption commerce event with add_shipping_info custom flag to GA4 add_shipping_info event', function (done) {
1024
- mParticle.forwarder.process({
1025
- CurrencyCode: 'USD',
1026
- EventName: 'Test add_shipping_info Event',
1027
- EventDataType: MessageType.Commerce,
1028
- EventCategory: CommerceEventType.ProductCheckoutOption,
1029
- CustomFlags: {
1030
- 'GA4.CommerceEventType': 'add_shipping_info',
1031
- 'GA4.ShippingTier': 'ground',
1032
- },
1033
- ProductAction: {
1034
- ProductActionType: ProductActionType.Click,
1035
- ProductList: [
1036
- {
1037
- Attributes: {
1038
- eventMetric1: 'metric2',
1039
- journeyType: 'testjourneytype1',
1040
- },
1041
- Brand: 'brand',
1042
- Category: 'category',
1043
- CouponCode: 'coupon',
1044
- Name: 'iphone',
1045
- Position: 1,
1046
- Price: 999,
1047
- Quantity: 1,
1048
- Sku: 'iphoneSKU',
1049
- TotalAmount: 999,
1050
- Variant: 'variant',
1051
- },
1052
- {
1053
- Attributes: {
1054
- eventMetric1: 'metric1',
1055
- journeyType: 'testjourneytype2',
1056
- },
1057
- Brand: 'brand',
1058
- Category: 'category',
1059
- CouponCode: 'coupon',
1060
- Name: 'iphone',
1061
- Position: 1,
1062
- Price: 999,
1063
- Quantity: 1,
1064
- Sku: 'iphoneSKU',
1065
- TotalAmount: 999,
1066
- Variant: 'variant',
1067
- },
1068
- ],
1069
- TotalAmount: 450,
1070
- TaxAmount: 40,
1071
- ShippingAmount: 10,
1072
- CouponCode: 'couponCode',
1073
- },
1074
- });
1075
-
1076
- result = [
1077
- 'event',
1078
- 'add_shipping_info',
1079
- {
1080
- shipping_tier: 'ground',
1081
- coupon: 'couponCode',
1082
- items: [
1083
- {
1084
- attributes: {
1085
- eventMetric1: 'metric2',
1086
- journeyType: 'testjourneytype1',
1087
- },
1088
- coupon_code: 'coupon',
1089
- item_brand: 'brand',
1090
- item_category: 'category',
1091
- item_id: 'iphoneSKU',
1092
- item_name: 'iphone',
1093
- item_variant: 'variant',
1094
- position: 1,
1095
- price: 999,
1096
- quantity: 1,
1097
- total_amount: 999,
1098
- },
1099
- {
1100
- attributes: {
1101
- eventMetric1: 'metric1',
1102
- journeyType: 'testjourneytype2',
1103
- },
1104
- coupon_code: 'coupon',
1105
- item_brand: 'brand',
1106
- item_category: 'category',
1107
- item_id: 'iphoneSKU',
1108
- item_name: 'iphone',
1109
- item_variant: 'variant',
1110
- position: 1,
1111
- price: 999,
1112
- quantity: 1,
1113
- total_amount: 999,
1114
- },
1115
- ],
1116
- },
1117
- ];
1118
-
1119
- window.dataLayer[0].should.match(result);
1120
-
1121
- done();
1122
- });
1123
-
1124
- it('should log a `set_checkout_option` event if a CheckoutOption is sent without a custom flag for GA4.CommerceEventType', function (done) {
1125
- mParticle.forwarder.process({
1126
- CurrencyCode: 'USD',
1127
- EventName: 'Test add_payment_info Event',
1128
- EventDataType: MessageType.Commerce,
1129
- EventCategory: CommerceEventType.ProductCheckoutOption,
1130
- EventAttributes: {
1131
- foo: 'bar',
1132
- },
1133
- CustomFlags: {},
1134
- ProductAction: {
1135
- ProductActionType: ProductActionType.Click,
1136
- ProductList: [],
1137
- },
1138
- });
1139
-
1140
- result = [
1141
- 'event',
1142
- 'set_checkout_option',
1143
- {
1144
- items: [],
1145
- foo: 'bar',
1146
- },
1147
- ];
1148
- window.dataLayer.length.should.eql(1);
1149
-
1150
- done();
1151
- });
1152
-
1153
- it('should log an event if a CheckoutOption is sent with GA4.CommerceEventType but without GA4.ShippingTier', function (done) {
1154
- mParticle.forwarder.process({
1155
- CurrencyCode: 'USD',
1156
- EventName: 'Test add_payment_info Event',
1157
- EventDataType: MessageType.Commerce,
1158
- EventCategory: CommerceEventType.ProductCheckoutOption,
1159
- CustomFlags: {
1160
- 'GA4.CommerceEventType': 'add_shipping_info',
1161
- },
1162
- ProductAction: {
1163
- ProductActionType: ProductActionType.Click,
1164
- ProductList: [],
1165
- },
1166
- });
1167
-
1168
- result = [
1169
- 'event',
1170
- 'add_shipping_info',
1171
- {
1172
- shipping_tier: null,
1173
- coupon: null,
1174
- items: [],
1175
- },
1176
- ];
1177
- window.dataLayer[0].should.eql(result);
1178
-
1179
- done();
1180
- });
1181
-
1182
- it('should log an event if a CheckoutOption is sent on with GA4.CommerceEventType but without GA4.PaymentType', function (done) {
1183
- mParticle.forwarder.process({
1184
- CurrencyCode: 'USD',
1185
- EventName: 'Test add_payment_info Event',
1186
- EventDataType: MessageType.Commerce,
1187
- EventCategory: CommerceEventType.ProductCheckoutOption,
1188
- CustomFlags: {
1189
- 'GA4.CommerceEventType': 'add_payment_info',
1190
- },
1191
- ProductAction: {
1192
- ProductActionType: ProductActionType.Click,
1193
- ProductList: [],
1194
- },
1195
- });
1196
-
1197
- result = [
1198
- 'event',
1199
- 'add_payment_info',
1200
- {
1201
- payment_type: null,
1202
- coupon: null,
1203
- items: [],
1204
- },
1205
- ];
1206
- window.dataLayer[0].should.eql(result);
1207
-
1208
- done();
1209
- });
1210
-
1211
- it('should log a view_cart event using ProductActionType.Unknown and a custom flag', function (done) {
1212
- mParticle.forwarder.process({
1213
- CurrencyCode: 'USD',
1214
- EventName: 'Unknown Test',
1215
- EventDataType: MessageType.Commerce,
1216
- EventCategory: CommerceEventType.Unknown,
1217
- CustomFlags: {
1218
- 'GA4.CommerceEventType': 'view_cart',
1219
- 'GA4.Value': 100,
1220
- },
1221
- ProductAction: {
1222
- ProductActionType: ProductActionType.Unknown,
1223
- ProductList: [
1224
- {
1225
- Attributes: {
1226
- eventMetric1: 'metric2',
1227
- journeyType: 'testjourneytype1',
1228
- },
1229
- Brand: 'brand',
1230
- Category: 'category',
1231
- CouponCode: 'coupon',
1232
- Name: 'iphone',
1233
- Position: 1,
1234
- Price: 999,
1235
- Quantity: 1,
1236
- Sku: 'iphoneSKU',
1237
- TotalAmount: 999,
1238
- Variant: 'variant',
1239
- },
1240
- {
1241
- Attributes: {
1242
- eventMetric1: 'metric1',
1243
- journeyType: 'testjourneytype2',
1244
- },
1245
- Brand: 'brand',
1246
- Category: 'category',
1247
- CouponCode: 'coupon',
1248
- Name: 'iphone',
1249
- Position: 1,
1250
- Price: 999,
1251
- Quantity: 1,
1252
- Sku: 'iphoneSKU',
1253
- TotalAmount: 999,
1254
- Variant: 'variant',
1255
- },
1256
- ],
1257
- },
1258
- });
1259
-
1260
- result = [
1261
- 'event',
1262
- 'view_cart',
1263
- {
1264
- value: 100,
1265
- currency: 'USD',
1266
- items: [
1267
- {
1268
- attributes: {
1269
- eventMetric1: 'metric2',
1270
- journeyType: 'testjourneytype1',
1271
- },
1272
- coupon_code: 'coupon',
1273
- item_brand: 'brand',
1274
- item_category: 'category',
1275
- item_id: 'iphoneSKU',
1276
- item_name: 'iphone',
1277
- item_variant: 'variant',
1278
- position: 1,
1279
- price: 999,
1280
- quantity: 1,
1281
- total_amount: 999,
1282
- },
1283
- {
1284
- attributes: {
1285
- eventMetric1: 'metric1',
1286
- journeyType: 'testjourneytype2',
1287
- },
1288
- coupon_code: 'coupon',
1289
- item_brand: 'brand',
1290
- item_category: 'category',
1291
- item_id: 'iphoneSKU',
1292
- item_name: 'iphone',
1293
- item_variant: 'variant',
1294
- position: 1,
1295
- price: 999,
1296
- quantity: 1,
1297
- total_amount: 999,
1298
- },
1299
- ],
1300
- },
1301
- ];
1302
-
1303
- window.dataLayer[0].should.eql(result);
1304
-
1305
- done();
1306
- });
1307
-
1308
- it('should not log an Unknown Product Action Type with no custom flags', function (done) {
1309
- mParticle.forwarder.process({
1310
- CurrencyCode: 'USD',
1311
- EventName: 'Unknown Test',
1312
- EventDataType: MessageType.Commerce,
1313
- EventCategory: CommerceEventType.Unknown,
1314
- CustomFlags: {},
1315
- });
1316
-
1317
- window.dataLayer.length.should.eql(0);
1318
-
1319
- done();
1320
- });
1321
- });
1322
-
1323
- describe('event mapping', function () {
1324
- it('should log the event name and event attributes of the page event', function (done) {
1325
- mParticle.forwarder.process({
1326
- EventDataType: MessageType.PageEvent,
1327
- EventCategory: EventType.Navigation,
1328
- EventName: 'Unmapped Event Name',
1329
- EventAttributes: {
1330
- foo: 'bar',
1331
- },
1332
- });
1333
-
1334
- var result = ['event', 'Unmapped Event Name', { foo: 'bar' }];
1335
- window.dataLayer[0].should.eql(result);
1336
-
1337
- done();
1338
- });
1339
-
1340
- it('should log page view', function (done) {
1341
- mParticle.forwarder.process({
1342
- EventDataType: MessageType.PageView,
1343
- EventName: 'test name',
1344
- EventAttributes: {
1345
- eventKey1: 'test1',
1346
- eventKey2: 'test2',
1347
- },
1348
- CustomFlags: {
1349
- 'Google.Title': 'Foo Page Title',
1350
- 'Google.Location': '/foo',
1351
- },
1352
- });
1353
-
1354
- var result = [
1355
- 'event',
1356
- 'page_view',
1357
- { page_title: 'Foo Page Title', page_location: '/foo' },
1358
- ];
1359
- window.dataLayer[0].should.eql(result);
1360
-
1361
- done();
1362
- });
1363
- });
1364
- });
1365
-
1366
- describe('identity', function () {
1367
- var mParticleUser = {
1368
- getMPID: function () {
1369
- return 'testMPID';
1370
- },
1371
- getUserIdentities: function () {
1372
- return {
1373
- userIdentities: {
1374
- customerid: 'testCustomerId',
1375
- other: 'other1',
1376
- },
1377
- };
1378
- },
1379
- };
1380
-
1381
- beforeEach(function () {
1382
- window.dataLayer = [];
1383
- });
1384
-
1385
- it('should handle onUserIdentified with customerid', function (done) {
1386
- kitSettings.externalUserIdentityType = 'CustomerId';
1387
- mParticle.forwarder.init(kitSettings, reportService.cb, true, null);
1388
-
1389
- mParticle.forwarder.onUserIdentified(mParticleUser);
1390
-
1391
- var result = [
1392
- 'config',
1393
- 'testMeasurementId',
1394
- { send_page_view: false, user_id: 'testCustomerId' },
1395
- ];
1396
- window.dataLayer[0].should.match(result);
1397
-
1398
- done();
1399
- });
1400
-
1401
- it('should handle onUserIdentified with other1', function (done) {
1402
- kitSettings.externalUserIdentityType = 'Other';
1403
- mParticle.forwarder.init(kitSettings, reportService.cb, true, null);
1404
-
1405
- mParticle.forwarder.onUserIdentified(mParticleUser);
1406
-
1407
- var result = [
1408
- 'config',
1409
- 'testMeasurementId',
1410
- { send_page_view: false, user_id: 'other1' },
1411
- ];
1412
- window.dataLayer[0].should.match(result);
1413
-
1414
- done();
1415
- });
1416
-
1417
- it('should handle onUserIdentified with mpid', function (done) {
1418
- kitSettings.externalUserIdentityType = 'mpid';
1419
- mParticle.forwarder.init(kitSettings, reportService.cb, true, null);
1420
-
1421
- mParticle.forwarder.onUserIdentified(mParticleUser);
1422
-
1423
- var result = [
1424
- 'config',
1425
- 'testMeasurementId',
1426
- { send_page_view: false, user_id: 'testMPID' },
1427
- ];
1428
- window.dataLayer[0].should.match(result);
1429
-
1430
- done();
1431
- });
1432
- });
1433
- });