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

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,10 +2,123 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var googleConsentValues = {
6
+ // Server Integration uses 'Unspecified' as a value when the setting is 'not set'.
7
+ // However, this is not used by Google's Web SDK. We are referencing it here as a comment
8
+ // as a record of this distinction and for posterity.
9
+ // If Google ever adds this for web, the line can just be uncommented to support this.
10
+ //
11
+ // Docs:
12
+ // Web: https://developers.google.com/tag-platform/gtagjs/reference#consent
13
+ // S2S: https://developers.google.com/google-ads/api/reference/rpc/v15/ConsentStatusEnum.ConsentStatus
14
+ //
15
+ // Unspecified: 'unspecified',
16
+ Denied: 'denied',
17
+ Granted: 'granted',
18
+ };
19
+
20
+ // Declares list of valid Google Consent Properties
21
+ var googleConsentProperties = [
22
+ 'ad_storage',
23
+ 'ad_user_data',
24
+ 'ad_personalization',
25
+ 'analytics_storage',
26
+ ];
27
+
28
+ function ConsentHandler(common) {
29
+ this.common = common || {};
30
+ }
31
+
32
+ ConsentHandler.prototype.getUserConsentState = function () {
33
+ var userConsentState = {};
34
+
35
+ if (mParticle.Identity && mParticle.Identity.getCurrentUser) {
36
+ var currentUser = mParticle.Identity.getCurrentUser();
37
+
38
+ if (!currentUser) {
39
+ return {};
40
+ }
41
+
42
+ var consentState =
43
+ mParticle.Identity.getCurrentUser().getConsentState();
44
+
45
+ if (consentState && consentState.getGDPRConsentState) {
46
+ userConsentState = consentState.getGDPRConsentState();
47
+ }
48
+ }
49
+
50
+ return userConsentState;
51
+ };
52
+
53
+ ConsentHandler.prototype.getEventConsentState = function (eventConsentState) {
54
+ return eventConsentState && eventConsentState.getGDPRConsentState
55
+ ? eventConsentState.getGDPRConsentState()
56
+ : {};
57
+ };
58
+
59
+ ConsentHandler.prototype.getConsentSettings = function () {
60
+ var consentSettings = {};
61
+
62
+ var googleToMpConsentSettingsMapping = {
63
+ ad_user_data: 'defaultAdUserDataConsentSDK',
64
+ ad_personalization: 'defaultAdPersonalizationConsentSDK',
65
+ ad_storage: 'defaultAdStorageConsentSDK',
66
+ analytics_storage: 'defaultAnalyticsStorageConsentSDK',
67
+ };
68
+
69
+ var forwarderSettings = this.common.forwarderSettings;
70
+
71
+ Object.keys(googleToMpConsentSettingsMapping).forEach(function (
72
+ googleConsentKey
73
+ ) {
74
+ var mpConsentSettingKey =
75
+ googleToMpConsentSettingsMapping[googleConsentKey];
76
+ var googleConsentValuesKey = forwarderSettings[mpConsentSettingKey];
77
+
78
+ if (googleConsentValuesKey && mpConsentSettingKey) {
79
+ consentSettings[googleConsentKey] =
80
+ googleConsentValues[googleConsentValuesKey];
81
+ }
82
+ });
83
+
84
+ return consentSettings;
85
+ };
86
+
87
+ ConsentHandler.prototype.generateConsentStatePayloadFromMappings = function (
88
+ consentState,
89
+ mappings
90
+ ) {
91
+ if (!mappings) return {};
92
+
93
+ var payload = this.common.cloneObject(this.common.consentPayloadDefaults);
94
+
95
+ for (var i = 0; i <= mappings.length - 1; i++) {
96
+ var mappingEntry = mappings[i];
97
+ var mpMappedConsentName = mappingEntry.map;
98
+ var googleMappedConsentName = mappingEntry.value;
99
+
100
+ if (
101
+ consentState[mpMappedConsentName] &&
102
+ googleConsentProperties.indexOf(googleMappedConsentName) !== -1
103
+ ) {
104
+ payload[googleMappedConsentName] = consentState[mpMappedConsentName]
105
+ .Consented
106
+ ? googleConsentValues.Granted
107
+ : googleConsentValues.Denied;
108
+ }
109
+ }
110
+
111
+ return payload;
112
+ };
113
+
114
+ var consent = ConsentHandler;
115
+
5
116
  // Google requires event and user attribute strings to have specific limits
