-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
201 lines (174 loc) · 5.27 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
const sendHttpRequest = require('sendHttpRequest');
const setCookie = require('setCookie');
const parseUrl = require('parseUrl');
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const getCookieValues = require('getCookieValues');
const getEventData = require('getEventData');
const getAllEventData = require('getAllEventData');
const logToConsole = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const makeString = require('makeString');
const getTimestampMillis = require('getTimestampMillis');
const makeTableMap = require('makeTableMap');
const encodeUriComponent = require('encodeUriComponent');
const Math = require('Math');
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');
const eventData = getAllEventData();
if (data.type === 'page_view') {
const url = getEventData('page_location') || getRequestHeader('referer');
if (url) {
const siteIdValue = parseUrl(url).searchParams.siteID;
const ranMidValue = parseUrl(url).searchParams.ranMID;
const ranEaidValue = parseUrl(url).searchParams.ranEAID;
const ranSiteIdValue = parseUrl(url).searchParams.ranSiteID;
const rakutenSiteIdValue = siteIdValue || ranSiteIdValue;
const options = {
domain: 'auto',
path: '/',
secure: true,
httpOnly: false,
'max-age': 63072000, // 2 years
};
if (rakutenSiteIdValue) {
setCookie(
'rakuten_site_id',
makeString(rakutenSiteIdValue),
options,
false
);
setCookie(
'rakuten_time_entered',
makeString(Math.round(getTimestampMillis() / 1000)),
options,
false
);
}
if (ranMidValue)
setCookie('rakuten_ran_mid', makeString(ranMidValue), options, false);
if (ranEaidValue)
setCookie('rakuten_ran_eaid', makeString(ranEaidValue), options, false);
if (ranSiteIdValue)
setCookie(
'rakuten_ran_site_id',
makeString(ranSiteIdValue),
options,
false
);
}
data.gtmOnSuccess();
} else {
const containerIdentifier = getRequestHeader('x-gtm-identifier');
const defaultDomain = getRequestHeader('x-gtm-default-domain');
const containerApiKey = getRequestHeader('x-gtm-api-key');
let requestUrl =
'https://' +
enc(containerIdentifier) +
'.' +
enc(defaultDomain) +
'/stape-api/' +
enc(containerApiKey) +
'/v1/rakuten/auth-proxy';
let requestBody = {
auth: {
affiliate_key: data.affiliateKey,
mid: data.mid,
},
sku_order: {
siteid: getCookieValues('rakuten_site_id')[0] || '',
time_entered: getCookieValues('rakuten_time_entered')[0] || '',
orderid: data.orderId,
currency: data.currency,
trans_date: Math.round(getTimestampMillis() / 1000),
optional_data: data.optionalData
? makeTableMap(data.optionalData, 'name', 'value')
: {},
items: data.items ? data.items : getItems(),
},
};
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Rakuten',
Type: 'Request',
TraceId: traceId,
EventName: 'Conversion',
RequestMethod: 'POST',
RequestUrl: requestUrl,
RequestBody: requestBody,
})
);
}
sendHttpRequest(
requestUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Rakuten',
Type: 'Response',
TraceId: traceId,
EventName: 'Conversion',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
})
);
}
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{ method: 'POST', headers: { 'Content-Type': 'application/json' } },
JSON.stringify(requestBody)
);
}
function getItems() {
let items = [];
if (eventData.items && eventData.items[0]) {
eventData.items.forEach((d, i) => {
let item = {};
if (d.name) item.product_name = d.name;
else if (d.item_name) item.product_name = d.item_name;
else if (d.title) item.product_name = d.title;
if (d.sku) item.sku = d.sku;
else if (d.item_sku) item.sku = d.item_sku;
else if (d.item_id) item.sku = d.item_id;
else if (d.id) item.sku = d.id;
if (d.quantity) item.quantity = d.quantity;
else if (d.item_quantity) item.quantity = d.item_quantity;
else if (d.qty) item.quantity = d.qty;
if (d.price) item.amount = d.price;
else if (d.item_price) item.amount = d.item_price;
if (item.amount && item.quantity) {
item.amount = item.amount * item.quantity;
}
items.push(item);
});
}
return items;
}
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);
}