@blotoutio/edgetag-sdk-browser 0.35.3 → 0.36.1

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 +124 -59
  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.3" ,
339
+ sdkVersion: "0.36.1" ,
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,20 +837,20 @@
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
  }
@@ -798,59 +869,53 @@
798
869
  }
799
870
  saveDataPerKey(persistType, provider, data, key);
800
871
  };
801
- const handleCapture = (provider, params) => {
872
+ const handleCapture = (provider, params, capture) => {
802
873
  params.forEach((param) => {
803
874
  switch (param.type) {
804
875
  case 'query': {
805
- handleCaptureQuery(provider, param.key, param.persist);
876
+ handleCaptureQuery(provider, param.key, param.persist, capture === null || capture === void 0 ? void 0 : capture.bind(null, param));
806
877
  break;
807
878
  }
808
879
  case 'storage': {
809
- 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));
810
881
  break;
811
882
  }
812
883
  }
813
884
  });
814
885
  };
815
886
 
816
- const handleGetData = (keys, callback) => {
817
- if (!keys || keys.length === 0) {
818
- error('Provide keys for get data API.');
819
- return;
820
- }
821
- getRequest(getGetDataURL(keys))
822
- .then((result) => {
823
- callback((result === null || result === void 0 ? void 0 : result.result) || {});
824
- })
825
- .catch(error);
826
- };
827
-
828
- const handleManifest = (manifest) => {
887
+ const handleManifest = (manifest, consent) => {
829
888
  const providerPackages = getProvidersPackage();
830
889
  const userId = handleGetUserId();
890
+ const executionContext = new Map();
831
891
  manifest.forEach((provider) => {
832
892
  addConfiguredTag(provider.package, provider.tagName);
833
893
  addProviderVariable(provider.package, provider.variables, provider.tagName);
894
+ const pkg = providerPackages[provider.package];
834
895
  if (provider.rules) {
835
896
  Object.entries(provider.rules).forEach(([name, recipe]) => {
836
897
  switch (name) {
837
898
  case 'capture': {
838
- handleCapture(provider.package, recipe);
899
+ handleCapture(provider.package, recipe, pkg === null || pkg === void 0 ? void 0 : pkg.capture);
839
900
  return;
840
901
  }
841
902
  }
842
903
  });
843
904
  }
844
- const pkg = providerPackages[provider.package];
845
905
  if (pkg && pkg.name && pkg.init) {
846
- pkg.init({
847
- userId,
848
- manifest: provider,
849
- sendTag,
850
- sendEdgeData: handleData,
851
- getEdgeData: handleGetData,
852
- keyName: `${keyPrefix}Store`,
853
- });
906
+ /* this defines if the consent is given for a specific instance of a provider */
907
+ const hasConsent = hasUserConsent(consent, pkg.name, provider.tagName);
908
+ if (hasConsent) {
909
+ pkg.init({
910
+ userId,
911
+ manifest: provider,
912
+ sendTag,
913
+ sendEdgeData: handleData,
914
+ getEdgeData: handleGetData,
915
+ keyName: `${keyPrefix}Store`,
916
+ executionContext,
917
+ });
918
+ }
854
919
  }
855
920
  });
856
921
  setInitialized();
@@ -887,7 +952,7 @@
887
952
  if (result.consent && !consent) {
888
953
  saveConsent(result.consent);
889
954
  }
890
- handleManifest(result.result);
955
+ handleManifest(result.result, consent || result.consent);
891
956
  })
892
957
  .catch(error);
893
958
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.35.3",
3
+ "version": "0.36.1",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",