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

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.
@@ -494,18 +494,18 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
494
494
  event.CustomFlags[GA4_COMMERCE_EVENT_TYPE] === VIEW_CART
495
495
  ) {
496
496
  isViewCartEvent = true;
497
- return logViewCart(event, affiliation);
497
+ return this.logViewCart(event, affiliation);
498
498
  }
499
499
  }
500
500
  // Handle Impressions
501
501
  if (event.EventCategory === ProductActionTypes.Impression) {
502
- return logImpressionEvent(event, affiliation);
502
+ return this.logImpressionEvent(event, affiliation);
503
503
  // Handle Promotions
504
504
  } else if (
505
505
  event.EventCategory === PromotionActionTypes.PromotionClick ||
506
506
  event.EventCategory === PromotionActionTypes.PromotionView
507
507
  ) {
508
- return logPromotionEvent(event);
508
+ return this.logPromotionEvent(event);
509
509
  }
510
510
 
511
511
  switch (event.EventCategory) {
@@ -539,7 +539,7 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
539
539
  break;
540
540
 
541
541
  case ProductActionTypes.CheckoutOption:
542
- return logCheckoutOptionEvent(event, affiliation);
542
+ return this.logCheckoutOptionEvent(event, affiliation);
543
543
 
544
544
  default:
545
545
  // a view cart event is handled at the beginning of this function
@@ -576,10 +576,146 @@ CommerceHandler.prototype.logCommerceEvent = function (event) {
576
576
  null;
577
577
  }
578
578
 
579
- gtag('event', mapGA4EcommerceEventName(event), ga4CommerceEventParameters);
579
+ return this.sendCommerceEventToGA4(
580
+ mapGA4EcommerceEventName(event),
581
+ ga4CommerceEventParameters
582
+ );
583
+ };
584
+
585
+ CommerceHandler.prototype.sendCommerceEventToGA4 = function (
586
+ eventName,
587
+ eventAttributes
588
+ ) {
589
+ if (this.common.forwarderSettings.measurementId) {
590
+ eventAttributes.send_to = this.common.forwarderSettings.measurementId;
591
+ }
592
+
593
+ gtag('event', eventName, eventAttributes);
594
+
580
595
  return true;
581
596
  };
582
597
 
598
+ // Google previously had a CheckoutOption event, and now this has been split into 2 GA4 events - add_shipping_info and add_payment_info
599
+ // Since MP still uses CheckoutOption, we must map this to the 2 events using custom flags. To prevent any data loss from customers
600
+ // migrating from UA to GA4, we will set a default `set_checkout_option` which matches Firebase's data model.
601
+ CommerceHandler.prototype.logCheckoutOptionEvent = function (
602
+ event,
603
+ affiliation
604
+ ) {
605
+ try {
606
+ var customFlags = event.CustomFlags,
607
+ GA4CommerceEventType = customFlags[GA4_COMMERCE_EVENT_TYPE],
608
+ ga4CommerceEventParameters;
609
+
610
+ // 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
611
+ if (!customFlags || !GA4CommerceEventType) {
612
+ console.error(
613
+ '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.'
614
+ );
615
+ }
616
+
617
+ switch (GA4CommerceEventType) {
618
+ case ADD_SHIPPING_INFO:
619
+ ga4CommerceEventParameters = buildAddShippingInfo(
620
+ event,
621
+ affiliation
622
+ );
623
+ break;
624
+ case ADD_PAYMENT_INFO:
625
+ ga4CommerceEventParameters = buildAddPaymentInfo(
626
+ event,
627
+ affiliation
628
+ );
629
+ break;
630
+ default:
631
+ ga4CommerceEventParameters = buildCheckoutOptions(
632
+ event,
633
+ affiliation
634
+ );
635
+ break;
636
+ }
637
+ } catch (error) {
638
+ console.error(
639
+ 'There is an issue with the custom flags in your CheckoutOption event. The event was not sent. Plrease review the docs and fix.',
640
+ error
641
+ );
642
+ return false;
643
+ }
644
+
645
+ return this.sendCommerceEventToGA4(
646
+ mapGA4EcommerceEventName(event),
647
+ ga4CommerceEventParameters
648
+ );
649
+ };
650
+
651
+ CommerceHandler.prototype.logPromotionEvent = function (event) {
652
+ var self = this;
653
+ try {
654
+ var ga4CommerceEventParameters;
655
+ event.PromotionAction.PromotionList.forEach(function (promotion) {
656
+ ga4CommerceEventParameters = buildPromotion(promotion);
657
+
658
+ self.sendCommerceEventToGA4(
659
+ mapGA4EcommerceEventName(event),
660
+ ga4CommerceEventParameters
661
+ );
662
+ });
663
+
664
+ return true;
665
+ } catch (error) {
666
+ console.error(
667
+ 'Error logging Promotions to GA4. Promotions not logged.',
668
+ error
669
+ );
670
+ return false;
671
+ }
672
+ };
673
+
674
+ CommerceHandler.prototype.logImpressionEvent = function (event, affiliation) {
675
+ var self = this;
676
+ try {
677
+ var ga4CommerceEventParameters;
678
+ event.ProductImpressions.forEach(function (impression) {
679
+ ga4CommerceEventParameters = parseImpression(
680
+ impression,
681
+ affiliation
682
+ );
683
+
684
+ self.sendCommerceEventToGA4(
685
+ mapGA4EcommerceEventName(event),
686
+ ga4CommerceEventParameters
687
+ );
688
+ });
689
+
690
+ return true;
691
+ } catch (error) {
692
+ console.log(
693
+ 'Error logging Impressions to GA4. Impressions not logged',
694
+ error
695
+ );
696
+ return false;
697
+ }
698
+ };
699
+
700
+ CommerceHandler.prototype.logViewCart = function (event, affiliation) {
701
+ var ga4CommerceEventParameters = buildViewCart(event, affiliation);
702
+ ga4CommerceEventParameters = this.common.mergeObjects(
703
+ ga4CommerceEventParameters,
704
+ this.common.limitEventAttributes(event.EventAttributes)
705
+ );
706
+ ga4CommerceEventParameters.currency = event.CurrencyCode;
707
+
708
+ ga4CommerceEventParameters.value =
709
+ (event.CustomFlags && event.CustomFlags['GA4.Value']) ||
710
+ event.ProductAction.TotalAmount ||
711
+ null;
712
+
713
+ return this.sendCommerceEventToGA4(
714
+ mapGA4EcommerceEventName(event),
715
+ ga4CommerceEventParameters
716
+ );
717
+ };
718
+
583
719
  function buildAddOrRemoveCartItem(event, affiliation) {
584
720
  return {
585
721
  items: buildProductsList(event.ProductAction.ProductList, affiliation),
@@ -834,117 +970,6 @@ function getCheckoutOptionEventName(customFlags) {
834
970
  }
835
971
  }
836
972
 
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
973
  function buildViewCart(event, affiliation) {
949
974
  return {
950
975
  items: buildProductsList(event.ProductAction.ProductList, affiliation),
@@ -1000,6 +1025,11 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
1000
1025
  standardizedAttributes
1001
1026
  );
1002
1027
 
1028
+ if (this.common.forwarderSettings.measurementId) {
1029
+ standardizedAttributes.send_to =
1030
+ this.common.forwarderSettings.measurementId;
1031
+ }
1032
+
1003
1033
  gtag(
1004
1034
  'event',
1005
1035
  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.1",
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",