@chordcommerce/analytics 1.13.0 → 1.14.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.
@@ -12,6 +12,7 @@ import { onetrustConsentAdapter } from './onetrust.js';
12
12
  import { shopifyConsentAdapter } from './shopify.js';
13
13
  export const addCdpConsent = (options) => {
14
14
  let adapter = null;
15
+ let lastProcessedConsent = null;
15
16
  const POLLING_INTERVAL_MS = 50;
16
17
  const TIMEOUT_MS = 60000;
17
18
  /**
@@ -56,8 +57,22 @@ export const addCdpConsent = (options) => {
56
57
  /**
57
58
  * Updates consent for all future events.
58
59
  */
59
- const updateCdpConsent = (updatedConsent) => {
60
+ const updateCdpConsent = (updatedConsent) => __awaiter(void 0, void 0, void 0, function* () {
61
+ if (lastProcessedConsent &&
62
+ JSON.stringify(lastProcessedConsent) === JSON.stringify(updatedConsent)) {
63
+ return;
64
+ }
65
+ lastProcessedConsent = updatedConsent;
66
+ const chordAnalytics = window[options.namespace];
60
67
  const cdp = window[`_${options.namespace}`];
68
+ const shouldTrackConsentUpdate = chordAnalytics &&
69
+ typeof chordAnalytics.track === 'function' &&
70
+ updatedConsent.consentCategories !== undefined;
71
+ if (shouldTrackConsentUpdate &&
72
+ // we have to send this BEFORE the CDP is reconfigured or else the track will not be sent, usually in the case of a user opting out
73
+ updatedConsent.dontSend === true) {
74
+ yield chordAnalytics.track('Consent Preferences Updated', updatedConsent);
75
+ }
61
76
  if (cdp && typeof cdp.configure === 'function') {
62
77
  cdp.configure({ privacy: updatedConsent });
63
78
  }
@@ -65,21 +80,25 @@ export const addCdpConsent = (options) => {
65
80
  // CDP has not yet loaded via the snippet.
66
81
  window.jitsuConfig = { privacy: updatedConsent };
67
82
  }
68
- };
83
+ if (shouldTrackConsentUpdate &&
84
+ // we have to send this AFTER the CDP is reconfigured to ensure the track is sent, usually in the case of a user opting in
85
+ updatedConsent.dontSend === false) {
86
+ yield chordAnalytics.track('Consent Preferences Updated', updatedConsent);
87
+ }
88
+ });
69
89
  /**
70
90
  * Sets up consent management by initializing the adapter and handling consent updates.
71
91
  */
72
92
  const setupConsentManagement = () => __awaiter(void 0, void 0, void 0, function* () {
73
93
  if (!adapter)
74
94
  return;
75
- const isReady = yield checkUserCanBeTracked();
76
- if (!isReady)
77
- return;
78
- const initialConsent = adapter.getCurrentConsent();
79
- updateCdpConsent(initialConsent);
95
+ // Set up the consent update handler first to catch any immediate updates
80
96
  adapter.handleConsentUpdates((updatedConsent) => {
81
97
  updateCdpConsent(updatedConsent);
82
98
  });
99
+ // Process initial consent - this will be skipped if the adapter already triggered an update
100
+ const initialConsent = adapter.getCurrentConsent();
101
+ updateCdpConsent(initialConsent);
83
102
  });
84
103
  /**
85
104
  * Initializes consent management.
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.13.0",
4
+ "version": "1.14.0",
5
5
  "author": "Chord Commerce <help@chord.co> (https://www.chord.co)",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",