@mparticle/web-google-analytics-4-client-kit 1.0.4 → 1.0.6

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/README.md CHANGED
@@ -11,7 +11,7 @@ npm i @mparticle/web-google-analytics-4-client-kit
11
11
 
12
12
  Full documentation on usage can be found [here](https://docs.mparticle.com/integrations/google-analytics-4/event/).
13
13
 
14
- #License
14
+ # License
15
15
 
16
16
  Copyright 2022 mParticle, Inc.
17
17
 
@@ -2,6 +2,26 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ // Google requires event and user attribute strings to have specific limits
6
+ // in place when sending to data layer.
7
+ // https://support.google.com/analytics/answer/9267744?hl=en
8
+
9
+ var EVENT_NAME_MAX_LENGTH = 40;
10
+ var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40;
11
+ var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100;
12
+ var USER_ATTRIBUTE_KEY_MAX_LENGTH = 24;
13
+ var USER_ATTRIBUTE_VALUE_MAX_LENGTH = 36;
14
+
15
+ function truncateString(string, limit) {
16
+ return !!string && string.length > limit
17
+ ? string.substring(0, limit)
18
+ : string;
19
+ }
20
+
21
+ function isEmpty(value) {
22
+ return value == null || !(Object.keys(value) || value).length;
23
+ }
24
+
5
25
  function Common() {}
6
26
 
7
27
  Common.prototype.forwarderSettings = null;
@@ -18,6 +38,44 @@ Common.prototype.mergeObjects = function () {
18
38
  return resObj;
19
39
  };
20
40
 
41
+ Common.prototype.truncateAttributes = function (
42
+ attributes,
43
+ keyLimit,
44
+ valueLimit
45
+ ) {
46
+ var truncatedAttributes = {};
47
+
48
+ if (!isEmpty(attributes)) {
49
+ Object.keys(attributes).forEach(function (attribute) {
50
+ var key = truncateString(attribute, keyLimit);
51
+ var val = truncateString(attributes[attribute], valueLimit);
52
+ truncatedAttributes[key] = val;
53
+ });
54
+ }
55
+
56
+ return truncatedAttributes;
57
+ };
58
+
59
+ Common.prototype.truncateEventName = function (eventName) {
60
+ return truncateString(eventName, EVENT_NAME_MAX_LENGTH);
61
+ };
62
+
63
+ Common.prototype.truncateEventAttributes = function (eventAttributes) {
64
+ return this.truncateAttributes(
65
+ eventAttributes,
66
+ EVENT_ATTRIBUTE_KEY_MAX_LENGTH,
67
+ EVENT_ATTRIBUTE_VAL_MAX_LENGTH
68
+ );
69
+ };
70
+
71
+ Common.prototype.truncateUserAttributes = function (userAttributes) {
72
+ return this.truncateAttributes(
73
+ userAttributes,
74
+ USER_ATTRIBUTE_KEY_MAX_LENGTH,
75
+ USER_ATTRIBUTE_VALUE_MAX_LENGTH
76
+ );
77
+ };
78
+
21
79
  Common.prototype.getUserId = function (
22
80
  user,
23
81
  externalUserIdentityType,
@@ -575,8 +633,16 @@ function EventHandler(common) {
575
633
  this.common = common || {};
576
634
  }
577
635
 
636
+ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
637
+ gtag(
638
+ 'event',
639
+ this.common.truncateEventName(eventName),
640
+ this.common.truncateEventAttributes(eventAttributes)
641
+ );
642
+ };
643
+
578
644
  EventHandler.prototype.logEvent = function (event) {
579
- gtag('event', event.EventName, event.EventAttributes || {});
645
+ this.sendEventToGA4(event.EventName, event.EventAttributes);
580
646
  };
581
647
 
582
648
  EventHandler.prototype.logError = function () {
@@ -584,20 +650,28 @@ EventHandler.prototype.logError = function () {
584
650
  };
585
651
 
586
652
  EventHandler.prototype.logPageView = function (event) {
587
- var TITLE = 'Google.Title';
588
- var LOCATION = 'Google.Location';
589
- var pageTitle, pageLocation;
653
+ var TITLE = 'GA4.Title';
654
+ var LOCATION = 'GA4.Location';
590
655
 
591
- if (event.CustomFlags && event.CustomFlags.hasOwnProperty(TITLE)) {
592
- pageTitle = event.CustomFlags[TITLE];
593
- } else {
656
+ // These are being included for backwards compatibility from the legacy Google Analytics custom flags
657
+ var LEGACY_GA_TITLE = 'Google.Title';
658
+ var LEGACY_GA_LOCATION = 'Google.Location';
659
+
660
+ var pageLocation = location.href,
594
661
  pageTitle = document.title;
595
- }
596
662
 
597
- if (event.CustomFlags && event.CustomFlags.hasOwnProperty(LOCATION)) {
598
- pageLocation = event.CustomFlags[LOCATION];
599
- } else {
600
- pageLocation = location.href;
663
+ if (event.CustomFlags) {
664
+ if (event.CustomFlags.hasOwnProperty(TITLE)) {
665
+ pageTitle = event.CustomFlags[TITLE];
666
+ } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_TITLE)) {
667
+ pageTitle = event.CustomFlags[LEGACY_GA_TITLE];
668
+ }
669
+
670
+ if (event.CustomFlags.hasOwnProperty(LOCATION)) {
671
+ pageLocation = event.CustomFlags[LOCATION];
672
+ } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_LOCATION)) {
673
+ pageLocation = event.CustomFlags[LEGACY_GA_LOCATION];
674
+ }
601
675
  }
