@mparticle/web-double-click-kit 2.0.3 → 2.1.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,7 +2,124 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function Common() {}
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: 'defaultAdUserDataConsent',
64
+ ad_personalization: 'defaultAdPersonalizationConsent',
65
+ ad_storage: 'defaultAdStorageConsentWeb',
66
+ analytics_storage: 'defaultAnalyticsStorageConsentWeb',
67
+ };
68
+
69
+ var settings = this.common.settings;
70
+
71
+ Object.keys(googleToMpConsentSettingsMapping).forEach(function (
72
+ googleConsentKey
73
+ ) {
74
+ var mpConsentSettingKey =
75
+ googleToMpConsentSettingsMapping[googleConsentKey];
76
+ var googleConsentValuesKey = settings[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
+
116
+ function Common() {
117
+ this.consentMappings = [];
118
+ this.consentPayloadDefaults = {};
119
+ this.consentPayloadAsString = '';
120
+
121
+ this.consentHandler = new consent(this);
122
+ }
6
123
 
7
124
  Common.prototype.eventMapping = {};
8
125
  Common.prototype.customVariablesMappings = {};
@@ -43,6 +160,21 @@ Common.prototype.sendGtag = function(type, properties, isInitialization) {
43
160
  }
44
161
  };
45
162
 
163
+ Common.prototype.sendGtagConsent = function (type, payload) {
164
+ function gtag() {
165
+ window.dataLayer.push(arguments);
166
+ }
167
+ gtag('consent', type, payload);
168
+ };
169
+
170
+ Common.prototype.cloneObject = function (obj) {
171
+ return JSON.parse(JSON.stringify(obj));
172
+ };
173
+
174
+ Common.prototype.isEmpty = function isEmpty(value) {
175
+ return value == null || !(Object.keys(value) || value).length;
176
+ };
177
+
46
178
  var common = Common;
47
179
 
48
180
  function findValueInMapping(jsHash, mapping) {
@@ -147,7 +279,35 @@ function EventHandler(common) {
147
279
  this.common = common || {};
148
280
  }
149
281
 
150
- EventHandler.prototype.logEvent = function(event) {
282
+ EventHandler.prototype.maybeSendConsentUpdateToGa4 = function (event) {
283
+ // If consent payload is empty,
284
+ // we never sent an initial default consent state
285
+ // so we shouldn't send an update.
286
+ if (this.common.consentPayloadAsString && this.common.consentMappings) {
287
+ var eventConsentState = this.common.consentHandler.getEventConsentState(
288
+ event.ConsentState
289
+ );
290
+
291
+ if (!this.common.isEmpty(eventConsentState)) {
292
+ var updatedConsentPayload =
293
+ this.common.consentHandler.generateConsentStatePayloadFromMappings(
294
+ eventConsentState,
295
+ this.common.consentMappings
296
+ );
297
+
298
+ var eventConsentAsString = JSON.stringify(updatedConsentPayload);
299
+
300
+ if (eventConsentAsString !== this.common.consentPayloadAsString) {
301
+ this.common.sendGtagConsent('update', updatedConsentPayload);
302
+ this.common.consentPayloadAsString = eventConsentAsString;
303
+ }
304
+ }
305
+ }
306
+ };
307
+
308
+ EventHandler.prototype.logEvent = function (event) {
309
+ this.maybeSendConsentUpdateToGa4(event);
310
+
151
311
  var gtagProperties = {};
152
312
  this.common.setCustomVariables(event, gtagProperties);
153
313
  var eventMapping = this.common.getEventMapping(event);
@@ -250,6 +410,18 @@ var initialization = {
250
410
  initForwarder: function(settings, testMode, userAttributes, userIdentities, processEvent, eventQueue, isInitialized, common) {
251
411
  common.settings = settings;
252
412
 
413
+ if (common.settings.consentMappingWeb) {
414
+ common.consentMappings = parseSettingsString(
415
+ common.settings.consentMappingWeb
416
+ );
417
+ } else {
418
+ // Ensures consent mappings is an empty array
419
+ // for future use
420
+ common.consentMappings = [];
421
+ common.consentPayloadDefaults = {};
422
+ common.consentPayloadAsString = '';
423
+ }
424
+
253
425
  window.dataLayer = window.dataLayer || [];
254
426
  if (!testMode) {
255
427
  var url = 'https://www.googletagmanager.com/gtag/js?id=' + settings.advertiserId;
@@ -275,13 +447,32 @@ var initialization = {
275
447
  } else {
276
448
  initializeGoogleDFP(common, settings);
277
449
  }
278
- }
450
+
451
+ common.consentPayloadDefaults =
452
+ common.consentHandler.getConsentSettings();
453
+ var initialConsentState = common.consentHandler.getUserConsentState();
454
+ var defaultConsentPayload =
455
+ common.consentHandler.generateConsentStatePayloadFromMappings(
456
+ initialConsentState,
457
+ common.consentMappings
458
+ );
459
+
460
+ if (!common.isEmpty(defaultConsentPayload)) {
461
+ common.consentPayloadAsString = JSON.stringify(
462
+ defaultConsentPayload
463
+ );
464
+
465
+ common.sendGtagConsent('default', defaultConsentPayload);
466
+ }
467
+ },
279
468
  };
280
469
 
281
470
  function initializeGoogleDFP(common, settings, isInitialized) {
282
- common.eventMapping = JSON.parse(settings.eventMapping.replace(/&quot;/g, '\"'));
471
+ common.eventMapping = parseSettingsString(settings.eventMapping);
283
472
 
284
- common.customVariablesMappings = JSON.parse(settings.customVariables.replace(/&quot;/g, '\"')).reduce(function(a, b) {
473
+ common.customVariablesMappings = parseSettingsString(
474
+ settings.customVariables
475
+ ).reduce(function (a, b) {
285
476
  a[b.map] = b.value;
286
477
  return a;
287
478
  }, {});
@@ -290,6 +481,10 @@ function initializeGoogleDFP(common, settings, isInitialized) {
290
481
  common.sendGtag('config', settings.advertiserId, true);
291
482
  }
292
483
 
484
+ function parseSettingsString(settingsString) {
485
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
486
+ }
487
+
293
488
  var initialization_1 = initialization;
294
489
 
295
490
  var sessionHandler = {
@@ -474,8 +669,7 @@ var constructor = function() {
474
669
 
475
670
  function logSessionStart(event) {
476
671
  try {
477
- sessionHandler_1.onSessionStart(event);
478
- return true;
672
+ return sessionHandler_1.onSessionStart(event);
479
673
  } catch (e) {
480
674
  return {
481
675
  error: 'Error starting session on forwarder ' + name + '; ' + e,
@@ -485,8 +679,7 @@ var constructor = function() {
485
679
 
486
680
  function logSessionEnd(event) {
487
681
  try {
488
- sessionHandler_1.onSessionEnd(event);
489
- return true;
682
+ return sessionHandler_1.onSessionEnd(event);
490
683
  } catch (e) {
491
684
  return {
492
685
  error: 'Error ending session on forwarder ' + name + '; ' + e,
@@ -496,8 +689,7 @@ var constructor = function() {
496
689
 
497
690
  function logError(event) {
498
691
  try {
499
- self.eventHandler.logError(event);
500
- return true;
692
+ return self.eventHandler.logError(event);
501
693
  } catch (e) {
502
694
  return {
503
695
  error: 'Error logging error on forwarder ' + name + '; ' + e,
@@ -507,8 +699,7 @@ var constructor = function() {
507
699
 
508
700
  function logPageView(event) {
509
701
  try {
510
- self.eventHandler.logPageView(event);
511
- return true;
702
+ return self.eventHandler.logPageView(event);
512
703
  } catch (e) {
513
704
  return {
514
705
  error:
@@ -519,8 +710,7 @@ var constructor = function() {
519
710
 
520
711
  function logEvent(event) {
521
712
  try {
522
- self.eventHandler.logEvent(event);
523
- return true;
713
+ return self.eventHandler.logEvent(event);
524
714
  } catch (e) {
525
715
  return {
526
716
  error: 'Error logging event on forwarder ' + name + '; ' + e,
@@ -530,8 +720,7 @@ var constructor = function() {
530
720
 
531
721
  function logEcommerceEvent(event) {
532
722
  try {
533
- self.commerceHandler.logCommerceEvent(event);
534
- return true;
723
+ return self.commerceHandler.logCommerceEvent(event);
535
724
  } catch (e) {
536
725
  return {
537
726
  error:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-double-click-kit",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-doubleclick",
5
5
  "main": "dist/DoubleClick-Kit.common.js",
6
6
  "browser": "dist/DoubleClick-Kit.common.js",
@@ -10,11 +10,17 @@
10
10
  "scripts": {
11
11
  "testKarma": "node test/boilerplate/test-karma.js",
12
12
  "build": "rollup --config rollup.config.js",
13
+ "build:test": "ENVIRONMENT=production rollup --config rollup.test.config.js",
13
14
  "watch": "rollup --config rollup.config.js -w",
15
+ "test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js",
16
+ "test:debug": "npm run build && npm run build:test && DEBUG=true karma start test/karma.config.js",
14
17
  "testEndToEnd": "browserify node_modules/@mparticle/web-kit-wrapper/end-to-end-testapp/index.js -v -o test/end-to-end-testapp/build/compilation.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"
15
18
  },
16
19
  "devDependencies": {
17
- "@mparticle/web-sdk": "^2.11.1",
20
+ "@mparticle/web-sdk": "^2.23.4",
21
+ "@semantic-release/changelog": "^5.0.1",
22
+ "@semantic-release/exec": "^5.0.0",
23
+ "@semantic-release/git": "^9.0.0",
18
24
  "chai": "^4.2.0",
19
25
  "karma": "^5.1.0",
20
26
  "karma-chai": "^0.1.0",
@@ -30,6 +36,6 @@
30
36
  "watchify": "^3.11.1"
31
37
  },
32
38
  "dependencies": {
33
- "@mparticle/web-kit-wrapper": "^1.0.5"
39
+ "@mparticle/web-kit-wrapper": "^1.0.6"
34
40
  }
35
41
  }