@blotoutio/edgetag-sdk-browser 0.35.2 → 0.36.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 +118 -54
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -336,7 +336,7 @@
336
336
  referrer: getReferrer(),
337
337
  search: getSearch(),
338
338
  locale: getLocale(),
339
- sdkVersion: "0.35.2" ,
339
+ sdkVersion: "0.36.0" ,
340
340
  ...(payload || {}),
341
341
  };
342
342
  let storage = {};
@@ -423,31 +423,6 @@
423
423
  return generateUrl(`/keys`);
424
424
  };
425
425
 
426
- let memoryConsent;
427
- const saveConsent = (consent) => {
428
- setConsent(consent);
429
- savePerKey('local', tagStorage, consent, consentKey);
430
- };
431
- const handleConsent = (consent, options) => {
432
- const payload = {
433
- consentString: consent,
434
- };
435
- saveConsent(consent);
436
- if (!(options === null || options === void 0 ? void 0 : options.localSave)) {
437
- postRequest(getConsentURL(), payload).catch(error);
438
- }
439
- };
440
- const setConsent = (newConsent) => {
441
- memoryConsent = newConsent;
442
- };
443
- const getConsent$1 = () => {
444
- const storageConsent = getDataPerKey('local', tagStorage, consentKey);
445
- if (storageConsent) {
446
- return storageConsent;
447
- }
448
- return memoryConsent;
449
- };
450
-
451
426
  const isBool = (v) => typeof v == 'boolean';
452
427
  const isRecord = (v) => !!v && typeof v == 'object' && !Array.isArray(v);
453
428
  /**
@@ -475,6 +450,25 @@
475
450
  }
476
451
  return allowed;
477
452
  };
453
+ /**
454
+ * This function validates user consent for a given provider type, not based on tagName.
455
+ */
456
+ const hasUserConsentForProvider = (consent, provider) => {
457
+ if (!isRecord(consent)) {
458
+ return false;
459
+ }
460
+ let allowed = isBool(consent.all) ? consent.all : false;
461
+ if (provider in consent) {
462
+ const providerSpecific = consent[provider];
463
+ if (isBool(providerSpecific)) {
464
+ allowed = providerSpecific;
465
+ }
466
+ else if (isRecord(providerSpecific)) {
467
+ return Object.keys(providerSpecific).some((instance) => providerSpecific[instance] === true);
468
+ }
469
+ }
470
+ return allowed;
471
+ };
478
472
  /**
479
473
  * This function validates provider allowance for a given provider and tag name.
480
474
  * It should not be used to validate `UserConsent`.
@@ -674,6 +668,18 @@
674
668
  return false;
675
669
  };
676
670
 
671
+ const handleGetData = (keys, callback) => {
672
+ if (!keys || keys.length === 0) {
673
+ error('Provide keys for get data API.');
674
+ return;
675
+ }
676
+ getRequest(getGetDataURL(keys))
677
+ .then((result) => {
678
+ callback((result === null || result === void 0 ? void 0 : result.result) || {});
679
+ })
680
+ .catch(error);
681
+ };
682
+
677
683
  const handleData = (data, providers, options) => {
678
684
  if (!data || Object.keys(data).length === 0) {
679
685
  error('Provide data for data API.');
@@ -712,7 +718,72 @@
712
718
  postRequest(getDataURL(), { data, providers }, options).catch(error);
713
719
  };
714
720
 
721
+ let memoryConsent;
722
+ const saveConsent = (consent) => {
723
+ setConsent(consent);
724
+ savePerKey('local', tagStorage, consent, consentKey);
725
+ };
726
+ const handleConsent = (consent, options) => {
727
+ const payload = {
728
+ consentString: consent,
729
+ };
730
+ saveConsent(consent);
731
+ if (!(options === null || options === void 0 ? void 0 : options.localSave)) {
732
+ postRequest(getConsentURL(), payload).catch(error);
733
+ }
734
+ const userId = handleGetUserId();
735
+ const providerPackages = getProvidersPackage();
736
+ const executionContext = new Map();
737
+ /* Calling Init for all provider instances based on consent check */
738
+ for (const pkg of Object.values(providerPackages)) {
739
+ if (!pkg || !pkg.name || !pkg.init) {
740
+ continue;
741
+ }
742
+ const variables = getProviderVariables(pkg.name);
743
+ const providerVariables = Object.entries(variables);
744
+ for (const [tagName, variablesSet] of providerVariables) {
745
+ const hasConsent = hasUserConsent(consent, pkg.name, tagName);
746
+ if (!hasConsent) {
747
+ continue;
748
+ }
749
+ pkg.init({
750
+ userId,
751
+ manifest: {
752
+ tagName,
753
+ variables: variablesSet,
754
+ package: pkg.name,
755
+ },
756
+ sendTag,
757
+ sendEdgeData: handleData,
758
+ getEdgeData: handleGetData,
759
+ keyName: `${keyPrefix}Store`,
760
+ executionContext,
761
+ });
762
+ }
763
+ }
764
+ /* Calling Consent for all providers, not on instance level */
765
+ for (const pkg of Object.values(providerPackages)) {
766
+ if (!pkg || !pkg.name || !pkg.consent) {
767
+ continue;
768
+ }
769
+ /* Returns True if any one instance of given provider has consent */
770
+ const hasConsent = hasUserConsentForProvider(consent, pkg.name);
771
+ pkg.consent({ hasConsent });
772
+ }
773
+ };
774
+ const setConsent = (newConsent) => {
775
+ memoryConsent = newConsent;
776
+ };
777
+ const getConsent$1 = () => {
778
+ const storageConsent = getDataPerKey('local', tagStorage, consentKey);
779
+ if (storageConsent) {
780
+ return storageConsent;
781
+ }
782
+ return memoryConsent;
783
+ };
784
+
715
785
  const cacheKey = `${keyPrefix}Cache`;
