-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
131 lines (111 loc) · 3.3 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
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 makeNumber = require('makeNumber');
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('__sscid', value, options, false);
}
}
data.gtmOnSuccess();
} else {
const requestUrl = getRequestUrl();
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'ShareASale',
Type: 'Request',
TraceId: traceId,
EventName: 'Conversion',
RequestMethod: 'GET',
RequestUrl: requestUrl
})
);
}
sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'ShareASale',
Type: 'Response',
TraceId: traceId,
EventName: 'Conversion',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
if (statusCode >= 200 && statusCode < 303) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{ method: 'GET' }
);
}
function getRequestUrl() {
let requestUrl = 'https://www.shareasale.com/sale.cfm?v=stape';
requestUrl = requestUrl + '&transtype=' + enc(data.transtype);
requestUrl = requestUrl + '&merchantID=' + enc(data.merchantID);
let amount = data.amount || eventData.amount || eventData.value;
if (amount) {
requestUrl = requestUrl + '&amount=' + enc(makeNumber(amount));
}
const cookie = getCookieValues('__sscid')[0] || '';
if (cookie) {
requestUrl = requestUrl + '&tracking=' + enc(cookie);
}
return requestUrl;
}
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';
}