@blotoutio/edgetag-sdk-browser 0.26.1 → 0.26.3

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 +166 -151
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -90,7 +90,18 @@
90
90
  return error;
91
91
  }
92
92
  };
93
+ const allowLog = () => {
94
+ try {
95
+ return localStorage.getItem('edgeTagDebug') === '1';
96
+ }
97
+ catch {
98
+ return false;
99
+ }
100
+ };
93
101
  const log = (data) => {
102
+ if (!allowLog()) {
103
+ return;
104
+ }
94
105
  console.log('[EdgeTag]', getMessage(data));
95
106
  };
96
107
  const error = (data) => {
@@ -324,7 +335,7 @@
324
335
  referrer: getReferrer(),
325
336
  search: getSearch(),
326
337
  locale: getLocale(),
327
- sdkVersion: "0.26.1" ,
338
+ sdkVersion: "0.26.3" ,
328
339
  ...(payload || {}),
329
340
  };
330
341
  let storage = {};
@@ -432,7 +443,64 @@
432
443
  return memoryConsent;
433
444
  };
434
445
 
435
- const allowedProvider = new Set();
446
+ const isBool = (v) => typeof v == 'boolean';
447
+ const isRecord = (v) => !!v && typeof v == 'object';
448
+ /**
449
+ * This function validates user consent for a given provider and tag name.
450
+ * It should be used in conjunction with `UserConsent`, not `ProvidersConfig`.
451
+ */
452
+ const hasUserConsent = (consent, provider, tagName) => {
453
+ if (!isRecord(consent)) {
454
+ return false;
455
+ }
456
+ let allowed = isBool(consent.all) ? consent.all : false;
457
+ if (provider in consent) {
458
+ const providerSpecific = consent[provider];
459
+ if (isBool(providerSpecific)) {
460
+ allowed = providerSpecific;
461
+ }
462
+ else if (isRecord(providerSpecific)) {
463
+ if ('all' in providerSpecific && isBool(providerSpecific.all)) {
464
+ allowed = providerSpecific.all;
465
+ }
466
+ if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
467
+ allowed = providerSpecific[tagName];
468
+ }
469
+ }
470
+ }
471
+ return allowed;
472
+ };
473
+ /**
474
+ * This function validates provider allowance for a given provider and tag name.
475
+ * It should not be used to validate `UserConsent`.
476
+ */
477
+ const isProviderInstanceAllowed = (providersConfig, provider, tagName) => {
478
+ if (!isRecord(providersConfig)) {
479
+ return true;
480
+ }
481
+ let allowed = isBool(providersConfig.all) ? providersConfig.all : true;
482
+ if (provider in providersConfig) {
483
+ const providerSpecific = providersConfig[provider];
484
+ if (isBool(providerSpecific)) {
485
+ allowed = providerSpecific;
486
+ }
487
+ else if (isRecord(providerSpecific)) {
488
+ if ('all' in providerSpecific && isBool(providerSpecific.all)) {
489
+ allowed = providerSpecific.all;
490
+ }
491
+ if (tagName in providerSpecific && isBool(providerSpecific[tagName])) {
492
+ allowed = providerSpecific[tagName];
493
+ }
494
+ }
495
+ }
496
+ return allowed;
497
+ };
498
+
499
+ const upsert = (map, key, update, createDefault) => {
500
+ const currentValue = map.has(key) ? map.get(key) : createDefault();
501
+ return map.set(key, update(currentValue));
502
+ };
503
+
436
504
  let initialized = false;
437
505
  const providersPackages = {};
438
506
  const setPreferences = (preferences) => {
@@ -472,12 +540,9 @@
472
540
  const setInitialized = () => {
473
541
  initialized = true;
474
542
  };
475
- const getAllowedProviders = () => allowedProvider;
476
- const setAllowedProviders = (providers) => {
477
- for (const provider of providers) {
478
- allowedProvider.add(provider);
479
- }
480
- };
543
+ const configuredTags = new Map();
544
+ const addConfiguredTag = (pkg, tagName) => upsert(configuredTags, pkg, (names) => names.add(tagName), () => new Set());
545
+ const getConfiguredTags = () => configuredTags;
481
546
 
482
547
  const manifestVariables = {};
483
548
  const addProviderVariable = (name, variables, tagName) => {
@@ -512,59 +577,6 @@
512
577
  }
513
578
  };
