@blotoutio/edgetag-sdk-browser 0.7.1 → 0.7.3
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/index.js +84 -39
- package/package.json +1 -1
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;
|
|
@@ -169,6 +190,43 @@
|
|
|
169
190
|
return '';
|
|
170
191
|
}
|
|
171
192
|
};
|
|
193
|
+
const getReferrer = () => {
|
|
194
|
+
let referrer = '';
|
|
195
|
+
try {
|
|
196
|
+
const referrerUrl = new URL(document.referrer);
|
|
197
|
+
if (referrerUrl.host !== window.location.host) {
|
|
198
|
+
referrer = referrerUrl.href;
|
|
199
|
+
}
|
|
200
|
+
return referrer;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
return referrer;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
const getPageUrl = () => {
|
|
207
|
+
try {
|
|
208
|
+
return window.location.href;
|
|
209
|
+
}
|
|
210
|
+
catch (_a) {
|
|
211
|
+
return '';
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
const getSearch = () => {
|
|
215
|
+
try {
|
|
216
|
+
return window.location.search;
|
|
217
|
+
}
|
|
218
|
+
catch (_a) {
|
|
219
|
+
return '';
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
const getPageTitle = () => {
|
|
223
|
+
try {
|
|
224
|
+
return document.title;
|
|
225
|
+
}
|
|
226
|
+
catch (_a) {
|
|
227
|
+
return '';
|
|
228
|
+
}
|
|
229
|
+
};
|
|
172
230
|
const isProviderIncluded = (providers, providerValue) => {
|
|
173
231
|
return (providerValue || (providers['all'] === true && providerValue === undefined));
|
|
174
232
|
};
|
|
@@ -322,7 +380,7 @@
|
|
|
322
380
|
localStorage.setItem(key, JSON.stringify(value));
|
|
323
381
|
}
|
|
324
382
|
catch (_a) {
|
|
325
|
-
|
|
383
|
+
log('Local storage not supported.');
|
|
326
384
|
}
|
|
327
385
|
};
|
|
328
386
|
const getLocal = (key) => {
|
|
@@ -348,7 +406,7 @@
|
|
|
348
406
|
sessionStorage.setItem(key, JSON.stringify(value));
|
|
349
407
|
}
|
|
350
408
|
catch (_a) {
|
|
351
|
-
|
|
409
|
+
log('Session storage not supported.');
|
|
352
410
|
}
|
|
353
411
|
};
|
|
354
412
|
const getSession = (key) => {
|
|
@@ -392,8 +450,7 @@
|
|
|
392
450
|
return navigator.sendBeacon(url, blob);
|
|
393
451
|
}
|
|
394
452
|
catch (e) {
|
|
395
|
-
|
|
396
|
-
return Promise.reject(new Error('Beacon not supported'));
|
|
453
|
+
return Promise.reject(new Error('Beacon not supported.'));
|
|
397
454
|
}
|
|
398
455
|
};
|
|
399
456
|
const fallbackAjax = (method, url, payload) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -431,7 +488,7 @@
|
|
|
431
488
|
return yield fallbackAjax(method, url, payload)
|
|
432
489
|
.then((data) => {
|
|
433
490
|
if (data.status < 200 || data.status >= 300) {
|
|
434
|
-
return Promise.reject(new Error(JSON.stringify(data.body)));
|
|
491
|
+
return Promise.reject(new Error(`Request failed with code ${data.status} occurred: ${JSON.stringify(data.body)}`));
|
|
435
492
|
}
|
|
436
493
|
return Promise.resolve(data.body);
|
|
437
494
|
})
|
|
@@ -448,25 +505,16 @@
|
|
|
448
505
|
.then((response) => response.json().then((data) => ({ status: response.status, body: data })))
|
|
449
506
|
.then((data) => {
|
|
450
507
|
if (data.status < 200 || data.status >= 300) {
|
|
451
|
-
|
|
452
|
-
return Promise.reject(new Error(JSON.stringify(data.body)));
|
|
508
|
+
return Promise.reject(new Error(`Request failed with code ${data.status}: ${JSON.stringify(data.body)}`));
|
|
453
509
|
}
|
|
454
510
|
return Promise.resolve(data.body);
|
|
455
511
|
})
|
|
456
512
|
.catch((error) => {
|
|
457
|
-
// Q: do we need to retry?
|
|
458
513
|
return Promise.reject(new Error(error));
|
|
459
514
|
});
|
|
460
515
|
});
|
|
461
516
|
const getStandardPayload = (payload) => {
|
|
462
|
-
|
|
463
|
-
try {
|
|
464
|
-
pageUrl = window.location.href;
|
|
465
|
-
}
|
|
466
|
-
catch (_a) {
|
|
467
|
-
pageUrl = '';
|
|
468
|
-
}
|
|
469
|
-
const data = Object.assign({ pageUrl, userAgent: getUserAgent() }, (payload || {}));
|
|
517
|
+
const data = Object.assign({ pageUrl: getPageUrl(), pageTitle: getPageTitle(), userAgent: getUserAgent(), referrer: getReferrer(), search: getSearch() }, (payload || {}));
|
|
470
518
|
let storage = {};
|
|
471
519
|
const session = getData$1('session');
|
|
472
520
|
if (session) {
|
|
@@ -482,7 +530,7 @@
|
|
|
482
530
|
function postRequest(url, data, options) {
|
|
483
531
|
return __awaiter(this, void 0, void 0, function* () {
|
|
484
532
|
if (!url) {
|
|
485
|
-
return Promise.reject(new Error('URL is empty'));
|
|
533
|
+
return Promise.reject(new Error('URL is empty.'));
|
|
486
534
|
}
|
|
487
535
|
const payload = getStandardPayload(data);
|
|
488
536
|
if (options && options.method === 'beacon') {
|
|
@@ -494,7 +542,7 @@
|
|
|
494
542
|
function getRequest(url, options) {
|
|
495
543
|
return __awaiter(this, void 0, void 0, function* () {
|
|
496
544
|
if (!url) {
|
|
497
|
-
return Promise.reject(new Error('URL is empty'));
|
|
545
|
+
return Promise.reject(new Error('URL is empty.'));
|
|
498
546
|
}
|
|
499
547
|
if (options && options.method === 'beacon') {
|
|
500
548
|
return {
|
|
@@ -505,9 +553,6 @@
|
|
|
505
553
|
});
|
|
506
554
|
}
|
|
507
555
|
|
|
508
|
-
const info = (data) => {
|
|
509
|
-
};
|
|
510
|
-
|
|
511
556
|
let memoryConsent;
|
|
512
557
|
const saveConsent = (consent) => {
|
|
513
558
|
setConsent(consent);
|
|
@@ -518,7 +563,7 @@
|
|
|
518
563
|
consentString: consent,
|
|
519
564
|
};
|
|
520
565
|
saveConsent(consent);
|
|
521
|
-
postRequest(getConsentURL(), payload).catch(
|
|
566
|
+
postRequest(getConsentURL(), payload).catch(error);
|
|
522
567
|
};
|
|
523
568
|
const setConsent = (newConsent) => {
|
|
524
569
|
memoryConsent = newConsent;
|
|
@@ -1050,13 +1095,13 @@
|
|
|
1050
1095
|
stubs = [];
|
|
1051
1096
|
}
|
|
1052
1097
|
catch (e) {
|
|
1053
|
-
|
|
1098
|
+
error(e);
|
|
1054
1099
|
}
|
|
1055
1100
|
};
|
|
1056
1101
|
|
|
1057
1102
|
const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
|
|
1058
1103
|
if (!allowProviders(providers)) {
|
|
1059
|
-
|
|
1104
|
+
log('Provider is not allowed.');
|
|
1060
1105
|
return;
|
|
1061
1106
|
}
|
|
1062
1107
|
const payload = {
|
|
@@ -1069,7 +1114,7 @@
|
|
|
1069
1114
|
if (providers) {
|
|
1070
1115
|
payload.providers = providers;
|
|
1071
1116
|
}
|
|
1072
|
-
postRequest(getTagURL(), payload, options).catch(
|
|
1117
|
+
postRequest(getTagURL(), payload, options).catch(error);
|
|
1073
1118
|
};
|
|
1074
1119
|
const handleTag = (eventName, data = {}, providers, options) => {
|
|
1075
1120
|
if (!isInitialized()) {
|
|
@@ -1080,11 +1125,11 @@
|
|
|
1080
1125
|
return;
|
|
1081
1126
|
}
|
|
1082
1127
|
if (!allowProviders(providers)) {
|
|
1083
|
-
|
|
1128
|
+
log('Provider is not allowed.');
|
|
1084
1129
|
return;
|
|
1085
1130
|
}
|
|
1086
1131
|
if (!allowTag(providers)) {
|
|
1087
|
-
|
|
1132
|
+
log('Consent is missing.');
|
|
1088
1133
|
return;
|
|
1089
1134
|
}
|
|
1090
1135
|
let eventId = data['eventId'];
|
|
@@ -1124,11 +1169,11 @@
|
|
|
1124
1169
|
|
|
1125
1170
|
const handleData = (data, options) => {
|
|
1126
1171
|
if (!data || Object.keys(data).length === 0) {
|
|
1127
|
-
|
|
1172
|
+
error('Provide data for data API.');
|
|
1128
1173
|
return;
|
|
1129
1174
|
}
|
|
1130
1175
|
saveKV(data);
|
|
1131
|
-
postRequest(getDataURL(), { data }, options).catch(
|
|
1176
|
+
postRequest(getDataURL(), { data }, options).catch(error);
|
|
1132
1177
|
};
|
|
1133
1178
|
|
|
1134
1179
|
const saveDataToEdge = (key, value, provider) => {
|
|
@@ -1140,7 +1185,7 @@
|
|
|
1140
1185
|
value = JSON.stringify(value);
|
|
1141
1186
|
}
|
|
1142
1187
|
catch (_a) {
|
|
1143
|
-
|
|
1188
|
+
log('Error stringify value.');
|
|
1144
1189
|
return;
|
|
1145
1190
|
}
|
|
1146
1191
|
}
|
|
@@ -1260,17 +1305,17 @@
|
|
|
1260
1305
|
getRequest(url.href)
|
|
1261
1306
|
.then((result) => {
|
|
1262
1307
|
if (!result) {
|
|
1263
|
-
|
|
1308
|
+
log('Initialization failed');
|
|
1264
1309
|
return;
|
|
1265
1310
|
}
|
|
1266
1311
|
handleManifest(result.result);
|
|
1267
1312
|
})
|
|
1268
|
-
.catch(
|
|
1313
|
+
.catch(error);
|
|
1269
1314
|
};
|
|
1270
1315
|
|
|
1271
1316
|
const handleUser = (key, value, options) => {
|
|
1272
1317
|
if (!key || !value) {
|
|
1273
|
-
|
|
1318
|
+
error('Key or Value is missing in user API.');
|
|
1274
1319
|
return;
|
|
1275
1320
|
}
|
|
1276
1321
|
saveKV({
|
|
@@ -1279,19 +1324,19 @@
|
|
|
1279
1324
|
postRequest(getUserURL(), {
|
|
1280
1325
|
key,
|
|
1281
1326
|
value,
|
|
1282
|
-
}, options).catch(
|
|
1327
|
+
}, options).catch(error);
|
|
1283
1328
|
};
|
|
1284
1329
|
|
|
1285
1330
|
const handleGetData = (keys, callback) => {
|
|
1286
1331
|
if (!keys || keys.length === 0) {
|
|
1287
|
-
|
|
1332
|
+
error('Provide keys for get data API.');
|
|
1288
1333
|
return;
|
|
1289
1334
|
}
|
|
1290
1335
|
getRequest(getGetDataURL(keys))
|
|
1291
1336
|
.then((result) => {
|
|
1292
1337
|
callback((result === null || result === void 0 ? void 0 : result.result) || {});
|
|
1293
1338
|
})
|
|
1294
|
-
.catch(
|
|
1339
|
+
.catch(error);
|
|
1295
1340
|
};
|
|
1296
1341
|
|
|
1297
1342
|
const handleKeys = (callback) => {
|
|
@@ -1299,7 +1344,7 @@
|
|
|
1299
1344
|
.then((result) => {
|
|
1300
1345
|
callback((result === null || result === void 0 ? void 0 : result.result) || []);
|
|
1301
1346
|
})
|
|
1302
|
-
.catch(
|
|
1347
|
+
.catch(error);
|
|
1303
1348
|
};
|
|
1304
1349
|
|
|
1305
1350
|
const init = (preferences) => {
|