@blotoutio/edgetag-sdk-js 0.52.0 → 0.52.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.
Files changed (3) hide show
  1. package/index.cjs.js +153 -149
  2. package/index.mjs +153 -149
  3. package/package.json +1 -1
package/index.cjs.js CHANGED
@@ -141,11 +141,12 @@ const initSettings = (destination, options) => {
141
141
  destination,
142
142
  initialized: false,
143
143
  stubs: [],
144
+ browserPackages: [],
144
145
  channels: new Map(),
145
146
  ...options,
146
147
  };
147
148
  };
148
- const setSetting = (options, destination) => {
149
+ const setSetting = (destination, options) => {
149
150
  if (!edgeTagSettings) {
150
151
  return;
151
152
  }
@@ -172,7 +173,7 @@ const getInstances = () => {
172
173
  };
173
174
  const addChannel = (destination, pkg, tagName) => upsert(getSetting(destination, 'channels'), pkg, (names) => names.add(tagName), () => new Set());
174
175
  const getProviderVariables = (destination, packageId) => {
175
- const setting = getSetting(destination, 'packages');
176
+ const setting = getSetting(destination, 'manifest');
176
177
  if (!setting) {
177
178
  return [];
178
179
  }
@@ -275,6 +276,7 @@ const areEqual = (a, b) => {
275
276
  const tagStorage = 'edgeTag';
276
277
  const consentKey = 'consent';
277
278
  const keyPrefix = `_worker`;
279
+ const cookieKey = 'tag_user_id';
278
280
 
279
281
  const getMessage = (error) => {
280
282
  if (error instanceof Error) {
@@ -420,8 +422,80 @@ const getSession = (key) => {
420
422
  }
421
423
  };
422
424
 
425
+ const encodeString = (name) => {
426
+ if (typeof btoa === 'undefined') {
427
+ return Buffer.from(name).toString('base64');
428
+ }
429
+ return btoa(name);
430
+ };
431
+ const getBasicRandomNumber = () => {
432
+ return parseInt((Math.random() * 10000000000).toString(), 10);
433
+ };
434
+ const generateUUID = () => {
435
+ let id = '';
436
+ try {
437
+ id = crypto.randomUUID();
438
+ if (!id) {
439
+ const array = new Uint32Array(20);
440
+ const numbers = crypto.getRandomValues(array);
441
+ for (let i = 0; i < 5; i++) {
442
+ const y = i * 3;
443
+ if (i !== 0) {
444
+ id += '-';
445
+ }
446
+ const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
447
+ id += sum.toString();
448
+ }
449
+ }
450
+ }
451
+ catch {
452
+ id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
453
+ console.log('[EdgeTag] Crypto module not found');
454
+ }
455
+ return id;
456
+ };
457
+ const generateEventId = (name) => {
458
+ let time = Date.now().toString();
459
+ if (typeof performance !== 'undefined' &&
460
+ typeof performance.now === 'function') {
461
+ const perf = performance.now();
462
+ if (perf) {
463
+ time = perf.toFixed(4);
464
+ }
465
+ }
466
+ return `${encodeString(name)}-${generateUUID()}-${time}`;
467
+ };
468
+
469
+ const getCookieValue = (key) => {
470
+ try {
471
+ if (!document || !document.cookie) {
472
+ return '';
473
+ }
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 '';
487
+ }
488
+ catch {
489
+ return '';
490
+ }
491
+ };
492
+
423
493
  const getUserId$1 = (destination) => {
424
- return getSetting(destination, 'userId');
494
+ const userId = getSetting(destination, 'userId');
495
+ if (userId) {
496
+ return userId;
497
+ }
498
+ return getCookieValue(cookieKey);
425
499
  };
426
500
  const handleGetUserId = (options) => {
427
501
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -430,7 +504,7 @@ const handleGetUserId = (options) => {
430
504
  const instances = getInstances();
431
505
  if (instances.length > 1) {
432
506
  error('Multiple instances detected! Please provide a destination.');
433
- return null;
507
+ return '';
434
508
  }
435
509
  return getUserId$1(instances[0]);
436
510
  };
@@ -474,7 +548,7 @@ const getStandardPayload = (destination, payload) => {
474
548
  referrer: getReferrer(destination),
475
549
  search: getSearch(destination),
476
550
  locale: getLocale(),
477
- sdkVersion: "0.52.0" ,
551
+ sdkVersion: "0.52.2" ,
478
552
  ...(payload || {}),
479
553
  };
480
554
  let storage = {};
@@ -554,121 +628,6 @@ const getKeysURL = (destination) => {
554
628
  return generateUrl(destination, `/keys`);
555
629
  };
556
630
 
557
- const providersPackages = {};
558
- const setPreferences = (preferences) => {
559
- var _a;
560
- if (!preferences) {
561
- return false;
562
- }
563
- if (!preferences.edgeURL) {
564
- error('Please provide URL for EdgeTag');
565
- return false;
566
- }
567
- (_a = preferences.providers) === null || _a === void 0 ? void 0 : _a.forEach((provider) => {
568
- if (!provider.name) {
569
- return;
570
- }
571
- providersPackages[provider.name] = provider;
572
- });
573
- try {
574
- if (window && Array.isArray(window.edgetagProviders)) {
575
- window.edgetagProviders.forEach((provider) => {
576
- if (!provider.name) {
577
- return;
578
- }
579
- providersPackages[provider.name] = provider;
580
- });
581
- }
582
- }
583
- catch {
584
- // do nothing
585
- }
586
- initSettings(preferences.edgeURL, {
587
- disableConsent: !!preferences.disableConsentCheck,
588
- });
589
- return true;
590
- };
591
- const getProvidersPackage = (destination) => {
592
- const packages = getSetting(destination, 'packages');
593
- const providers = [];
594
- packages === null || packages === void 0 ? void 0 : packages.forEach((pkg) => {
595
- const provider = providersPackages[pkg.package];
596
- if (!provider) {
597
- return;
598
- }
599
- providers.push(provider);
600
- });
601
- return providers;
602
- };
603
-
604
- const encodeString = (name) => {
605
- if (typeof btoa === 'undefined') {
606
- return Buffer.from(name).toString('base64');
607
- }
608
- return btoa(name);
609
- };
610
- const getBasicRandomNumber = () => {
611
- return parseInt((Math.random() * 10000000000).toString(), 10);
612
- };
613
- const generateUUID = () => {
614
- let id = '';
615
- try {
616
- id = crypto.randomUUID();
617
- if (!id) {
618
- const array = new Uint32Array(20);
619
- const numbers = crypto.getRandomValues(array);
620
- for (let i = 0; i < 5; i++) {
621
- const y = i * 3;
622
- if (i !== 0) {
623
- id += '-';
624
- }
625
- const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
626
- id += sum.toString();
627
- }
628
- }
629
- }
630
- catch {
631
- id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
632
- console.log('[EdgeTag] Crypto module not found');
633
- }
634
- return id;
635
- };
636
- const generateEventId = (name) => {
637
- let time = Date.now().toString();
638
- if (typeof performance !== 'undefined' &&
639
- typeof performance.now === 'function') {
640
- const perf = performance.now();
641
- if (perf) {
642
- time = perf.toFixed(4);
643
- }
644
- }
645
- return `${encodeString(name)}-${generateUUID()}-${time}`;
646
- };
647
-
648
- const getCookieValue = (key) => {
649
- try {
650
- if (!document || !document.cookie) {
651
- return '';
652
- }
653
- const name = `${key}=`;
654
- const decodedCookie = decodeURIComponent(document.cookie);
655
- const ca = decodedCookie.split(';');
656
- for (let i = 0; i < ca.length; i++) {
657
- let c = ca[i];
658
- while (c.charAt(0) === ' ') {
659
- c = c.substring(1);
660
- }
661
- if (c.indexOf(name) === 0) {
662
- return c.substring(name.length, c.length);
663
- }
664
- }
665
- return '';
666
- }
667
- catch {
668
- return '';
669
- }
670
- };
671
-
672
631
  const getConsent$1 = (destination) => {
673
632
  const storageConsent = getDataPerKey(destination, 'local', tagStorage, consentKey);
674
633
  if (storageConsent) {
@@ -683,9 +642,9 @@ const processStubs = (destination) => {
683
642
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
684
643
  // @ts-ignore
685
644
  stubs.forEach((stub) => api[stub.name](...(stub.arguments || [])));
686
- setSetting({
645
+ setSetting(destination, {
687
646
  stubs: [],
688
- }, destination);
647
+ });
689
648
  }
690
649
  catch (e) {
691
650
  error(e);
@@ -693,9 +652,9 @@ const processStubs = (destination) => {
693
652
  };
694
653
  const addStub = (destination, stub) => {
695
654
  const otherStubs = getSetting(destination, 'stubs') || [];
696
- setSetting({
655
+ setSetting(destination, {
697
656
  stubs: [...otherStubs, stub],
698
- }, destination);
657
+ });
699
658
  };
700
659
 
701
660
  const sendTag = (destination, { eventName, eventId, data, providerData, providers, options }) => {
@@ -723,12 +682,12 @@ const processTag = (destination, eventName, data = {}, providers, options) => {
723
682
  if (!eventId) {
724
683
  eventId = generateEventId(eventName);
725
684
  }
726
- const providerPackages = getProvidersPackage(destination);
685
+ const providerPackages = getSetting(destination, 'browserPackages');
727
686
  const configuredTags = getSetting(destination, 'channels');
728
687
  const userId = getUserId$1(destination);
729
688
  const providerData = {};
730
689
  const consent = getConsent$1(destination);
731
- for (const pkg of Object.values(providerPackages)) {
690
+ for (const pkg of providerPackages) {
732
691
  if (!pkg || !pkg.name || !pkg.tag) {
733
692
  continue;
734
693
  }
@@ -820,11 +779,11 @@ const handleGetData = (keys, callback, options) => {
820
779
 
821
780
  const processData = (destination, data, providers, options) => {
822
781
  saveKV(destination, data);
823
- const providerPackages = getProvidersPackage(destination);
782
+ const providerPackages = getSetting(destination, 'browserPackages');
824
783
  const configuredTags = getSetting(destination, 'channels');
825
784
  const userId = getUserId$1(destination);
826
785
  const consent = getConsent$1(destination);
827
- for (const pkg of Object.values(providerPackages)) {
786
+ for (const pkg of providerPackages) {
828
787
  if (!pkg || !pkg.user || !pkg.name) {
829
788
  continue;
830
789
  }
@@ -875,9 +834,9 @@ const handleConsent = (consent, options) => {
875
834
  });
876
835
  };
877
836
  const saveConsent = (destination, consent) => {
878
- setSetting({
837
+ setSetting(destination, {
879
838
  consent,
880
- }, destination);
839
+ });
881
840
  savePerKey(destination, 'local', tagStorage, consent, consentKey);
882
841
  };
883
842
  const processConsent = (destination, consent, localSave) => {
@@ -893,10 +852,10 @@ const processConsent = (destination, consent, localSave) => {
893
852
  postRequest(getConsentURL(destination), payload).catch(error);
894
853
  }
895
854
  const userId = getUserId$1(destination);
896
- const providerPackages = getProvidersPackage(destination);
855
+ const providerPackages = getSetting(destination, 'browserPackages');
897
856
  const executionContext = new Map();
898
857
  /* Calling Init for all provider instances based on consent check */
899
- for (const pkg of Object.values(providerPackages)) {
858
+ for (const pkg of providerPackages) {
900
859
  if (!pkg || !pkg.name || !pkg.init) {
901
860
  continue;
902
861
  }
@@ -947,6 +906,41 @@ const processConsent = (destination, consent, localSave) => {
947
906
  }
948
907
  };
949
908
 
909
+ const setPreferences = (preferences) => {
910
+ var _a;
911
+ if (!preferences) {
912
+ return null;
913
+ }
914
+ if (!preferences.edgeURL) {
915
+ error('Please provide URL for EdgeTag');
916
+ return null;
917
+ }
918
+ const providersPackages = {};
919
+ (_a = preferences.providers) === null || _a === void 0 ? void 0 : _a.forEach((provider) => {
920
+ if (!provider.name) {
921
+ return;
922
+ }
923
+ providersPackages[provider.name] = provider;
924
+ });
925
+ try {
926
+ if (window && Array.isArray(window.edgetagProviders)) {
927
+ window.edgetagProviders.forEach((provider) => {
928
+ if (!provider.name) {
929
+ return;
930
+ }
931
+ providersPackages[provider.name] = provider;
932
+ });
933
+ }
934
+ }
935
+ catch {
936
+ // do nothing
937
+ }
938
+ initSettings(preferences.edgeURL, {
939
+ disableConsent: !!preferences.disableConsentCheck,
940
+ });
941
+ return providersPackages;
942
+ };
943
+
950
944
  const cacheKey = `${keyPrefix}Cache`;
951
945
  const identity = (v) => v;
952
946
  const saveDataToEdge = (destination, key, value, provider) => {
@@ -1055,13 +1049,13 @@ const handleCapture = (destination, provider, params, capture) => {
1055
1049
  };
1056
1050
 
1057
1051
  const handleManifest = (destination, response) => {
1058
- const providerPackages = getProvidersPackage(destination);
1052
+ const providerPackages = getSetting(destination, 'browserPackages');
1059
1053
  const userId = getUserId$1(destination);
1060
1054
  const executionContext = new Map();
1061
1055
  const manifest = response.result;
1062
1056
  manifest.forEach((provider) => {
1063
1057
  addChannel(destination, provider.package, provider.tagName);
1064
- const pkg = providerPackages[provider.package];
1058
+ const pkg = providerPackages.find((pkg) => pkg.name === provider.package);
1065
1059
  if (provider.rules) {
1066
1060
  Object.entries(provider.rules).forEach(([name, recipe]) => {
1067
1061
  switch (name) {
@@ -1091,21 +1085,21 @@ const handleManifest = (destination, response) => {
1091
1085
  }
1092
1086
  }
1093
1087
  });
1094
- setSetting({
1088
+ setSetting(destination, {
1095
1089
  initialized: true,
1096
- }, destination);
1090
+ });
1097
1091
  processStubs(destination);
1098
1092
  };
1099
1093
 
1100
1094
  const handleInit = (preferences) => {
1101
- const success = setPreferences(preferences);
1102
- if (!success) {
1095
+ const browserPackages = setPreferences(preferences);
1096
+ if (!browserPackages) {
1103
1097
  return;
1104
1098
  }
1105
1099
  if (preferences.afterManifestEvents) {
1106
- setSetting({
1100
+ setSetting(preferences.edgeURL, {
1107
1101
  stubs: preferences.afterManifestEvents,
1108
- }, preferences.edgeURL);
1102
+ });
1109
1103
  }
1110
1104
  const url = new URL(getInitURL(preferences.edgeURL));
1111
1105
  if (preferences.disableConsentCheck) {
@@ -1113,21 +1107,31 @@ const handleInit = (preferences) => {
1113
1107
  saveConsent(preferences.edgeURL, { all: true });
1114
1108
  }
1115
1109
  if (preferences.userId) {
1116
- setSetting({ userId: preferences.userId }, preferences.edgeURL);
1110
+ setSetting(preferences.edgeURL, { userId: preferences.userId });
1117
1111
  url.searchParams.set('userId', preferences.userId);
1118
1112
  }
1119
1113
  getRequest(url.href)
1120
1114
  .then((result) => {
1115
+ var _a;
1121
1116
  if (!result) {
1122
1117
  error('Initialization failed');
1123
1118
  return;
1124
1119
  }
1125
- setSetting({
1120
+ const providers = {};
1121
+ (_a = result.result) === null || _a === void 0 ? void 0 : _a.forEach((pkg) => {
1122
+ const provider = browserPackages[pkg.package];
1123
+ if (!provider) {
1124
+ return;
1125
+ }
1126
+ providers[pkg.package] = provider;
1127
+ });
1128
+ setSetting(preferences.edgeURL, {
1126
1129
  isNewUser: result.isNewUser,
1127
1130
  consent: getConsent$1(preferences.edgeURL) || result.consent,
1128
1131
  userId: result.userId,
1129
- packages: result.result,
1130
- }, preferences.edgeURL);
1132
+ manifest: result.result,
1133
+ browserPackages: Object.values(providers),
1134
+ });
1131
1135
  handleManifest(preferences.edgeURL, result);
1132
1136
  try {
1133
1137
  window.dispatchEvent(new CustomEvent('edgetag-initialized', {
@@ -1152,11 +1156,11 @@ const processUser = (destination, key, value, providers, options) => {
1152
1156
  saveKV(destination, {
1153
1157
  [key]: value,
1154
1158
  });
1155
- const providerPackages = getProvidersPackage(destination);
1159
+ const providerPackages = getSetting(destination, 'browserPackages');
1156
1160
  const configuredTags = getSetting(destination, 'channels');
1157
1161
  const consent = getConsent$1(destination);
1158
1162
  const userId = getUserId$1(destination);
1159
- for (const pkg of Object.values(providerPackages)) {
1163
+ for (const pkg of providerPackages) {
1160
1164
  if (!pkg || !pkg.name || !pkg.user) {
1161
1165
  continue;
1162
1166
  }
@@ -1171,7 +1175,7 @@ const processUser = (destination, key, value, providers, options) => {
1171
1175
  continue;
1172
1176
  }
1173
1177
  if (!hasUserConsent(consent, pkg.name, variable.tagName)) {
1174
- log(`User doesn't have consent for ${pkg.name} (${variable.tagName})`);
1178
+ log(`User do not have consent for ${pkg.name} (${variable.tagName})`);
1175
1179
  continue;
1176
1180
  }
1177
1181
  pkg.user({
@@ -1266,12 +1270,12 @@ const handleIsNewUser = (options) => {
1266
1270
 
1267
1271
  const processConfig = (destination, config) => {
1268
1272
  const existingConfig = getSetting(destination, 'config');
1269
- setSetting({
1273
+ setSetting(destination, {
1270
1274
  config: {
1271
1275
  ...existingConfig,
1272
1276
  ...config,
1273
1277
  },
1274
- }, destination);
1278
+ });
1275
1279
  };
1276
1280
  const handleConfig = (config, options) => {
1277
1281
  if (options === null || options === void 0 ? void 0 : options.destination) {
package/index.mjs CHANGED
@@ -139,11 +139,12 @@ const initSettings = (destination, options) => {
139
139
  destination,
140
140
  initialized: false,
141
141
  stubs: [],
142
+ browserPackages: [],
142
143
  channels: new Map(),
143
144
  ...options,
144
145
  };
145
146
  };
146
- const setSetting = (options, destination) => {
147
+ const setSetting = (destination, options) => {
147
148
  if (!edgeTagSettings) {
148
149
  return;
149
150
  }
@@ -170,7 +171,7 @@ const getInstances = () => {
170
171
  };
171
172
  const addChannel = (destination, pkg, tagName) => upsert(getSetting(destination, 'channels'), pkg, (names) => names.add(tagName), () => new Set());
172
173
  const getProviderVariables = (destination, packageId) => {
173
- const setting = getSetting(destination, 'packages');
174
+ const setting = getSetting(destination, 'manifest');
174
175
  if (!setting) {
175
176
  return [];
176
177
  }
@@ -273,6 +274,7 @@ const areEqual = (a, b) => {
273
274
  const tagStorage = 'edgeTag';
274
275
  const consentKey = 'consent';
275
276
  const keyPrefix = `_worker`;
277
+ const cookieKey = 'tag_user_id';
276
278
 
277
279
  const getMessage = (error) => {
278
280
  if (error instanceof Error) {
@@ -418,8 +420,80 @@ const getSession = (key) => {
418
420
  }
419
421
  };
420
422
 
423
+ const encodeString = (name) => {
424
+ if (typeof btoa === 'undefined') {
425
+ return Buffer.from(name).toString('base64');
426
+ }
427
+ return btoa(name);
428
+ };
429
+ const getBasicRandomNumber = () => {
430
+ return parseInt((Math.random() * 10000000000).toString(), 10);
431
+ };
432
+ const generateUUID = () => {
433
+ let id = '';
434
+ try {
435
+ id = crypto.randomUUID();
436
+ if (!id) {
437
+ const array = new Uint32Array(20);
438
+ const numbers = crypto.getRandomValues(array);
439
+ for (let i = 0; i < 5; i++) {
440
+ const y = i * 3;
441
+ if (i !== 0) {
442
+ id += '-';
443
+ }
444
+ const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
445
+ id += sum.toString();
446
+ }
447
+ }
448
+ }
449
+ catch {
450
+ id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
451
+ console.log('[EdgeTag] Crypto module not found');
452
+ }
453
+ return id;
454
+ };
455
+ const generateEventId = (name) => {
456
+ let time = Date.now().toString();
457
+ if (typeof performance !== 'undefined' &&
458
+ typeof performance.now === 'function') {
459
+ const perf = performance.now();
460
+ if (perf) {
461
+ time = perf.toFixed(4);
462
+ }
463
+ }
464
+ return `${encodeString(name)}-${generateUUID()}-${time}`;
465
+ };
466
+
467
+ const getCookieValue = (key) => {
468
+ try {
469
+ if (!document || !document.cookie) {
470
+ return '';
471
+ }
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 '';
485
+ }
486
+ catch {
487
+ return '';
488
+ }
489
+ };
490
+
421
491
  const getUserId$1 = (destination) => {
422
- return getSetting(destination, 'userId');
492
+ const userId = getSetting(destination, 'userId');
493
+ if (userId) {
494
+ return userId;
495
+ }
496
+ return getCookieValue(cookieKey);
423
497
  };
424
498
  const handleGetUserId = (options) => {
425
499
  if (options === null || options === void 0 ? void 0 : options.destination) {
@@ -428,7 +502,7 @@ const handleGetUserId = (options) => {
428
502
  const instances = getInstances();
429
503
  if (instances.length > 1) {
430
504
  error('Multiple instances detected! Please provide a destination.');
431
- return null;
505
+ return '';
432
506
  }
433
507
  return getUserId$1(instances[0]);
434
508
  };
@@ -472,7 +546,7 @@ const getStandardPayload = (destination, payload) => {
472
546
  referrer: getReferrer(destination),
473
547
  search: getSearch(destination),
474
548
  locale: getLocale(),
475
- sdkVersion: "0.52.0" ,
549
+ sdkVersion: "0.52.2" ,
476
550
  ...(payload || {}),
477
551
  };
478
552
  let storage = {};
@@ -552,121 +626,6 @@ const getKeysURL = (destination) => {
552
626
  return generateUrl(destination, `/keys`);
553
627
  };
554
628
 
555
- const providersPackages = {};
556
- const setPreferences = (preferences) => {
557
- var _a;
558
- if (!preferences) {
559
- return false;
560
- }
561
- if (!preferences.edgeURL) {
562
- error('Please provide URL for EdgeTag');
563
- return false;
564
- }
565
- (_a = preferences.providers) === null || _a === void 0 ? void 0 : _a.forEach((provider) => {
566
- if (!provider.name) {
567
- return;
568
- }
569
- providersPackages[provider.name] = provider;
570
- });
571
- try {
572
- if (window && Array.isArray(window.edgetagProviders)) {
573
- window.edgetagProviders.forEach((provider) => {
574
- if (!provider.name) {
575
- return;
576
- }
577
- providersPackages[provider.name] = provider;
578
- });
579
- }
580
- }
581
- catch {
582
- // do nothing
583
- }
584
- initSettings(preferences.edgeURL, {
585
- disableConsent: !!preferences.disableConsentCheck,
586
- });
587
- return true;
588
- };
589
- const getProvidersPackage = (destination) => {
590
- const packages = getSetting(destination, 'packages');
591
- const providers = [];
592
- packages === null || packages === void 0 ? void 0 : packages.forEach((pkg) => {
593
- const provider = providersPackages[pkg.package];
594
- if (!provider) {
595
- return;
596
- }
597
- providers.push(provider);
598
- });
599
- return providers;
600
- };
601
-
602
- const encodeString = (name) => {
603
- if (typeof btoa === 'undefined') {
604
- return Buffer.from(name).toString('base64');
605
- }
606
- return btoa(name);
607
- };
608
- const getBasicRandomNumber = () => {
609
- return parseInt((Math.random() * 10000000000).toString(), 10);
610
- };
611
- const generateUUID = () => {
612
- let id = '';
613
- try {
614
- id = crypto.randomUUID();
615
- if (!id) {
616
- const array = new Uint32Array(20);
617
- const numbers = crypto.getRandomValues(array);
618
- for (let i = 0; i < 5; i++) {
619
- const y = i * 3;
620
- if (i !== 0) {
621
- id += '-';
622
- }
623
- const sum = numbers[y + 1] + numbers[y + 2] + numbers[y + 3];
624
- id += sum.toString();
625
- }
626
- }
627
- }
628
- catch {
629
- id = `${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}-${getBasicRandomNumber()}`;
630
- console.log('[EdgeTag] Crypto module not found');
631
- }
632
- return id;
633
- };
634
- const generateEventId = (name) => {
635
- let time = Date.now().toString();
636
- if (typeof performance !== 'undefined' &&
637
- typeof performance.now === 'function') {
638
- const perf = performance.now();
639
- if (perf) {
640
- time = perf.toFixed(4);
641
- }
642
- }
643
- return `${encodeString(name)}-${generateUUID()}-${time}`;
644
- };
645
-
646
- const getCookieValue = (key) => {
647
- try {
648
- if (!document || !document.cookie) {
649
- return '';
650
- }
651
- const name = `${key}=`;
652
- const decodedCookie = decodeURIComponent(document.cookie);
653
- const ca = decodedCookie.split(';');
654
- for (let i = 0; i < ca.length; i++) {
655
- let c = ca[i];
656
- while (c.charAt(0) === ' ') {
657
- c = c.substring(1);
658
- }
659
- if (c.indexOf(name) === 0) {
660
- return c.substring(name.length, c.length);
661
- }
662
- }
663
- return '';
664
- }
665
- catch {
666
- return '';
667
- }
668
- };
669
-
670
629
  const getConsent$1 = (destination) => {
671
630
  const storageConsent = getDataPerKey(destination, 'local', tagStorage, consentKey);
672
631
  if (storageConsent) {
@@ -681,9 +640,9 @@ const processStubs = (destination) => {
681
640
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
682
641
  // @ts-ignore
683
642
  stubs.forEach((stub) => api[stub.name](...(stub.arguments || [])));
684
- setSetting({
643
+ setSetting(destination, {
685
644
  stubs: [],
686
- }, destination);
645
+ });
687
646
  }
688
647
  catch (e) {
689
648
  error(e);
@@ -691,9 +650,9 @@ const processStubs = (destination) => {
691
650
  };
692
651
  const addStub = (destination, stub) => {
693
652
  const otherStubs = getSetting(destination, 'stubs') || [];
694
- setSetting({
653
+ setSetting(destination, {
695
654
  stubs: [...otherStubs, stub],
696
- }, destination);
655
+ });
697
656
  };
698
657
 
699
658
  const sendTag = (destination, { eventName, eventId, data, providerData, providers, options }) => {
@@ -721,12 +680,12 @@ const processTag = (destination, eventName, data = {}, providers, options) => {
721
680
  if (!eventId) {
722
681
  eventId = generateEventId(eventName);
723
682
  }
724
- const providerPackages = getProvidersPackage(destination);
683
+ const providerPackages = getSetting(destination, 'browserPackages');
725
684
  const configuredTags = getSetting(destination, 'channels');
726
685
  const userId = getUserId$1(destination);
727
686
  const providerData = {};
728
687
  const consent = getConsent$1(destination);
729
- for (const pkg of Object.values(providerPackages)) {
688
+ for (const pkg of providerPackages) {
730
689
  if (!pkg || !pkg.name || !pkg.tag) {
731
690
  continue;
732
691
  }
@@ -818,11 +777,11 @@ const handleGetData = (keys, callback, options) => {
818
777
 
819
778
  const processData = (destination, data, providers, options) => {
820
779
  saveKV(destination, data);
821
- const providerPackages = getProvidersPackage(destination);
780
+ const providerPackages = getSetting(destination, 'browserPackages');
822
781
  const configuredTags = getSetting(destination, 'channels');
823
782
  const userId = getUserId$1(destination);
824
783
  const consent = getConsent$1(destination);
825
- for (const pkg of Object.values(providerPackages)) {
784
+ for (const pkg of providerPackages) {
826
785
  if (!pkg || !pkg.user || !pkg.name) {
827
786
  continue;
828
787
  }
@@ -873,9 +832,9 @@ const handleConsent = (consent, options) => {
873
832
  });
874
833
  };
875
834
  const saveConsent = (destination, consent) => {
876
- setSetting({
835
+ setSetting(destination, {
877
836
  consent,
878
- }, destination);
837
+ });
879
838
  savePerKey(destination, 'local', tagStorage, consent, consentKey);
880
839
  };
881
840
  const processConsent = (destination, consent, localSave) => {
@@ -891,10 +850,10 @@ const processConsent = (destination, consent, localSave) => {
891
850
  postRequest(getConsentURL(destination), payload).catch(error);
892
851
  }
893
852
  const userId = getUserId$1(destination);
894
- const providerPackages = getProvidersPackage(destination);
853
+ const providerPackages = getSetting(destination, 'browserPackages');
895
854
  const executionContext = new Map();
896
855
  /* Calling Init for all provider instances based on consent check */
897
- for (const pkg of Object.values(providerPackages)) {
856
+ for (const pkg of providerPackages) {
898
857
  if (!pkg || !pkg.name || !pkg.init) {
899
858
  continue;
900
859
  }
@@ -945,6 +904,41 @@ const processConsent = (destination, consent, localSave) => {
945
904
  }
946
905
  };
947
906
 
907
+ const setPreferences = (preferences) => {
908
+ var _a;
909
+ if (!preferences) {
910
+ return null;
911
+ }
912
+ if (!preferences.edgeURL) {
913
+ error('Please provide URL for EdgeTag');
914
+ return null;
915
+ }
916
+ const providersPackages = {};
917
+ (_a = preferences.providers) === null || _a === void 0 ? void 0 : _a.forEach((provider) => {
918
+ if (!provider.name) {
919
+ return;
920
+ }
921
+ providersPackages[provider.name] = provider;
922
+ });
923
+ try {
924
+ if (window && Array.isArray(window.edgetagProviders)) {
925
+ window.edgetagProviders.forEach((provider) => {
926
+ if (!provider.name) {
927
+ return;
928
+ }
929
+ providersPackages[provider.name] = provider;
930
+ });
931
+ }
932
+ }
933
+ catch {
934
+ // do nothing
935
+ }
936
+ initSettings(preferences.edgeURL, {
937
+ disableConsent: !!preferences.disableConsentCheck,
938
+ });
939
+ return providersPackages;
940
+ };
941
+
948
942
  const cacheKey = `${keyPrefix}Cache`;
949
943
  const identity = (v) => v;
950
944
  const saveDataToEdge = (destination, key, value, provider) => {
@@ -1053,13 +1047,13 @@ const handleCapture = (destination, provider, params, capture) => {
1053
1047
  };
1054
1048
 
1055
1049
  const handleManifest = (destination, response) => {
1056
- const providerPackages = getProvidersPackage(destination);
1050
+ const providerPackages = getSetting(destination, 'browserPackages');
1057
1051
  const userId = getUserId$1(destination);
1058
1052
  const executionContext = new Map();
1059
1053
  const manifest = response.result;
1060
1054
  manifest.forEach((provider) => {
1061
1055
  addChannel(destination, provider.package, provider.tagName);
1062
- const pkg = providerPackages[provider.package];
1056
+ const pkg = providerPackages.find((pkg) => pkg.name === provider.package);
1063
1057
  if (provider.rules) {
1064
1058
  Object.entries(provider.rules).forEach(([name, recipe]) => {
1065
1059
  switch (name) {
@@ -1089,21 +1083,21 @@ const handleManifest = (destination, response) => {
1089
1083
  }
1090
1084
  }
1091
1085
  });
1092
- setSetting({
1086
+ setSetting(destination, {
1093
1087
  initialized: true,
1094
- }, destination);
1088
+ });
1095
1089
  processStubs(destination);
1096
1090
  };
1097
1091
 
1098
1092
  const handleInit = (preferences) => {
1099
- const success = setPreferences(preferences);
1100
- if (!success) {
1093
+ const browserPackages = setPreferences(preferences);
1094
+ if (!browserPackages) {
1101
1095
  return;
1102
1096
  }
1103
1097
  if (preferences.afterManifestEvents) {
1104
- setSetting({
1098
+ setSetting(preferences.edgeURL, {
1105
1099
  stubs: preferences.afterManifestEvents,
1106
- }, preferences.edgeURL);
1100
+ });
1107
1101
  }
1108
1102
  const url = new URL(getInitURL(preferences.edgeURL));
1109
1103
  if (preferences.disableConsentCheck) {
@@ -1111,21 +1105,31 @@ const handleInit = (preferences) => {
1111
1105
  saveConsent(preferences.edgeURL, { all: true });
1112
1106
  }
1113
1107
  if (preferences.userId) {
1114
- setSetting({ userId: preferences.userId }, preferences.edgeURL);
1108
+ setSetting(preferences.edgeURL, { userId: preferences.userId });
1115
1109
  url.searchParams.set('userId', preferences.userId);
1116
1110
  }
1117
1111
  getRequest(url.href)
1118
1112
  .then((result) => {
1113
+ var _a;
1119
1114
  if (!result) {
1120
1115
  error('Initialization failed');
1121
1116
  return;
1122
1117
  }
1123
- setSetting({
1118
+ const providers = {};
1119
+ (_a = result.result) === null || _a === void 0 ? void 0 : _a.forEach((pkg) => {
1120
+ const provider = browserPackages[pkg.package];
1121
+ if (!provider) {
1122
+ return;
1123
+ }
1124
+ providers[pkg.package] = provider;
1125
+ });
1126
+ setSetting(preferences.edgeURL, {
1124
1127
  isNewUser: result.isNewUser,
1125
1128
  consent: getConsent$1(preferences.edgeURL) || result.consent,
1126
1129
  userId: result.userId,
1127
- packages: result.result,
1128
- }, preferences.edgeURL);
1130
+ manifest: result.result,
1131
+ browserPackages: Object.values(providers),
1132
+ });
1129
1133
  handleManifest(preferences.edgeURL, result);
1130
1134
  try {
1131
1135
  window.dispatchEvent(new CustomEvent('edgetag-initialized', {
@@ -1150,11 +1154,11 @@ const processUser = (destination, key, value, providers, options) => {
1150
1154
  saveKV(destination, {
1151
1155
  [key]: value,
1152
1156
  });
1153
- const providerPackages = getProvidersPackage(destination);
1157
+ const providerPackages = getSetting(destination, 'browserPackages');
1154
1158
  const configuredTags = getSetting(destination, 'channels');
1155
1159
  const consent = getConsent$1(destination);
1156
1160
  const userId = getUserId$1(destination);
1157
- for (const pkg of Object.values(providerPackages)) {
1161
+ for (const pkg of providerPackages) {
1158
1162
  if (!pkg || !pkg.name || !pkg.user) {
1159
1163
  continue;
1160
1164
  }
@@ -1169,7 +1173,7 @@ const processUser = (destination, key, value, providers, options) => {
1169
1173
  continue;
1170
1174
  }
1171
1175
  if (!hasUserConsent(consent, pkg.name, variable.tagName)) {
1172
- log(`User doesn't have consent for ${pkg.name} (${variable.tagName})`);
1176
+ log(`User do not have consent for ${pkg.name} (${variable.tagName})`);
1173
1177
  continue;
1174
1178
  }
1175
1179
  pkg.user({
@@ -1264,12 +1268,12 @@ const handleIsNewUser = (options) => {
1264
1268
 
1265
1269
  const processConfig = (destination, config) => {
1266
1270
  const existingConfig = getSetting(destination, 'config');
1267
- setSetting({
1271
+ setSetting(destination, {
1268
1272
  config: {
1269
1273
  ...existingConfig,
1270
1274
  ...config,
1271
1275
  },
1272
- }, destination);
1276
+ });
1273
1277
  };
1274
1278
  const handleConfig = (config, options) => {
1275
1279
  if (options === null || options === void 0 ? void 0 : options.destination) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-js",
3
- "version": "0.52.0",
3
+ "version": "0.52.2",
4
4
  "description": "JS SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",