@blotoutio/edgetag-sdk-browser 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.js +158 -64
- package/package.json +1 -1
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -38,11 +38,32 @@
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
const getMessage = (error) => {
|
|
42
|
+
if (error instanceof Error) {
|
|
43
|
+
return error.message;
|
|
44
|
+
}
|
|
45
|
+
if (typeof error === 'string') {
|
|
46
|
+
return error;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return JSON.stringify(error);
|
|
50
|
+
}
|
|
51
|
+
catch (_a) {
|
|
52
|
+
return error;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const log = (data) => {
|
|
56
|
+
console.log('[EdgeTag]', getMessage(data));
|
|
57
|
+
};
|
|
58
|
+
const error = (data) => {
|
|
59
|
+
console.error('[EdgeTag]', getMessage(data));
|
|
60
|
+
};
|
|
61
|
+
|
|
41
62
|
let endpointUrl = '';
|
|
42
63
|
const generateUrl = (path) => {
|
|
43
64
|
const endpoint = getUrl();
|
|
44
65
|
if (!endpoint) {
|
|
45
|
-
|
|
66
|
+
log('URL is not valid');
|
|
46
67
|
return '';
|
|
47
68
|
}
|
|
48
69
|
return `${endpoint}${path}`;
|
|
@@ -88,7 +109,7 @@
|
|
|
88
109
|
return false;
|
|
89
110
|
}
|
|
90
111
|
if (!preferences.edgeURL) {
|
|
91
|
-
|
|
112
|
+
error('Please provide URL for EdgeTag');
|
|
92
113
|
return false;
|
|
93
114
|
}
|
|
94
115
|
consentDisabled = !!preferences.disableConsentCheck;
|
|
@@ -125,6 +146,39 @@
|
|
|
125
146
|
allowedProvider = providers;
|
|
126
147
|
};
|
|
127
148
|
|
|
149
|
+
const checkConsent = (providers) => {
|
|
150
|
+
if (isConsentDisabled()) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
const consent = getConsent();
|
|
154
|
+
if (!consent) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const consentLength = Object.keys(consent).length;
|
|
158
|
+
if (!consentLength || (consentLength === 1 && consent['all'] === false)) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
const providersLength = Object.keys(providers || {}).length;
|
|
162
|
+
if (!providers ||
|
|
163
|
+
providersLength === 0 ||
|
|
164
|
+
(providersLength === 1 && providers['all'])) {
|
|
165
|
+
return (Object.values(consent).find((isAllowed) => isAllowed) || false);
|
|
166
|
+
}
|
|
167
|
+
for (const [key, value] of Object.entries(providers)) {
|
|
168
|
+
if (value === false || consent[key] === false) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
// we have consent
|
|
172
|
+
if (consent[key] ||
|
|
173
|
+
(consent['all'] === true && consent[key] === undefined)) {
|
|
174
|
+
// we have provider
|
|
175
|
+
if (value || (providers['all'] && providers[key] === undefined)) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
};
|
|
128
182
|
const getUserAgent = () => {
|
|
129
183
|
try {
|
|
130
184
|
const nav = navigator;
|
|
@@ -140,23 +194,20 @@
|
|
|
140
194
|
return (providerValue || (providers['all'] === true && providerValue === undefined));
|
|
141
195
|
};
|
|
142
196
|
const allowTag = (providers) => {
|
|
143
|
-
|
|
144
|
-
if (isConsentDisabled()) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
if (!consent) {
|
|
197
|
+
if (!checkConsent(providers)) {
|
|
148
198
|
return false;
|
|
149
199
|
}
|
|
150
|
-
|
|
200
|
+
const providersLength = Object.values(providers || {}).length;
|
|
201
|
+
if (!providers || providersLength === 0) {
|
|
151
202
|
return true;
|
|
152
203
|
}
|
|
153
|
-
if (!providers) {
|
|
154
|
-
return (Object.values(consent).find((isAllowed) => isAllowed) || false);
|
|
155
|
-
}
|
|
156
204
|
const allProviders = getAllowedProviders();
|
|
205
|
+
const allProvidersLength = Object.keys(allProviders || {}).length;
|
|
206
|
+
if (allProvidersLength === 0) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
157
209
|
for (const provider of allProviders) {
|
|
158
|
-
if (isProviderIncluded(providers,
|
|
159
|
-
consent[provider]) {
|
|
210
|
+
if (isProviderIncluded(providers, providers[provider])) {
|
|
160
211
|
return true;
|
|
161
212
|
}
|
|
162
213
|
}
|
|
@@ -195,25 +246,15 @@
|
|
|
195
246
|
if (!allowProvider(providerId)) {
|
|
196
247
|
return false;
|
|
197
248
|
}
|
|
249
|
+
if (!checkConsent(providers)) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
198
252
|
if (providers && Object.keys(providers).length) {
|
|
199
|
-
|
|
200
|
-
if (!(tagProvider ||
|
|
201
|
-
(providers['all'] === true && tagProvider === undefined) ||
|
|
202
|
-
(providers['all'] === false && tagProvider === true))) {
|
|
253
|
+
if (!isProviderIncluded(providers, providers[providerId])) {
|
|
203
254
|
return false;
|
|
204
255
|
}
|
|
205
256
|
}
|
|
206
|
-
|
|
207
|
-
if (isConsentDisabled()) {
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
if (!consent) {
|
|
211
|
-
return false;
|
|
212
|
-
}
|
|
213
|
-
if (consent['all']) {
|
|
214
|
-
return true;
|
|
215
|
-
}
|
|
216
|
-
return consent[providerId];
|
|
257
|
+
return true;
|
|
217
258
|
};
|
|
218
259
|
|
|
219
260
|
const tagStorage = 'edgeTag';
|
|
@@ -302,7 +343,7 @@
|
|
|
302
343
|
localStorage.setItem(key, JSON.stringify(value));
|
|
303
344
|
}
|
|
304
345
|
catch (_a) {
|
|
305
|
-
|
|
346
|
+
log('Local storage not supported.');
|
|
306
347
|
}
|
|
307
348
|
};
|
|
308
349
|
const getLocal = (key) => {
|
|
@@ -328,7 +369,7 @@
|
|
|
328
369
|
sessionStorage.setItem(key, JSON.stringify(value));
|
|
329
370
|
}
|
|
330
371
|
catch (_a) {
|
|
331
|
-
|
|
372
|
+
log('Session storage not supported.');
|
|
332
373
|
}
|
|
333
374
|
};
|
|
334
375
|
const getSession = (key) => {
|
|
@@ -358,34 +399,80 @@
|
|
|
358
399
|
initUserId = userId;
|
|
359
400
|
};
|
|
360
401
|
|
|
402
|
+
const getHeaders = () => ({
|
|
403
|
+
'Content-type': 'application/json; charset=utf-8',
|
|
404
|
+
Accept: 'application/json; charset=utf-8',
|
|
405
|
+
EdgeTagUserId: handleGetUserId(),
|
|
406
|
+
});
|
|
361
407
|
const beacon = (url, payload) => {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
408
|
+
try {
|
|
409
|
+
let blob;
|
|
410
|
+
if (payload) {
|
|
411
|
+
blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
|
|
412
|
+
}
|
|
413
|
+
return navigator.sendBeacon(url, blob);
|
|
414
|
+
}
|
|
415
|
+
catch (e) {
|
|
416
|
+
return Promise.reject(new Error('Beacon not supported.'));
|
|
365
417
|
}
|
|
366
|
-
return navigator.sendBeacon(url, blob);
|
|
367
418
|
};
|
|
419
|
+
const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
|
|
420
|
+
const https = yield import('https');
|
|
421
|
+
const options = {
|
|
422
|
+
method,
|
|
423
|
+
headers: getHeaders(),
|
|
424
|
+
};
|
|
425
|
+
return new Promise((resolve, reject) => {
|
|
426
|
+
const req = https.request(url, options, (res) => {
|
|
427
|
+
res.on('data', (data) => {
|
|
428
|
+
try {
|
|
429
|
+
data = JSON.parse(data);
|
|
430
|
+
}
|
|
431
|
+
catch (_a) {
|
|
432
|
+
// do nothing
|
|
433
|
+
}
|
|
434
|
+
resolve({
|
|
435
|
+
body: data,
|
|
436
|
+
status: res.statusCode || 500,
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
req.on('error', (e) => {
|
|
441
|
+
reject(new Error(e.message));
|
|
442
|
+
});
|
|
443
|
+
if (payload && method !== 'GET') {
|
|
444
|
+
req.write(JSON.stringify(payload));
|
|
445
|
+
}
|
|
446
|
+
req.end();
|
|
447
|
+
});
|
|
448
|
+
});
|
|
368
449
|
const ajax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
|
|
450
|
+
if (typeof fetch === 'undefined') {
|
|
451
|
+
return yield fallbackAjax(method, url, payload)
|
|
452
|
+
.then((data) => {
|
|
453
|
+
if (data.status < 200 || data.status >= 300) {
|
|
454
|
+
return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
|
|
455
|
+
}
|
|
456
|
+
return Promise.resolve(data.body);
|
|
457
|
+
})
|
|
458
|
+
.catch((error) => {
|
|
459
|
+
return Promise.reject(new Error(error));
|
|
460
|
+
});
|
|
461
|
+
}
|
|
369
462
|
return yield fetch(url, {
|
|
370
463
|
method,
|
|
371
|
-
headers:
|
|
372
|
-
'Content-type': 'application/json; charset=utf-8',
|
|
373
|
-
Accept: 'application/json; charset=utf-8',
|
|
374
|
-
EdgeTagUserId: handleGetUserId(),
|
|
375
|
-
},
|
|
464
|
+
headers: getHeaders(),
|
|
376
465
|
body: JSON.stringify(payload),
|
|
377
466
|
credentials: 'include',
|
|
378
467
|
})
|
|
379
468
|
.then((response) => response.json().then((data) => ({ status: response.status, body: data })))
|
|
380
469
|
.then((data) => {
|
|
381
470
|
if (data.status < 200 || data.status >= 300) {
|
|
382
|
-
|
|
383
|
-
return Promise.reject(new Error(JSON.stringify(data.body)));
|
|
471
|
+
return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
|
|
384
472
|
}
|
|
385
473
|
return Promise.resolve(data.body);
|
|
386
474
|
})
|
|
387
475
|
.catch((error) => {
|
|
388
|
-
// Q: do we need to retry?
|
|
389
476
|
return Promise.reject(new Error(error));
|
|
390
477
|
});
|
|
391
478
|
});
|
|
@@ -413,7 +500,7 @@
|
|
|
413
500
|
function postRequest(url, data, options) {
|
|
414
501
|
return __awaiter(this, void 0, void 0, function* () {
|
|
415
502
|
if (!url) {
|
|
416
|
-
return Promise.reject(new Error('URL is empty'));
|
|
503
|
+
return Promise.reject(new Error('URL is empty.'));
|
|
417
504
|
}
|
|
418
505
|
const payload = getStandardPayload(data);
|
|
419
506
|
if (options && options.method === 'beacon') {
|
|
@@ -425,7 +512,7 @@
|
|
|
425
512
|
function getRequest(url, options) {
|
|
426
513
|
return __awaiter(this, void 0, void 0, function* () {
|
|
427
514
|
if (!url) {
|
|
428
|
-
return Promise.reject(new Error('URL is empty'));
|
|
515
|
+
return Promise.reject(new Error('URL is empty.'));
|
|
429
516
|
}
|
|
430
517
|
if (options && options.method === 'beacon') {
|
|
431
518
|
return {
|
|
@@ -436,9 +523,6 @@
|
|
|
436
523
|
});
|
|
437
524
|
}
|
|
438
525
|
|
|
439
|
-
const info = (data) => {
|
|
440
|
-
};
|
|
441
|
-
|
|
442
526
|
let memoryConsent;
|
|
443
527
|
const saveConsent = (consent) => {
|
|
444
528
|
setConsent(consent);
|
|
@@ -449,7 +533,7 @@
|
|
|
449
533
|
consentString: consent,
|
|
450
534
|
};
|
|
451
535
|
saveConsent(consent);
|
|
452
|
-
postRequest(getConsentURL(), payload).catch(
|
|
536
|
+
postRequest(getConsentURL(), payload).catch(error);
|
|
453
537
|
};
|
|
454
538
|
const setConsent = (newConsent) => {
|
|
455
539
|
memoryConsent = newConsent;
|
|
@@ -942,6 +1026,12 @@
|
|
|
942
1026
|
|
|
943
1027
|
v35('v5', 0x50, sha1);
|
|
944
1028
|
|
|
1029
|
+
const encodeString = (name) => {
|
|
1030
|
+
if (typeof btoa === 'undefined') {
|
|
1031
|
+
return Buffer.from(name).toString('base64');
|
|
1032
|
+
}
|
|
1033
|
+
return btoa(name);
|
|
1034
|
+
};
|
|
945
1035
|
const generateEventId = (name) => {
|
|
946
1036
|
let time = Date.now().toString();
|
|
947
1037
|
if (typeof performance !== 'undefined' &&
|
|
@@ -951,7 +1041,7 @@
|
|
|
951
1041
|
time = perf.toFixed(4);
|
|
952
1042
|
}
|
|
953
1043
|
}
|
|
954
|
-
return `${
|
|
1044
|
+
return `${encodeString(name)}-${v4()}-${time}`;
|
|
955
1045
|
};
|
|
956
1046
|
|
|
957
1047
|
const manifestVariables = {};
|
|
@@ -975,13 +1065,13 @@
|
|
|
975
1065
|
stubs = [];
|
|
976
1066
|
}
|
|
977
1067
|
catch (e) {
|
|
978
|
-
|
|
1068
|
+
error(e);
|
|
979
1069
|
}
|
|
980
1070
|
};
|
|
981
1071
|
|
|
982
1072
|
const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
|
|
983
1073
|
if (!allowProviders(providers)) {
|
|
984
|
-
|
|
1074
|
+
log('Provider is not allowed.');
|
|
985
1075
|
return;
|
|
986
1076
|
}
|
|
987
1077
|
const payload = {
|
|
@@ -994,7 +1084,7 @@
|
|
|
994
1084
|
if (providers) {
|
|
995
1085
|
payload.providers = providers;
|
|
996
1086
|
}
|
|
997
|
-
postRequest(getTagURL(), payload, options).catch(
|
|
1087
|
+
postRequest(getTagURL(), payload, options).catch(error);
|
|
998
1088
|
};
|
|
999
1089
|
const handleTag = (eventName, data = {}, providers, options) => {
|
|
1000
1090
|
if (!isInitialized()) {
|
|
@@ -1005,11 +1095,11 @@
|
|
|
1005
1095
|
return;
|
|
1006
1096
|
}
|
|
1007
1097
|
if (!allowProviders(providers)) {
|
|
1008
|
-
|
|
1098
|
+
log('Provider is not allowed.');
|
|
1009
1099
|
return;
|
|
1010
1100
|
}
|
|
1011
1101
|
if (!allowTag(providers)) {
|
|
1012
|
-
|
|
1102
|
+
log('Consent is missing.');
|
|
1013
1103
|
return;
|
|
1014
1104
|
}
|
|
1015
1105
|
let eventId = data['eventId'];
|
|
@@ -1049,11 +1139,11 @@
|
|
|
1049
1139
|
|
|
1050
1140
|
const handleData = (data, options) => {
|
|
1051
1141
|
if (!data || Object.keys(data).length === 0) {
|
|
1052
|
-
|
|
1142
|
+
error('Provide data for data API.');
|
|
1053
1143
|
return;
|
|
1054
1144
|
}
|
|
1055
1145
|
saveKV(data);
|
|
1056
|
-
postRequest(getDataURL(), { data }, options).catch(
|
|
1146
|
+
postRequest(getDataURL(), { data }, options).catch(error);
|
|
1057
1147
|
};
|
|
1058
1148
|
|
|
1059
1149
|
const saveDataToEdge = (key, value, provider) => {
|
|
@@ -1065,7 +1155,7 @@
|
|
|
1065
1155
|
value = JSON.stringify(value);
|
|
1066
1156
|
}
|
|
1067
1157
|
catch (_a) {
|
|
1068
|
-
|
|
1158
|
+
log('Error stringify value.');
|
|
1069
1159
|
return;
|
|
1070
1160
|
}
|
|
1071
1161
|
}
|
|
@@ -1185,17 +1275,17 @@
|
|
|
1185
1275
|
getRequest(url.href)
|
|
1186
1276
|
.then((result) => {
|
|
1187
1277
|
if (!result) {
|
|
1188
|
-
|
|
1278
|
+
log('Initialization failed');
|
|
1189
1279
|
return;
|
|
1190
1280
|
}
|
|
1191
1281
|
handleManifest(result.result);
|
|
1192
1282
|
})
|
|
1193
|
-
.catch(
|
|
1283
|
+
.catch(error);
|
|
1194
1284
|
};
|
|
1195
1285
|
|
|
1196
1286
|
const handleUser = (key, value, options) => {
|
|
1197
1287
|
if (!key || !value) {
|
|
1198
|
-
|
|
1288
|
+
error('Key or Value is missing in user API.');
|
|
1199
1289
|
return;
|
|
1200
1290
|
}
|
|
1201
1291
|
saveKV({
|
|
@@ -1204,19 +1294,19 @@
|
|
|
1204
1294
|
postRequest(getUserURL(), {
|
|
1205
1295
|
key,
|
|
1206
1296
|
value,
|
|
1207
|
-
}, options).catch(
|
|
1297
|
+
}, options).catch(error);
|
|
1208
1298
|
};
|
|
1209
1299
|
|
|
1210
1300
|
const handleGetData = (keys, callback) => {
|
|
1211
1301
|
if (!keys || keys.length === 0) {
|
|
1212
|
-
|
|
1302
|
+
error('Provide keys for get data API.');
|
|
1213
1303
|
return;
|
|
1214
1304
|
}
|
|
1215
1305
|
getRequest(getGetDataURL(keys))
|
|
1216
1306
|
.then((result) => {
|
|
1217
1307
|
callback((result === null || result === void 0 ? void 0 : result.result) || {});
|
|
1218
1308
|
})
|
|
1219
|
-
.catch(
|
|
1309
|
+
.catch(error);
|
|
1220
1310
|
};
|
|
1221
1311
|
|
|
1222
1312
|
const handleKeys = (callback) => {
|
|
@@ -1224,7 +1314,7 @@
|
|
|
1224
1314
|
.then((result) => {
|
|
1225
1315
|
callback((result === null || result === void 0 ? void 0 : result.result) || []);
|
|
1226
1316
|
})
|
|
1227
|
-
.catch(
|
|
1317
|
+
.catch(error);
|
|
1228
1318
|
};
|
|
1229
1319
|
|
|
1230
1320
|
const init = (preferences) => {
|
|
@@ -1282,6 +1372,10 @@
|
|
|
1282
1372
|
keys(callback) {
|
|
1283
1373
|
keys(callback);
|
|
1284
1374
|
}
|
|
1375
|
+
|
|
1376
|
+
getUserId() {
|
|
1377
|
+
return getUserId()
|
|
1378
|
+
}
|
|
1285
1379
|
}
|
|
1286
1380
|
|
|
1287
1381
|
var api = new API();
|