514
579
 
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
580
  const sendTag = ({ eventName, eventId, data, providerData, providers, options, }) => {
569
581
  const payload = {
570
582
  eventName,
@@ -591,47 +603,44 @@
591
603
  eventId = generateEventId(eventName);
592
604
  }
593
605
  const providerPackages = getProvidersPackage();
606
+ const configuredTags = getConfiguredTags();
594
607
  const userId = handleGetUserId();
595
608
  const providerData = {};
596
609
  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) {
610
+ for (const pkg of Object.values(providerPackages)) {
611
+ if (!pkg || !pkg.name || !pkg.tag) {
612
+ continue;
613
+ }
614
+ if (!configuredTags.has(pkg.name)) {
615
+ log(`Provider ${pkg.name} is not in allow list`);
616
+ continue;
617
+ }
618
+ const variables = getProviderVariables(pkg.name);
619
+ const result = {};
620
+ const providerVariables = Object.entries(variables);
621
+ const executionContext = new Map();
622
+ for (const [tagName, variableSet] of providerVariables) {
623
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
624
+ log(`Provider instance is not allowed (${pkg.name}: ${tagName})`);
602
625
  continue;
603
626
  }
604
- if (!allowedProviders.has(pkg.name)) {
627
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
628
+ log(`Consent is missing (${pkg.name}: ${tagName})`);
605
629
  continue;
606
630
  }
607
- const variables = getProviderVariables(pkg.name);
608
- const result = {};
609
- const providerVariables = Object.entries(variables);
610
- const executionContext = new Map();
611
- for (const [tagName, variableSet] of providerVariables) {
612
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
613
- log(`Provider instance is not allowed (${pkg.name}: ${tagName})`);
614
- continue;
615
- }
616
- if (!hasUserConsent(consent, pkg.name, tagName)) {
617
- log(`Consent is missing (${pkg.name}: ${tagName})`);
618
- continue;
619
- }
620
- anyProviderCalled = true;
621
- result[tagName] = pkg.tag({
622
- userId,
623
- eventName,
624
- eventId,
625
- data: JSON.parse(JSON.stringify(data)),
626
- sendTag,
627
- manifestVariables: variableSet,
628
- executionContext,
629
- });
630
- }
631
- providerData[pkg.name] = result;
631
+ result[tagName] = pkg.tag({
632
+ userId,
633
+ eventName,
634
+ eventId,
635
+ data: JSON.parse(JSON.stringify(data)),
636
+ sendTag,
637
+ manifestVariables: variableSet,
638
+ executionContext,
639
+ });
632
640
  }
641
+ providerData[pkg.name] = result;
633
642
  }
634
- if (!anyProviderCalled) {
643
+ if (!hasAllowedManifestTags(configuredTags, consent, providers)) {
635
644
  return;
636
645
  }
637
646
  sendTag({
@@ -643,6 +652,17 @@
643
652
  options,
644
653
  });
645
654
  };
655
+ const hasAllowedManifestTags = (tags, consent, providersConfig) => {
656
+ for (const [pkg, tagNames] of tags) {
657
+ for (const tagName of tagNames) {
658
+ if (hasUserConsent(consent, pkg, tagName) &&
659
+ isProviderInstanceAllowed(providersConfig, pkg, tagName)) {
660
+ return true;
661
+ }
662
+ }
663
+ }
664
+ return false;
665
+ };
646
666
 
