@mparticle/web-adwords-kit 2.2.0 → 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,17 +82,37 @@ Object.defineProperty(exports, '__esModule', { value: true });
55
82
 
56
83
  try {
57
84
  if (window.gtag && forwarderSettings.enableGtag == 'True') {
58
- if (event.CustomFlags &&
59
- Object.keys(event.CustomFlags).length &&
60
- event.CustomFlags[ENHANCED_CONVERSION_DATA]
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
+
107
+ if (
108
+ forwarderSettings.enableEnhancedConversions ===
109
+ 'True' &&
110
+ hasEnhancedConversionData(event.CustomFlags)
61
111
  ) {
62
- if (forwarderSettings.enableEnhancedConversions === 'True') {
63
- setEnhancedConversionData(
112
+ window.enhanced_conversion_data =
113
+ parseEnhancedConversionData(
64
114
  event.CustomFlags[ENHANCED_CONVERSION_DATA]
65
115
  );
66
- } else {
67
- console.warn('You have a custom flag of enhanced conversions, but you have not enabled enhanced converisons');
68
- }
69
116
  }
70
117
 
71
118
  sendEventFunction = sendGtagEvent;
@@ -111,8 +158,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
111
158
  }
112
159
 
113
160
  return 'Can\'t send to forwarder: ' + name + '. Event not mapped';
114
- }
115
- catch (e) {
161
+ } catch (e) {
116
162
  console.error('Can\t send to forwarder', e);
117
163
  return 'Can\'t send to forwarder: ' + name + ' ' + e;
118
164
  }
@@ -126,23 +172,29 @@ Object.defineProperty(exports, '__esModule', { value: true });
126
172
  return 'Can\'t send to forwarder ' + name + ', not initialized. Event added to queue.';
127
173
  }
128
174
 
129
- function setEnhancedConversionData(enhancedConversionData) {
130
- if (enhancedConversionData.email) {
131
- window.enhanced_conversion_data.email = enhancedConversionData.email;
132
- }
133
- if (enhancedConversionData.phone_number) {
134
- window.enhanced_conversion_data.phone_number = enhancedConversionData.phone_number;
135
- }
136
- if (enhancedConversionData.first_name) {
137
- window.enhanced_conversion_data.first_name = enhancedConversionData.first_name;
138
- }
139
- if (enhancedConversionData.last_name) {
140
- window.enhanced_conversion_data.last_name = enhancedConversionData.last_name;
141
- }
142
- if (enhancedConversionData.home_address) {
143
- window.enhanced_conversion_data.home_address =
144
- enhancedConversionData.home_address;
175
+ function hasEnhancedConversionData(customFlags) {
176
+ return customFlags && Object.keys(customFlags).length && customFlags[ENHANCED_CONVERSION_DATA];
177
+ }
178
+
179
+ function parseEnhancedConversionData(conversionData) {
180
+ // Checks if conversion data in custom flags is a stringified object
181
+ // Conversion data should only be a stringified object or a JS object
182
+ if (typeof conversionData === 'string') {
183
+ try {
184
+ return JSON.parse(conversionData);
185
+ } catch (error) {
186
+ console.warn('Unrecognized Enhanced Conversion Data Format', conversionData, error);
187
+ return {};
188
+ }
189
+ } else if (typeof conversionData === 'object') {
190
+ // Not a stringified object so it can be used as-is. However,
191
+ // we want to avoid mutating the original state, so we make a copy.
192
+ return cloneObject(conversionData);
193
+ } else {
194
+ console.warn('Unrecognized Enhanced Conversion Data Format', conversionData);
195
+ return {};
145
196
  }
197
+
146
198
  }
147
199
 
148
200
  // Converts an mParticle Commerce Event into either Legacy or gtag Event
@@ -165,7 +217,32 @@ Object.defineProperty(exports, '__esModule', { value: true });
165
217
  }
166
218
  }
167
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
+ }
168
240
 
241
+ function getEventConsentState(eventConsentState) {
242
+ return eventConsentState && eventConsentState.getGDPRConsentState
243
+ ? eventConsentState.getGDPRConsentState()
244
+ : {};
245
+ }
169
246
  // ** Adwords Events
170
247
  function getBaseAdWordEvent() {
171
248
  var adWordEvent = {};
@@ -207,6 +284,31 @@ Object.defineProperty(exports, '__esModule', { value: true });
207
284
  return adWordEvent;
208
285
  }
209
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
+
210
312
  // gtag Events
