@mparticle/web-google-analytics-4-client-kit 1.1.0 → 1.3.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.
@@ -4,14 +4,29 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  // Google requires event and user attribute strings to have specific limits
6
6
  // in place when sending to data layer.
7
- // https://support.google.com/analytics/answer/9267744?hl=en
7
+ // https://support.google.com/analytics/answer/11202874?sjid=7958830619827381593-NA
8
8
 
9
9
  var EVENT_NAME_MAX_LENGTH = 40;
10
10
  var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40;
11
11
  var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100;
12
+ var EVENT_ATTRIBUTE_MAX_NUMBER = 100;
13
+
12
14
  var USER_ATTRIBUTE_KEY_MAX_LENGTH = 24;
13
15
  var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36;
14
16
 
17
+ var PRODUCT_ATTRIBUTE_MAX_NUMBER = 10;
18
+
19
+ var RESERVED_PRODUCT_KEYS = [
20
+ 'item_category',
21
+ 'item_category2',
22
+ 'item_category3',
23
+ 'item_category4',
24
+ 'item_category5',
25
+ ];
26
+
27
+ var FORBIDDEN_PREFIXES = ['google_', 'firebase_', 'ga_'];
28
+ var FORBIDDEN_CHARACTERS_REGEX = /[^a-zA-Z0-9_]/g;
29
+
15
30
  function truncateString(string, limit) {
16
31
  return !!string && string.length > limit
17
32
  ? string.substring(0, limit)
@@ -56,6 +71,49 @@ Common.prototype.truncateAttributes = function (
56
71
  return truncatedAttributes;
57
72
  };
58
73
 
74
+ Common.prototype.limitAttributes = function (attributes, limitNumber) {
75
+ if (isEmpty(attributes)) {
76
+ return {};
77
+ }
78
+
79
+ var attributeKeys = Object.keys(attributes);
80
+
81
+ attributeKeys.sort();
82
+
83
+ var limitedAttributes = attributeKeys
84
+ .slice(0, limitNumber)
85
+ .reduce(function (obj, key) {
86
+ obj[key] = attributes[key];
87
+ return obj;
88
+ }, {});
89
+
90
+ return limitedAttributes;
91
+ };
92
+
93
+ Common.prototype.limitEventAttributes = function (attributes) {
94
+ return this.limitAttributes(attributes, EVENT_ATTRIBUTE_MAX_NUMBER);
95
+ };
96
+
97
+ Common.prototype.limitProductAttributes = function (attributes) {
98
+ var productAttributes = {};
99
+ var reservedAttributes = {};
100
+
101
+ for (var key in attributes) {
102
+ if (RESERVED_PRODUCT_KEYS.indexOf(key) >= 0) {
103
+ reservedAttributes[key] = attributes[key];
104
+ } else {
105
+ productAttributes[key] = attributes[key];
106
+ }
107
+ }
108
+
109
+ var limitedProductAttributes = this.limitAttributes(
110
+ productAttributes,
111
+ PRODUCT_ATTRIBUTE_MAX_NUMBER
112
+ );
113
+
114
+ return this.mergeObjects(limitedProductAttributes, reservedAttributes);
115
+ };
116
+
59
117
  Common.prototype.truncateEventName = function (eventName) {
60
118
  return truncateString(eventName, EVENT_NAME_MAX_LENGTH);
61
119
  };
@@ -68,6 +126,92 @@ Common.prototype.truncateEventAttributes = function (eventAttributes) {
68
126
  );
69
127
  };
70
128
 
