-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
146 lines (124 loc) · 4.04 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
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const getAllEventData = require('getAllEventData');
const getTimestampMillis = require('getTimestampMillis');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const getType = require('getType');
const sendHttpRequest = require('sendHttpRequest');
const encodeUriComponent = require('encodeUriComponent');
const makeString = require('makeString');
const generateRandom = require('generateRandom');
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;
let storeUrl = getStoreUrl();
let writeUrl = getWriteUrl(data.documentKey || generateDocumentKey());
let method = data.storeMerge ? 'PATCH' : 'PUT';
let input = data.addEventData ? getAllEventData() : {};
if (data.addTimestamp) input[data.timestampFieldName] = getTimestampMillis();
if (data.customDataList) {
data.customDataList.forEach((d) => {
if (data.skipNilValues) {
const dType = getType(d.value);
if (dType === 'undefined' || dType === 'null') return;
}
if (getType(d.name) === 'string' && d.name.indexOf('.') !== -1) {
const nameParts = d.name.split('.');
let obj = input;
for (let i = 0; i < nameParts.length - 1; i++) {
const part = nameParts[i];
if (!obj[part]) {
obj[part] = {};
}
obj = obj[part];
}
obj[nameParts[nameParts.length - 1]] = d.value;
} else {
input[d.name] = d.value;
}
});
}
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'StapeStore',
Type: 'Request',
TraceId: traceId,
EventName: 'Store',
RequestMethod: method,
RequestUrl: writeUrl,
RequestBody: input,
})
);
}
sendHttpRequest(writeUrl, {method: method, headers: { 'Content-Type': 'application/json' }}, JSON.stringify(input))
.then(() => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'StapeStore',
Type: 'Response',
TraceId: traceId,
EventName: 'Store',
ResponseStatusCode: 200,
ResponseHeaders: {},
ResponseBody: {},
})
);
}
data.gtmOnSuccess();
}, function () {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'StapeStore',
Type: 'Response',
TraceId: traceId,
EventName: 'Store',
ResponseStatusCode: 500,
ResponseHeaders: {},
ResponseBody: {},
})
);
}
data.gtmOnFailure();
});
function getStoreUrl() {
const containerIdentifier = getRequestHeader('x-gtm-identifier');
const defaultDomain = getRequestHeader('x-gtm-default-domain');
const containerApiKey = getRequestHeader('x-gtm-api-key');
return (
'https://' +
enc(containerIdentifier) +
'.' +
enc(defaultDomain) +
'/stape-api/' +
enc(containerApiKey) +
'/v1/store'
);
}
function getWriteUrl(documentKey) {
return storeUrl + '/' + enc(documentKey);
}
function generateDocumentKey() {
const rnd = makeString(generateRandom(1000000000, 2147483647));
return 'store_' + makeString(getTimestampMillis()) + rnd;
}
function enc(data) {
data = data || '';
return encodeUriComponent(data);
}
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';
}