602
676
 
603
677
  var eventAttributes = this.common.mergeObjects(
@@ -608,7 +682,7 @@ EventHandler.prototype.logPageView = function (event) {
608
682
  event.EventAttributes
609
683
  );
610
684
 
611
- gtag('event', 'page_view', eventAttributes);
685
+ this.sendEventToGA4('page_view', eventAttributes);
612
686
 
613
687
  return true;
614
688
  };
@@ -711,6 +785,21 @@ var initialization = {
711
785
  window.gtag = function () {
712
786
  window.dataLayer.push(arguments);
713
787
  };
788
+ gtag('js', new Date());
789
+
790
+ // Check to see if mParticle SDK is V2 because V1 does not have the
791
+ // Identity API
792
+ if (window.mParticle.getVersion().split('.')[0] === '2') {
793
+ var userId = common.getUserId(
794
+ mParticle.Identity.getCurrentUser(),
795
+ userIdType,
796
+ hashUserId
797
+ );
798
+ if (userId) {
799
+ configSettings.user_id = userId;
800
+ }
801
+ }
802
+ gtag('config', measurementId, configSettings);
714
803
 
715
804
  if (!testMode) {
716
805
  var clientScript = document.createElement('script');
@@ -724,19 +813,6 @@ var initialization = {
724
813
  ).appendChild(clientScript);
725
814
 
726
815
  clientScript.onload = function () {
727
- gtag('js', new Date());
728
-
729
- if (window.mParticle.getVersion().split('.')[0] === '2') {
730
- var userId = common.getUserId(
731
- mParticle.Identity.getCurrentUser(),
732
- userIdType,
733
- hashUserId
734
- );
735
- if (userId) {
736
- configSettings.user_id = userId;
737
- }
738
- }
739
- gtag('config', measurementId, configSettings);
740
816
  isInitialized = true;
741
817
 
742
818
  if (window.dataLayer && gtag && eventQueue.length > 0) {
@@ -770,20 +846,30 @@ function UserAttributeHandler(common) {
770
846
  this.common = common || {};
771
847
  }
772
848
 
849
+ UserAttributeHandler.prototype.sendUserPropertiesToGA4 = function (
850
+ userAttributes
851
+ ) {
852
+ gtag(
853
+ 'set',
854
+ 'user_properties',
855
+ this.common.truncateUserAttributes(userAttributes)
856
+ );
857
+ };
858
+
773
859
  // `mParticleUser` was removed from the function signatures onRemoveUserAttribute, onSetUserAttribute, and onConsentStateUpload because they were not being used
774
860
  // In the future if mParticleUser is ever required for an implementation of any of the below APIs, see https://github.com/mparticle-integrations/mparticle-javascript-integration-example/blob/master/src/user-attribute-handler.js
775
861
  // for previous function signatures
776
862
 
777
- UserAttributeHandler.prototype.onRemoveUserAttribute = function(key) {
863
+ UserAttributeHandler.prototype.onRemoveUserAttribute = function (key) {
778
864
  var userAttributes = {};
779
865
  userAttributes[key] = null;
780
- gtag('set', 'user_properties', userAttributes);
866
+ this.sendUserPropertiesToGA4(userAttributes);
781
867
  };
782
868
 
783
- UserAttributeHandler.prototype.onSetUserAttribute = function(key, value) {
869
+ UserAttributeHandler.prototype.onSetUserAttribute = function (key, value) {
784
870
  var userAttributes = {};
785
871
  userAttributes[key] = value;
786
- gtag('set', 'user_properties', userAttributes);
872
+ this.sendUserPropertiesToGA4(userAttributes);
787
873
  };
788
874
 
789
875
  // TODO: Commenting this out for now because Integrations PM still determining if this is in scope or not
package/package.json CHANGED
@@ -1,52 +1,50 @@
1
1
  {
2
- "name": "@mparticle/web-google-analytics-4-client-kit",
3
- "version": "1.0.4",
4
- "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
- "description": "mParticle integration sdk for Google Analytics",
6
- "main": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",
7
- "browser": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",
8
- "files": [
9
- "dist/GoogleAnalyticsEvent4ForwarderClientSide-Kit.common.js"
10
- ],
11
- "scripts": {
12
- "test": "node test/boilerplate/test-karma.js",
13
- "build": "ENVIRONMENT=production rollup --config rollup.config.js",
14
- "watch": "ENVIRONMENT=production rollup --config rollup.config.js -w",
15
- "testEndToEnd": "ENVIRONMENT=testEndToEnd rollup --config rollup.config.js && open http://localhost:8082/node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/index.html && node node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/server"
16
- },
17
- "publishConfig": {
18
- "access": "public"
19
- },
20
- "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-google-analytics-4/tree/master/packages/GA4Client",
21
- "devDependencies": {
22
- "@mparticle/web-kit-wrapper": "^1.0.5",
23
- "@semantic-release/changelog": "^5.0.1",
24
- "@semantic-release/exec": "^5.0.0",
25
- "@semantic-release/git": "^9.0.0",
26
- "mocha": "^5.2.0",
27
- "chai": "^4.2.0",
28
- "karma": "^3.1.1",
29
- "karma-chai": "^0.1.0",
30
- "karma-chrome-launcher": "^2.2.0",
31
- "karma-edge-launcher": "^0.4.2",
32
- "karma-firefox-launcher": "^1.1.0",
33
- "karma-ie-launcher": "^1.0.0",
34
- "karma-junit-reporter": "^1.2.0",
35
- "karma-mocha": "^1.3.0",
36
- "karma-safari-launcher": "^1.0.0",
37
- "rollup": "^1.15.6",
38
- "rollup-plugin-commonjs": "^10.0.0",
39
- "rollup-plugin-node-resolve": "^5.0.3",
40
- "shelljs": "0.8.3",
41
- "eslint": "^7.25.0",
42
- "eslint-config-prettier": "8.3.0",
43
- "eslint-plugin-prettier": "3.4.1",
44
- "should": "13.2.3",
45
- "prettier": "^2.4.1",
46
- "watchify": "^3.11.0"
47
- },
48
- "dependencies": {
49
- "@mparticle/web-sdk": "^2.12.2"
50
- },
51
- "license": "Apache-2.0"
2
+ "name": "@mparticle/web-google-analytics-4-client-kit",
3
+ "version": "1.0.6",
4
+ "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
+ "description": "mParticle integration sdk for Google Analytics",
6
+ "main": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",
7
+ "browser": "dist/GoogleAnalytics4EventForwarderClientSide-Kit.common.js",
8
+ "files": [
9
+ "dist/GoogleAnalyticsEvent4ForwarderClientSide-Kit.common.js"
10
+ ],
11
+ "scripts": {
12
+ "build": "ENVIRONMENT=production rollup --config rollup.config.js",
13
+ "build:test": "ENVIRONMENT=production rollup --config rollup.test.config.js",
14
+ "test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js",
15
+ "test:debug": "npm run build && npm run build:test && DEBUG=true karma start test/karma.config.js",
16
+ "watch": "ENVIRONMENT=production rollup --config rollup.config.js -w",
17
+ "testEndToEnd": "ENVIRONMENT=testEndToEnd rollup --config rollup.config.js && open http://localhost:8082/node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/index.html && node node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/server"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-google-analytics-4/tree/master/packages/GA4Client",
23
+ "devDependencies": {
24
+ "@mparticle/web-kit-wrapper": "^1.0.5",
25
+ "@semantic-release/changelog": "^5.0.1",
26
+ "@semantic-release/exec": "^5.0.0",
27
+ "@semantic-release/git": "^9.0.0",
28
+ "mocha": "^5.2.0",
29
+ "chai": "^4.2.0",
30
+ "karma": "^5.1.0",
31
+ "karma-chai": "^0.1.0",
32
+ "karma-chrome-launcher": "^3.1.0",
33
+ "karma-firefox-launcher": "^1.3.0",
34
+ "karma-mocha": "^2.0.1",
35
+ "rollup": "^1.15.6",
36
+ "rollup-plugin-commonjs": "^10.0.0",
37
+ "rollup-plugin-node-resolve": "^5.0.3",
38
+ "shelljs": "0.8.3",
39
+ "eslint": "^7.25.0",
40
+ "eslint-config-prettier": "8.3.0",
41
+ "eslint-plugin-prettier": "3.4.1",
42
+ "should": "13.2.3",
43
+ "prettier": "^2.4.1",
44
+ "watchify": "^3.11.0"
45
+ },
46
+ "dependencies": {
47
+ "@mparticle/web-sdk": "^2.12.2"
48
+ },
49
+ "license": "Apache-2.0"
52
50
  }