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

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.
@@ -2,6 +2,22 @@
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
+
5
21
  function Common() {}
6
22
 
7
23
  Common.prototype.forwarderSettings = null;
@@ -18,6 +34,42 @@ Common.prototype.mergeObjects = function () {
18
34
  return resObj;
19
35
  };
20
36
 
37
+ Common.prototype.truncateAttributes = function (
38
+ attributes,
39
+ keyLimit,
40
+ valueLimit
41
+ ) {
42
+ var truncatedAttributes = {};
43
+
44
+ Object.keys(attributes).forEach(function (attribute) {
45
+ var key = truncateString(attribute, keyLimit);
46
+ var val = truncateString(attributes[attribute], valueLimit);
47
+ truncatedAttributes[key] = val;
48
+ });
49
+
50
+ return truncatedAttributes;
51
+ };
52
+
53
+ Common.prototype.truncateEventName = function (eventName) {
54
+ return truncateString(eventName, EVENT_NAME_MAX_LENGTH);
55
+ };
56
+
57
+ Common.prototype.truncateEventAttributes = function (eventAttributes) {
58
+ return this.truncateAttributes(
59
+ eventAttributes,
60
+ EVENT_ATTRIBUTE_KEY_MAX_LENGTH,
61
+ EVENT_ATTRIBUTE_VAL_MAX_LENGTH
62
+ );
63
+ };
64
+
65
+ Common.prototype.truncateUserAttributes = function (userAttributes) {
66
+ return this.truncateAttributes(
67
+ userAttributes,
68
+ USER_ATTRIBUTE_KEY_MAX_LENGTH,
69
+ USER_ATTRIBUTE_VALUE_MAX_LENGTH
70
+ );
71
+ };
72
+
21
73
  Common.prototype.getUserId = function (
22
74
  user,
23
75
  externalUserIdentityType,
@@ -575,8 +627,16 @@ function EventHandler(common) {
575
627
  this.common = common || {};
576
628
  }
577
629
 
630
+ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
631
+ gtag(
632
+ 'event',
633
+ this.common.truncateEventName(eventName),
634
+ this.common.truncateEventAttributes(eventAttributes)
635
+ );
636
+ };
637
+
578
638
  EventHandler.prototype.logEvent = function (event) {
579
- gtag('event', event.EventName, event.EventAttributes || {});
639
+ this.sendEventToGA4(event.EventName, event.EventAttributes);
580
640
  };
581
641
 
582
642
  EventHandler.prototype.logError = function () {
@@ -584,20 +644,28 @@ EventHandler.prototype.logError = function () {
584
644
  };
585
645
 
586
646
  EventHandler.prototype.logPageView = function (event) {
587
- var TITLE = 'Google.Title';
588
- var LOCATION = 'Google.Location';
589
- var pageTitle, pageLocation;
647
+ var TITLE = 'GA4.Title';
648
+ var LOCATION = 'GA4.Location';
590
649
 
591
- if (event.CustomFlags && event.CustomFlags.hasOwnProperty(TITLE)) {
592
- pageTitle = event.CustomFlags[TITLE];
593
- } else {
650
+ // These are being included for backwards compatibility from the legacy Google Analytics custom flags
651
+ var LEGACY_GA_TITLE = 'Google.Title';
652
+ var LEGACY_GA_LOCATION = 'Google.Location';
653
+
654
+ var pageLocation = location.href,
594
655
  pageTitle = document.title;
595
- }
596
656
 
597
- if (event.CustomFlags && event.CustomFlags.hasOwnProperty(LOCATION)) {
598
- pageLocation = event.CustomFlags[LOCATION];
599
- } else {
600
- pageLocation = location.href;
657
+ if (event.CustomFlags) {
658
+ if (event.CustomFlags.hasOwnProperty(TITLE)) {
659
+ pageTitle = event.CustomFlags[TITLE];
660
+ } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_TITLE)) {
661
+ pageTitle = event.CustomFlags[LEGACY_GA_TITLE];
662
+ }
663
+
664
+ if (event.CustomFlags.hasOwnProperty(LOCATION)) {
665
+ pageLocation = event.CustomFlags[LOCATION];
666
+ } else if (event.CustomFlags.hasOwnProperty(LEGACY_GA_LOCATION)) {
667
+ pageLocation = event.CustomFlags[LEGACY_GA_LOCATION];
668
+ }
601
669
  }
602
670
 
603
671
  var eventAttributes = this.common.mergeObjects(
@@ -608,7 +676,7 @@ EventHandler.prototype.logPageView = function (event) {
608
676
  event.EventAttributes
609
677
  );
610
678
 
611
- gtag('event', 'page_view', eventAttributes);
679
+ this.sendEventToGA4('page_view', eventAttributes);
612
680
 
613
681
  return true;
614
682
  };
@@ -770,20 +838,30 @@ function UserAttributeHandler(common) {
770
838
  this.common = common || {};
771
839
  }
772
840
 
841
+ UserAttributeHandler.prototype.sendUserPropertiesToGA4 = function (
842
+ userAttributes
843
+ ) {
844
+ gtag(
845
+ 'set',
846
+ 'user_properties',
847
+ this.common.truncateUserAttributes(userAttributes)
848
+ );
849
+ };
850
+
773
851
  // `mParticleUser` was removed from the function signatures onRemoveUserAttribute, onSetUserAttribute, and onConsentStateUpload because they were not being used
774
852
  // 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
853
  // for previous function signatures
776
854
 
777
- UserAttributeHandler.prototype.onRemoveUserAttribute = function(key) {
855
+ UserAttributeHandler.prototype.onRemoveUserAttribute = function (key) {
778
856
  var userAttributes = {};
779
857
  userAttributes[key] = null;
780
- gtag('set', 'user_properties', userAttributes);
858
+ this.sendUserPropertiesToGA4(userAttributes);
781
859
  };
782
860
 
783
- UserAttributeHandler.prototype.onSetUserAttribute = function(key, value) {
861
+ UserAttributeHandler.prototype.onSetUserAttribute = function (key, value) {
784
862
  var userAttributes = {};
785
863
  userAttributes[key] = value;
786
- gtag('set', 'user_properties', userAttributes);
864
+ this.sendUserPropertiesToGA4(userAttributes);
787
865
  };
788
866
 
789
867
  // 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.5",
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
  }