@chordcommerce/analytics 1.12.1 → 1.12.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.
@@ -21,9 +21,21 @@ export const addCdpSnippet = (options) => __awaiter(void 0, void 0, void 0, func
21
21
  s.setAttribute('data-init-only', 'true');
22
22
  s.setAttribute('data-namespace', `_${options.namespace}`);
23
23
  s.setAttribute('data-write-key', options.cdpWriteKey);
24
- if (options.consent)
25
- yield addCdpConsent(options).init();
26
- document.head.appendChild(s);
24
+ if (options.consent) {
25
+ // If awaitConsent is true, wait for consent before loading CDP
26
+ if (options.awaitConsent) {
27
+ yield addCdpConsent(options).init();
28
+ document.head.appendChild(s);
29
+ }
30
+ else {
31
+ document.head.appendChild(s);
32
+ // If not awaiting consent, initialize in the background
33
+ addCdpConsent(options).init().catch(console.error);
34
+ }
35
+ }
36
+ else {
37
+ document.head.appendChild(s);
38
+ }
27
39
  });
28
40
  /*
29
41
  * Returns the CDP queue to hold events ahead of CDP initialization.
@@ -11,7 +11,9 @@ import { ketchConsentAdapter } from './ketch.js';
11
11
  import { onetrustConsentAdapter } from './onetrust.js';
12
12
  import { shopifyConsentAdapter } from './shopify.js';
13
13
  export const addCdpConsent = (options) => {
14
- let adapter;
14
+ let adapter = null;
15
+ const POLLING_INTERVAL_MS = 50;
16
+ const TIMEOUT_MS = 60000;
15
17
  /**
16
18
  * Returns the adapter for the specified consent management platform.
17
19
  */
@@ -29,10 +31,10 @@ export const addCdpConsent = (options) => {
29
31
  }
30
32
  };
31
33
  /**
32
- * Returns whether the CDP should be loaded based on the current consent.
33
- * Resolves to true when consent is granted, or false after a 10-second timeout.
34
+ * Polls if user can be tracked based on consent
35
+ * @returns Promise that resolves to true if condition is met within timeout, false otherwise
34
36
  */
35
- const shouldLoadCdp = () => __awaiter(void 0, void 0, void 0, function* () {
37
+ const checkUserCanBeTracked = () => __awaiter(void 0, void 0, void 0, function* () {
36
38
  return new Promise((resolve) => {
37
39
  // Checking immediately first
38
40
  if (adapter === null || adapter === void 0 ? void 0 : adapter.userCanBeTracked()) {
@@ -44,11 +46,11 @@ export const addCdpConsent = (options) => {
44
46
  clearInterval(interval);
45
47
  resolve(true);
46
48
  }
47
- }, 50);
49
+ }, POLLING_INTERVAL_MS);
48
50
  setTimeout(() => {
49
51
  clearInterval(interval);
50
52
  resolve(false);
51
- }, 10000);
53
+ }, TIMEOUT_MS);
52
54
  });
53
55
  });
54
56
  /**
@@ -65,16 +67,40 @@ export const addCdpConsent = (options) => {
65
67
  }
66
68
  };
67
69
  /**
68
- * Adds consent management by setting defaults and handling changes.
70
+ * Sets up consent management by initializing the adapter and handling consent updates.
71
+ */
72
+ const setupConsentManagement = () => __awaiter(void 0, void 0, void 0, function* () {
73
+ if (!adapter)
74
+ return;
75
+ const isReady = yield checkUserCanBeTracked();
76
+ if (!isReady)
77
+ return;
78
+ const initialConsent = adapter.getCurrentConsent();
79
+ updateCdpConsent(initialConsent);
80
+ adapter.handleConsentUpdates((updatedConsent) => {
81
+ updateCdpConsent(updatedConsent);
82
+ });
83
+ });
84
+ /**
85
+ * Initializes consent management.
86
+ * Returns a promise that resolves to true if CDP should be loaded, false otherwise.
69
87
  */
70
88
  const init = () => __awaiter(void 0, void 0, void 0, function* () {
71
89
  adapter = getAdapter();
72
- if (adapter === null)
73
- return Promise.resolve(false);
74
- yield shouldLoadCdp();
75
- updateCdpConsent(adapter === null || adapter === void 0 ? void 0 : adapter.getCurrentConsent());
76
- adapter === null || adapter === void 0 ? void 0 : adapter.handleConsentUpdates(updateCdpConsent);
77
- return Promise.resolve(true);
90
+ if (adapter === null) {
91
+ console.error(`Consent management adapter "${options.consent}" is not supported.`);
92
+ return false;
93
+ }
94
+ if (options.awaitConsent) {
95
+ const canBeTracked = yield checkUserCanBeTracked();
96
+ if (!canBeTracked)
97
+ return false;
98
+ }
99
+ // Set up consent management in the background
100
+ setupConsentManagement().catch((error) => {
101
+ console.error('Error setting up consent management:', error);
102
+ });
103
+ return true;
78
104
  });
79
105
  return {
80
106
  init,
@@ -609,6 +609,7 @@ export class ChordAnalytics {
609
609
  enableLogging: debug,
610
610
  namespace: 'chord',
611
611
  stripNull: true,
612
+ awaitConsent: true,
612
613
  };
613
614
  this.options = Object.assign(Object.assign({}, defaults), options);
614
615
  if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.cdpDomain) && ((_c = this.options) === null || _c === void 0 ? void 0 : _c.cdpWriteKey) && window) {
@@ -31,6 +31,7 @@ export interface ChordAnalyticsOptions<T extends ObjectTypes = ObjectTypes> {
31
31
  cdpDomain?: string;
32
32
  cdpWriteKey?: string;
33
33
  consent?: 'ketch' | 'onetrust' | 'shopify';
34
+ awaitConsent?: boolean;
34
35
  debug?: boolean;
35
36
  enableLogging?: boolean;
36
37
  formatters: ChordAnalyticsFormatters<T>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chordcommerce/analytics",
3
3
  "description": "Chord Commerce event tracking",
4
- "version": "1.12.1",
4
+ "version": "1.12.2",
5
5
  "author": "Chord Commerce <help@chord.co> (https://www.chord.co)",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",