@chordcommerce/analytics 1.12.2 → 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.
@@ -67,7 +67,7 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
67
67
  * Send a `page` event to the CDP.
68
68
  */
69
69
  page: {
70
- (): void;
70
+ (props?: Record<string, any>): void;
71
71
  };
72
72
  reset: () => void;
73
73
  trackCartViewed: (props: CartViewedInput<T>, options?: AnyOptions) => Promise<void>;
@@ -167,19 +167,19 @@ export class ChordAnalytics {
167
167
  /**
168
168
  * Send a `page` event to the CDP.
169
169
  */
170
- this.page = () => {
170
+ this.page = (props = {}) => {
171
171
  try {
172
172
  const pagePromises = [];
173
173
  const a = this.ccdp();
174
174
  if (a && typeof a.page === 'function') {
175
175
  pagePromises.push(new Promise((resolve) => {
176
- a.page({ meta: this.meta() }, () => resolve());
176
+ a.page(Object.assign(Object.assign({}, props), { meta: this.meta() }), () => resolve());
177
177
  }));
178
178
  }
179
179
  const b = this.cdp();
180
180
  if (b && typeof b.page === 'function') {
181
181
  pagePromises.push(new Promise((resolve) => {
182
- b.page({ meta: this.meta() }, () => resolve());
182
+ b.page(Object.assign(Object.assign({}, props), { meta: this.meta() }), () => resolve());
183
183
  }));
184
184
  }
185
185
  // We don't return the promise to maintain the void return type
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.2",
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",