@blotoutio/edgetag-sdk-browser 0.26.0 → 0.26.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 +151 -153
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -324,7 +324,7 @@
324
324
  referrer: getReferrer(),
325
325
  search: getSearch(),
326
326
  locale: getLocale(),
327
- sdkVersion: "0.26.0" ,
327
+ sdkVersion: "0.26.2" ,
328
328
  ...(payload || {}),
329
329
  };
330
330
  let storage = {};
@@ -432,7 +432,64 @@
432
432
  return memoryConsent;
433
433
  };
434
434
 
435
- const allowedProvider = new Set();
435
+ const isBool = (v) => typeof v == 'boolean';
436
+ const isRecord = (v) => !!v && typeof v == 'object';
437
+ /**
438
+ * This function validates user consent for a given provider and tag name.
439
+ * It should be used in conjunction with `UserConsent`, not `ProvidersConfig`.
440
+ */
441
+ const hasUserConsent = (consent, provider, tagName) => {
442
+ if (!isRecord(consent)) {
443
+ return false;
444
+ }
445
+ let allowed = isBool(consent.all) ? consent.all : false;
446
+ if (provider in consent) {
447
+ const providerSpecific = consent[provider];
448
+ if (isBool(providerSpecific)) {
449
+ allowed = providerSpecific;
450
+ }
451
+ else if (isRecord(providerSpecific)) {
452
+ if ('all' in providerSpecific && isBool(providerSpecific.all)) {
453
+ allowed = providerSpecific.all;
454
+ }
455
+ if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
456
+ allowed = providerSpecific[tagName];
457
+ }
458
+ }
459
+ }
460
+ return allowed;
461
+ };
462
+ /**
463
+ * This function validates provider allowance for a given provider and tag name.
464
+ * It should not be used to validate `UserConsent`.
465
+ */
466
+ const isProviderInstanceAllowed = (providersConfig, provider, tagName) => {
467
+ if (!isRecord(providersConfig)) {
468
+ return true;
469
+ }
470
+ let allowed = isBool(providersConfig.all) ? providersConfig.all : true;
471
+ if (provider in providersConfig) {
472
+ const providerSpecific = providersConfig[provider];
473
+ if (isBool(providerSpecific)) {
474
+ allowed = providerSpecific;
475
+ }
476
+ else if (isRecord(providerSpecific)) {
477
+ if ('all' in providerSpecific && isBool(providerSpecific.all)) {
478
+ allowed = providerSpecific.all;
479
+ }
480
+ if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
481
+ allowed = providerSpecific[tagName];
482
+ }
483
+ }
484
+ }
485
+ return allowed;
486
+ };
487
+
488
+ const upsert = (map, key, update, createDefault) => {
489
+ const currentValue = map.has(key) ? map.get(key) : createDefault();
490
+ return map.set(key, update(currentValue));
491
+ };
492
+
436
493
  let initialized = false;
437
494
  const providersPackages = {};
438
495
  const setPreferences = (preferences) => {
@@ -472,12 +529,9 @@
472
529
  const setInitialized = () => {
473
530
  initialized = true;
474
531
  };
475
- const getAllowedProviders = () => allowedProvider;
476
- const setAllowedProviders = (providers) => {
477
- for (const provider of providers) {
478
- allowedProvider.add(provider);
479
- }
480
- };
532
+ const configuredTags = new Map();
533
+ const addConfiguredTag = (pkg, tagName) => upsert(configuredTags, pkg, (names) => names.add(tagName), () => new Set());
534
+ const getConfiguredTags = () => configuredTags;
481
535
 
482
536
  const manifestVariables = {};
483
537
  const addProviderVariable = (name, variables, tagName) => {
@@ -512,59 +566,6 @@
512
566
  }
513
567
  };
514
568
 
515
- const isBool = (v) => typeof v == 'boolean';
516
- const isRecord = (v) => !!v && typeof v == 'object';
517
- /**
518
- * This function validates user consent for a given provider and tag name.
519
- * It should be used in conjunction with `UserConsent`, not `ProvidersConfig`.
520
- */
521
- const hasUserConsent = (consent, provider, tagName) => {
522
- if (!isRecord(consent)) {
523
- return false;
524
- }
525
- let allowed = isBool(consent.all) ? consent.all : false;
526
- if (provider in consent) {
527
- const providerSpecific = consent[provider];
528
- if (isBool(providerSpecific)) {
529
- allowed = providerSpecific;
530
- }
531
- else if (isRecord(providerSpecific)) {
532
- if ('all' in providerSpecific && isBool(providerSpecific.all)) {
533
- allowed = providerSpecific.all;
534
- }
535
- if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
536
- allowed = providerSpecific[tagName];
537
- }
538
- }
539
- }
540
- return allowed;
541
- };
542
- /**
543
- * This function validates provider allowance for a given provider and tag name.
544
- * It should not be used to validate `UserConsent`.
545
- */
546
- const isProviderInstanceAllowed = (providersConfig, provider, tagName) => {
547
- if (!isRecord(providersConfig)) {
548
- return true;
549
- }
550
- let allowed = isBool(providersConfig.all) ? providersConfig.all : true;
551
- if (provider in providersConfig) {
552
- const providerSpecific = providersConfig[provider];
553
- if (isBool(providerSpecific)) {
554
- allowed = providerSpecific;
555
- }
556
- else if (isRecord(providerSpecific)) {
557
- if ('all' in providerSpecific && isBool(providerSpecific.all)) {
558
- allowed = providerSpecific.all;
559
- }
560
- if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
561
- allowed = providerSpecific[tagName];
562
- }
563
- }
564
- }
565
- return allowed;
566
- };
567
-
568
569
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
569
570
  const payload = {
570
571
  eventName,
@@ -591,48 +592,43 @@
591
592
  eventId = generateEventId(eventName);
592
593
  }
