@blotoutio/edgetag-sdk-browser 0.68.2 → 0.69.0

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.
Files changed (2) hide show
  1. package/index.js +131 -128
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -281,21 +281,87 @@
281
281
  const fallbackSessionKey = 'fallback_tag_user_id';
282
282
  const storageIdKey = 'storageId';
283
283
 
284
- const getMessage = (error) => {
285
- if (error instanceof Error) {
286
- return error.message;
284
+ const encodeString = (name) => {
285
+ if (typeof btoa === 'undefined') {
286
+ return Buffer.from(name).toString('base64');
287
+ }
288
+ return btoa(name);
289
+ };
290
+ const getBasicRandomNumber = () => {
291
+ return parseInt((Math.random() * 10000000000).toString(), 10);
292
+ };
293
+ const generateUUID = () => {
294
+ let id = '';
295
+ try {
296
+ id = crypto.randomUUID();
297
+ if (!id) {
298
+ const array = new Uint32Array(20);
299
+ const numbers = crypto.getRandomValues(array);
300
+ for (let i = 0; i < 5; i++) {
301
+ const y = i * 3;
302
+ if (i !== 0) {
303
+ id += '-';
304
+ }
305
+ const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
306
+ id += sum.toString();
307
+ }
308
+ }
287
309
  }
288
- if (typeof error === 'string') {
289
- return error;
310
+ catch {
311
+ id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
312
+ console.log('[EdgeTag] Crypto module not found');
290
313
  }
314
+ return id;
315
+ };
316
+ const generateEventId = (name) => {
317
+ let time = Date.now().toString();
318
+ if (typeof performance !== 'undefined' &&
319
+ typeof performance.now === 'function') {
320
+ const perf = performance.now();
321
+ if (perf) {
322
+ time = perf.toFixed(4);
323
+ }
324
+ }
325
+ return `${encodeString(name)}-${generateUUID()}-${time}`;
326
+ };
327
+
328
+ const getCookieValue = (key) => {
329
+ var _a;
291
330
  try {
292
- return JSON.stringify(error);
331
+ if (!document || !document.cookie) {
332
+ return '';
333
+ }
334
+ const cookies = parseCookies(document.cookie);
335
+ return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
293
336
  }
294
337
  catch {
295
- return error;
338
+ return '';
296
339
  }
297
340
  };
298
- const allowLog = () => {
341
+ const parseCookies = (cookie) => {
342
+ return Object.fromEntries(cookie
343
+ .split(/;\s+/)
344
+ .map((r) => r.split('=').map((str) => str.trim()))
345
+ .map(([cookieKey, ...cookieValues]) => {
346
+ const cookieValue = cookieValues.join('=');
347
+ if (!cookieKey) {
348
+ return [];
349
+ }
350
+ let decodedValue = '';
351
+ if (cookieValue) {
352
+ try {
353
+ decodedValue = decodeURIComponent(cookieValue);
354
+ }
355
+ catch (e) {
356
+ console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
357
+ decodedValue = cookieValue;
358
+ }
359
+ }
360
+ return [cookieKey, decodedValue];
361
+ }));
362
+ };
363
+
364
+ const canLog = () => {
299
365
  try {
300
366
  return localStorage.getItem('edgeTagDebug') === '1';
301
367
  }
@@ -303,14 +369,28 @@
303
369
  return false;
304
370
  }
305
371
  };
306
- const log = (data) => {
307
- if (!allowLog()) {
308
- return;
309
- }
310
- console.log('[EdgeTag]', getMessage(data));
311
- };
312
- const error = (data) => {
313
- console.error('[EdgeTag]', getMessage(data));
372
+ const prefix = `[EdgeTag]`;
373
+ const logger = {
374
+ log: (...args) => {
375
+ if (canLog()) {
376
+ console.log(prefix, ...args);
377
+ }
378
+ },
379
+ error: (...args) => {
380
+ if (canLog()) {
381
+ console.error(prefix, ...args);
382
+ }
383
+ },
384
+ info: (...args) => {
385
+ if (canLog()) {
386
+ console.info(prefix, ...args);
387
+ }
388
+ },
389
+ trace: (...args) => {
390
+ if (canLog()) {
391
+ console.trace(prefix, ...args);
392
+ }
393
+ },
314
394
  };
315
395
 
316
396
  const initKey = `${keyPrefix}StoreMultiple`;
@@ -380,7 +460,7 @@
380
460
  localStorage.setItem(key, JSON.stringify(value));
381
461
  }
382
462
  catch {
383
- log('Local storage not supported.');
463
+ logger.log('Local storage not supported.');
384
464
  }
385
465
  };
386
466
  const getLocal = (key) => {
@@ -406,7 +486,7 @@
406
486
  sessionStorage.setItem(key, JSON.stringify(value));
407
487
  }
408
488
  catch {
409
- log('Session storage not supported.');
489
+ logger.log('Session storage not supported.');
410
490
  }
411
491
  };
412
492
  const getSession = (key) => {
@@ -425,86 +505,6 @@
425
505
  }
426
506
  };
