-
Notifications
You must be signed in to change notification settings - Fork 4
/
template.js
433 lines (371 loc) · 11.6 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
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const getAllEventData = require("getAllEventData");
const sendHttpRequest = require("sendHttpRequest");
const JSON = require("JSON");
const logToConsole = require("logToConsole");
const sha256Sync = require("sha256Sync");
const parseUrl = require("parseUrl");
const decodeUriComponent = require("decodeUriComponent");
const getCookieValues = require("getCookieValues");
const setCookie = require("setCookie");
const makeNumber = require("makeNumber");
const getContainerVersion = require('getContainerVersion');
const getRequestHeader = require('getRequestHeader');
const getTimestampMillis = require("getTimestampMillis");
const Math = require("Math");
const gtmVersion = "s2s_0_2_01";
const eventData = getAllEventData();
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;
const TTEventMap = {
'add_payment_info': 'AddPaymentInfo',
'add_to_cart': 'AddToCart',
'add_to_wishlist': 'AddToWishlist',
'begin_checkout': 'InitiateCheckout',
'page_view': 'Pageview',
'purchase': 'CompletePayment',
'search': 'Search',
'sign_up': 'CompleteRegistration',
'view_item': 'ViewContent',
};
function isHashed(val) {
return val && val.match("^[A-Fa-f0-9]{64}$") != null;
}
function hash(val, lowerCase) {
if (!val) {
return undefined;
}
if (isHashed(val)) {
return val;
}
if (lowerCase) {
val = val.toLowerCase();
}
val = val.trim();
if (!val) {
return undefined;
}
return sha256Sync(val, { outputEncoding: 'hex' });
}
function getTtclidFromUrl() {
const locationData = getDataFromUrl(eventData.page_location);
if (locationData.ttclid) {
return decodeUriComponent(locationData.ttclid);
}
const referrerData = getDataFromUrl(eventData.page_referrer);
if (referrerData.ttclid && referrerData.hostname === locationData.hostname) {
return decodeUriComponent(referrerData.ttclid);
}
return null;
}
function getDataFromUrl(url) {
const result = {};
if (!url) {
return result;
}
const parsed = parseUrl(url);
if (!parsed) {
return result;
}
if (parsed.searchParams && parsed.searchParams.ttclid) {
result.ttclid = parsed.searchParams.ttclid;
}
result.hostname = parsed.hostname;
return result;
}
function getTtclidCookie() {
return getCookieValues("ttclid")[0];
}
function setTtclidCookie(cookie) {
if (cookie) {
setCookie("ttclid", cookie, {
domain: "auto",
httpOnly: false,
"max-age": 2592000, // 30 days
path: "/",
secure: true,
samesite: "none",
});
}
}
function getTtpCookie() {
return getCookieValues("_ttp")[0];
}
function getTtclid() {
return (
getTtclidFromUrl() || getTtclidCookie() || data.ttclid || eventData.ttclid
);
}
function getTtp() {
return getTtpCookie();
}
function makeGAContent(item) {
const price = makeNumber(item.price);
const quantity = makeNumber(item.quantity || 1);
const category = item.item_category || item.item_category1 || item.item_category2;
const content = {};
content.content_type = eventData.tt_content_type || 'product';
if (item.item_id) content.content_id = item.item_id;
if (item.item_name) content.content_name = item.item_name;
if (price) content.price = price;
if (quantity) content.quantity = quantity;
if (category) content.content_category = category;
if (item.item_brand) content.brand = item.item_brand;
return content;
}
function _getGaContents(items) {
// Standard Ecommerce: https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtm
// Enhanced Ecommerce: https://developers.google.com/analytics/devguides/collection/ua/gtm/enhanced-ecommerce#details
// Both formats are normalized by the server-side "GA4 Client" or "UA Client"
if (!items) {
return undefined;
}
const contents = [];
for (const item of items) {
const content = makeGAContent(item);
if (content) {
contents.push(content);
}
}
return contents;
}
function makeDataContent(data) {
const price = makeNumber(data.price);
const quantity = makeNumber(data.quantity || 1);
const content = {};
content.content_type = eventData.tt_content_type || data.content_type || 'product';
if (data.content_id) content.content_id = data.content_id;
if (data.content_name) content.content_name = data.content_name;
if (price) content.price = price;
if (quantity) content.quantity = quantity;
if (data.content_category) content.content_category = data.content_category;
if (data.brand) content.brand = data.brand;
return content;
}
function getContents(items) {
const gaContents = _getGaContents(items);
if (data.object_property_source == "ga") {
return gaContents;
}
if (data.single_multi_product == "multiple") {
// Note: JSON.parse returns undefined if the string is not valid.
const contents = JSON.parse(data.contents);
if (contents) {
for (const c of contents) {
if (!c.content_type) c.content_type = eventData.tt_content_type || 'product';
}
return contents;
}
return data.contents;
}
if (data.single_multi_product == "single") {
const content = makeDataContent(data);
return [content];
}
// Backup
if (gaContents && gaContents.length > 0) {
return gaContents;
}
return undefined;
}
function getValue(contents) {
var val = makeNumber(data.value) || makeNumber(eventData.value);
if (val) {
return val;
}
if (contents && contents.length > 0) {
var totalValue = 0;
for (const content of contents) {
if (content.price !== undefined && content.quantity !== undefined) {
totalValue += content.price * content.quantity;
}
}
return totalValue;
}
return undefined;
}
function getCurrency(items) {
if (data.currency || eventData.currency) {
return data.currency || eventData.currency;
}
if (items) {
for (const item of items) {
if (item && item.currency !== undefined) {
return item.currency;
}
}
}
return undefined;
}
function getItems() {
if (eventData.items) {
return eventData.items;
}
if (eventData.tt_contents) {
const contents = JSON.parse(eventData.tt_contents);
if (contents) {
return contents;
}
}
return undefined;
}
function getBody(ttclid, ttp) {
const _external_id = data.external_id || data.sha256_external_id || eventData.tt_external_id || eventData.user_id;
const _phone = data.phone ||
data.sha256_phone ||
(eventData.user_data && (eventData.user_data.phone_number || eventData.user_data.sha256_phone_number));
const _email = data.email ||
data.sha256_email ||
(eventData.user_data && (eventData.user_data.email_address || eventData.user_data.sha256_email_address)) ||
eventData.email;
const _ip = eventData.ip_override || eventData.ip;
const _event = data.event || eventData.event_name;
const body = {
event_source: "web",
partner_name: "GoogleTagManagerServer",
};
const bodyData = {
event_time: Math.floor(getTimestampMillis() / 1000),
};
if (data.pixel_code) body.event_source_id = data.pixel_code;
if (_event) bodyData.event = TTEventMap[_event] || _event;
if (data.event_id || eventData.event_id) bodyData.event_id = data.event_id || eventData.event_id;
if (data.test_event_code) body.test_event_code = data.test_event_code;
const page = {};
if (eventData.page_location) page.url = eventData.page_location;
if (eventData.page_referrer) page.referrer = eventData.page_referrer;
bodyData.page = page;
const user = {};
if (ttclid) user.ttclid = ttclid;
if (_external_id) user.external_id = hash(_external_id, false);
if (_phone) user.phone = hash(_phone, true);
if (_email) user.email = hash(_email, true);
if (ttp) user.ttp = ttp;
if (_ip) user.ip = _ip;
if (eventData.user_agent) user.user_agent = eventData.user_agent;
bodyData.user = user;
const device = {};
if (eventData.client_hints) {
const ch = eventData.client_hints;
if (ch.architecture) device.architecture = ch.architecture;
if (ch.bitness) device.bitness = ch.bitness;
if (ch.full_version_list) device.browser_version_list = ch.full_version_list;
if (ch.mobile != undefined) device.mobile = ch.mobile;
if (ch.model) device.model = ch.model;
if (ch.platform) device.platform = ch.platform;
if (ch.platform_version) device.platform_version = ch.platform_version;
}
bodyData.device = device;
const properties = {};
const items = getItems();
const contents = getContents(items);
const currency = getCurrency(items);
const value = getValue(contents);
const description = data.description;
const query = data.query || eventData.query || eventData.search_term;
if (contents) properties.contents = contents;
if (currency) properties.currency = currency;
if (value) properties.value = value;
if (description) properties.description = description;
if (query) properties.query = query;
// Additional Object Properties
if (data.custom_properties && data.custom_properties.length > 0) {
for (let i = 0; i < data.custom_properties.length; i++) {
let objectParam = data.custom_properties[i];
properties[objectParam.key] = objectParam.value;
}
}
properties.gtm_version = gtmVersion;
properties.event_trigger_source = 'GoogleTagManagerServer';
bodyData.properties = properties;
body.data = [bodyData];
return body;
}
function send(accessToken, requestBody, onSuccess, onFailure) {
const url = "https://business-api.tiktok.com/open_api/v1.3/event/track/";
const headers = {
"Content-Type": "application/json",
"Access-Token": accessToken,
};
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'TikTok',
Type: 'Request',
TraceId: traceId,
EventName: requestBody.event,
RequestMethod: 'POST',
RequestUrl: url,
RequestBody: requestBody,
})
);
}
sendHttpRequest(
url,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'TikTok',
Type: 'Response',
TraceId: traceId,
EventName: requestBody.event,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
})
);
}
if (statusCode >= 200 && statusCode < 400) {
onSuccess();
} else {
onFailure();
}
},
{
headers: headers,
method: "POST",
},
JSON.stringify(requestBody)
);
}
function main() {
const ttclid = getTtclid();
const ttp = getTtp();
setTtclidCookie(ttclid);
const body = getBody(ttclid, ttp);
const accessToken = data.access_token;
send(accessToken, body, data.gtmOnSuccess, data.gtmOnFailure);
}
main();
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';
}