786
+ const identity = (v) => v;
716
787
  const saveDataToEdge = (key, value, provider) => {
717
788
  if (!value) {
718
789
  return;
@@ -728,7 +799,7 @@
728
799
  }
729
800
  handleData({ [`${provider}::${key}`]: value });
730
801
  };
731
- const handleCaptureQuery = (provider, key, persistType) => {
802
+ const handleCaptureQuery = (provider, key, persistType, map) => {
732
803
  try {
733
804
  if (!window) {
734
805
  return;
@@ -741,7 +812,7 @@
741
812
  if (!params || !params.get(key)) {
742
813
  return;
743
814
  }
744
- const data = params.get(key);
815
+ const data = (map !== null && map !== void 0 ? map : identity)(params.get(key));
745
816
  if (!data) {
746
817
  return;
747
818
  }
@@ -766,82 +837,74 @@
766
837
  }
767
838
  saveData(persistType === 'edge' ? 'local' : persistType, cache, cacheKey);
768
839
  };
769
- const handleCaptureStorage = (provider, key, persistType, location) => {
840
+ const handleCaptureStorage = (provider, key, persistType, location, map) => {
770
841
  let data;
771
842
  try {
772
843
  switch (location) {
773
844
  case 'cookie': {
774
- data = getCookieValue(key);
845
+ data = (map !== null && map !== void 0 ? map : identity)(getCookieValue(key));
775
846
  break;
776
847
  }
777
848
  case 'local': {
778
- data = localStorage.getItem(key);
849
+ data = (map !== null && map !== void 0 ? map : identity)(localStorage.getItem(key));
779
850
  break;
780
851
  }
781
852
  case 'session': {
782
- data = sessionStorage.getItem(key);
853
+ data = (map !== null && map !== void 0 ? map : identity)(sessionStorage.getItem(key));
783
854
  }
784
855
  }
785
856
  }
786
857
  catch {
787
858
  return;
788
859
  }
789
- const cachedValue = getFromCache(persistType, provider, key);
790
- saveToCache(persistType, provider, key, data);
791
860
  if (!data) {
792
861
  return;
793
862
  }
863
+ const cachedKey = `${handleGetUserId()}/${key}`;
864
+ const cachedValue = getFromCache(persistType, provider, cachedKey);
794
865
  if (persistType === 'edge' && cachedValue !== data) {
795
866
  saveDataToEdge(key, data, provider);
867
+ saveToCache(persistType, provider, cachedKey, data);
796
868
  return;
797
869
  }
798
870
  saveDataPerKey(persistType, provider, data, key);
799
871
  };
800
- const handleCapture = (provider, params) => {
872
+ const handleCapture = (provider, params, capture) => {
801
873
  params.forEach((param) => {
802
874
  switch (param.type) {
803
875
  case 'query': {
804
- handleCaptureQuery(provider, param.key, param.persist);
876
+ handleCaptureQuery(provider, param.key, param.persist, capture === null || capture === void 0 ? void 0 : capture.bind(null, param));
805
877
  break;
806
878
  }
807
879
  case 'storage': {
808
- handleCaptureStorage(provider, param.key, param.persist, param.location);
880
+ handleCaptureStorage(provider, param.key, param.persist, param.location, capture === null || capture === void 0 ? void 0 : capture.bind(null, param));
809
881
  break;
810
882
  }
811
883
  }
812
884
  });
813
885
  };
814
886
 
815
- const handleGetData = (keys, callback) => {
816
- if (!keys || keys.length === 0) {
817
- error('Provide keys for get data API.');
818
- return;
819
- }
820
- getRequest(getGetDataURL(keys))
821
- .then((result) => {
822
- callback((result === null || result === void 0 ? void 0 : result.result) || {});
823
- })
824
- .catch(error);
825
- };
826
-
827
- const handleManifest = (manifest) => {
887
+ const handleManifest = (manifest, consent) => {
828
888
  const providerPackages = getProvidersPackage();
829
889
  const userId = handleGetUserId();
890
+ const executionContext = new Map();
830
891
  manifest.forEach((provider) => {
831
892
  addConfiguredTag(provider.package, provider.tagName);
832
893
  addProviderVariable(provider.package, provider.variables, provider.tagName);
894
+ const pkg = providerPackages[provider.package];
833
895
  if (provider.rules) {
834
896
  Object.entries(provider.rules).forEach(([name, recipe]) => {
835
897
  switch (name) {
836
898
  case 'capture': {
837
- handleCapture(provider.package, recipe);
899
+ handleCapture(provider.package, recipe, pkg === null || pkg === void 0 ? void 0 : pkg.capture);
838
900
  return;
839
901
  }
840
902
  }
841
903
  });
842
904
  }
843
- const pkg = providerPackages[provider.package];
844
- if (pkg && pkg.name && pkg.init) {
905
+ /* this defines if the consent is given for a specific instance of a provider */
906
+ const hasConsent = hasUserConsent(consent, pkg.name, provider.tagName);
907
+ if (pkg && pkg.name && pkg.init && hasConsent) {
845
908
  pkg.init({
846
909
  userId,
847
910
  manifest: provider,
@@ -849,6 +912,7 @@
849
912
  sendEdgeData: handleData,
850
913
  getEdgeData: handleGetData,
851
914
  keyName: `${keyPrefix}Store`,
915
+ executionContext,
852
916
  });
853
917
  }
854
918
  });
@@ -886,7 +950,7 @@
886
950
  if (result.consent && !consent) {
887
951
  saveConsent(result.consent);
888
952
  }
889
- handleManifest(result.result);
953
+ handleManifest(result.result, consent || result.consent);
890
954
  })
891
955
  .catch(error);
892
956
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.35.2",
3
+ "version": "0.36.0",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",