-
Notifications
You must be signed in to change notification settings - Fork 14
/
template.js
472 lines (399 loc) · 11.7 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
const copyFromDataLayer = require('copyFromDataLayer');
const JSON = require('JSON');
const getUrl = require('getUrl');
const getReferrerUrl = require('getReferrerUrl');
const readTitle = require('readTitle');
const injectScript = require('injectScript');
const callInWindow = require('callInWindow');
const makeNumber = require('makeNumber');
const readCharacterSet = require('readCharacterSet');
const localStorage = require('localStorage');
const sendPixel = require('sendPixel');
const encodeUriComponent = require('encodeUriComponent');
const toBase64 = require('toBase64');
const makeString = require('makeString');
const setCookie = require('setCookie');
const getCookieValues = require('getCookieValues');
const getContainerVersion = require('getContainerVersion');
const isConsentGranted = require('isConsentGranted');
let pageLocation = getUrl();
if (
pageLocation &&
pageLocation.lastIndexOf('https://gtm-msr.appspot.com/', 0) === 0
) {
data.gtmOnSuccess();
return;
}
const userAndCustomData = getUserAndCustomDataArray();
let requestType = determinateRequestType();
if (requestType === 'post') {
const dataScriptVersion = 'v8';
const dataTagScriptUrl =
typeof data.data_tag_load_script_url !== 'undefined'
? data.data_tag_load_script_url.replace(
'${data-script-version}',
dataScriptVersion
)
: 'https://stapecdn.com/dtag/' + dataScriptVersion + '.js';
injectScript(
dataTagScriptUrl,
sendPostRequest,
data.gtmOnFailure,
dataTagScriptUrl
);
} else {
sendGetRequest();
}
function sendPostRequest() {
let eventData = {};
eventData = addCommonDataForPostRequest(data, eventData);
eventData = addRequiredDataForPostRequest(data, eventData);
eventData = addGaRequiredData(data, eventData);
callInWindow(
'dataTagSendData',
eventData,
data.gtm_server_domain,
data.request_path +
'?v=' +
eventData.v +
'&event_name=' +
encodeUriComponent(eventData.event_name) +
(data.richsstsse ? '&richsstsse' : ''),
data.dataLayerEventName,
data.dataLayerVariableName,
data.waitForCookies,
data.useFetchInsteadOfXHR
);
data.gtmOnSuccess();
}
function sendGetRequest() {
sendPixel(
addDataForGetRequest(data, buildEndpoint()),
data.gtmOnSuccess,
data.gtmOnFailure
);
}
function buildEndpoint() {
return data.gtm_server_domain + data.request_path;
}
function addRequiredDataForPostRequest(data, eventData) {
eventData.event_name = getEventName(data);
eventData.v = makeNumber(data.protocol_version);
let customData = getCustomData(data, true);
for (let key in customData) {
eventData[customData[key].name] = customData[key].value;
}
return eventData;
}
function addGaRequiredData(data, eventData) {
if (data.addGaParameters && data.gaId) {
eventData['x-ga-measurement_id'] = data.gaId;
eventData['x-ga-page_id'] = copyFromDataLayer('gtm.start');
eventData['x-ga-mp2-richsstsse'] = '';
eventData['x-ga-mp2-seg'] = 1;
eventData['x-ga-request_count'] = 1;
eventData['x-ga-protocol_version'] = 2;
eventData.v = 2;
}
return eventData;
}
function addDataForGetRequest(data, url) {
let eventData = {};
url +=
'?v=' +
data.protocol_version +
'&event_name=' +
encodeUriComponent(getEventName(data));
if (data.add_common) {
eventData = addCommonData(data, eventData);
}
if (data.add_consent_state) {
eventData = addConsentStateData(eventData);
}
if (data.add_common_cookie) {
eventData = addCommonCookie(eventData);
}
let customData = getCustomData(data, false);
if (customData.length) {
for (let customDataKey in customData) {
eventData[customData[customDataKey].name] =
customData[customDataKey].value;
}
}
if (data.request_type === 'auto') {
return (
url + '&dtdc=' + encodeUriComponent(toBase64(JSON.stringify(eventData)))
);
}
for (let eventDataKey in eventData) {
url +=
'&' +
eventDataKey +
'=' +
(eventData[eventDataKey]
? encodeUriComponent(eventData[eventDataKey])
: '');
}
return url;
}
function addCommonDataForPostRequest(data, eventData) {
if (data.add_common || data.add_data_layer) {
const dataTagData = callInWindow(
'dataTagGetData',
getContainerVersion()['containerId']
);
if (data.add_data_layer && dataTagData.dataModel) {
for (let dataKey in dataTagData.dataModel) {
eventData[dataKey] = dataTagData.dataModel[dataKey];
}
}
if (data.add_common) {
eventData = addCommonData(data, eventData);
eventData.screen_resolution =
dataTagData.screen.width + 'x' + dataTagData.screen.height;
eventData.viewport_size =
dataTagData.innerWidth + 'x' + dataTagData.innerHeight;
}
}
if (data.add_consent_state) {
eventData = addConsentStateData(eventData);
}
if (data.add_common_cookie) {
eventData = addCommonCookie(eventData);
}
return eventData;
}
function addCommonData(data, eventData) {
eventData.page_location = getUrl();
eventData.page_hostname = getUrl('host');
eventData.page_referrer = getReferrerUrl();
eventData.page_title = readTitle();
eventData.page_encoding = readCharacterSet();
return eventData;
}
function addConsentStateData(eventData) {
eventData.consent_state = {
ad_storage: isConsentGranted('ad_storage'),
ad_user_data: isConsentGranted('ad_user_data'),
ad_personalization: isConsentGranted('ad_personalization'),
analytics_storage: isConsentGranted('analytics_storage'),
functionality_storage: isConsentGranted('functionality_storage'),
personalization_storage: isConsentGranted('personalization_storage'),
security_storage: isConsentGranted('security_storage'),
};
return eventData;
}
function getEventName(data) {
const eventName = 'page_view';
if (data.event_type === 'standard') {
return data.event_name_standard ? data.event_name_standard : eventName;
}
if (data.event_type === 'custom') {
return data.event_name_custom ? data.event_name_custom : eventName;
}
return eventName;
}
function getCustomData(data, dtagLoaded) {
let dataToStore = [];
let customData = userAndCustomData;
for (let dataKey in customData) {
let dataValue = customData[dataKey].value;
let dataTransformation = customData[dataKey].transformation;
if (dataValue) {
if (dataTransformation === 'trim') {
dataValue = makeString(dataValue);
dataValue = dataValue.trim();
}
if (dataTransformation === 'to_lower_case') {
dataValue = makeString(dataValue);
dataValue = dataValue.trim().toLowerCase();
}
if (dataTransformation === 'base64') {
dataValue = makeString(dataValue);
dataValue = toBase64(dataValue);
}
if (dtagLoaded && dataTransformation === 'md5') {
dataValue = makeString(dataValue);
dataValue = callInWindow('dataTagMD5', dataValue.trim().toLowerCase());
}
if (dtagLoaded && dataTransformation === 'sha256base64') {
dataValue = makeString(dataValue);
dataValue = callInWindow(
'dataTag256',
dataValue.trim().toLowerCase(),
'B64'
);
}
if (dtagLoaded && dataTransformation === 'sha256hex') {
dataValue = makeString(dataValue);
dataValue = callInWindow(
'dataTag256',
dataValue.trim().toLowerCase(),
'HEX'
);
}
if (customData[dataKey].store && customData[dataKey].store !== 'none') {
dataToStore.push({
store: customData[dataKey].store,
name: customData[dataKey].name,
value: dataValue,
});
}
customData[dataKey].value = dataValue;
}
}
if (dataToStore.length !== 0) {
storeData(dataToStore);
}
return customData;
}
function storeData(dataToStore) {
let dataToStoreCookieResult = {};
let dataToStoreLocalStorageResult = {};
let dataToStoreCookie = getCookieValues('stape')[0];
if (dataToStoreCookie) {
dataToStoreCookie = JSON.parse(dataToStoreCookie);
if (dataToStoreCookie) {
for (let attrName in dataToStoreCookie) {
if (dataToStoreCookie[attrName])
dataToStoreCookieResult[attrName] = dataToStoreCookie[attrName];
}
}
}
if (localStorage) {
let dataToStoreLocalStorage = localStorage.getItem('stape');
if (dataToStoreLocalStorage) {
dataToStoreLocalStorage = JSON.parse(dataToStoreLocalStorage);
if (dataToStoreLocalStorage) {
for (let attrName in dataToStoreLocalStorage) {
if (dataToStoreLocalStorage[attrName])
dataToStoreLocalStorageResult[attrName] =
dataToStoreLocalStorage[attrName];
}
}
}
}
for (let attrName in dataToStore) {
if (dataToStore[attrName].value) {
if (
dataToStore[attrName].store === 'all' ||
dataToStore[attrName].store === 'localStorage'
) {
dataToStoreLocalStorageResult[dataToStore[attrName].name] =
dataToStore[attrName].value;
}
if (
dataToStore[attrName].store === 'all' ||
dataToStore[attrName].store === 'cookies'
) {
dataToStoreCookieResult[dataToStore[attrName].name] =
dataToStore[attrName].value;
}
}
}
if (localStorage && getObjectLength(dataToStoreLocalStorageResult) !== 0) {
localStorage.setItem(
'stape',
JSON.stringify(dataToStoreLocalStorageResult)
);
}
if (getObjectLength(dataToStoreCookieResult) !== 0) {
setCookie('stape', JSON.stringify(dataToStoreCookieResult), {
secure: true,
domain: 'auto',
path: '/',
});
}
}
function getObjectLength(object) {
let length = 0;
for (let key in object) {
if (object.hasOwnProperty(key)) {
++length;
}
}
return length;
}
function determinateRequestType() {
if (data.request_type !== 'auto') {
return data.request_type;
}
if (data.add_data_layer) {
return 'post';
}
if (data.dataLayerEventPush) {
return 'post';
}
if (data.richsstsse) {
return 'post';
}
const isHashingEnabled = userAndCustomData.some(
(item) =>
item.transformation === 'md5' ||
item.transformation === 'sha256base64' ||
item.transformation === 'sha256hex'
);
if (isHashingEnabled) return 'post';
const userAndCustomDataLength = makeNumber(
JSON.stringify(userAndCustomData).length
);
return userAndCustomDataLength > 1500 ? 'post' : 'get';
}
function getUserAndCustomDataArray() {
let userAndCustomDataArray = [];
if (data.custom_data && data.custom_data.length) {
userAndCustomDataArray = data.custom_data;
}
if (data.user_data && data.user_data.length) {
for (let userDataKey in data.user_data) {
userAndCustomDataArray.push(data.user_data[userDataKey]);
}
}
return userAndCustomDataArray;
}
function addCommonCookie(eventData) {
const cookieNames = [
// FB cookies
'_fbc',
'_fbp',
'_gtmeec',
// TikTok cookies
'ttclid',
'_ttp',
// Pinterest cookies
'_epik',
// Snapchat cookies
'_scid',
'_scclid',
// Taboola cookies
'taboola_cid',
// Awin cookies
'awin_awc',
'awin_source',
// Rakuten cookies
'rakuten_site_id',
'rakuten_time_entered',
'rakuten_ran_mid',
'rakuten_ran_eaid',
'rakuten_ran_site_id',
// Klaviyo cookies
'stape_klaviyo_email',
'stape_klaviyo_kx',
'stape_klaviyo_viewed_items',
// Outbrain cookies
'outbrain_cid',
];
let commonCookie = null;
for (var i = 0; i < cookieNames.length; i++) {
const name = cookieNames[i];
var cookie = getCookieValues(name)[0];
if (cookie) {
commonCookie = commonCookie || {};
commonCookie[name] = cookie;
}
}
if (commonCookie) {
eventData.common_cookie = commonCookie;
}
return eventData;
}