427
507
 
428
- const encodeString = (name) => {
429
- if (typeof btoa === 'undefined') {
430
- return Buffer.from(name).toString('base64');
431
- }
432
- return btoa(name);
433
- };
434
- const getBasicRandomNumber = () => {
435
- return parseInt((Math.random() * 10000000000).toString(), 10);
436
- };
437
- const generateUUID = () => {
438
- let id = '';
439
- try {
440
- id = crypto.randomUUID();
441
- if (!id) {
442
- const array = new Uint32Array(20);
443
- const numbers = crypto.getRandomValues(array);
444
- for (let i = 0; i < 5; i++) {
445
- const y = i * 3;
446
- if (i !== 0) {
447
- id += '-';
448
- }
449
- const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
450
- id += sum.toString();
451
- }
452
- }
453
- }
454
- catch {
455
- id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
456
- console.log('[EdgeTag] Crypto module not found');
457
- }
458
- return id;
459
- };
460
- const generateEventId = (name) => {
461
- let time = Date.now().toString();
462
- if (typeof performance !== 'undefined' &&
463
- typeof performance.now === 'function') {
464
- const perf = performance.now();
465
- if (perf) {
466
- time = perf.toFixed(4);
467
- }
468
- }
469
- return `${encodeString(name)}-${generateUUID()}-${time}`;
470
- };
471
-
472
- const getCookieValue = (key) => {
473
- var _a;
474
- try {
475
- if (!document || !document.cookie) {
476
- return '';
477
- }
478
- const cookies = parseCookies(document.cookie);
479
- return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
480
- }
481
- catch {
482
- return '';
483
- }
484
- };
485
- const parseCookies = (cookie) => {
486
- return Object.fromEntries(cookie
487
- .split(/;\s+/)
488
- .map((r) => r.split('=').map((str) => str.trim()))
489
- .map(([cookieKey, ...cookieValues]) => {
490
- const cookieValue = cookieValues.join('=');
491
- if (!cookieKey) {
492
- return [];
493
- }
494
- let decodedValue = '';
495
- if (cookieValue) {
496
- try {
497
- decodedValue = decodeURIComponent(cookieValue);
498
- }
499
- catch (e) {
500
- console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
501
- decodedValue = cookieValue;
502
- }
503
- }
504
- return [cookieKey, decodedValue];
505
- }));
506
- };
507
-
508
508
  const getUserId$1 = (destination) => {
509
509
  const userId = getSetting(destination, 'userId');
510
510
  if (userId) {
@@ -518,7 +518,7 @@
518
518
  }
519
519
  const instances = getInstances();
520
520
  if (instances.length > 1) {
521
- error('Multiple instances detected! Please provide a destination.');
521
+ logger.error('Multiple instances detected! Please provide a destination.');
522
522
  return '';
523
523
  }
524
524
  return getUserId$1(instances[0]);
@@ -563,7 +563,7 @@
563
563
  referrer: getReferrer(destination),
564
564
  search: getSearch(destination),
565
565
  locale: getLocale(),
566
- sdkVersion: "0.68.2" ,
566
+ sdkVersion: "0.69.0" ,
567
567
  ...(payload || {}),
568
568
  };
569
569
  let storage = {};
@@ -612,7 +612,7 @@
612
612
 
613
613
  const generateUrl = (destination, path) => {
614
614
  if (!destination) {
615
- log('URL is not valid');
615
+ logger.log('URL is not valid');
616
616
  return '';
617
617
  }
618
618
  return `${destination}${path}`;
@@ -665,7 +665,7 @@
665
665
  });
666
666
  }
667
667
  catch (e) {
668
- error(e);
668
+ logger.error(e);
669
669
  }
670
670
  };
671
671
  const addStub = (destination, stub) => {
@@ -713,7 +713,7 @@
713
713
  if (providers) {
714
714
  payload.providers = providers;
715
715
  }
716
- postRequest(getTagURL(destination, eventName, options), payload, options).catch(error);
716
+ postRequest(getTagURL(destination, eventName, options), payload, options).catch(logger.error);
717
717
  };
718
718
  const processTag = (destination, eventName, data = {}, providers, options) => {
719
719
  var _a, _b;
@@ -739,7 +739,7 @@
739
739
  continue;
740
740
  }
