forked from stape-io/reddit-tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
403 lines (335 loc) · 12 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
const getAllEventData = require('getAllEventData');
const JSON = require('JSON');
const sendHttpRequest = require('sendHttpRequest');
const setCookie = require('setCookie');
const getCookieValues = require('getCookieValues');
const getContainerVersion = require('getContainerVersion');
const logToConsole = require('logToConsole');
const getRequestHeader = require('getRequestHeader');
const parseUrl = require('parseUrl');
const decodeUriComponent = require('decodeUriComponent');
const getType = require('getType');
const getTimestampMillis = require('getTimestampMillis');
const Math = require('Math');
const makeNumber = require('makeNumber');
const makeString = require('makeString');
const encodeUriComponent = require('encodeUriComponent');
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;
const eventData = getAllEventData();
const apiVersion = '2.0';
const postUrl = 'https://ads-api.reddit.com/api/v' + apiVersion + '/conversions/events/' + enc(data.accountId);
const eventType = getEventType(eventData, data);
const eventName = eventType.tracking_type === 'Custom' ? eventType.custom_event_name : eventType.tracking_type;
const postBody = mapEvent(eventData, data);
const url = eventData.page_location || getRequestHeader('referer');
const deprecatedCookie = getCookieValues('rdt_cid')[0];
if (deprecatedCookie) {
setCookie('rdt_cid', '', {
domain: 'auto',
path: '/',
samesite: 'Lax',
secure: true,
'max-age': 0,
httpOnly: false
});
}
let rdtcid = deprecatedCookie || getCookieValues('_rdt_cid')[0] || eventData.rdt_cid;
if (url) {
const urlParsed = parseUrl(url);
if (urlParsed && urlParsed.searchParams.rdt_cid) {
rdtcid = decodeUriComponent(urlParsed.searchParams.rdt_cid);
}
}
if (rdtcid) {
setCookie('_rdt_cid', rdtcid, {
domain: 'auto',
path: '/',
samesite: 'Lax',
secure: true,
'max-age': 2592000, // 30 days
httpOnly: false
});
}
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Reddit',
Type: 'Request',
TraceId: traceId,
EventName: eventName,
RequestMethod: 'POST',
RequestUrl: postUrl,
RequestBody: postBody
})
);
}
sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Reddit',
Type: 'Response',
TraceId: traceId,
EventName: eventName,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}
},
{
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + data.accessToken
},
method: 'POST'
},
JSON.stringify(postBody)
);
if (data.useOptimisticScenario) {
data.gtmOnSuccess();
}
function mapEvent(eventData, data) {
let mappedData = {
event_type: eventType,
event_at: data.eventAt ? data.eventAt : convertToRFC3339UsingMath(getTimestampMillis())
};
if (data.clickId) {
mappedData.click_id = data.clickId;
} else if (rdtcid) {
mappedData.click_id = rdtcid;
}
mappedData = addUserData(eventData, mappedData);
mappedData = addPropertiesData(eventData, mappedData);
return {
events: [mappedData],
test_mode: data.testMode
};
}
function addPropertiesData(eventData, mappedData) {
mappedData.event_metadata = {};
if (eventData.event_id) mappedData.event_metadata.conversion_id = eventData.event_id;
else if (eventData.transaction_id) mappedData.event_metadata.conversion_id = eventData.transaction_id;
if (eventData.currency) mappedData.event_metadata.currency = eventData.currency;
if (eventData.item_count) mappedData.event_metadata.item_count = eventData.item_count;
if (eventData.value) mappedData.event_metadata.value_decimal = makeNumber(eventData.value);
else if (eventData['x-ga-mp1-ev']) mappedData.event_metadata.value_decimal = makeNumber(eventData['x-ga-mp1-ev']);
else if (eventData['x-ga-mp1-tr']) mappedData.event_metadata.value_decimal = makeNumber(eventData['x-ga-mp1-tr']);
if (eventData.products) mappedData.event_metadata.products = eventData.products;
else if (eventData.items && eventData.items[0]) {
mappedData.event_metadata.products = [];
eventData.items.forEach((d, i) => {
let item = {};
if (d.item_id) item.id = d.item_id;
else if (d.id) item.id = d.id;
if (d.content_category) item.category = d.content_category;
else if (d.category) item.category = d.category;
if (d.content_name) item.name = d.content_name;
else if (d.name) item.name = d.name;
mappedData.event_metadata.products.push(item);
});
}
if (data.serverEventDataList) {
data.serverEventDataList.forEach((d) => {
mappedData.event_metadata[d.name] = d.value;
});
}
return mappedData;
}
function addUserData(eventData, mappedData) {
let userEventData = {};
mappedData.user = {};
if (getType(eventData.user_data) === 'object') {
userEventData = eventData.user_data || eventData.user_properties || eventData.user;
}
const uuid = getUUIDFromCookie() || eventData.rdt_uuid;
if (uuid) mappedData.user.uuid = uuid;
if (eventData.aaid) mappedData.user.aaid = eventData.aaid;
else if (userEventData.aaid) mappedData.user.aaid = userEventData.aaid;
if (eventData.email) mappedData.user.email = eventData.email;
else if (eventData.email_address) mappedData.user.email = eventData.email_address;
else if (userEventData.email) mappedData.user.email = userEventData.email;
else if (userEventData.email_address) mappedData.user.email = userEventData.email_address;
else if (getCookieValues('_rdt_em')[0]) mappedData.user.email = getCookieValues('_rdt_em')[0];
if (eventData.external_id) mappedData.user.external_id = eventData.external_id;
else if (eventData.user_id) mappedData.user.external_id = eventData.user_id;
else if (eventData.userId) mappedData.user.external_id = eventData.userId;
else if (userEventData.external_id) mappedData.user.external_id = userEventData.external_id;
if (eventData.idfa) mappedData.user.idfa = eventData.idfa;
else if (userEventData.idfa) mappedData.user.idfa = userEventData.idfa;
if (eventData.ip_override) mappedData.user.ip_address = eventData.ip_override;
else if (eventData.ip_address) mappedData.user.ip_address = eventData.ip_address;
else if (eventData.ip) mappedData.user.ip_address = eventData.ip;
if (eventData.opt_out) mappedData.user.opt_out = eventData.opt_out;
if (eventData.user_agent) mappedData.user.user_agent = eventData.user_agent;
if (eventData.viewport_size && eventData.viewport_size.split('x').length === 2) {
mappedData.user.screen_dimensions = {
width: eventData.viewport_size.split('x')[0],
height: eventData.viewport_size.split('x')[1]
};
} else if (eventData.height && eventData.width) {
mappedData.user.screen_dimensions = {
height: eventData.height,
width: eventData.width
};
}
if (data.userDataList) {
data.userDataList.forEach((d) => {
mappedData.user[d.name] = d.value;
});
}
return mappedData;
}
function getUUIDFromCookie() {
const uuidsWithTimestamps = getCookieValues('_rdt_uuid');
if (!uuidsWithTimestamps || !uuidsWithTimestamps.length) return null;
let oldest = null;
for (let i = 0; i < uuidsWithTimestamps.length; i++) {
const current = getUUIDAndTimestamp(uuidsWithTimestamps[i]);
if (!current) continue;
if (!oldest || current.timestamp < oldest.timestamp) oldest = current;
}
return oldest && oldest.uuid;
}
function getUUIDAndTimestamp(uuidWithTimestamp) {
if (!uuidWithTimestamp) return null;
const parts = uuidWithTimestamp.split('.');
if (parts.length !== 2) return null;
return {
timestamp: makeNumber(parts[0]),
uuid: makeString(parts[1])
};
}
function getEventType(eventData, data) {
if (data.eventType === 'inherit') {
let eventName = eventData.event_name;
let gaToEventName = {
page_view: 'PageVisit',
click: 'Lead',
download: 'Lead',
file_download: 'Lead',
complete_registration: 'SignUp',
'gtm.dom': 'PageVisit',
add_payment_info: 'Lead',
add_to_cart: 'AddToCart',
add_to_wishlist: 'AddToWishlist',
sign_up: 'SignUp',
begin_checkout: 'Lead',
generate_lead: 'Lead',
purchase: 'Purchase',
search: 'Search',
view_item: 'ViewContent',
contact: 'Lead',
find_location: 'Search',
submit_application: 'Lead',
subscribe: 'Lead',
'gtm4wp.addProductToCartEEC': 'AddToCart',
'gtm4wp.productClickEEC': 'ViewContent',
'gtm4wp.checkoutOptionEEC': 'Lead',
'gtm4wp.checkoutStepEEC': 'Lead',
'gtm4wp.orderCompletedEEC': 'Purchase'
};
if (!gaToEventName[eventName]) {
return {
tracking_type: 'Custom',
custom_event_name: eventName
};
}
return {
tracking_type: gaToEventName[eventName]
};
}
if (data.eventNameCustom == 'Purchase' || data.eventNameCustom == 'SignUp') {
return {
tracking_type: data.eventNameCustom
};
}
if (data.eventType === 'custom') {
return {
tracking_type: 'Custom',
custom_event_name: data.eventNameCustom
};
}
return {
tracking_type: data.eventName
};
}
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';
}
function enc(data) {
data = data || '';
return encodeUriComponent(data);
}
function convertToRFC3339UsingMath(milliseconds) {
// Constants
const msPerSecond = 1000;
const msPerMinute = 60 * msPerSecond;
const msPerHour = 60 * msPerMinute;
const msPerDay = 24 * msPerHour;
// Calculate the number of days since epoch
let daysSinceEpoch = Math.floor(milliseconds / msPerDay);
// Calculate the number of milliseconds remaining after the full days are accounted for
let remainingMilliseconds = milliseconds % msPerDay;
// Calculate the UTC hours, minutes, seconds, and milliseconds
let hours = Math.floor(remainingMilliseconds / msPerHour);
remainingMilliseconds = remainingMilliseconds % msPerHour;
let minutes = Math.floor(remainingMilliseconds / msPerMinute);
remainingMilliseconds = remainingMilliseconds % msPerMinute;
let seconds = Math.floor(remainingMilliseconds / msPerSecond);
// Calculate the date (year, month, day) from the number of days since epoch
let year = 1970;
let month = 1;
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
while (daysSinceEpoch >= 365) {
if (isLeapYear(year)) {
if (daysSinceEpoch >= 366) {
daysSinceEpoch = daysSinceEpoch - 366;
year++;
}
} else {
daysSinceEpoch = daysSinceEpoch - 365;
year++;
}
}
while (daysSinceEpoch >= daysInMonth[month - 1]) {
if (month === 2 && isLeapYear(year) && daysSinceEpoch >= 29) {
daysSinceEpoch = daysSinceEpoch - 29;
} else {
daysSinceEpoch = daysSinceEpoch - daysInMonth[month - 1];
}
month++;
}
let day = daysSinceEpoch + 1;
// Helper function to check if a year is a leap year
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
// Format components to ensure two digits where needed
const pad = (n) => n < 10 ? '0' + n : n;
// Construct the RFC 3339 timestamp
const rfc3339Timestamp = year + '-' + pad(month) + '-' + pad(day) + 'T' + pad(hours) + ':' + pad(minutes) + ':' + pad(seconds) + 'Z';
return rfc3339Timestamp;
}