-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
444 lines (365 loc) · 13.5 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const getRequestHeader = require('getRequestHeader');
const getAllEventData = require('getAllEventData');
const setCookie = require('setCookie');
const getCookieValues = require('getCookieValues');
const encodeUriComponent = require('encodeUriComponent');
const makeString = require('makeString');
const makeInteger = require('makeInteger');
const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const logToConsole = require('logToConsole');
const generateRandom = require('generateRandom');
const getTimestampMillis = require('getTimestampMillis');
const getContainerVersion = require('getContainerVersion');
const parseUrl = require('parseUrl');
const Math = require('Math');
const getType = require('getType');
/**********************************************************************************************/
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;
const eventData = getAllEventData();
if (!isConsentGivenOrNotRequired()) {
return data.gtmOnSuccess();
}
const url = eventData.page_location || getRequestHeader('referer');
if (url && url.lastIndexOf('https://gtm-msr.appspot.com/', 0) === 0) {
return data.gtmOnSuccess();
}
const mappedEventData = mapEvent(eventData, data);
if (areThereRequiredFieldsMissing(mappedEventData)) {
log({
Name: 'AdRoll',
Type: 'Message',
TraceId: traceId,
EventName: mappedEventData.event_name,
Message: 'Event was not sent to AdRoll.',
Reason: 'Missing required fields'
});
return data.gtmOnFailure();
}
setCookiesIfNeeded(mappedEventData);
sendTrackRequest(mappedEventData);
if (data.useOptimisticScenario) {
data.gtmOnSuccess();
}
/**********************************************************************************************/
// Vendor related functions
function areThereRequiredFieldsMissing(mappedEventData) {
// advertisable_eid, pixel_eid - verified by the template UI validation rules
const requiredFields = ['event_name', 'page_location', 'ip'];
const identifiers = [
'adct',
'email',
'email_sha256',
'email_md5',
'device_id',
'first_party_cookie'
];
const missingBaseRequiredFields = requiredFields.some(
(field) => !isValidValue(mappedEventData[field])
);
if (missingBaseRequiredFields) return true;
const missingIdentifiers = identifiers.every(
(id) => !isValidValue(mappedEventData.identifiers[id])
);
if (missingIdentifiers) return true;
return false;
}
function getPostUrl() {
const isTestMode = [true, 'true'].indexOf(data.testMode) !== -1;
return (
'https://srv.adroll.com/api?' +
'advertisable=' +
enc(data.advertisableId) +
(isTestMode ? '&dry_run=1' : '')
);
}
function sendTrackRequest(mappedEventData) {
const postBody = mappedEventData;
const postUrl = getPostUrl();
log({
Name: 'AdRoll',
Type: 'Request',
TraceId: traceId,
EventName: mappedEventData.event_name,
RequestMethod: 'POST',
RequestUrl: postUrl,
RequestBody: postBody
});
sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
log({
Name: 'AdRoll',
Type: 'Response',
TraceId: traceId,
EventName: mappedEventData.event_name,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
});
if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}
},
{
headers: {
Authorization: 'Token ' + data.accessToken,
'Content-Type': 'application/json'
},
method: 'POST'
},
JSON.stringify(postBody)
);
}
function mapEvent(eventData, data) {
let mappedData = {
advertisable_eid: data.advertisableId,
pixel_eid: data.pixelId,
event_name: getEventName(eventData, data),
event_attributes: {},
identifiers: {}
};
mappedData = addUserData(eventData, mappedData);
mappedData = addAppDeviceData(eventData, mappedData);
mappedData = addServerData(eventData, mappedData); // Must go after addAppDeviceData.
mappedData = addCustomData(eventData, mappedData);
return mappedData;
}
function getEventName(eventData, data) {
if (data.eventType === 'inherit') {
const eventName = eventData.event_name;
const gaToEventName = {
page_view: 'pageView',
'gtm.dom': 'pageView',
search: 'productSearch',
view_search_results: 'productSearch',
view_item_list: 'productSearch',
add_to_cart: 'addToCart',
complete_registration: 'purchase',
sign_up: 'purchase',
generate_lead: 'purchase',
purchase: 'purchase',
'gtm4wp.addProductToCartEEC': 'addToCart',
'gtm4wp.orderCompletedEEC': 'purchase'
};
if (!gaToEventName[eventName]) {
return eventName;
}
return gaToEventName[eventName];
}
return data.eventType === 'standard' ? data.eventNameStandard : data.eventNameCustom;
}
function addServerData(eventData, mappedData) {
// Web (default)
if (eventData.page_location) mappedData.page_location = eventData.page_location;
// App (default)
else if (mappedData.package_app_name && mappedData.package_app_version) {
mappedData.page_location =
'https://' +
mappedData.package_app_name + // Used the package name to avoid adding a new field to the UI.
'?' +
'device_os=' +
mappedData.device_os +
'&device_type=' +
mappedData.device_type +
'&package_app_name=' +
mappedData.package_app_name +
'&package_app_version=' +
mappedData.package_app_version;
}
if (eventData.timestamp) mappedData.timestamp = eventData.timestamp;
else mappedData.timestamp = Math.round(getTimestampMillis() / 1000);
// Override with user input data from template fields.
if (data.serverDataList) {
data.serverDataList.forEach((d) => {
mappedData[d.name] = d.value;
});
}
return mappedData;
}
function addUserData(eventData, mappedData) {
const user_data = eventData.user_data || {};
const clickId = getClickId();
if (clickId) mappedData.identifiers.adct = clickId;
const browserId = getBrowserId();
if (browserId) mappedData.identifiers.first_party_cookie = browserId;
if (eventData.email) mappedData.identifiers.email = eventData.email;
else if (user_data.email_address) mappedData.identifiers.email = user_data.email_address;
else if (user_data.email) mappedData.identifiers.email = user_data.email;
if (eventData.email_sha256) mappedData.identifiers.email_sha256 = eventData.email_sha256;
else if (user_data.sha256_email_address) {
mappedData.identifiers.email_sha256 = user_data.sha256_email_address;
}
if (eventData.email_md5) mappedData.identifiers.email_md5 = eventData.email_md5;
if (eventData.external_id) mappedData.identifiers.user_id = eventData.external_id;
else if (eventData.user_id) mappedData.identifiers.user_id = eventData.user_id;
else if (eventData.userId) mappedData.identifiers.user_id = eventData.userId;
if (eventData.device_id) mappedData.identifiers.device_id = eventData.device_id;
else if (eventData['x-ga-resettable_device_id']) {
// Firebase events - IDFA (iOS) or GAID (Google)
mappedData.identifiers.device_id = eventData['x-ga-resettable_device_id'];
} else if (eventData['x-ga-vendor_device_id']) {
// Firebase events - IDFV (iOS)
mappedData.identifiers.device_id = eventData['x-ga-vendor_device_id'];
}
if (eventData.ip_override) {
mappedData.ip = eventData.ip_override.split(' ').join('').split(',')[0];
}
if (eventData.user_agent) mappedData.user_agent = eventData.user_agent;
// Override with user input data from template fields.
if (data.userDataList) {
data.userDataList.forEach((d) => {
// These go in the root level of the object.
if (['ip', 'user_agent'].indexOf(d.name) !== -1) mappedData[d.name] = d.value;
else mappedData.identifiers[d.name] = d.value;
});
}
return mappedData;
}
function addAppDeviceData(eventData, mappedData) {
if (eventData.device_os) mappedData.device_os = eventData.device_os;
else if (eventData['x-ga-platform']) mappedData.device_os = eventData['x-ga-platform']; // Firebase events
if (eventData.device_type) mappedData.device_type = eventData.device_type;
if (eventData.package_app_name) mappedData.package_app_name = eventData.package_app_name;
else if (eventData.app_id) mappedData.package_app_name = eventData.app_id; // Firebase events
if (eventData.package_app_version) mappedData.package_app_version = eventData.package_app_version;
else if (eventData.app_version) mappedData.package_app_version = eventData.app_version; // Firebase events
// Override with user input data from template fields.
if (data.appDeviceDataList) {
data.appDeviceDataList.forEach((d) => {
mappedData[d.name] = d.value;
});
}
return mappedData;
}
function addCustomData(eventData, mappedData) {
let currencyFromItems = '';
let valueFromItems = 0;
if (eventData.items && eventData.items[0]) {
mappedData.event_attributes.products = [];
currencyFromItems = eventData.items[0].currency;
const itemIdKey = data.itemIdKey ? data.itemIdKey : 'item_id';
eventData.items.forEach((d, i) => {
let content = {};
if (d[itemIdKey]) content.product_id = makeString(d[itemIdKey]);
if (d.quantity) content.quantity = makeInteger(d.quantity);
if (d.item_category) content.category = d.item_category;
if (d.product_group) content.product_group = d.product_group;
if (d.price) {
content.price = makeString(d.price);
valueFromItems += d.quantity ? d.quantity * d.price : d.price;
}
mappedData.event_attributes.products.push(content);
});
}
if (eventData.value) mappedData.conversion_value = eventData.value;
else if (valueFromItems) mappedData.conversion_value = valueFromItems;
if (eventData.currency) mappedData.currency = eventData.currency;
else if (currencyFromItems) mappedData.currency = currencyFromItems;
if (eventData.external_data) mappedData.external_data = eventData.external_data;
if (eventData.search_term) mappedData.event_attributes.keywords = eventData.search_term;
if (eventData.transaction_id) mappedData.event_attributes.order_id = eventData.transaction_id;
if (data.customDataList) {
data.customDataList.forEach((d) => {
// These go in the 'event_attributes' object.
if (['order_id', 'products', 'keywords'].indexOf(d.name) !== -1)
mappedData.event_attributes[d.name] = d.value;
else mappedData[d.name] = d.value;
});
}
return mappedData;
}
function getClickId() {
const searchParams = parseUrl(url).searchParams;
if (searchParams && searchParams.adct) return searchParams.adct;
const commonCookie = eventData.commonCookie || {};
return (
getCookieValues('__adroll_adct')[0] || eventData.__adroll_adct || commonCookie.__adroll_adct
);
}
function getBrowserId() {
const commonCookie = eventData.commonCookie || {};
return (
getCookieValues('__adroll_fpc')[0] ||
eventData.__adroll_fpc ||
commonCookie.__adroll_fpc ||
(!data.notSetBrowserIdCookie && generateBrowserId())
);
}
function generateBrowserId() {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 32; i++) {
const randomIndex = generateRandom(0, characters.length - 1);
result += characters.charAt(randomIndex);
}
result += '-' + getTimestampMillis();
return result;
}
function setCookiesIfNeeded(eventData) {
const cookieOptions = {
domain: data.overridenCookieDomain || 'auto',
path: '/',
samesite: 'Lax',
secure: true,
'max-age': 31536000, // 365 days
httpOnly: false
};
// "__adroll_adct" cookie creation. It contains the value of the "adct" query parameter (Click ID).
// AdRoll client script currently doesn't save it to a cookie. Only the Landing Page to Session Storage.
// We defined the cookie name using the same convention as used by the Browser ID.
if (!data.notSetClickIdCookie && eventData.identifiers && eventData.identifiers.adct) {
setCookie('__adroll_adct', eventData.identifiers.adct, cookieOptions);
}
// "__adroll_fpc" cookie creation. It contains the value of the Browser ID.
if (
!data.notSetBrowserIdCookie &&
eventData.identifiers &&
eventData.identifiers.first_party_cookie
) {
setCookie('__adroll_fpc', eventData.identifiers.first_party_cookie, cookieOptions);
}
}
/**********************************************************************************************/
// Helpers
function isValidValue(value) {
const valueType = getType(value);
return valueType !== 'null' && valueType !== 'undefined' && value !== '';
}
function enc(data) {
data = data || '';
return encodeUriComponent(makeString(data));
}
function isConsentGivenOrNotRequired() {
if (data.adStorageConsent !== 'required') return true;
if (eventData.consent_state) return !!eventData.consent_state.ad_storage;
const xGaGcs = eventData['x-ga-gcs'] || ''; // x-ga-gcs is a string like "G110"
return xGaGcs[2] === '1';
}
function log(logObject) {
if (isLoggingEnabled) {
logToConsole(JSON.stringify(logObject));
}
}
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(
containerVersion &&
(containerVersion.debugMode || containerVersion.previewMode)
);
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}