211
313
  function getBaseGtagEvent(conversionLabel) {
212
314
  return {
@@ -247,6 +349,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
247
349
  }
248
350
 
249
351
  function sendGtagEvent(payload) {
352
+ // https://go.mparticle.com/work/SQDSDKS-6165
250
353
  try {
251
354
  gtag('event', 'conversion', payload);
252
355
  } catch (e) {
@@ -256,6 +359,28 @@ Object.defineProperty(exports, '__esModule', { value: true });
256
359
  return true;
257
360
  }
258
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
+
259
384
  function sendAdwordsEvent(payload) {
260
385
  try {
261
386
  window.google_trackConversion(payload);
@@ -266,6 +391,30 @@ Object.defineProperty(exports, '__esModule', { value: true });
266
391
  return true;
267
392
  }
268
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
+
269
418
  // Looks up an Event's conversionLabel from customAttributeMappings based on computed jsHash value
270
419
  function getConversionLabel(event) {
271
420
  var jsHash = calculateJSHash(event.EventDataType, event.EventCategory, event.EventName);
@@ -363,6 +512,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
363
512
  })();
364
513
  }
365
514
 
515
+ // https://go.mparticle.com/work/SQDSDKS-6166
366
516
  function initForwarder(settings, service, testMode) {
367
517
  window.enhanced_conversion_data = {};
368
518
  forwarderSettings = settings;
@@ -390,6 +540,36 @@ Object.defineProperty(exports, '__esModule', { value: true });
390
540
  return 'Can\'t initialize forwarder: ' + name + ', conversionId is not defined';
391
541
  }
392
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
+
393
573
  forwarderSettings.remarketingOnly = forwarderSettings.remarketingOnly == 'True';
394
574
 
395
575
  try {
@@ -459,10 +639,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
459
639
  console.log('Successfully registered ' + name + ' to your mParticle configuration');
460
640
  }
461
641
 
642
+ function parseSettingsString(settingsString) {
643
+ return JSON.parse(settingsString.replace(/&quot;/g, '"'));
644
+ }
645
+
462
646
  function isObject(val) {
463
647
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
464
648
  }
465
649
 
650
+ function isEmpty(value) {
651
+ return value == null || !(Object.keys(value) || value).length;
652
+ }
653
+
466
654
  function mergeObjects() {
467
655
  var resObj = {};
468
656
  for(var i=0; i < arguments.length; i += 1) {
@@ -475,6 +663,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
475
663
  return resObj;
476
664
  }
477
665
 
666
+ function cloneObject(obj) {
667
+ return JSON.parse(JSON.stringify(obj));
668
+ }
669
+
478
670
  if (typeof window !== 'undefined') {
479
671
  if (window && window.mParticle && window.mParticle.addForwarder) {
480
672
  window.mParticle.addForwarder({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-adwords-kit",
3
- "version": "2.2.0",
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",
@@ -11,8 +11,10 @@
11
11
  "repository": "https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords",
12
12
  "scripts": {
13
13
  "build": "rollup --config rollup.config.js",
14
+ "build:test": "rollup --config rollup.test.config.js",
14
15
  "watch": "rollup --config rollup.config.js -w",
15
- "test": "node test/boilerplate/test-karma.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"
16
18
  },
17
19
  "devDependencies": {
18
20
  "chai": "^4.2.0",
package/CHANGELOG.md DELETED
@@ -1,38 +0,0 @@
1
- # [2.2.0](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/compare/v2.1.1...v2.2.0) (2021-11-11)
2
-
3
-
4
- ### Features
5
-
6
- * Add support for enhanced conversions ([7c3c5d6](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/commit/7c3c5d6092b0574c3f54ca5651c27a2b3d8edb4c))
7
-
8
- ## [2.1.1](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/compare/v2.1.0...v2.1.1) (2021-11-11)
9
-
10
-
11
- ### Bug Fixes
12
-
13
- * Correct send_to in gtag API ([#23](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/issues/23)) ([af01adc](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/commit/af01adc2b04453f77845e228bff4aa6c49824fde))
14
- * Prevent unmapped events from being forwarded ([#22](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/issues/22)) ([50e6d27](https://github.com/mparticle-integrations/mparticle-javascript-integration-adwords/commit/50e6d279dff666f5621e585878672707a21d911f))
15
-
16
- ## Releases
17
- --
18
-
19
- #### 2.1.0 - 2021-10-25
20
- * Implement Google Site Tag (GTAG) support as an opt-in feature
21
-
22
- #### 2.0.5 - 2020-11-11
23
- * There are no code changes in this commit. NPM published 2.0.4 but with the dist/ file from 2.0.3. This commit will include the correct dist/ file as we bump to 2.0.5
24
-
25
- #### 2.0.4 - 2020-11-10
26
- * Bugfix - Check window for node environments
27
- * Bugfix - rename event --> adWordEvent
28
-
29
- #### 2.0.3 - 2020-02-03
30
- * Update package.json
31
-
32
- #### 2.0.2 - 2019-10-03
33
- * Bugfix - Remove parseInt(conversionId) which could result in NaN and data not sent to AdWords
34
- * Turn src file into an ESM module
35
- * Remove isObject dependency
36
-
37
- #### 2.0.1 - 2019-08-12
38
- * Bugfix - Refactor calculateJSHash