@mparticle/web-adwords-kit 2.0.5 → 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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,9 @@
1
1
  ## Releases
2
2
  --
3
3
 
4
+ #### 2.1.0 - 2021-10-25
5
+ * Implement Google Site Tag (GTAG) support as an opt-in feature
6
+
4
7
  #### 2.0.5 - 2020-11-11
5
8
  * 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
6
9
 
@@ -37,23 +37,56 @@ Object.defineProperty(exports, '__esModule', { value: true });
37
37
  labels,
38
38
  customAttributeMappings,
39
39
  reportingService,
40
- eventQueue = [];
40
+ eventQueue = [],
41
+ gtagSiteId;
41
42
 
42
43
  self.name = name;
43
44
 
44
45
  function processEvent(event) {
45
46
  var reportEvent = false;
47
+ var sendEventFunction = function() {};
48
+ var generateEventFunction = function () {};
49
+ var conversionLabel;
50
+ var eventPayload;
46
51
 
47
52
  if (isInitialized) {
53
+ // First, process anything in the queue
54
+ processQueue(eventQueue);
55
+
48
56
  try {
49
- if (event.EventDataType == MessageType.PageView) {
50
- reportEvent = logPageEvent(event, false);
57
+ if (window.gtag && forwarderSettings.enableGtag == 'True') {
58
+ sendEventFunction = sendGtagEvent;
59
+ generateEventFunction = generateGtagEvent;
60
+ generateCommerceEvent = generateGtagCommerceEvent;
61
+
62
+ } else if (window.google_trackConversion) {
63
+ // window.google_trackConversion is a legacy API and will be deprecated
64
+ sendEventFunction = sendAdwordsEvent;
65
+ generateEventFunction = generateAdwordsEvent;
66
+ generateCommerceEvent = generateAdwordsCommerceEvent;
67
+
68
+ } else {
69
+ eventQueue.push({
70
+ action: processEvent,
71
+ data: event
72
+ });
73
+
74
+ return 'Can\'t send to forwarder ' + name + ', not initialized. Event added to queue.';
51
75
  }
52
- else if (event.EventDataType == MessageType.PageEvent) {
53
- reportEvent = logPageEvent(event, true);
76
+
77
+ // Get conversionLabel to be used for event generation
78
+ var conversionLabel = getConversionLabel(event);
79
+ var customProps = getCustomProps(event);
80
+
81
+ // Determines the proper event to fire
82
+ if (event.EventDataType == MessageType.PageView || event.EventDataType == MessageType.PageEvent) {
83
+ eventPayload = generateEventFunction(event, conversionLabel, customProps);
84
+ } else if (event.EventDataType == MessageType.Commerce && event.ProductAction) {
85
+ eventPayload = generateCommerceEvent(event, conversionLabel, customProps);
54
86
  }
55
- else if (event.EventDataType == MessageType.Commerce) {
56
- reportEvent = logCommerce(event);
87
+
88
+ if (eventPayload) {
89
+ reportEvent = sendEventFunction(eventPayload);
57
90
  }
58
91
 
59
92
  if (reportEvent && reportingService) {
@@ -65,89 +98,142 @@ Object.defineProperty(exports, '__esModule', { value: true });
65
98
  return 'Can\'t send to forwarder: ' + name + '. Event not mapped';
66
99
  }
67
100
  catch (e) {
101
+ console.error('Can\t send to forwarder', e);
68
102
  return 'Can\'t send to forwarder: ' + name + ' ' + e;
69
103
  }
104
+ } else {
105
+ eventQueue.push({
106
+ action: processEvent,
107
+ data: event
108
+ });
70
109
  }
71
110
 
72
- return 'Can\'t send to forwarder ' + name + ', not initialized';
111
+ return 'Can\'t send to forwarder ' + name + ', not initialized. Event added to queue.';
73
112
  }
74
113
 
75
- function sendOrQueueEvent(adWordEvent) {
76
- if (window.google_trackConversion) {
77
- window.google_trackConversion(adWordEvent);
114
+ // Converts an mParticle Commerce Event into either Legacy or gtag Event
115
+ function generateCommerceEvent(mPEvent, conversionLabel, isPageEvent) {
116
+ if (mPEvent.ProductAction
117
+ && mPEvent.ProductAction.ProductList
118
+ && mPEvent.ProductAction.ProductActionType) {
119
+
120
+ if (window.gtag && forwarderSettings.enableGtag == 'True') {
121
+ return generateGtagCommerceEvent(mPEvent, conversionLabel, isPageEvent);
122
+ } else if (window.google_trackConversion) {
123
+ return generateAdwordsCommerceEvent(mPEvent, conversionLabel, isPageEvent);
124
+ } else {
125
+ console.error('Unrecognized Commerce Event', mPEvent);
126
+ return false;
127
+ }
128
+
78
129
  } else {
79
- eventQueue.push(adWordEvent);
130
+ return false;
80
131
  }
81
132
  }
82
133
 
83
- function logCommerce(event) {
84
- if (event.ProductAction
85
- && event.ProductAction.ProductList
86
- && event.ProductAction.ProductActionType) {
87
134
 
88
- var isPageEvent = false;
89
- var conversionLabel = getConversionLabel(event, isPageEvent);
135
+ // ** Adwords Events
90
136
 
91
- if (typeof (conversionLabel) !== 'string') {
92
- return false;
93
- }
137
+ function getBaseAdWordEvent() {
138
+ var adWordEvent = {};
139
+ adWordEvent.google_conversion_value = 0;
140
+ adWordEvent.google_conversion_language = 'en';
141
+ adWordEvent.google_conversion_format = '3';
142
+ adWordEvent.google_conversion_color = 'ffffff';
143
+ adWordEvent.google_remarketing_only = forwarderSettings.remarketingOnly == 'True';
144
+ adWordEvent.google_conversion_id = forwarderSettings.conversionId;
145
+ return adWordEvent;
146
+ }
94
147
 
95
- var adWordEvent = getBaseAdWordEvent();
96
- adWordEvent.google_conversion_label = conversionLabel;
148
+ function generateAdwordsEvent(mPEvent, conversionLabel, customProps) {
149
+ var adWordEvent = getBaseAdWordEvent();
150
+ adWordEvent.google_conversion_label = conversionLabel;
151
+ adWordEvent.google_custom_params = customProps;
97
152
 
153
+ return adWordEvent;
154
+ }
98
155
 
99
- if (event.ProductAction.ProductActionType === mParticle.ProductActionType.Purchase
100
- && event.ProductAction.TransactionId) {
101
- adWordEvent.google_conversion_order_id = event.ProductAction.TransactionId;
102
- }
156
+ function generateAdwordsCommerceEvent(mPEvent, conversionLabel, customProps) {
157
+ var adWordEvent = getBaseAdWordEvent();
158
+ adWordEvent.google_conversion_label = conversionLabel;
103
159
 
104
- if (event.CurrencyCode) {
105
- adWordEvent.google_conversion_currency = event.CurrencyCode;
106
- }
160
+ if (mPEvent.ProductAction.ProductActionType === mParticle.ProductActionType.Purchase
161
+ && mPEvent.ProductAction.TransactionId) {
162
+ adWordEvent.google_conversion_order_id = mPEvent.ProductAction.TransactionId;
163
+ }
107
164
 
108
- if (event.ProductAction.TotalAmount) {
109
- adWordEvent.google_conversion_value = event.ProductAction.TotalAmount;
110
- }
165
+ if (mPEvent.CurrencyCode) {
166
+ adWordEvent.google_conversion_currency = mPEvent.CurrencyCode;
167
+ }
111
168
 
112
- adWordEvent.google_custom_params = getCustomProps(event, isPageEvent);
169
+ if (mPEvent.ProductAction.TotalAmount) {
170
+ adWordEvent.google_conversion_value = mPEvent.ProductAction.TotalAmount;
171
+ }
113
172
 
114
- sendOrQueueEvent(adWordEvent);
173
+ adWordEvent.google_custom_params = customProps;
174
+ return adWordEvent;
175
+ }
115
176
 
116
- return true;
177
+ // gtag Events
178
+ function getBaseGtagEvent(conversionLabel) {
179
+ return {
180
+ 'send-to': gtagSiteId + '/' + conversionLabel,
181
+ 'value': 0,
182
+ 'language': 'en',
183
+ 'remarketing_only': forwarderSettings.remarketingOnly == 'True'
117
184
  }
185
+ }
118
186
 
119
- return false;
187
+ function generateGtagEvent(mPEvent, conversionLabel, customProps) {
188
+ if (!conversionLabel) { return null; }
189
+ var conversionPayload = getBaseGtagEvent(conversionLabel);
190
+ return mergeObjects(conversionPayload, customProps);
120
191
  }
121
192
 
122
- function logPageEvent(event, isPageEvent) {
123
- var conversionLabel = getConversionLabel(event, isPageEvent);
124
- if (typeof (conversionLabel) != 'string') {
125
- return false;
193
+ function generateGtagCommerceEvent(mPEvent, conversionLabel, customProps) {
194
+ if (!conversionLabel) { return null; }
195
+ var conversionPayload = getBaseGtagEvent(conversionLabel);
196
+
197
+ if (mPEvent.ProductAction.ProductActionType === mParticle.ProductActionType.Purchase
198
+ && mPEvent.ProductAction.TransactionId) {
199
+ conversionPayload.order_id = mPEvent.ProductAction.TransactionId;
126
200
  }
127
201
 
128
- var adWordEvent = getBaseAdWordEvent();
129
- adWordEvent.google_conversion_label = conversionLabel;
130
- adWordEvent.google_custom_params = getCustomProps(event, isPageEvent);
202
+ if (mPEvent.CurrencyCode) {
203
+ conversionPayload.currency = mPEvent.CurrencyCode;
204
+ }
131
205
 
132
- sendOrQueueEvent(adWordEvent);
206
+ if (mPEvent.ProductAction.TotalAmount) {
207
+ conversionPayload.value = mPEvent.ProductAction.TotalAmount;
208
+ }
209
+
210
+ return mergeObjects(conversionPayload, customProps);
211
+ }
133
212
 
213
+ function sendGtagEvent(payload) {
214
+ try {
215
+ gtag('event', 'conversion', payload);
216
+ } catch (e) {
217
+ console.error('gtag is not available to send payload: ', payload, e);
218
+ return false;
219
+ }
134
220
  return true;
135
221
  }
136
222
 
137
- function getBaseAdWordEvent() {
138
- var adWordEvent = {};
139
- adWordEvent.google_conversion_value = 0;
140
- adWordEvent.google_conversion_language = 'en';
141
- adWordEvent.google_conversion_format = '3';
142
- adWordEvent.google_conversion_color = 'ffffff';
143
- adWordEvent.google_remarketing_only = forwarderSettings.remarketingOnly == 'True';
144
- adWordEvent.google_conversion_id = forwarderSettings.conversionId;
145
- return adWordEvent;
223
+ function sendAdwordsEvent(payload) {
224
+ try {
225
+ window.google_trackConversion(payload);
226
+ } catch (e) {
227
+ console.error('google_trackConversion is not available to send payload: ', payload, e);
228
+ return false;
229
+ }
230
+ return true;
146
231
  }
147
232
 
148
- function getConversionLabel(event, isPageEvent) {
233
+ // Looks up an Event's conversionLabel from customAttributeMappings based on computed jsHash value
234
+ function getConversionLabel(event) {
149
235
  var jsHash = calculateJSHash(event.EventDataType, event.EventCategory, event.EventName);
150
- var type = isPageEvent ? 'EventClass.Id' : 'EventClassDetails.Id';
236
+ var type = event.EventDataType === MessageType.PageEvent ? 'EventClass.Id' : 'EventClassDetails.Id';
151
237
  var conversionLabel = null;
152
238
  var mappingEntry = findValueInMapping(jsHash, type, labels);
153
239
 
@@ -158,10 +244,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
158
244
  return conversionLabel;
159
245
  }
160
246
 
161
- function getCustomProps(event, isPageEvent) {
247
+ // Filters Event.EventAttributes for attributes that are in customAttributeMappings
248
+ function getCustomProps(event) {
162
249
  var customProps = {};
163
250
  var attributes = event.EventAttributes;
164
- var type = isPageEvent ? 'EventAttributeClass.Id' : 'EventAttributeClassDetails.Id';
251
+ var type = event.EventDataType === MessageType.PageEvent ? 'EventAttributeClass.Id' : 'EventAttributeClassDetails.Id';
165
252
 
166
253
  if (attributes) {
167
254
  for (var attributeKey in attributes) {
@@ -202,27 +289,59 @@ Object.defineProperty(exports, '__esModule', { value: true });
202
289
  return mParticle.generateHash(preHash);
203
290
  }
204
291
 
292
+ function loadGtagSnippet() {
293
+ (function () {
294
+ window.dataLayer = window.dataLayer || [];
295
+ window.gtag = function(){dataLayer.push(arguments);};
296
+
297
+ var gTagScript = document.createElement('script');
298
+ gTagScript.async = true;
299
+ gTagScript.onload = function () {
300
+ gtag('js', new Date());
301
+ gtag('config', gtagSiteId);
302
+ isInitialized = true;
303
+ processQueue(eventQueue);
304
+ };
305
+ gTagScript.src = 'https://www.googletagmanager.com/gtag/js?id=' + gtagSiteId;
306
+ document.getElementsByTagName('head')[0].appendChild(gTagScript);
307
+ })();
308
+ }
309
+
310
+ function loadLegacySnippet() {
311
+ (function () {
312
+ var googleAdwords = document.createElement('script');
313
+ googleAdwords.type = 'text/javascript';
314
+ googleAdwords.async = true;
315
+ googleAdwords.onload = function() {
316
+ isInitialized = true;
317
+ processQueue(eventQueue);
318
+ };
319
+ googleAdwords.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://www.googleadservices.com/pagead/conversion_async.js';
320
+ document.getElementsByTagName('head')[0].appendChild(googleAdwords);
321
+ })();
322
+ }
323
+
205
324
  function initForwarder(settings, service, testMode) {
325
+
206
326
  forwarderSettings = settings;
207
327
  reportingService = service;
208
328
 
209
329
  try {
330
+ if (!forwarderSettings.conversionId) {
331
+ return 'Can\'t initialize forwarder: ' + name + ', conversionId is not defined';
332
+ }
333
+
334
+ gtagSiteId = "AW-" + forwarderSettings.conversionId;
335
+
210
336
  if (testMode !== true) {
211
- (function () {
212
- var googleAdwords = document.createElement('script');
213
- googleAdwords.type = 'text/javascript';
214
- googleAdwords.async = true;
215
- googleAdwords.onload = function() {
216
- if (eventQueue.length) {
217
- eventQueue.forEach(function(adWordEvent) {
218
- window.google_trackConversion(adWordEvent);
219
- });
220
- eventQueue = [];
221
- }
222
- };
223
- googleAdwords.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://www.googleadservices.com/pagead/conversion_async.js';
224
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(googleAdwords, s);
225
- })();
337
+ if (forwarderSettings.enableGtag == 'True') {
338
+ loadGtagSnippet();
339
+ } else {
340
+ loadLegacySnippet();
341
+ }
342
+ } else {
343
+ isInitialized = true;
344
+ processQueue(eventQueue);
226
345
  }
227
346
 
228
347
  if (!forwarderSettings.conversionId) {
@@ -243,8 +362,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
243
362
  return 'Can\'t initialize forwarder: ' + name + ', Could not process event to label mapping';
244
363
  }
245
364
 
246
- isInitialized = true;
247
-
248
365
  return 'Successfully initialized: ' + name;
249
366
  }
250
367
  catch (e) {
@@ -252,8 +369,24 @@ Object.defineProperty(exports, '__esModule', { value: true });
252
369
  }
253
370
  }
254
371
 
372
+ function processQueue(queue) {
373
+ var item;
374
+
375
+ if ((window.gtag || window.google_trackConversion) && queue.length > 0) {
376
+ try {
377
+ while (queue.length > 0) {
378
+ item = queue.shift();
379
+ item.action(item.data);
380
+ }
381
+ } catch (e) {
382
+ console.error('Error on mParticle Adwords Kit', e);
383
+ }
384
+ }
385
+ }
386
+
255
387
  this.init = initForwarder;
256
388
  this.process = processEvent;
389
+ this.processQueue = processQueue;
257
390
  };
258
391
 
259
392
  function getId() {
@@ -288,6 +421,18 @@ Object.defineProperty(exports, '__esModule', { value: true });
288
421
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
289
422
  }
290
423
 
424
+ function mergeObjects() {
425
+ var resObj = {};
426
+ for(var i=0; i < arguments.length; i += 1) {
427
+ var obj = arguments[i],
428
+ keys = Object.keys(obj);
429
+ for(var j=0; j < keys.length; j += 1) {
430
+ resObj[keys[j]] = obj[keys[j]];
431
+ }
432
+ }
433
+ return resObj;
434
+ }
435
+
291
436
  if (typeof window !== 'undefined') {
292
437
  if (window && window.mParticle && window.mParticle.addForwarder) {
293
438
  window.mParticle.addForwarder({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-adwords-kit",
3
- "version": "2.0.5",
3
+ "version": "2.1.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",