741
741
  if (!configuredTags.has(pkg.name)) {
742
- log(`Provider ${pkg.name} is not in allow list`);
742
+ logger.log(`Provider ${pkg.name} is not in allow list`);
743
743
  continue;
744
744
  }
745
745
  const variables = getProviderVariables(destination, pkg.name);
@@ -747,11 +747,11 @@
747
747
  const executionContext = new Map();
748
748
  for (const variable of variables) {
749
749
  if (!isProviderInstanceAllowed(providers, pkg.name, variable.tagName)) {
750
- log(`Provider instance is not allowed (${pkg.name}: ${variable.tagName})`);
750
+ logger.log(`Provider instance is not allowed (${pkg.name}: ${variable.tagName})`);
751
751
  continue;
752
752
  }
753
753
  if (!hasUserConsent(consent, pkg.name, variable.tagName)) {
754
- log(`Consent is missing (${pkg.name}: ${variable.tagName})`);
754
+ logger.log(`Consent is missing (${pkg.name}: ${variable.tagName})`);
755
755
  continue;
756
756
  }
757
757
  const payload = ((_a = conversion === null || conversion === void 0 ? void 0 : conversion.providers) === null || _a === void 0 ? void 0 : _a.length) === 0 ||
@@ -809,11 +809,11 @@
809
809
  .then((result) => {
810
810
  callback((result === null || result === void 0 ? void 0 : result.result) || {});
811
811
  })
812
- .catch(error);
812
+ .catch(logger.error);
813
813
  };
814
814
  const handleGetData = (keys, callback, options) => {
815
815
  if (!keys || keys.length === 0) {
816
- error('Provide keys for get data API.');
816
+ logger.error('Provide keys for get data API.');
817
817
  return;
818
818
  }
819
819
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -822,7 +822,7 @@
822
822
  }
823
823
  const instances = getInstances();
824
824
  if (instances.length > 1) {
825
- error('Multiple instances found! Please provide destination.');
825
+ logger.error('Multiple instances found! Please provide destination.');
826
826
  callback({});
827
827
  return;
828
828
  }
@@ -840,17 +840,17 @@
840
840
  continue;
841
841
  }
842
842
  if (!configuredTags.has(pkg.name)) {
843
- log(`Provider ${pkg.name} is not in allow list`);
843
+ logger.log(`Provider ${pkg.name} is not in allow list`);
844
844
  continue;
845
845
  }
846
846
  const variables = getProviderVariables(destination, pkg.name);
847
847
  for (const variable of variables) {
848
848
  if (!isProviderInstanceAllowed(providers, pkg.name, variable.tagName)) {
849
- log(`Data not allowed for ${pkg.name} (${variable.tagName})`);
849
+ logger.log(`Data not allowed for ${pkg.name} (${variable.tagName})`);
850
850
  continue;
851
851
  }
852
852
  if (!hasUserConsent(consent, pkg.name, variable.tagName)) {
853
- log(`Consent is missing for ${pkg.name} (${variable.tagName})`);
853
+ logger.log(`Consent is missing for ${pkg.name} (${variable.tagName})`);
854
854
  continue;
855
855
  }
856
856
  pkg.user({
@@ -860,11 +860,11 @@
860
860
  });
861
861
  }
862
862
  }
863
- postRequest(getDataURL(destination), { data, providers }, options).catch(error);
863
+ postRequest(getDataURL(destination), { data, providers }, options).catch(logger.error);
864
864
  };
865
865
  const handleData = (data, providers, options) => {
866
866
  if (!data || Object.keys(data).length === 0) {
867
- error('Provide data for data API.');
867
+ logger.error('Provide data for data API.');
868
868
  return;
869
869
  }
870
870
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -908,7 +908,7 @@
908
908
  };
909
909
  saveConsent(destination, consent);
910
910
  if (!(options === null || options === void 0 ? void 0 : options.localSave)) {
911
- postRequest(getConsentURL(destination), payload).catch(error);
911
+ postRequest(getConsentURL(destination), payload).catch(logger.error);
912
912
  }
913
913
  const userId = getUserId$1(destination);
914
914
  const providerPackages = getSetting(destination, 'browserPackages');
@@ -939,6 +939,7 @@
939
939
  keyName: `${keyPrefix}Store`,
940
940
  executionContext,
941
941
  session: null,
942
+ destination,
942
943
  });
943
944
  }
944
945
  }
@@ -971,7 +972,7 @@
971
972
  return null;
972
973
  }
973
974
  if (!preferences.edgeURL) {
974
- error('Please provide URL for EdgeTag');
975
+ logger.error('Please provide URL for EdgeTag');
975
976
  return null;
976
977
  }
