@mparticle/web-google-analytics-4-client-kit 1.0.3 → 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,24 +644,39 @@ 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;
590
- if (event.CustomFlags.hasOwnProperty(TITLE)) {
591
- pageTitle = event.CustomFlags[TITLE];
592
- } else {
647
+ var TITLE = 'GA4.Title';
648
+ var LOCATION = 'GA4.Location';
649
+
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,
593
655
  pageTitle = document.title;
594
- }
595
656
 
596
- if (event.CustomFlags.hasOwnProperty(LOCATION)) {
597
- pageLocation = event.CustomFlags[LOCATION];
598
- } else {
599
- 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
+ }
600
669
  }
601
- gtag('event', 'page_view', {
602
- page_title: pageTitle,
603
- page_location: pageLocation,
604
- });
670
+
671
+ var eventAttributes = this.common.mergeObjects(
672
+ {
673
+ page_title: pageTitle,
674
+ page_location: pageLocation,
675
+ },
676
+ event.EventAttributes
677
+ );
678
+
679
+ this.sendEventToGA4('page_view', eventAttributes);
605
680
 
606
681
  return true;
607
682
  };
@@ -763,20 +838,30 @@ function UserAttributeHandler(common) {
763
838
  this.common = common || {};
764
839
  }
765
840
 
841
+ UserAttributeHandler.prototype.sendUserPropertiesToGA4 = function (
842
+ userAttributes
843
+ ) {
844
+ gtag(
845
+ 'set',
846
+ 'user_properties',
847
+ this.common.truncateUserAttributes(userAttributes)
848
+ );
849
+ };
850
+
766
851
  // `mParticleUser` was removed from the function signatures onRemoveUserAttribute, onSetUserAttribute, and onConsentStateUpload because they were not being used
767
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
768
853
  // for previous function signatures
769
854
 
770
- UserAttributeHandler.prototype.onRemoveUserAttribute = function(key) {
855
+ UserAttributeHandler.prototype.onRemoveUserAttribute = function (key) {
771
856
  var userAttributes = {};
772
857
  userAttributes[key] = null;
773
- gtag('set', 'user_properties', userAttributes);
858
+ this.sendUserPropertiesToGA4(userAttributes);
774
859
  };
775
860
 
776
- UserAttributeHandler.prototype.onSetUserAttribute = function(key, value) {
861
+ UserAttributeHandler.prototype.onSetUserAttribute = function (key, value) {
777
862
  var userAttributes = {};
778
863
  userAttributes[key] = value;
779
- gtag('set', 'user_properties', userAttributes);
864
+ this.sendUserPropertiesToGA4(userAttributes);
780
865
  };
781
866
 
782
867
  // TODO: Commenting this out for now because Integrations PM still determining if this is in scope or not
package/package.json CHANGED
@@ -1,49 +1,50 @@
1
1
  {
2
- "name": "@mparticle/web-google-analytics-4-client-kit",
3
- "version": "1.0.3",
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
- "mocha": "^5.2.0",
24
- "chai": "^4.2.0",
25
- "karma": "^3.1.1",
26
- "karma-chai": "^0.1.0",
27
- "karma-chrome-launcher": "^2.2.0",
28
- "karma-edge-launcher": "^0.4.2",
29
- "karma-firefox-launcher": "^1.1.0",
30
- "karma-ie-launcher": "^1.0.0",
31
- "karma-junit-reporter": "^1.2.0",
32
- "karma-mocha": "^1.3.0",
33
- "karma-safari-launcher": "^1.0.0",
34
- "rollup": "^1.15.6",
35
- "rollup-plugin-commonjs": "^10.0.0",
36
- "rollup-plugin-node-resolve": "^5.0.3",
37
- "shelljs": "0.8.3",
38
- "eslint": "^7.25.0",
39
- "eslint-config-prettier": "8.3.0",
40
- "eslint-plugin-prettier": "3.4.1",
41
- "should": "13.2.3",
42
- "prettier": "^2.4.1",
43
- "watchify": "^3.11.0"
44
- },
45
- "dependencies": {
46
- "@mparticle/web-sdk": "^2.12.2"
47
- },
48
- "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"
49
50
  }