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