6
117
  // in place when sending to data layer.
7
118
  // https://support.google.com/analytics/answer/11202874?sjid=7958830619827381593-NA
8
119
 
120
+
121
+
9
122
  var EVENT_NAME_MAX_LENGTH = 40;
10
123
  var EVENT_ATTRIBUTE_KEY_MAX_LENGTH = 40;
11
124
  var EVENT_ATTRIBUTE_VAL_MAX_LENGTH = 100;
@@ -37,7 +150,13 @@ function isEmpty(value) {
37
150
  return value == null || !(Object.keys(value) || value).length;
38
151
  }
39
152
 
40
- function Common() {}
153
+ function Common() {
154
+ this.consentMappings = [];
155
+ this.consentPayloadDefaults = {};
156
+ this.consentPayloadAsString = '';
157
+
158
+ this.consentHandler = new consent(this);
159
+ }
41
160
 
42
161
  Common.prototype.forwarderSettings = null;
43
162
 
@@ -136,13 +255,15 @@ Common.prototype.standardizeParameters = function (parameters) {
136
255
  };
137
256
 
138
257
  Common.prototype.standardizeName = function (name) {
139
- try {
140
- name = window.GoogleAnalytics4Kit.setCustomNameStandardization(name);
141
- } catch (e) {
142
- console.error(
143
- 'Error calling setCustomNameStandardization callback. Check your callback. Data will still be sent without user-defined standardization. See our docs for proper use - https://docs.mparticle.com/integrations/google-analytics-4/event/',
144
- e
145
- );
258
+ if (window.GoogleAnalytics4Kit.hasOwnProperty('setCustomNameStandardization')) {
259
+ try {
260
+ name = window.GoogleAnalytics4Kit.setCustomNameStandardization(name);
261
+ } catch (e) {
262
+ console.warn(
263
+ 'Error calling setCustomNameStandardization callback. Check your callback. Data will still be sent without user-defined standardization. See our docs for proper use - https://docs.mparticle.com/integrations/google-analytics-4/event/',
264
+ e
265
+ );
266
+ }
146
267
  }
147
268
 
148
269
  // names of events and parameters have the following requirements:
@@ -307,6 +428,12 @@ Common.prototype.getUserId = function (
307
428
  }
308
429
  };
309
430
 
431
+ Common.prototype.cloneObject = function (obj) {
432
+ return JSON.parse(JSON.stringify(obj));
433
+ };
434
+
435
+ Common.prototype.isEmpty = isEmpty;
436
+
310
437
  var common = Common;
311
438
 
312
439
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -830,6 +957,32 @@ function EventHandler(common) {
830
957
  this.common = common || {};
831
958
  }
832
959
 
960
+ EventHandler.prototype.maybeSendConsentUpdateToGa4 = function (event) {
961
+ // If consent payload is empty,
962
+ // we never sent an initial default consent state
963
+ // so we shouldn't send an update.
964
+ if (this.common.consentPayloadAsString && this.common.consentMappings) {
965
+ var eventConsentState = this.common.consentHandler.getEventConsentState(
966
+ event.ConsentState
967
+ );
968
+
969
+ if (!this.common.isEmpty(eventConsentState)) {
970
+ var updatedConsentPayload =
971
+ this.common.consentHandler.generateConsentStatePayloadFromMappings(
972
+ eventConsentState,
973
+ this.common.consentMappings
974
+ );
975
+
976
+ var eventConsentAsString = JSON.stringify(updatedConsentPayload);
977
+
978
+ if (eventConsentAsString !== this.common.consentPayloadAsString) {
979
+ gtag('consent', 'update', updatedConsentPayload);
980
+ this.common.consentPayloadAsString = eventConsentAsString;
981
+ }
982
+ }
983
+ }
984
+ };
985
+
833
986
  // TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
834
987
  EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
835
988
  var standardizedEventName;
@@ -855,6 +1008,7 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
855
1008
  };
