-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.js
159 lines (133 loc) · 3.87 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
const sendHttpRequest = require('sendHttpRequest');
const JSON = require('JSON');
const makeTableMap = require('makeTableMap');
const getRequestHeader = require('getRequestHeader');
const getAllEventData = require('getAllEventData');
const makeString = require('makeString');
const makeInteger = require('makeInteger');
const makeNumber = require('makeNumber');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
const eventData = getAllEventData();
const requestUrl = generateRequestUrl();
const requestHeaders = {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer ' + data.apiKey,
};
const eventName =
data.eventType === 'custom' ? data.customEvent : data.eCommerceEvent;
const postBody = generatePostBody();
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Attentive',
Type: 'Request',
TraceId: traceId,
EventName: eventName,
RequestMethod: 'POST',
RequestUrl: requestUrl,
RequestBody: postBody,
})
);
}
sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Attentive',
Type: 'Response',
TraceId: traceId,
EventName: eventName,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
})
);
}
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{ headers: requestHeaders, method: 'POST' },
JSON.stringify(postBody)
);
function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
}
if (data.logType === 'no') {
return false;
}
if (data.logType === 'debug') {
return isDebug;
}
return data.logType === 'always';
}
function generateRequestUrl() {
const url = 'https://api.attentivemobile.com/v1/events/';
if (data.eventType === 'custom') {
return url + 'custom';
}
if (data.eCommerceEvent === 'ProductView') {
return url + 'ecommerce/product-view';
}
if (data.eCommerceEvent === 'addToCart') {
return url + 'ecommerce/add-to-cart';
}
return url + 'ecommerce/purchase';
}
function generatePostBody() {
const postBody = {
user: {},
};
if (data.phone) postBody.user.phone = data.phone;
if (data.email) postBody.user.email = data.email;
if (data.eventType === 'custom') {
postBody.type = data.customEvent;
if (data.customProperties) {
postBody.properties = makeTableMap(
data.customProperties,
'name',
'value'
);
}
return postBody;
}
if (eventData.items) {
postBody.items = [];
eventData.items.forEach((d) => {
let content = {};
if (d.item_id) content.productId = makeString(d.item_id);
if (d.item_variant) content.productVariantId = makeString(d.item_variant);
if (d.item_name) content.name = makeString(d.item_name);
if (d.item_image) content.productImage = makeString(d.item_image);
if (d.item_url) content.productUrl = makeString(d.item_url);
if (d.quantity) content.quantity = makeInteger(d.quantity);
if (d.price) {
content.price = { value: makeNumber(d.price) };
if (eventData.currency)
content.price.currency = makeString(eventData.currency);
if (d.currency) content.price.currency = makeString(d.currency);
content.price = [content.price];
} else {
content.price = [
{
value: 1,
currency: 'USD',
},
];
}
postBody.items.push(content);
});
}
return postBody;
}