647
667
  const handleData = (data, providers, options) => {
648
668
  if (!data || Object.keys(data).length === 0) {
@@ -651,33 +671,32 @@
651
671
  }
652
672
  saveKV(data);
653
673
  const providerPackages = getProvidersPackage();
674
+ const configuredTags = getConfiguredTags();
654
675
  const userId = handleGetUserId();
655
676
  const consent = getConsent();
656
- const allowedProviders = getAllowedProviders();
657
- if (providerPackages) {
658
- for (const pkg of Object.values(providerPackages)) {
659
- if (!pkg || !pkg.user || !pkg.name) {
677
+ for (const pkg of Object.values(providerPackages)) {
678
+ if (!pkg || !pkg.user || !pkg.name) {
679
+ continue;
680
+ }
681
+ if (!configuredTags.has(pkg.name)) {
682
+ log(`Provider ${pkg.name} is not in allow list`);
683
+ continue;
684
+ }
685
+ const variables = getProviderVariables(pkg.name);
686
+ for (const [tagName, variableSet] of Object.entries(variables)) {
687
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
688
+ log(`Data not allowed for ${pkg.name} (${tagName})`);
660
689
  continue;
661
690
  }
662
- if (!allowedProviders.has(pkg.name)) {
691
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
692
+ log(`Consent is missing for ${pkg.name} (${tagName})`);
663
693
  continue;
664
694
  }
665
- const variables = getProviderVariables(pkg.name);
666
- for (const [tagName, variableSet] of Object.entries(variables)) {
667
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
668
- log(`Data not allowed for ${pkg.name} (${tagName})`);
669
- continue;
670
- }
671
- if (!hasUserConsent(consent, pkg.name, tagName)) {
672
- log(`Consent is missing for ${pkg.name} (${tagName})`);
673
- continue;
674
- }
675
- pkg.user({
676
- userId,
677
- data,
678
- manifestVariables: variableSet,
679
- });
680
- }
695
+ pkg.user({
696
+ userId,
697
+ data,
698
+ manifestVariables: variableSet,
699
+ });
681
700
  }
682
701
  }
683
702
  postRequest(getDataURL(), { data, providers }, options).catch(error);
@@ -780,8 +799,8 @@
780
799
  const handleManifest = (manifest) => {
781
800
  const providerPackages = getProvidersPackage();
782
801
  const userId = handleGetUserId();
783
- setAllowedProviders(manifest.map((provider) => provider.package));
784
802
  manifest.forEach((provider) => {
803
+ addConfiguredTag(provider.package, provider.tagName);
785
804
  addProviderVariable(provider.package, provider.variables, provider.tagName);
786
805
  if (provider.rules) {
787
806
  Object.entries(provider.rules).forEach(([name, recipe]) => {
@@ -793,18 +812,16 @@
793
812
  }
794
813
  });
795
814
  }
796
- if (providerPackages) {
797
- const pkg = providerPackages[provider.package];
798
- if (pkg && pkg.name && pkg.init) {
799
- pkg.init({
800
- userId,
801
- manifest: provider,
802
- sendTag,
803
- sendEdgeData: handleData,
804
- getEdgeData: handleGetData,
805
- keyName: `${keyPrefix}Store`,
806
- });
807
- }
815
+ const pkg = providerPackages[provider.package];
816
+ if (pkg && pkg.name && pkg.init) {
817
+ pkg.init({
818
+ userId,
819
+ manifest: provider,
820
+ sendTag,
821
+ sendEdgeData: handleData,
822
+ getEdgeData: handleGetData,
823
+ keyName: `${keyPrefix}Store`,
824
+ });
808
825
  }
809
826
  });
810
827
  setInitialized();
@@ -831,7 +848,7 @@
831
848
  getRequest(url.href)
832
849
  .then((result) => {
833
850
  if (!result) {
834
- log('Initialization failed');
851
+ error('Initialization failed');
835
852
  return;
836
853
  }
837
854
  if (result.userId) {
@@ -854,35 +871,33 @@
854
871
  saveKV({
855
872
  [key]: value,
856
873
  });
857
- const consent = getConsent();
858
- const allowedProviders = getAllowedProviders();
859
874
  const providerPackages = getProvidersPackage();
875
+ const configuredTags = getConfiguredTags();
876
+ const consent = getConsent();
860
877
  const userId = handleGetUserId();
861
- if (providerPackages) {
862
- for (const pkg of Object.values(providerPackages)) {
863
- if (!pkg || !pkg.name || !pkg.user) {
878
+ for (const pkg of Object.values(providerPackages)) {
879
+ if (!pkg || !pkg.name || !pkg.user) {
880
+ continue;
881
+ }
882
+ if (!configuredTags.has(pkg.name)) {
883
+ log(`Provider ${pkg.name} is not in allow list`);
884
+ continue;
885
+ }
886
+ const variables = getProviderVariables(pkg.name);
887
+ for (const [tagName, variableSet] of Object.entries(variables)) {
888
+ if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
889
+ log(`User not allowed for ${pkg.name} (${tagName})`);
864
890
  continue;
865
891
  }
866
- if (!allowedProviders.has(pkg.name)) {
892
+ if (!hasUserConsent(consent, pkg.name, tagName)) {
893
+ log(`User doesn't have consent for ${pkg.name} (${tagName})`);
867
894
  continue;
868
895
  }
869
- const variables = getProviderVariables(pkg.name);
870
- for (const [tagName, variableSet] of Object.entries(variables)) {
871
- if (!isProviderInstanceAllowed(providers, pkg.name, tagName)) {
872
- // should we be logging this?
873
- log(`User not allowed for ${pkg.name} (${tagName})`);
874
- continue;
875
- }
876
- if (!hasUserConsent(consent, pkg.name, tagName)) {
877
- log(`User doesn't have consent for ${pkg.name} (${tagName})`);
878
- continue;
879
- }
880
- pkg.user({
881
- userId,
882
- data: { [key]: value },
883
- manifestVariables: variableSet,
884
- });
885
- }
896
+ pkg.user({
897
+ userId,
898
+ data: { [key]: value },
899
+ manifestVariables: variableSet,
900
+ });
886
901
  }
887
902
  }
888
903
  postRequest(getUserURL(), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blotoutio/edgetag-sdk-browser",
3
- "version": "0.26.1",
3
+ "version": "0.26.3",
4
4
  "description": "Browser SDK for EdgeTag",
5
5
  "author": "Blotout",
6
6
  "license": "MIT",