856
1009
 
857
1010
  EventHandler.prototype.logEvent = function (event) {
1011
+ this.maybeSendConsentUpdateToGa4(event);
858
1012
  this.sendEventToGA4(event.EventName, event.EventAttributes);
859
1013
  };
860
1014
 
@@ -895,6 +1049,7 @@ EventHandler.prototype.logPageView = function (event) {
895
1049
  event.EventAttributes
896
1050
  );
897
1051
 
1052
+ this.maybeSendConsentUpdateToGa4(event);
898
1053
  this.sendEventToGA4('page_view', eventAttributes);
899
1054
 
900
1055
  return true;
@@ -994,7 +1149,7 @@ var initialization = {
994
1149
  // The API to allow a customer to provide the cleansing callback
995
1150
  // relies on window.GoogleAnalytics4Kit to exist. When MP is initialized
996
1151
  // via the snippet, the kit code adds it to the window automatically.
997
- // However, when initialized via npm, it does not exist, and so we have
1152
+ // However, when initialized via npm, it does not exist, and so we have
998
1153
  // to set it manually here.
999
1154
  window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {};
1000
1155
 
@@ -1008,6 +1163,19 @@ var initialization = {
1008
1163
  var configSettings = {
1009
1164
  send_page_view: forwarderSettings.enablePageView === 'True',
1010
1165
  };
1166
+
1167
+ if (forwarderSettings.consentMappingSDK) {
1168
+ common.consentMappings = parseSettingsString(
1169
+ forwarderSettings.consentMappingSDK
1170
+ );
1171
+ } else {
1172
+ // Ensures consent mappings is an empty array
1173
+ // for future use
1174
+ common.consentMappings = [];
1175
+ common.consentPayloadDefaults = {};
1176
+ common.consentPayloadAsString = '';
1177
+ }
1178
+
1011
1179
  window.dataLayer = window.dataLayer || [];
1012
1180
 
1013
1181
  window.gtag = function () {
@@ -1057,6 +1225,25 @@ var initialization = {
1057
1225
  } else {
1058
1226
  isInitialized = true;
1059
1227
  }
1228
+
1229
+ common.consentPayloadDefaults =
1230
+ common.consentHandler.getConsentSettings();
1231
+ var initialConsentState = common.consentHandler.getUserConsentState();
1232
+
1233
+ var defaultConsentPayload =
1234
+ common.consentHandler.generateConsentStatePayloadFromMappings(
1235
+ initialConsentState,
1236
+ common.consentMappings
1237
+ );
1238
+
1239
+ if (!common.isEmpty(defaultConsentPayload)) {
1240
+ common.consentPayloadAsString = JSON.stringify(
1241
+ defaultConsentPayload
1242
+ );
1243
+
1244
+ gtag('consent', 'default', defaultConsentPayload);
1245
+ }
1246
+
1060
1247
  return isInitialized;
1061
1248
  },
1062
1249
  };
@@ -1072,6 +1259,10 @@ function setClientId(clientId, moduleId) {
1072
1259
  window.mParticle._setIntegrationDelay(moduleId, false);
1073
1260
  }
1074
1261
 
1262
+ function parseSettingsString(settingsString) {
1263
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
1264
+ }
1265
+
1075
1266
  var initialization_1 = initialization;
1076
1267
 
1077
1268
  var sessionHandler = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-google-analytics-4-client-kit",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
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",