977
978
  const providersPackages = {};
@@ -1012,7 +1013,7 @@
1012
1013
  updatedValue = JSON.stringify(value);
1013
1014
  }
1014
1015
  catch {
1015
- log('Error stringify value.');
1016
+ logger.log('Error stringify value.');
1016
1017
  return;
1017
1018
  }
1018
1019
  }
@@ -1107,6 +1108,7 @@
1107
1108
  });
1108
1109
  };
1109
1110
 
1111
+ const keyName = `${keyPrefix}Store`;
1110
1112
  const handleManifest = (destination, response) => {
1111
1113
  const providerPackages = getSetting(destination, 'browserPackages');
1112
1114
  const userId = getUserId$1(destination);
@@ -1138,8 +1140,9 @@
1138
1140
  sendTag: sendTag.bind(null, destination),
1139
1141
  sendEdgeData: processData.bind(null, destination),
1140
1142
  getEdgeData: processGetData.bind(null, destination),
1141
- keyName: `${keyPrefix}Store`,
1143
+ keyName,
1142
1144
  executionContext,
1145
+ destination,
1143
1146
  });
1144
1147
  }
1145
1148
  }
@@ -1200,7 +1203,7 @@
1200
1203
  .then((result) => {
1201
1204
  var _a;
1202
1205
  if (!result) {
1203
- error('Initialization failed');
1206
+ logger.error('Initialization failed');
1204
1207
  return;
1205
1208
  }
1206
1209
  if (result.isNewUser && result.consent) {
@@ -1243,7 +1246,7 @@
1243
1246
  // do nothing
1244
1247
  }
1245
1248
  })
1246
- .catch(error);
1249
+ .catch(logger.error);
1247
1250
  };
1248
1251
 
1249
1252
  const processUser = (destination, key, value, providers, options) => {
@@ -1259,17 +1262,17 @@
1259
1262
  continue;
1260
1263
  }
1261
1264
  if (!configuredTags.has(pkg.name)) {
1262
- log(`Provider ${pkg.name} is not in allow list`);
1265
+ logger.log(`Provider ${pkg.name} is not in allow list`);
1263
1266
  continue;
1264
1267
  }
1265
1268
  const variables = getProviderVariables(destination, pkg.name);
1266
1269
  for (const variable of variables) {
1267
1270
  if (!isProviderInstanceAllowed(providers, pkg.name, variable.tagName)) {
1268
- log(`User not allowed for ${pkg.name} (${variable.tagName})`);
1271
+ logger.log(`User not allowed for ${pkg.name} (${variable.tagName})`);
1269
1272
  continue;
1270
1273
  }
1271
1274
  if (!hasUserConsent(consent, pkg.name, variable.tagName)) {
1272
- log(`User do not have consent for ${pkg.name} (${variable.tagName})`);
1275
+ logger.log(`User do not have consent for ${pkg.name} (${variable.tagName})`);
1273
1276
  continue;
1274
1277
  }
1275
1278
  pkg.user({
@@ -1283,11 +1286,11 @@
1283
1286
  key,
1284
1287
  value,
1285
1288
  providers,
1286
- }, options).catch(error);
1289
+ }, options).catch(logger.error);
1287
1290
  };
1288
1291
  const handleUser = (key, value, providers, options) => {
1289
1292
  if (!key || !value) {
1290
- error('Key or Value is missing in user API.');
1293
+ logger.error('Key or Value is missing in user API.');
1291
1294
  return;
1292
1295
  }
1293
1296
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -1304,7 +1307,7 @@
1304
1307
  .then((result) => {
1305
1308
  callback((result === null || result === void 0 ? void 0 : result.result) || []);
1306
1309
  })
1307
- .catch(error);
1310
+ .catch(logger.error);
1308
1311
  };
1309
1312
  const handleKeys = (callback, options) => {
1310
1313
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -1313,7 +1316,7 @@
1313
1316
  }
1314
1317
  const instances = getInstances();
1315
1318
  if (instances.length > 1) {
1316
- error('Multiple instances found! Please provide destination.');
1319
+ logger.error('Multiple instances found! Please provide destination.');
1317
1320
  callback([]);
1318
1321
  return;
1319
1322
  }
@@ -1356,7 +1359,7 @@
1356
1359
  }
1357
1360
  const instances = getInstances();
1358
1361
  if (instances.length > 1) {
1359
- error('Multiple instances found! Please provide destination.');
1362
+ logger.error('Multiple instances found! Please provide destination.');
1360
1363
  return undefined;
1361
1364
  }
1362
1365
  return getSetting(instances[0], 'isNewUser');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.68.2",
3
+ "version": "0.69.0",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",