-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
225 lines (182 loc) · 5.74 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
const sendHttpRequest = require('sendHttpRequest');
const setCookie = require('setCookie');
const parseUrl = require('parseUrl');
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const encodeUriComponent = require('encodeUriComponent');
const getCookieValues = require('getCookieValues');
const getAllEventData = require('getAllEventData');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const makeString = require('makeString');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
const eventData = getAllEventData();
if (!isConsentGivenOrNotRequired()) {
return data.gtmOnSuccess();
}
if (data.type === 'page_view') {
const url = eventData.page_location || getRequestHeader('referer');
if (url) {
const value = parseUrl(url).searchParams[data.clickIdParameterName];
if (value) {
const options = {
domain: 'auto',
path: '/',
secure: true,
httpOnly: false,
'max-age': 86400 * 395
};
setCookie('_aid', value, options, false);
}
}
} else {
const requestUrl = getRequestUrls();
for (let i = 0; i < requestUrl.length; i++) {
if (requestUrl[i]) {
sendRequest(requestUrl[i]);
}
}
}
data.gtmOnSuccess();
function sendRequest(requestUrl) {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Admitad',
Type: 'Request',
TraceId: traceId,
EventName: 'Conversion',
RequestMethod: 'GET',
RequestUrl: requestUrl
})
);
}
sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Admitad',
Type: 'Response',
TraceId: traceId,
EventName: 'Conversion',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
},
{ method: 'GET' }
);
}
function getRequestUrls() {
let requestUrl = 'https://ad.admitad.com/r?postback=1';
requestUrl = requestUrl + '&campaign_code=' + enc(data.campaignCode);
requestUrl = requestUrl + '&postback_key=' + enc(data.postbackKey);
requestUrl = requestUrl + '&action_code=' + enc(data.actionCode);
requestUrl = requestUrl + '&tariff_code=' + enc(data.tariffCode);
requestUrl = requestUrl + '&payment_type=' + enc(data.paymentType);
const orderId = data.orderId || eventData.orderId || eventData.order_id || eventData.transaction_id;
if (orderId) {
requestUrl = requestUrl + '&order_id=' + enc(orderId);
}
const price = data.price || eventData.amount || eventData.value || eventData.price;
if (price) {
requestUrl = requestUrl + '&price=' + enc(price);
}
const clientId = data.clientId || eventData.external_id;
if (clientId) {
requestUrl = requestUrl + '&client_id=' + enc(clientId);
}
const currency = data.currencyCode || eventData.currencyCode || eventData.currency;
if (currency) {
requestUrl = requestUrl + '¤cy_code=' + enc(currency);
}
const city = data.city || eventData.city || eventData.city;
if (city) {
requestUrl = requestUrl + '&city=' + enc(city);
}
const coupon = data.promocode || eventData.promocode || eventData.coupon;
if (coupon) {
requestUrl = requestUrl + '&promocode=' + enc(coupon);
}
const cookie = getCookieValues('_aid')[0] || '';
if (cookie) {
requestUrl = requestUrl + '&uid=' + enc(cookie);
}
if (data.quantity || data.positionId || data.positionCount || data.productId) {
const quantity = data.quantity || eventData.quantity;
if (quantity) {
requestUrl = requestUrl + '&quantity=' + enc(quantity);
}
if (data.positionId) {
requestUrl = requestUrl + '&position_id=' + enc(data.positionId);
}
if (data.positionCount) {
requestUrl = requestUrl + '&position_count=' + enc(data.positionCount);
}
const productId = data.productId || eventData.productId;
if (productId) {
requestUrl = requestUrl + '&product_id=' + enc(productId);
}
return [requestUrl];
}
const items = data.items || eventData.items || {};
if (!items && !items.length) {
return [requestUrl];
}
let requestUrls = [];
for (let i = 0; i < items.length; i++) {
let item = items[i];
let itemUrl = requestUrl + '&quantity=' + enc((item.quantity || item.item_quantity));
if (item.positionId) {
itemUrl = itemUrl + '&position_id=' + enc((i+1));
}
if (item.positionCount) {
itemUrl = itemUrl + '&position_count=' + enc((items.length+1));
}
if (item.productId) {
itemUrl = itemUrl + '&product_id=' + enc((item.productId || item.product_id || item.item_id));
}
if (sameUrlExists(requestUrls, itemUrl)) {
continue;
}
requestUrls.push(itemUrl);
}
return requestUrls;
}
function sameUrlExists(urls, url) {
for (let i = 0; i < urls.length; i++) {
if (urls[i] === url) {
return true;
}
}
return false;
}
function enc(data) {
data = data || '';
return encodeUriComponent(makeString(data));
}
function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}
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';
}