@mparticle/web-adwords-kit 2.2.1 → 2.3.1

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.
@@ -30,12 +30,39 @@ Object.defineProperty(exports, '__esModule', { value: true });
30
30
  },
31
31
  ENHANCED_CONVERSION_DATA = "GoogleAds.ECData";
32
32
 
33
+ // Declares valid Google consent values
34
+ var googleConsentValues = {
35
+ // Server Integration uses 'Unspecified' as a value when the setting is 'not set'.
36
+ // However, this is not used by Google's Web SDK. We are referencing it here as a comment
37
+ // as a record of this distinction and for posterity.
38
+ // If Google ever adds this for web, the line can just be uncommented to support this.
39
+ //
40
+ // Docs:
41
+ // Web: https://developers.google.com/tag-platform/gtagjs/reference#consent
42
+ // S2S: https://developers.google.com/google-ads/api/reference/rpc/v15/ConsentStatusEnum.ConsentStatus
43
+ //
44
+ // Unspecified: 'unspecified',
45
+ Denied: 'denied',
46
+ Granted: 'granted',
47
+ };
48
+
49
+ // Declares list of valid Google Consent Properties
50
+ var googleConsentProperties = [
51
+ 'ad_storage',
52
+ 'ad_user_data',
53
+ 'ad_personalization',
54
+ 'analytics_storage',
55
+ ];
56
+
33
57
  var constructor = function () {
34
58
  var self = this,
35
59
  isInitialized = false,
36
60
  forwarderSettings,
37
61
  labels,
38
62
  customAttributeMappings,
63
+ consentMappings = [],
64
+ consentPayloadDefaults = {},
65
+ consentPayloadAsString = '',
39
66
  reportingService,
40
67
  eventQueue = [],
41
68
  gtagSiteId;
@@ -55,6 +82,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
55
82
 
56
83
  try {
57
84
  if (window.gtag && forwarderSettings.enableGtag == 'True') {
85
+ // If consent payload is empty,
86
+ // we never sent an initial default consent state
87
+ // so we shouldn't send an update.
88
+ if (consentPayloadAsString && forwarderSettings.consentMappingWeb) {
89
+ var eventConsentState = getEventConsentState(event.ConsentState);
90
+
91
+ if (!isEmpty(eventConsentState)) {
92
+ var updatedConsentPayload = generateConsentStatePayloadFromMappings(
93
+ eventConsentState,
94
+ consentMappings,
95
+ );
96
+
97
+ var eventConsentAsString =
98
+ JSON.stringify(updatedConsentPayload);
99
+
100
+ if (eventConsentAsString !== consentPayloadAsString) {
101
+ sendGtagConsentUpdate(updatedConsentPayload);
102
+ consentPayloadAsString = eventConsentAsString;
103
+ }
104
+ }
105
+ }
106
+
58
107
  if (
59
108
  forwarderSettings.enableEnhancedConversions ===
60
109
  'True' &&
@@ -168,7 +217,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
168
217
  }
169
218
  }
170
219
 
220
+ function getUserConsentState() {
221
+ var userConsentState = {};
222
+
223
+ if (window.mParticle && window.mParticle.Identity && window.mParticle.Identity.getCurrentUser) {
224
+ var currentUser = window.mParticle.Identity.getCurrentUser();
225
+
226
+ if (!currentUser) {
227
+ return {};
228
+ }
229
+
230
+ var consentState =
231
+ window.mParticle.Identity.getCurrentUser().getConsentState();
232
+
233
+ if (consentState && consentState.getGDPRConsentState) {
234
+ userConsentState = consentState.getGDPRConsentState();
235
+ }
236
+ }
237
+
238
+ return userConsentState;
239
+ }
171
240
 
241
+ function getEventConsentState(eventConsentState) {
242
+ return eventConsentState && eventConsentState.getGDPRConsentState
243
+ ? eventConsentState.getGDPRConsentState()
244
+ : {};
245
+ }
172
246
  // ** Adwords Events
173
247
  function getBaseAdWordEvent() {
174
248
  var adWordEvent = {};
@@ -210,6 +284,31 @@ Object.defineProperty(exports, '__esModule', { value: true });
210
284
  return adWordEvent;
211
285
  }
212
286
 
287
+ function getConsentSettings() {
288
+ var consentSettings = {};
289
+
290
+ var googleToMpConsentSettingsMapping = {
291
+ // Inherited from S2S Integration Settings
292
+ ad_user_data: 'defaultAdUserDataConsent',
293
+ ad_personalization: 'defaultAdPersonalizationConsent',
294
+
295
+ // Unique to Web Kits
296
+ ad_storage: 'defaultAdStorageConsentWeb',
297
+ analytics_storage: 'defaultAnalyticsStorageConsentWeb'
298
+ };
299
+
300
+ Object.keys(googleToMpConsentSettingsMapping).forEach(function (googleConsentKey) {
301
+ var mpConsentSettingKey = googleToMpConsentSettingsMapping[googleConsentKey];
302
+ var googleConsentValuesKey = forwarderSettings[mpConsentSettingKey];
303
+
304
+ if (googleConsentValuesKey && mpConsentSettingKey){
305
+ consentSettings[googleConsentKey] = googleConsentValues[googleConsentValuesKey];
306
+ }
307
+ });
308
+
309
+ return consentSettings;
310
+ }
311
+
213
312
  // gtag Events
214
313
  function getBaseGtagEvent(conversionLabel) {
215
314
  return {
@@ -250,6 +349,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
250
349
  }
251
350
 
252
351
  function sendGtagEvent(payload) {
352
+ // https://go.mparticle.com/work/SQDSDKS-6165
253
353
  try {
254
354
  gtag('event', 'conversion', payload);
255
355
  } catch (e) {
@@ -259,6 +359,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
259
359
  return true;
260
360
  }
261
361
 
362
+ function sendGtagConsentDefaults(payload) {
363
+ // https://go.mparticle.com/work/SQDSDKS-6165
364
+ try {
365
+ gtag('consent', 'default', payload);
366
+ } catch (e) {
367
+ console.error('gtag is not available to send consent defaults: ', payload, e);
368
+ return false;
369
+ }
370
+ return true;
371
+ }
372
+
373
+ function sendGtagConsentUpdate(payload) {
374
+ // https://go.mparticle.com/work/SQDSDKS-6165
375
+ try {
376
+ gtag('consent', 'update', payload);
377
+ } catch (e) {
378
+ console.error('gtag is not available to send consent update: ', payload, e);
379
+ return false;
380
+ }
381
+ return true;
382
+ }
383
+
262
384
  function sendAdwordsEvent(payload) {
263
385
  try {
264
386
  window.google_trackConversion(payload);
@@ -269,6 +391,34 @@ Object.defineProperty(exports, '__esModule', { value: true });
269
391
  return true;
270
392
  }
271
393
 
394
+ // Creates a new Consent State Payload based on Consent State and Mapping
395
+ function generateConsentStatePayloadFromMappings(consentState, mappings) {
396
+ if (!mappings) return {};
397
+ var payload = cloneObject(consentPayloadDefaults);
398
+
399
+ for (var i = 0; i <= mappings.length - 1; i++) {
400
+ var mappingEntry = mappings[i];
401
+ // Although consent purposes can be inputted into the UI in any casing
402
+ // the SDK will automatically lowercase them to prevent pseudo-duplicate
403
+ // consent purposes, so we call `toLowerCase` on the consentMapping purposes here
404
+ var mpMappedConsentName = mappingEntry.map.toLowerCase();
405
+ // var mpMappedConsentName = mappingEntry.map;
406
+ var googleMappedConsentName = mappingEntry.value;
407
+
408
+ if (
409
+ consentState[mpMappedConsentName] &&
410
+ googleConsentProperties.indexOf(googleMappedConsentName) !== -1
411
+ ) {
412
+ payload[googleMappedConsentName] = consentState[mpMappedConsentName]
413
+ .Consented
414
+ ? googleConsentValues.Granted
415
+ : googleConsentValues.Denied;
416
+ }
417
+ }
418
+
419
+ return payload;
420
+ }
421
+
272
422
  // Looks up an Event's conversionLabel from customAttributeMappings based on computed jsHash value
273
423
  function getConversionLabel(event) {
274
424
  var jsHash = calculateJSHash(event.EventDataType, event.EventCategory, event.EventName);
@@ -366,6 +516,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
366
516
  })();
367
517
  }
368
518
 
519
+ // https://go.mparticle.com/work/SQDSDKS-6166
369
520
  function initForwarder(settings, service, testMode) {
370
521
  window.enhanced_conversion_data = {};
371
522
  forwarderSettings = settings;
@@ -393,6 +544,36 @@ Object.defineProperty(exports, '__esModule', { value: true });
393
544
  return 'Can\'t initialize forwarder: ' + name + ', conversionId is not defined';
394
545
  }
395
546
 
547
+ // https://go.mparticle.com/work/SQDSDKS-6165
548
+ if (window.gtag && forwarderSettings.enableGtag === 'True') {
549
+ if (forwarderSettings.consentMappingWeb) {
550
+ consentMappings = parseSettingsString(
551
+ forwarderSettings.consentMappingWeb
552
+ );
553
+ } else {
554
+ // Ensures consent mappings is an empty array
555
+ // for future use
556
+ consentMappings = [];
557
+ }
558
+
559
+ consentPayloadDefaults = getConsentSettings();
560
+ var initialConsentState = getUserConsentState();
561
+
562
+ var defaultConsentPayload =
563
+ generateConsentStatePayloadFromMappings(
564
+ initialConsentState,
565
+ consentMappings
566
+ );
567
+
568
+ if(!isEmpty(defaultConsentPayload)) {
569
+ consentPayloadAsString = JSON.stringify(
570
+ defaultConsentPayload
571
+ );
572
+
573
+ sendGtagConsentDefaults(defaultConsentPayload);
574
+ }
575
+ }
576
+
396
577
  forwarderSettings.remarketingOnly = forwarderSettings.remarketingOnly == 'True';
397
578
 
398
579
  try {
@@ -462,10 +643,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
462
643
  console.log('Successfully registered ' + name + ' to your mParticle configuration');
463
644
  }
464
645
 
646
+ function parseSettingsString(settingsString) {
647
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
648
+ }
649
+
465
650
  function isObject(val) {
466
651
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
467
652
  }
468
653
 
654
+ function isEmpty(value) {
655
+ return value == null || !(Object.keys(value) || value).length;
656
+ }
657
+
469
658
  function mergeObjects() {
470
659
  var resObj = {};
471
660
  for(var i=0; i < arguments.length; i += 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-adwords-kit",
3
- "version": "2.2.1",
3
+ "version": "2.3.1",
4
4
  "author": "mParticle Developers <developers@mparticle.com> (https://www.mparticle.com)",
5
5
  "description": "mParticle integration sdk for Google Adwords",
6
6
  "main": "dist/GoogleAdWordsEventForwarder.common.js",
@@ -13,7 +13,8 @@
13
13
  "build": "rollup --config rollup.config.js",
14
14
  "build:test": "rollup --config rollup.test.config.js",
15
15
  "watch": "rollup --config rollup.config.js -w",
16
- "test": "npm run build && npm run build:test && DEBUG=false karma start test/karma.config.js"
16
+ "test:karma": "DEBUG=false karma start test/karma.config.js",
17
+ "test": "npm run build && npm run build:test && npm run test:karma"
17
18
  },
18
19
  "devDependencies": {
19
20
  "chai": "^4.2.0",