@mparticle/web-google-analytics-4-client-kit 1.4.0 → 1.4.2

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.
@@ -94,7 +94,11 @@ ConsentHandler.prototype.generateConsentStatePayloadFromMappings = function (
94
94
 
95
95
  for (var i = 0; i <= mappings.length - 1; i++) {
96
96
  var mappingEntry = mappings[i];
97
- var mpMappedConsentName = mappingEntry.map;
97
+
98
+ // Although consent purposes can be inputted into the UI in any casing
99
+ // the SDK will automatically lowercase them to prevent pseudo-duplicate
100
+ // consent purposes, so we call `toLowerCase` on the consentMapping purposes here
101
+ var mpMappedConsentName = mappingEntry.map.toLowerCase();
98
102
  var googleMappedConsentName = mappingEntry.value;
99
103
 
100
104
  if (
@@ -494,18 +498,18 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
494
498
  event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
495
499
  ) {
496
500
  isViewCartEvent = true;
497
- return logViewCart(event, affiliation);
501
+ return this.logViewCart(event, affiliation);
498
502
  }
499
503
  }
500
504
  // Handle Impressions
501
505
  if (event.EventCategory === ProductActionTypes.Impression) {
502
- return logImpressionEvent(event, affiliation);
506
+ return this.logImpressionEvent(event, affiliation);
503
507
  // Handle Promotions
504
508
  } else if (
505
509
  event.EventCategory === PromotionActionTypes.PromotionClick ||
506
510
  event.EventCategory === PromotionActionTypes.PromotionView
507
511
  ) {
508
- return logPromotionEvent(event);
512
+ return this.logPromotionEvent(event);
509
513
  }
510
514
 
511
515
  switch (event.EventCategory) {
@@ -539,7 +543,7 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
539
543
  break;
540
544
 
541
545
  case ProductActionTypes.CheckoutOption:
542
- return logCheckoutOptionEvent(event, affiliation);
546
+ return this.logCheckoutOptionEvent(event, affiliation);
543
547
 
544
548
  default:
545
549
  // a view cart event is handled at the beginning of this function
@@ -576,10 +580,146 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
576
580
  null;
577
581
  }
578
582
 
579
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
583
+ return this.sendCommerceEventToGA4(
584
+ mapGA4EcommerceEventName(event),
585
+ ga4CommerceEventParameters
586
+ );
587
+ };
588
+
589
+ CommerceHandler.prototype.sendCommerceEventToGA4 = function (
590
+ eventName,
591
+ eventAttributes
592
+ ) {
593
+ if (this.common.forwarderSettings.measurementId) {
594
+ eventAttributes.send_to = this.common.forwarderSettings.measurementId;
595
+ }
596
+
597
+ gtag('event', eventName, eventAttributes);
598
+
580
599
  return true;
581
600
  };
582
601
 
