@mparticle/web-google-analytics-4-client-kit 1.3.1 → 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
|
|
|
@@ -309,6 +428,12 @@ Common.prototype.getUserId = function (
|
|
|
309
428
|
}
|
|
310
429
|
};
|
|
311
430
|
|
|
431
|
+
Common.prototype.cloneObject = function (obj) {
|
|
432
|
+
return JSON.parse(JSON.stringify(obj));
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
Common.prototype.isEmpty = isEmpty;
|
|
436
|
+
|
|
312
437
|
var common = Common;
|
|
313
438
|
|
|
314
439
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
@@ -832,6 +957,32 @@ function EventHandler(common) {
|
|
|
832
957
|
this.common = common || {};
|
|
833
958
|
}
|
|
834
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
|
+
|
|
835
986
|
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
|
|
836
987
|
EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
|
|
837
988
|
var standardizedEventName;
|
|
@@ -857,6 +1008,7 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
|
|
|
857
1008
|
};
|
|
858
1009
|
|
|
859
1010
|
EventHandler.prototype.logEvent = function (event) {
|
|
1011
|
+
this.maybeSendConsentUpdateToGa4(event);
|
|
860
1012
|
this.sendEventToGA4(event.EventName, event.EventAttributes);
|
|
861
1013
|
};
|
|
862
1014
|
|
|
@@ -897,6 +1049,7 @@ EventHandler.prototype.logPageView = function (event) {
|
|
|
897
1049
|
event.EventAttributes
|
|
898
1050
|
);
|
|
899
1051
|
|
|
1052
|
+
this.maybeSendConsentUpdateToGa4(event);
|
|
900
1053
|
this.sendEventToGA4('page_view', eventAttributes);
|
|
901
1054
|
|
|
902
1055
|
return true;
|
|
@@ -996,7 +1149,7 @@ var initialization = {
|
|
|
996
1149
|
// The API to allow a customer to provide the cleansing callback
|
|
997
1150
|
// relies on window.GoogleAnalytics4Kit to exist. When MP is initialized
|
|
998
1151
|
// via the snippet, the kit code adds it to the window automatically.
|
|
999
|
-
// 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
|
|
1000
1153
|
// to set it manually here.
|
|
1001
1154
|
window.GoogleAnalytics4Kit = window.GoogleAnalytics4Kit || {};
|
|
1002
1155
|
|
|
@@ -1010,6 +1163,19 @@ var initialization = {
|
|
|
1010
1163
|
var configSettings = {
|
|
1011
1164
|
send_page_view: forwarderSettings.enablePageView === 'True',
|
|
1012
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
|
+
|
|
1013
1179
|
window.dataLayer = window.dataLayer || [];
|
|
1014
1180
|
|
|
1015
1181
|
window.gtag = function () {
|
|
@@ -1059,6 +1225,25 @@ var initialization = {
|
|
|
1059
1225
|
} else {
|
|
1060
1226
|
isInitialized = true;
|
|
1061
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
|
+
|
|
1062
1247
|
return isInitialized;
|
|
1063
1248
|
},
|
|
1064
1249
|
};
|
|
@@ -1074,6 +1259,10 @@ function setClientId(clientId, moduleId) {
|
|
|
1074
1259
|
window.mParticle._setIntegrationDelay(moduleId, false);
|
|
1075
1260
|
}
|
|
1076
1261
|
|
|
1262
|
+
function parseSettingsString(settingsString) {
|
|
1263
|
+
return JSON.parse(settingsString.replace(/"/g, '"'));
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1077
1266
|
var initialization_1 = initialization;
|
|
1078
1267
|
|
|
1079
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
|
+
"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",
|