@mparticle/web-adwords-kit 2.2.1 → 2.3.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.
@@ -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,30 @@ 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
+ var mpMappedConsentName = mappingEntry.map;
402
+ var googleMappedConsentName = mappingEntry.value;
403
+
404
+ if (
405
+ consentState[mpMappedConsentName] &&
406
+ googleConsentProperties.indexOf(googleMappedConsentName) !== -1
407
+ ) {
408
+ payload[googleMappedConsentName] = consentState[mpMappedConsentName]
409
+ .Consented
410
+ ? googleConsentValues.Granted
411
+ : googleConsentValues.Denied;
412
+ }
413
+ }
414
+
415
+ return payload;
416
+ }
417
+
272
418
  // Looks up an Event's conversionLabel from customAttributeMappings based on computed jsHash value
273
419
  function getConversionLabel(event) {
274
420
  var jsHash = calculateJSHash(event.EventDataType, event.EventCategory, event.EventName);
@@ -366,6 +512,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
366
512
  })();
367
513
  }
368
514
 
515
+ // https://go.mparticle.com/work/SQDSDKS-6166
369
516
  function initForwarder(settings, service, testMode) {
370
517
  window.enhanced_conversion_data = {};
371
518
  forwarderSettings = settings;
@@ -393,6 +540,36 @@ Object.defineProperty(exports, '__esModule', { value: true });
393
540
  return 'Can\'t initialize forwarder: ' + name + ', conversionId is not defined';
394
541
  }
395
542
 
543
+ // https://go.mparticle.com/work/SQDSDKS-6165
544
+ if (window.gtag && forwarderSettings.enableGtag === 'True') {
545
+ if (forwarderSettings.consentMappingWeb) {
546
+ consentMappings = parseSettingsString(
547
+ forwarderSettings.consentMappingWeb
548
+ );
549
+ } else {
550
+ // Ensures consent mappings is an empty array
551
+ // for future use
552
+ consentMappings = [];
553
+ }
554
+
555
+ consentPayloadDefaults = getConsentSettings();
556
+ var initialConsentState = getUserConsentState();
557
+
558
+ var defaultConsentPayload =
559
+ generateConsentStatePayloadFromMappings(
560
+ initialConsentState,
561
+ consentMappings
562
+ );
563
+
564
+ if(!isEmpty(defaultConsentPayload)) {
565
+ consentPayloadAsString = JSON.stringify(
566
+ defaultConsentPayload
567
+ );
568
+
569
+ sendGtagConsentDefaults(defaultConsentPayload);
570
+ }
571
+ }
572
+
396
573
  forwarderSettings.remarketingOnly = forwarderSettings.remarketingOnly == 'True';
397
574
 
398
575
  try {
@@ -462,10 +639,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
462
639
  console.log('Successfully registered ' + name + ' to your mParticle configuration');
463
640
  }
464
641
 
642
+ function parseSettingsString(settingsString) {
643
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
644
+ }
645
+
465
646
  function isObject(val) {
466
647
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
467
648
  }
468
649
 
650
+ function isEmpty(value) {
651
+ return value == null || !(Object.keys(value) || value).length;
652
+ }
653
+
469
654
  function mergeObjects() {
470
655
  var resObj = {};
471
656
  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.0",
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",