129
+ Common.prototype.standardizeParameters = function (parameters) {
130
+ var standardizedParameters = {};
131
+ for (var key in parameters) {
132
+ var standardizedKey = this.standardizeName(key);
133
+ standardizedParameters[standardizedKey] = parameters[key];
134
+ }
135
+ return standardizedParameters;
136
+ };
137
+
138
+ Common.prototype.standardizeName = function (name) {
139
+ try {
140
+ name = window.GoogleAnalytics4Kit.setCustomNameStandardization(name);
141
+ } catch (e) {
142
+ console.error(
143
+ 'Error calling setCustomNameStandardization callback. Check your callback. Data will still be sent without user-defined standardization. See our docs for proper use - https://docs.mparticle.com/integrations/google-analytics-4/event/',
144
+ e
145
+ );
146
+ }
147
+
148
+ // names of events and parameters have the following requirements:
149
+ // 1. They must only contain letters, numbers, and underscores
150
+ function removeForbiddenCharacters(name) {
151
+ return name.replace(FORBIDDEN_CHARACTERS_REGEX, '_');
152
+ }
153
+
154
+ // 2. They must start with a letter
155
+ function doesNameStartsWithLetter(name) {
156
+ return !isEmpty(name) && /^[a-zA-Z]/.test(name.charAt(0));
157
+ }
158
+
159
+ // 3. They must not start with certain prefixes
160
+ function doesNameStartWithForbiddenPrefix(name) {
161
+ var hasPrefix = false;
162
+ if (!isEmpty(name)) {
163
+ for (var i = 0; i < FORBIDDEN_PREFIXES.length; i++) {
164
+ var prefix = FORBIDDEN_PREFIXES[i];
165
+ if (name.indexOf(prefix) === 0) {
166
+ hasPrefix = true;
167
+ break;
168
+ }
169
+ }
170
+ }
171
+
172
+ return hasPrefix;
173
+ }
174
+
175
+ function removeNonAlphabetCharacterFromStart(name) {
176
+ var str = name.slice();
177
+ while (!isEmpty(str) && str.charAt(0).match(/[^a-zA-Z]/i)) {
178
+ str = str.substring(1);
179
+ }
180
+ return str;
181
+ }
182
+
183
+ function removeForbiddenPrefix(name) {
184
+ var str = name.slice();
185
+
186
+ FORBIDDEN_PREFIXES.forEach(function (prefix) {
187
+ if (str.indexOf(prefix) === 0) {
188
+ str = str.replace(prefix, '');
189
+ }
190
+ });
191
+
192
+ return str;
193
+ }
194
+
195
+ var standardizedName = removeForbiddenCharacters(name);
196
+
197
+ // While loops is required because there is a chance that once certain sanitization
198
+ // occurs, that the resulting string will end up violating a different criteria.
199
+ // An example is 123___google_$$google_test_event. If letters, are removed and
200
+ // prefix is removed once, the remaining string will be __google_test_event which violates
201
+ // a string starting with a letter. We have to repeat the sanitizations repeatedly
202
+ // until all criteria checks pass.
203
+ while (
204
+ !doesNameStartsWithLetter(standardizedName) ||
205
+ doesNameStartWithForbiddenPrefix(standardizedName)
206
+ ) {
207
+ standardizedName =
208
+ removeNonAlphabetCharacterFromStart(standardizedName);
209
+ standardizedName = removeForbiddenPrefix(standardizedName);
210
+ }
211
+
212
+ return standardizedName;
213
+ };
214
+
71
215
  Common.prototype.truncateUserAttributes = function (userAttributes) {
72
216
  return this.truncateAttributes(
73
217
  userAttributes,
@@ -165,8 +309,11 @@ Common.prototype.getUserId = function (
165
309
 
166
310
  var common = Common;
167
311
 
312
+ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
313
+
314
+ var self$1 = commonjsGlobal;
168
315
  function CommerceHandler(common) {
169
- this.common = common || {};
316
+ self$1.common = this.common = common || {};
170
317
  }
171
318
 
172
319
  var ProductActionTypes = {
@@ -200,8 +347,14 @@ var ADD_SHIPPING_INFO = 'add_shipping_info',
200
347
  CommerceHandler.prototype.logCommerceEvent = function (event) {
201
348
  var needsCurrency = true,
202
349
  needsValue = true,
203
- ga4CommerceEventParameters,
204
- isViewCartEvent = false;
350
+ ga4CommerceEventParameters = {},
351
+ isViewCartEvent = false,
352
+ customEventAttributes = event.EventAttributes || {},
353
+ // affiliation potentially lives on any commerce event with items
354
+ affiliation =
355
+ event && event.ProductAction
356
+ ? event.ProductAction.Affiliation
357
+ : null;
205
358
 
206
359
  // GA4 has a view_cart event which MP does not support via a ProductActionType
207
360
  // In order to log a view_cart event, pass ProductActionType.Unknown along with
@@ -214,12 +367,12 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
214
367
  event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
215
368
  ) {
216
369
  isViewCartEvent = true;
217
- return logViewCart(event);
370
+ return logViewCart(event, affiliation);
218
371
  }
219
372
  }
220
373
  // Handle Impressions
221
374
  if (event.EventCategory === ProductActionTypes.Impression) {
222
- return logImpressionEvent(event);
375
+ return logImpressionEvent(event, affiliation);
223
376
  // Handle Promotions
224
377
  } else if (
225
378
  event.EventCategory === PromotionActionTypes.PromotionClick ||
@@ -228,43 +381,38 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
228
381
  return logPromotionEvent(event);
229
382
  }
230
383
 
231
- ga4CommerceEventParameters = buildParameters(event);
232
- if (event.EventAttributes) {
233
- ga4CommerceEventParameters = this.common.mergeObjects(
234
- ga4CommerceEventParameters,
235
- event.EventAttributes
236
- );
237
- }
238
-
239
384
  switch (event.EventCategory) {
240
385
  case ProductActionTypes.AddToCart:
241
386
  case ProductActionTypes.RemoveFromCart:
242
- ga4CommerceEventParameters = buildAddOrRemoveCartItem(event);
387
+ ga4CommerceEventParameters = buildAddOrRemoveCartItem(
388
+ event,
389
+ affiliation
390
+ );
243
391
  break;
244
392
  case ProductActionTypes.Checkout:
245
- ga4CommerceEventParameters = buildCheckout(event);
393
+ ga4CommerceEventParameters = buildCheckout(event, affiliation);
246
394
  break;
247
395
  case ProductActionTypes.Click:
248
- ga4CommerceEventParameters = buildSelectItem(event);
396
+ ga4CommerceEventParameters = buildSelectItem(event, affiliation);
249
397
 
250
398
  needsCurrency = false;
251
399
  needsValue = false;
252
400
  break;
253
401
  case ProductActionTypes.Purchase:
254
- ga4CommerceEventParameters = buildPurchase(event);
402
+ ga4CommerceEventParameters = buildPurchase(event, affiliation);
255
403
  break;
256
404
  case ProductActionTypes.Refund:
257
- ga4CommerceEventParameters = buildRefund(event);
405
+ ga4CommerceEventParameters = buildRefund(event, affiliation);
258
406
  break;
259
407
  case ProductActionTypes.ViewDetail:
260
- ga4CommerceEventParameters = buildViewItem(event);
408
+ ga4CommerceEventParameters = buildViewItem(event, affiliation);
261
409
  break;
262
410
  case ProductActionTypes.AddToWishlist:
263
- ga4CommerceEventParameters = buildAddToWishlist(event);
411
+ ga4CommerceEventParameters = buildAddToWishlist(event, affiliation);
264
412
  break;
265
413
 
266
414
  case ProductActionTypes.CheckoutOption:
267
- return logCheckoutOptionEvent(event);
415
+ return logCheckoutOptionEvent(event, affiliation);
268
416
 
269
417
  default:
270
418
  // a view cart event is handled at the beginning of this function
@@ -277,6 +425,18 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
277
425
  }
278
426
  }
279
427
 
428
+ // TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5714
429
+ if (this.common.forwarderSettings.enableDataCleansing) {
430
+ customEventAttributes = this.common.standardizeParameters(
431
+ customEventAttributes
432
+ );
433
+ }
434
+
435
+ ga4CommerceEventParameters = this.common.mergeObjects(
436
+ ga4CommerceEventParameters,
437
+ this.common.limitEventAttributes(customEventAttributes)
438
+ );
439
+
280
440
  // CheckoutOption, Promotions, and Impressions will not make it to this code
281
441
  if (needsCurrency) {
282
442
  ga4CommerceEventParameters.currency = event.CurrencyCode;
@@ -293,29 +453,25 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
293
453
  return true;
294
454
  };
295
455
 
296
- function buildParameters(event) {
456
+ function buildAddOrRemoveCartItem(event, affiliation) {
297
457
  return {
298
- items: buildProductsList(event.ProductAction.ProductList),
299
- coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
300
- };
301
- }
302
-
303
- function buildAddOrRemoveCartItem(event) {
304
- return {
305
- items: buildProductsList(event.ProductAction.ProductList),
458
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
306
459
  };
307
460
  }
308
461
 
309
- function buildCheckout(event) {
462
+ function buildCheckout(event, affiliation) {
310
463
  return {
311
- items: buildProductsList(event.ProductAction.ProductList),
464
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
312
465
  coupon: event.ProductAction ? event.ProductAction.CouponCode : null,
313
466
  };
314
467
  }
315
468
 
316
- function buildCheckoutOptions(event) {
469
+ function buildCheckoutOptions(event, affiliation) {
317
470
  var parameters = event.EventAttributes;
318
- parameters.items = buildProductsList(event.ProductAction.ProductList);
471
+ parameters.items = buildProductsList(
472
+ event.ProductAction.ProductList,
473
+ affiliation
474
+ );
319
475
  parameters.coupon = event.ProductAction
320
476
  ? event.ProductAction.CouponCode
321
477
  : null;
@@ -323,23 +479,23 @@ function buildCheckoutOptions(event) {
323
479
  return parameters;
324
480
  }
325
481
 
326
- function parseImpression(impression) {
482
+ function parseImpression(impression, affiliation) {
327
483
  return {
328
484
  item_list_id: impression.ProductImpressionList,
329
485
  item_list_name: impression.ProductImpressionList,
330
- items: buildProductsList(impression.ProductList),
486
+ items: buildProductsList(impression.ProductList, affiliation),
331
487
  };
332
488
  }
333
489
 
334
- function buildSelectItem(event) {
490
+ function buildSelectItem(event, affiliation) {
335
491
  return {
336
- items: buildProductsList(event.ProductAction.ProductList),
492
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
337
493
  };
338
494
  }
339
495
 
340
- function buildViewItem(event) {
496
+ function buildViewItem(event, affiliation) {
341
497
  return {
342
- items: buildProductsList(event.ProductAction.ProductList),
498
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
343
499
  };
344
500
  }
345
501
 
@@ -347,36 +503,34 @@ function buildPromotion(promotion) {
347
503
  return parsePromotion(promotion);
348
504
  }
349
505
 
350
- function buildPurchase(event) {
506
+ function buildPurchase(event, affiliation) {
351
507
  return {
352
508
  transaction_id: event.ProductAction.TransactionId,
353
509
  value: event.ProductAction.TotalAmount,
354
- affiliation: event.ProductAction.Affiliation,
355
510
  coupon: event.ProductAction.CouponCode,
356
511
  shipping: event.ProductAction.ShippingAmount,
357
512
  tax: event.ProductAction.TaxAmount,
358
- items: buildProductsList(event.ProductAction.ProductList),
513
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
359
514
  };
360
515
  }
361
- function buildRefund(event) {
516
+ function buildRefund(event, affiliation) {
362
517
  return {
363
518
  transaction_id: event.ProductAction.TransactionId,
364
519
  value: event.ProductAction.TotalAmount,
365
- affiliation: event.ProductAction.Affiliation,
366
520
  coupon: event.ProductAction.CouponCode,
367
521
  shipping: event.ProductAction.ShippingAmount,
368
522
  tax: event.ProductAction.TaxAmount,
369
- items: buildProductsList(event.ProductAction.ProductList),
523
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
370
524
  };
371
525
  }
372
- function buildAddToWishlist(event) {
526
+ function buildAddToWishlist(event, affiliation) {
373
527
  return {
374
528
  value: event.ProductAction.TotalAmount,
375
- items: buildProductsList(event.ProductAction.ProductList),
529
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
376
530
  };
377
531
  }
378
532
 
379
- function buildAddShippingInfo(event) {
533
+ function buildAddShippingInfo(event, affiliation) {
380
534
  return {
381
535
  coupon:
382
536
  event.ProductAction && event.ProductAction.CouponCode
@@ -386,11 +540,11 @@ function buildAddShippingInfo(event) {
386
540
  event.CustomFlags && event.CustomFlags[GA4_SHIPPING_TIER]
387
541
  ? event.CustomFlags[GA4_SHIPPING_TIER]
388
542
  : null,
389
- items: buildProductsList(event.ProductAction.ProductList),
543
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
390
544
  };
391
545
  }
392
546
 
393
- function buildAddPaymentInfo(event) {
547
+ function buildAddPaymentInfo(event, affiliation) {
394
548
  return {
395
549
  coupon:
396
550
  event.ProductAction && event.ProductAction.CouponCode
@@ -400,7 +554,7 @@ function buildAddPaymentInfo(event) {
400
554
  event.CustomFlags && event.CustomFlags[GA4_PAYMENT_TYPE]
401
555
  ? event.CustomFlags[GA4_PAYMENT_TYPE]
402
556
  : null,
403
- items: buildProductsList(event.ProductAction.ProductList),
557
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
404
558
  };
405
559
  }
406
560
 
@@ -412,32 +566,55 @@ function toUnderscore(string) {
412
566
  .toLowerCase();
413
567
  }
414
568
 
415
- function parseProduct(_product) {
416
- var product = {};
569
+ function parseProduct(product, affiliation) {
570
+ // 1. Move key/value pairs from product.Attributes to be at the same level
571
+ // as all keys in product, limiting them to 10 in the process.
417
572
 
418
- for (var key in _product) {
573
+ var productWithAllAttributes = {};
574
+
575
+ if (affiliation) {
576
+ productWithAllAttributes.affiliation = affiliation;
577
+ }
578
+
579
+ productWithAllAttributes = self$1.common.mergeObjects(
580
+ productWithAllAttributes,
581
+ self$1.common.limitProductAttributes(product.Attributes)
582
+ );
583
+
584
+ // 2. Copy key/value pairs in product
585
+ for (var key in product) {
419
586
  switch (key) {
420
587
  case 'Sku':
421
- product.item_id = _product.Sku;
588
+ productWithAllAttributes.item_id = product.Sku;
422
589
  break;
423
590
  case 'Name':
424
- product.item_name = _product.Name;
591
+ productWithAllAttributes.item_name = product.Name;
425
592
  break;
426
593
  case 'Brand':
427
- product.item_brand = _product.Brand;
594
+ productWithAllAttributes.item_brand = product.Brand;
428
595
  break;
429
596
  case 'Category':
430
- product.item_category = _product.Category;
597
+ productWithAllAttributes.item_category = product.Category;
598
+ break;
599
+ case 'CouponCode':
600
+ productWithAllAttributes.coupon = product.CouponCode;
431
601
  break;
432
602
  case 'Variant':
433
- product.item_variant = _product.Variant;
603
+ productWithAllAttributes.item_variant = product.Variant;
604
+ break;
605
+ case 'Attributes':
434
606
  break;
435
607
  default:
436
- product[toUnderscore(key)] = _product[key];
608
+ productWithAllAttributes[toUnderscore(key)] = product[key];
437
609
  }
438
610
  }
439
611
 
440
- return product;
612
+ // TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5716
613
+ if (self$1.common.forwarderSettings.enableDataCleansing) {
614
+ return self$1.common.standardizeParameters(productWithAllAttributes);
615
+ } else {
616
+ return productWithAllAttributes;
617
+ }
441
618
  }
442
619
 
443
620
  function parsePromotion(_promotion) {
@@ -462,14 +639,18 @@ function parsePromotion(_promotion) {
462
639
  }
463
640
  }
464
641
 
465
- return promotion;
642
+ if (self$1.common.forwarderSettings.enableDataCleansing) {
643
+ return self$1.common.standardizeParameters(promotion);
644
+ } else {
645
+ return promotion;
646
+ }
466
647
  }
467
648
 
468
- function buildProductsList(products) {
649
+ function buildProductsList(products, affiliation) {
469
650
  var productsList = [];
470
651
 
471
652
  products.forEach(function (product) {
472
- productsList.push(parseProduct(product));
653
+ productsList.push(parseProduct(product, affiliation));
473
654
  });
474
655
 
475
656
  return productsList;
@@ -529,7 +710,7 @@ function getCheckoutOptionEventName(customFlags) {
529
710
  // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
530
711
  // Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
531
712
  // migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
532
- function logCheckoutOptionEvent(event) {
713
+ function logCheckoutOptionEvent(event, affiliation) {
533
714
  try {
534
715
  var customFlags = event.CustomFlags,
535
716
  GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
@@ -544,13 +725,22 @@ function logCheckoutOptionEvent(event) {
544
725
 
545
726
  switch (GA4CommerceEventType) {
546
727
  case ADD_SHIPPING_INFO:
547
- ga4CommerceEventParameters = buildAddShippingInfo(event);
728
+ ga4CommerceEventParameters = buildAddShippingInfo(
729
+ event,
730
+ affiliation
731
+ );
548
732
  break;
549
733
  case ADD_PAYMENT_INFO:
550
- ga4CommerceEventParameters = buildAddPaymentInfo(event);
734
+ ga4CommerceEventParameters = buildAddPaymentInfo(
735
+ event,
736
+ affiliation
737
+ );
551
738
  break;
552
739
  default:
553
- ga4CommerceEventParameters = buildCheckoutOptions(event);
740
+ ga4CommerceEventParameters = buildCheckoutOptions(
741
+ event,
742
+ affiliation
743
+ );
554
744
  break;
555
745
  }
556
746
  } catch (error) {
@@ -587,11 +777,14 @@ function logPromotionEvent(event) {
587
777
  return false;
588
778
  }
589
779
 
590
- function logImpressionEvent(event) {
780
+ function logImpressionEvent(event, affiliation) {
591
781
  try {
592
782
  var ga4CommerceEventParameters;
593
783
  event.ProductImpressions.forEach(function (impression) {
594
- ga4CommerceEventParameters = parseImpression(impression);
784
+ ga4CommerceEventParameters = parseImpression(
785
+ impression,
786
+ affiliation
787
+ );
595
788
 
596
789
  gtag(
597
790
  'event',
@@ -609,8 +802,12 @@ function logImpressionEvent(event) {
609
802
  return true;
610
803
  }
611
804
 
612
- function logViewCart(event) {
613
- var ga4CommerceEventParameters = buildViewCart(event);
805
+ function logViewCart(event, affiliation) {
806
+ var ga4CommerceEventParameters = buildViewCart(event, affiliation);
807
+ ga4CommerceEventParameters = self$1.common.mergeObjects(
808
+ ga4CommerceEventParameters,
809
+ self$1.common.limitEventAttributes(event.EventAttributes)
810
+ );
614
811
  ga4CommerceEventParameters.currency = event.CurrencyCode;
615
812
 
616
813
  ga4CommerceEventParameters.value =
@@ -621,9 +818,9 @@ function logViewCart(event) {
621
818
  return true;
622
819
  }
623
820
 
624
- function buildViewCart(event) {
821
+ function buildViewCart(event, affiliation) {
625
822
  return {
626
- items: buildProductsList(event.ProductAction.ProductList),
823
+ items: buildProductsList(event.ProductAction.ProductList, affiliation),
627
824
  };
628
825
  }
629
826
 
@@ -633,11 +830,27 @@ function EventHandler(common) {
633
830
  this.common = common || {};
634
831
  }
635
832
 
833
+ // TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
636
834
  EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
835
+ var standardizedEventName;
836
+ var standardizedAttributes;
837
+ if (this.common.forwarderSettings.enableDataCleansing) {
838
+ standardizedEventName = this.common.standardizeName(eventName);
839
+ standardizedAttributes =
840
+ this.common.standardizeParameters(eventAttributes);
841
+ } else {
842
+ standardizedEventName = eventName;
843
+ standardizedAttributes = eventAttributes;
844
+ }
845
+
846
+ standardizedAttributes = this.common.limitEventAttributes(
847
+ standardizedAttributes
848
+ );
849
+
637
850
  gtag(
638
851
  'event',
639
- this.common.truncateEventName(eventName),
640
- this.common.truncateEventAttributes(eventAttributes)
852
+ this.common.truncateEventName(standardizedEventName),
853
+ this.common.truncateEventAttributes(standardizedAttributes)
641
854
  );
642
855
  };
643
856
 
@@ -754,6 +967,8 @@ IdentityHandler.prototype.onSetUserIdentity = function (
754
967
  var identityHandler = IdentityHandler;
755
968
 
756
969
  var initialization = {
970
+ // This name matches the mParticle database. This should not be changed unless the database name is being changed
971
+ // Changing this will also break the API for the cleansing data callback that a customer uses.
757
972
  name: 'GoogleAnalytics4',
758
973
  moduleId: 160,
759
974
  /* ****** Fill out initForwarder to load your SDK ******
@@ -776,10 +991,20 @@ var initialization = {
776
991
  ) {
777
992
  mParticle._setIntegrationDelay(this.moduleId, true);
778
993
 
994
+ // The API to allow a customer to provide the cleansing callback
995
+ // relies on window.GoogleAnalytics4Kit to exist. When MP is initialized
996
+ // via the snippet, the kit code adds it to the window automatically.
997
+ // However, when initialized via npm, it does not exist, and so we have
998
+ // to set it manually here.
999
+ window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {};
1000
+
779
1001
  common.forwarderSettings = forwarderSettings;
1002
+ common.forwarderSettings.enableDataCleansing =
1003
+ common.forwarderSettings.enableDataCleansing === 'True';
780
1004
  var measurementId = forwarderSettings.measurementId;
781
1005
  var userIdType = forwarderSettings.externalUserIdentityType;
782
1006
  var hashUserId = forwarderSettings.hashUserId;
1007
+
783
1008
  var configSettings = {
784
1009
  send_page_view: forwarderSettings.enablePageView === 'True',
785
1010
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-google-analytics-4-client-kit",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
5
  "description": "mParticle integration sdk for Google Analytics",
6
6
  "main": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",