602
+ // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
603
+ // Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
604
+ // migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
605
+ CommerceHandler.prototype.logCheckoutOptionEvent = function (
606
+ event,
607
+ affiliation
608
+ ) {
609
+ try {
610
+ var customFlags = event.CustomFlags,
611
+ GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
612
+ ga4CommerceEventParameters;
613
+
614
+ // if no custom flags exist or there is no GA4CommerceEventType, the user has not updated their code from using legacy GA which still supports checkout_option
615
+ if (!customFlags || !GA4CommerceEventType) {
616
+ console.error(
617
+ 'Your checkout option event for the Google Analytics 4 integration is missing a custom flag for "GA4.CommerceEventType". The event was sent using the deprecated set_checkout_option event. Review mParticle docs for GA4 for the custom flags to add.'
618
+ );
619
+ }
620
+
621
+ switch (GA4CommerceEventType) {
622
+ case ADD_SHIPPING_INFO:
623
+ ga4CommerceEventParameters = buildAddShippingInfo(
624
+ event,
625
+ affiliation
626
+ );
627
+ break;
628
+ case ADD_PAYMENT_INFO:
629
+ ga4CommerceEventParameters = buildAddPaymentInfo(
630
+ event,
631
+ affiliation
632
+ );
633
+ break;
634
+ default:
635
+ ga4CommerceEventParameters = buildCheckoutOptions(
636
+ event,
637
+ affiliation
638
+ );
639
+ break;
640
+ }
641
+ } catch (error) {
642
+ console.error(
643
+ 'There is an issue with the custom flags in your CheckoutOption event. The event was not sent. Plrease review the docs and fix.',
644
+ error
645
+ );
646
+ return false;
647
+ }
648
+
649
+ return this.sendCommerceEventToGA4(
650
+ mapGA4EcommerceEventName(event),
651
+ ga4CommerceEventParameters
652
+ );
653
+ };
654
+
655
+ CommerceHandler.prototype.logPromotionEvent = function (event) {
656
+ var self = this;
657
+ try {
658
+ var ga4CommerceEventParameters;
659
+ event.PromotionAction.PromotionList.forEach(function (promotion) {
660
+ ga4CommerceEventParameters = buildPromotion(promotion);
661
+
662
+ self.sendCommerceEventToGA4(
663
+ mapGA4EcommerceEventName(event),
664
+ ga4CommerceEventParameters
665
+ );
666
+ });
667
+
668
+ return true;
669
+ } catch (error) {
670
+ console.error(
671
+ 'Error logging Promotions to GA4. Promotions not logged.',
672
+ error
673
+ );
674
+ return false;
675
+ }
676
+ };
677
+
678
+ CommerceHandler.prototype.logImpressionEvent = function (event, affiliation) {
679
+ var self = this;
680
+ try {
681
+ var ga4CommerceEventParameters;
682
+ event.ProductImpressions.forEach(function (impression) {
683
+ ga4CommerceEventParameters = parseImpression(
684
+ impression,
685
+ affiliation
686
+ );
687
+
688
+ self.sendCommerceEventToGA4(
689
+ mapGA4EcommerceEventName(event),
690
+ ga4CommerceEventParameters
691
+ );
692
+ });
693
+
694
+ return true;
695
+ } catch (error) {
696
+ console.log(
697
+ 'Error logging Impressions to GA4. Impressions not logged',
698
+ error
699
+ );
700
+ return false;
701
+ }
702
+ };
703
+
704
+ CommerceHandler.prototype.logViewCart = function (event, affiliation) {
705
+ var ga4CommerceEventParameters = buildViewCart(event, affiliation);
706
+ ga4CommerceEventParameters = this.common.mergeObjects(
707
+ ga4CommerceEventParameters,
708
+ this.common.limitEventAttributes(event.EventAttributes)
709
+ );
710
+ ga4CommerceEventParameters.currency = event.CurrencyCode;
711
+
712
+ ga4CommerceEventParameters.value =
713
+ (event.CustomFlags && event.CustomFlags['GA4.Value']) ||
714
+ event.ProductAction.TotalAmount ||
715
+ null;
716
+
717
+ return this.sendCommerceEventToGA4(
718
+ mapGA4EcommerceEventName(event),
719
+ ga4CommerceEventParameters
720
+ );
721
+ };
722
+
583
723
  function buildAddOrRemoveCartItem(event, affiliation) {
584
724
  return {
585
725
  items: buildProductsList(event.ProductAction.ProductList, affiliation),
@@ -834,117 +974,6 @@ function getCheckoutOptionEventName(customFlags) {
834
974
  }
835
975
  }
836
976
 
837
- // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
838
- // Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
839
- // migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
840
- function logCheckoutOptionEvent(event, affiliation) {
841
- try {
842
- var customFlags = event.CustomFlags,
843
- GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
844
- ga4CommerceEventParameters;
845
-
846
- // if no custom flags exist or there is no GA4CommerceEventType, the user has not updated their code from using legacy GA which still supports checkout_option
847
- if (!customFlags || !GA4CommerceEventType) {
848
- console.error(
849
- 'Your checkout option event for the Google Analytics 4 integration is missing a custom flag for "GA4.CommerceEventType". The event was sent using the deprecated set_checkout_option event. Review mParticle docs for GA4 for the custom flags to add.'
850
- );
851
- }
852
-
853
- switch (GA4CommerceEventType) {
854
- case ADD_SHIPPING_INFO:
855
- ga4CommerceEventParameters = buildAddShippingInfo(
856
- event,
857
- affiliation
858
- );
859
- break;
860
- case ADD_PAYMENT_INFO:
861
- ga4CommerceEventParameters = buildAddPaymentInfo(
862
- event,
863
- affiliation
864
- );
865
- break;
866
- default:
867
- ga4CommerceEventParameters = buildCheckoutOptions(
868
- event,
869
- affiliation
870
- );
871
- break;
872
- }
873
- } catch (error) {
874
- console.error(
875
- 'There is an issue with the custom flags in your CheckoutOption event. The event was not sent. Plrease review the docs and fix.',
876
- error
877
- );
878
- return false;
879
- }
880
-
881
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
882
-
883
- return true;
884
- }
885
-
886
- function logPromotionEvent(event) {
887
- try {
888
- var ga4CommerceEventParameters;
889
- event.PromotionAction.PromotionList.forEach(function (promotion) {
890
- ga4CommerceEventParameters = buildPromotion(promotion);
891
- gtag(
892
- 'event',
893
- mapGA4EcommerceEventName(event),
894
- ga4CommerceEventParameters
895
- );
896
- });
897
- return true;
898
- } catch (error) {
899
- console.error(
900
- 'Error logging Promotions to GA4. Promotions not logged.',
901
- error
902
- );
903
- }
904
- return false;
905
- }
906
-
907
- function logImpressionEvent(event, affiliation) {
908
- try {
909
- var ga4CommerceEventParameters;
910
- event.ProductImpressions.forEach(function (impression) {
911
- ga4CommerceEventParameters = parseImpression(
912
- impression,
913
- affiliation
914
- );
915
-
916
- gtag(
917
- 'event',
918
- mapGA4EcommerceEventName(event),
919
- ga4CommerceEventParameters
920
- );
921
- });
922
- } catch (error) {
923
- console.log(
924
- 'Error logging Impressions to GA4. Impressions not logged',
925
- error
926
- );
927
- return false;
928
- }
929
- return true;
930
- }
931
-
932
- function logViewCart(event, affiliation) {
933
- var ga4CommerceEventParameters = buildViewCart(event, affiliation);
934
- ga4CommerceEventParameters = self$1.common.mergeObjects(
935
- ga4CommerceEventParameters,
936
- self$1.common.limitEventAttributes(event.EventAttributes)
937
- );
938
- ga4CommerceEventParameters.currency = event.CurrencyCode;
939
-
940
- ga4CommerceEventParameters.value =
941
- (event.CustomFlags && event.CustomFlags['GA4.Value']) ||
942
- event.ProductAction.TotalAmount ||
943
- null;
944
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
945
- return true;
946
- }
947
-
948
977
  function buildViewCart(event, affiliation) {
949
978
  return {
950
979
  items: buildProductsList(event.ProductAction.ProductList, affiliation),
@@ -1000,6 +1029,11 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
1000
1029
  standardizedAttributes
1001
1030
  );
1002
1031
 
1032
+ if (this.common.forwarderSettings.measurementId) {
1033
+ standardizedAttributes.send_to =
1034
+ this.common.forwarderSettings.measurementId;
1035
+ }
1036
+
1003
1037
  gtag(
1004
1038
  'event',
1005
1039
  this.common.truncateEventName(standardizedEventName),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-google-analytics-4-client-kit",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
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",