@blotoutio/edgetag-sdk-js 0.60.0 → 0.61.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.
package/index.cjs.js CHANGED
@@ -277,6 +277,7 @@ const tagStorage = 'edgeTag';
277
277
  const consentKey = 'consent';
278
278
  const keyPrefix = `_worker`;
279
279
  const cookieKey = 'tag_user_id';
280
+ const fallbackSessionKey = 'fallback_tag_user_id';
280
281
 
281
282
  const getMessage = (error) => {
282
283
  if (error instanceof Error) {
@@ -467,28 +468,39 @@ const generateEventId = (name) => {
467
468
  };
468
469
 
469
470
  const getCookieValue = (key) => {
471
+ var _a;
470
472
  try {
471
473
  if (!document || !document.cookie) {
472
474
  return '';
473
475
  }
474
- const name = `${key}=`;
475
- const decodedCookie = decodeURIComponent(document.cookie);
476
- const ca = decodedCookie.split(';');
477
- for (let i = 0; i < ca.length; i++) {
478
- let c = ca[i];
479
- while (c.charAt(0) === ' ') {
480
- c = c.substring(1);
481
- }
482
- if (c.indexOf(name) === 0) {
483
- return c.substring(name.length, c.length);
484
- }
485
- }
486
- return '';
476
+ const cookies = parseCookies(document.cookie);
477
+ return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
487
478
  }
488
479
  catch {
489
480
  return '';
490
481
  }
491
482
  };
483
+ const parseCookies = (cookie) => {
484
+ return Object.fromEntries(cookie
485
+ .split(/;\s+/)
486
+ .map((r) => r.split('=').map((str) => str.trim()))
487
+ .map(([cookieKey, cookieValue]) => {
488
+ if (!cookieKey) {
489
+ return [];
490
+ }
491
+ let decodedValue = '';
492
+ if (cookieValue) {
493
+ try {
494
+ decodedValue = decodeURIComponent(cookieValue);
495
+ }
496
+ catch (e) {
497
+ console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
498
+ decodedValue = cookieValue;
499
+ }
500
+ }
501
+ return [cookieKey, decodedValue];
502
+ }));
503
+ };
492
504
 
493
505
  const getUserId$1 = (destination) => {
494
506
  const userId = getSetting(destination, 'userId');
@@ -548,7 +560,7 @@ const getStandardPayload = (destination, payload) => {
548
560
  referrer: getReferrer(destination),
549
561
  search: getSearch(destination),
550
562
  locale: getLocale(),
551
- sdkVersion: "0.60.0" ,
563
+ sdkVersion: "0.61.0" ,
552
564
  ...(payload || {}),
553
565
  };
554
566
  let storage = {};
@@ -1096,6 +1108,26 @@ const handleInit = (preferences) => {
1096
1108
  if (!browserPackages) {
1097
1109
  return;
1098
1110
  }
1111
+ let fallbackId;
1112
+ try {
1113
+ fallbackId = preferences.fallbackUserId;
1114
+ if (!fallbackId) {
1115
+ const fallbackSessionValue = sessionStorage.getItem(fallbackSessionKey);
1116
+ if (!fallbackSessionValue) {
1117
+ const randomId = crypto.randomUUID();
1118
+ if (randomId) {
1119
+ fallbackId = `${randomId}-${Date.now()}`;
1120
+ sessionStorage.setItem(fallbackSessionKey, fallbackId);
1121
+ }
1122
+ }
1123
+ else {
1124
+ fallbackId = fallbackSessionValue;
1125
+ }
1126
+ }
1127
+ }
1128
+ catch {
1129
+ // do nothing
1130
+ }
1099
1131
  if (preferences.afterManifestEvents) {
1100
1132
  setSetting(preferences.edgeURL, {
1101
1133
  stubs: preferences.afterManifestEvents,
@@ -1110,6 +1142,9 @@ const handleInit = (preferences) => {
1110
1142
  setSetting(preferences.edgeURL, { userId: preferences.userId });
1111
1143
  url.searchParams.set('userId', preferences.userId);
1112
1144
  }
1145
+ if (fallbackId) {
1146
+ url.searchParams.set('fallbackUserId', fallbackId);
1147
+ }
1113
1148
  getRequest(url.href)
1114
1149
  .then((result) => {
1115
1150
  var _a;
package/index.d.ts CHANGED
@@ -42,6 +42,7 @@ export type InitPreferences = {
42
42
  edgeURL: string
43
43
  disableConsentCheck?: boolean
44
44
  userId?: string
45
+ fallbackUserId?: string
45
46
  providers?: ProviderInit[]
46
47
  afterManifestEvents?: Stub[]
47
48
  }
package/index.mjs CHANGED
@@ -275,6 +275,7 @@ const tagStorage = 'edgeTag';
275
275
  const consentKey = 'consent';
276
276
  const keyPrefix = `_worker`;
277
277
  const cookieKey = 'tag_user_id';
278
+ const fallbackSessionKey = 'fallback_tag_user_id';
278
279
 
279
280
  const getMessage = (error) => {
280
281
  if (error instanceof Error) {
@@ -465,28 +466,39 @@ const generateEventId = (name) => {
465
466
  };
466
467
 
467
468
  const getCookieValue = (key) => {
469
+ var _a;
468
470
  try {
469
471
  if (!document || !document.cookie) {
470
472
  return '';
471
473
  }
472
- const name = `${key}=`;
473
- const decodedCookie = decodeURIComponent(document.cookie);
474
- const ca = decodedCookie.split(';');
475
- for (let i = 0; i < ca.length; i++) {
476
- let c = ca[i];
477
- while (c.charAt(0) === ' ') {
478
- c = c.substring(1);
479
- }
480
- if (c.indexOf(name) === 0) {
481
- return c.substring(name.length, c.length);
482
- }
483
- }
484
- return '';
474
+ const cookies = parseCookies(document.cookie);
475
+ return (_a = cookies[key]) !== null && _a !== void 0 ? _a : '';
485
476
  }
486
477
  catch {
487
478
  return '';
488
479
  }
489
480
  };
481
+ const parseCookies = (cookie) => {
482
+ return Object.fromEntries(cookie
483
+ .split(/;\s+/)
484
+ .map((r) => r.split('=').map((str) => str.trim()))
485
+ .map(([cookieKey, cookieValue]) => {
486
+ if (!cookieKey) {
487
+ return [];
488
+ }
489
+ let decodedValue = '';
490
+ if (cookieValue) {
491
+ try {
492
+ decodedValue = decodeURIComponent(cookieValue);
493
+ }
494
+ catch (e) {
495
+ console.log(`Unable to decode cookie ${cookieKey}: ${e}`);
496
+ decodedValue = cookieValue;
497
+ }
498
+ }
499
+ return [cookieKey, decodedValue];
500
+ }));
501
+ };
490
502
 
491
503
  const getUserId$1 = (destination) => {
492
504
  const userId = getSetting(destination, 'userId');
@@ -546,7 +558,7 @@ const getStandardPayload = (destination, payload) => {
546
558
  referrer: getReferrer(destination),
547
559
  search: getSearch(destination),
548
560
  locale: getLocale(),
549
- sdkVersion: "0.60.0" ,
561
+ sdkVersion: "0.61.0" ,
550
562
  ...(payload || {}),
551
563
  };
552
564
  let storage = {};
@@ -1094,6 +1106,26 @@ const handleInit = (preferences) => {
1094
1106
  if (!browserPackages) {
1095
1107
  return;
1096
1108
  }
1109
+ let fallbackId;
1110
+ try {
1111
+ fallbackId = preferences.fallbackUserId;
1112
+ if (!fallbackId) {
1113
+ const fallbackSessionValue = sessionStorage.getItem(fallbackSessionKey);
1114
+ if (!fallbackSessionValue) {
1115
+ const randomId = crypto.randomUUID();
1116
+ if (randomId) {
1117
+ fallbackId = `${randomId}-${Date.now()}`;
1118
+ sessionStorage.setItem(fallbackSessionKey, fallbackId);
1119
+ }
1120
+ }
1121
+ else {
1122
+ fallbackId = fallbackSessionValue;
1123
+ }
1124
+ }
1125
+ }
1126
+ catch {
1127
+ // do nothing
1128
+ }
1097
1129
  if (preferences.afterManifestEvents) {
1098
1130
  setSetting(preferences.edgeURL, {
1099
1131
  stubs: preferences.afterManifestEvents,
@@ -1108,6 +1140,9 @@ const handleInit = (preferences) => {
1108
1140
  setSetting(preferences.edgeURL, { userId: preferences.userId });
1109
1141
  url.searchParams.set('userId', preferences.userId);
1110
1142
  }
1143
+ if (fallbackId) {
1144
+ url.searchParams.set('fallbackUserId', fallbackId);
1145
+ }
1111
1146
  getRequest(url.href)
1112
1147
  .then((result) => {
1113
1148
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-js",
3
- "version": "0.60.0",
3
+ "version": "0.61.0",
4
4
  "description": "JS SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",