593
594
  const providerPackages = getProvidersPackage();
595
+ const configuredTags = getConfiguredTags();
594
596
  const userId = handleGetUserId();
595
597
  const providerData = {};
596
598
  const consent = getConsent();
597
- const allowedProviders = getAllowedProviders();
598
- let anyProviderCalled = false;
599
- if (providerPackages) {
600
- for (const pkg of Object.values(providerPackages)) {
601
- if (!pkg || !pkg.name || !pkg.tag) {
599
+ for (const pkg of Object.values(providerPackages)) {
600
+ if (!pkg || !pkg.name || !pkg.tag) {
601
+ continue;
602
+ }
603
+ if (!configuredTags.has(pkg.name)) {
604
+ continue;
605
+ }
606
+ const variables = getProviderVariables(pkg.name);
607
+ const result = {};
608
+ const providerVariables = Object.entries(variables);
609
+ const executionContext = new Map();
610
+ for (const [tagName, variableSet] of providerVariables) {
611
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
612
+ log(`Provider instance is not allowed (${pkg.name}: ${tagName})`);
602
613
  continue;
603
614
  }
604
- if (!allowedProviders.has(pkg.name)) {
605
- log(`Provider ${pkg.name} is not in allow list`);
615
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
616
+ log(`Consent is missing (${pkg.name}: ${tagName})`);
606
617
  continue;
607
618
  }
608
- const variables = getProviderVariables(pkg.name);
609
- const result = {};
610
- const providerVariables = Object.entries(variables);
611
- const executionContext = new Map();
612
- for (const [tagName, variableSet] of providerVariables) {
613
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
614
- log(`Provider instance is not allowed (${pkg.name}: ${tagName})`);
615
- continue;
616
- }
617
- if (!hasUserConsent(consent, pkg.name, tagName)) {
618
- log(`Consent is missing (${pkg.name}: ${tagName})`);
619
- continue;
620
- }
621
- anyProviderCalled = true;
622
- result[tagName] = pkg.tag({
623
- userId,
624
- eventName,
625
- eventId,
626
- data: JSON.parse(JSON.stringify(data)),
627
- sendTag,
628
- manifestVariables: variableSet,
629
- executionContext,
630
- });
631
- }
632
- providerData[pkg.name] = result;
619
+ result[tagName] = pkg.tag({
620
+ userId,
621
+ eventName,
622
+ eventId,
623
+ data: JSON.parse(JSON.stringify(data)),
624
+ sendTag,
625
+ manifestVariables: variableSet,
626
+ executionContext,
627
+ });
633
628
  }
629
+ providerData[pkg.name] = result;
634
630
  }
635
- if (!anyProviderCalled) {
631
+ if (!hasAllowedManifestTags(configuredTags, consent, providers)) {
636
632
  return;
637
633
  }
638
634
  sendTag({
@@ -644,6 +640,17 @@
644
640
  options,
645
641
  });
646
642
  };
643
+ const hasAllowedManifestTags = (tags, consent, providersConfig) => {
644
+ for (const [pkg, tagNames] of tags) {
645
+ for (const tagName of tagNames) {
646
+ if (hasUserConsent(consent, pkg, tagName) &&
647
+ isProviderInstanceAllowed(providersConfig, pkg, tagName)) {
648
+ return true;
649
+ }
650
+ }
651
+ }
652
+ return false;
653
+ };
647
654
 
648
655
  const handleData = (data, providers, options) => {
649
656
  if (!data || Object.keys(data).length === 0) {
@@ -652,34 +659,31 @@
652
659
  }
653
660
  saveKV(data);
654
661
  const providerPackages = getProvidersPackage();
662
+ const configuredTags = getConfiguredTags();
655
663
  const userId = handleGetUserId();
656
664
  const consent = getConsent();
657
- const allowedProviders = getAllowedProviders();
658
- if (providerPackages) {
659
- for (const pkg of Object.values(providerPackages)) {
660
- if (!pkg || !pkg.user || !pkg.name) {
665
+ for (const pkg of Object.values(providerPackages)) {
666
+ if (!pkg || !pkg.user || !pkg.name) {
667
+ continue;
668
+ }
669
+ if (!configuredTags.has(pkg.name)) {
670
+ continue;
671
+ }
672
+ const variables = getProviderVariables(pkg.name);
673
+ for (const [tagName, variableSet] of Object.entries(variables)) {
674
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
675
+ log(`Data not allowed for ${pkg.name} (${tagName})`);
661
676
  continue;
662
677
  }
663
- if (!allowedProviders.has(pkg.name)) {
664
- log(`Provider ${pkg.name} is not in allow list`);
678
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
679
+ log(`Consent is missing for ${pkg.name} (${tagName})`);
665
680
  continue;
666
681
  }
667
- const variables = getProviderVariables(pkg.name);
668
- for (const [tagName, variableSet] of Object.entries(variables)) {
669
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
670
- log(`Data not allowed for ${pkg.name} (${tagName})`);
671
- continue;
672
- }
673
- if (!hasUserConsent(consent, pkg.name, tagName)) {
674
- log(`Consent is missing for ${pkg.name} (${tagName})`);
675
- continue;
676
- }
677
- pkg.user({
678
- userId,
679
- data,
680
- manifestVariables: variableSet,
681
- });
682
- }
682
+ pkg.user({
683
+ userId,
684
+ data,
685
+ manifestVariables: variableSet,
686
+ });
683
687
  }
684
688
  }
685
689
  postRequest(getDataURL(), { data, providers }, options).catch(error);
@@ -782,8 +786,8 @@
782
786
  const handleManifest = (manifest) => {
783
787
  const providerPackages = getProvidersPackage();
784
788
  const userId = handleGetUserId();
785
- setAllowedProviders(manifest.map((provider) => provider.package));
786
789
  manifest.forEach((provider) => {
790
+ addConfiguredTag(provider.package, provider.tagName);
787
791
  addProviderVariable(provider.package, provider.variables, provider.tagName);
788
792
  if (provider.rules) {
789
793
  Object.entries(provider.rules).forEach(([name, recipe]) => {
@@ -795,18 +799,16 @@
795
799
  }
796
800
  });
797
801
  }
798
- if (providerPackages) {
799
- const pkg = providerPackages[provider.package];
800
- if (pkg && pkg.name && pkg.init) {
801
- pkg.init({
802
- userId,
803
- manifest: provider,
804
- sendTag,
805
- sendEdgeData: handleData,
806
- getEdgeData: handleGetData,
807
- keyName: `${keyPrefix}Store`,
808
- });
809
- }
802
+ const pkg = providerPackages[provider.package];
803
+ if (pkg && pkg.name && pkg.init) {
804
+ pkg.init({
805
+ userId,
806
+ manifest: provider,
807
+ sendTag,
808
+ sendEdgeData: handleData,
809
+ getEdgeData: handleGetData,
810
+ keyName: `${keyPrefix}Store`,
811
+ });
810
812
  }
811
813
  });
812
814
  setInitialized();
@@ -856,36 +858,32 @@
856
858
  saveKV({
857
859
  [key]: value,
858
860
  });
859
- const consent = getConsent();
860
- const allowedProviders = getAllowedProviders();
861
861
  const providerPackages = getProvidersPackage();
862
+ const configuredTags = getConfiguredTags();
863
+ const consent = getConsent();
862
864
  const userId = handleGetUserId();
863
- if (providerPackages) {
864
- for (const pkg of Object.values(providerPackages)) {
865
- if (!pkg || !pkg.name || !pkg.user) {
865
+ for (const pkg of Object.values(providerPackages)) {
866
+ if (!pkg || !pkg.name || !pkg.user) {
867
+ continue;
868
+ }
869
+ if (!configuredTags.has(pkg.name)) {
870
+ continue;
871
+ }
872
+ const variables = getProviderVariables(pkg.name);
873
+ for (const [tagName, variableSet] of Object.entries(variables)) {
874
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
875
+ log(`User not allowed for ${pkg.name} (${tagName})`);
866
876
  continue;
867
877
  }
868
- if (!allowedProviders.has(pkg.name)) {
869
- log(`Provider ${pkg.name} is not in allow list`);
878
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
879
+ log(`User doesn't have consent for ${pkg.name} (${tagName})`);
870
880
  continue;
871
881
  }
872
- const variables = getProviderVariables(pkg.name);
873
- for (const [tagName, variableSet] of Object.entries(variables)) {
874
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
875
- // should we be logging this?
876
- log(`User not allowed for ${pkg.name} (${tagName})`);
877
- continue;
878
- }
879
- if (!hasUserConsent(consent, pkg.name, tagName)) {
880
- log(`User doesn't have consent for ${pkg.name} (${tagName})`);
881
- continue;
882
- }
883
- pkg.user({
884
- userId,
885
- data: { [key]: value },
886
- manifestVariables: variableSet,
887
- });
888
- }
882
+ pkg.user({
883
+ userId,
884
+ data: { [key]: value },
885
+ manifestVariables: variableSet,
886
+ });
889
887
  }
890
888
  }
891
889
  postRequest(getUserURL(), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.26.0",
3
+ "version": "0.26.2",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",