@chordcommerce/analytics 1.23.0 → 1.25.0-beta.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.
package/README.md CHANGED
@@ -121,8 +121,16 @@ export const cartFormatter: CartFormatter<StorefrontCart> = (props) => {
121
121
  addSourceMiddleware(func: AnalyticsMiddleware)
122
122
 
123
123
  Registers a middleware function to modify event properties before they are
124
- sent to analytics providers. Middleware functions are executed
125
- synchronously in the order they were added during event tracking.
124
+ sent to analytics providers. Middleware functions are invoked in the order they
125
+ were added during event tracking, and each one's properties are merged into the
126
+ event in that order.
127
+
128
+ A middleware may be synchronous or asynchronous. Synchronous middleware is never
129
+ awaited, so it adds no delay to event delivery. Asynchronous middleware is
130
+ awaited before the event is sent, capped at one second — a middleware that has
131
+ not resolved by then is abandoned, a warning is logged, and the event is sent
132
+ without its properties. A middleware that throws or rejects is logged and
133
+ skipped. A middleware never prevents an event from being sent.
126
134
 
127
135
  Parameters
128
136
 
@@ -135,6 +143,15 @@ The middleware function can return:
135
143
 
136
144
  - Partial<EventProperties> - Properties to merge into the event payload
137
145
  - void - No additional properties to add
146
+ - A Promise of either of the above - Awaited before the event is sent
147
+
148
+ The `payload` argument carries `payload.obj` (the event's properties, name and
149
+ page context) plus `payload.userId` and `payload.anonymousId` as the CDP
150
+ currently knows them — either may be undefined.
151
+
152
+ Note that middleware contributes properties by **returning** them. Mutating the
153
+ `payload` argument has no effect; `next` is accepted for signature
154
+ compatibility and does nothing.
138
155
 
139
156
  Useage Example
140
157
 
@@ -156,6 +173,15 @@ Useage Example
156
173
  timestamp: Date.now()
157
174
  };
158
175
  });
176
+
177
+ // Asynchronous middleware: the event waits for the lookup, up to one second.
178
+ analytics.addSourceMiddleware(async ({ payload }) => {
179
+ if (payload.obj.event !== 'Order Completed') return;
180
+
181
+ const isNewCustomer = await fetchIsNewCustomer();
182
+
183
+ return { is_new_customer: isNewCustomer ? 1 : 0 };
184
+ });
159
185
  ```
160
186
 
161
187
  ## API Reference
@@ -42,9 +42,12 @@ var ketch_js_1 = require("./ketch.js");
42
42
  var onetrust_js_1 = require("./onetrust.js");
43
43
  var shopify_js_1 = require("./shopify.js");
44
44
  var segment_js_1 = require("./segment.js");
45
+ var persistence_js_1 = require("./persistence.js");
45
46
  var addCdpConsent = function (options) {
46
- var lastProcessedConsent = null;
47
47
  var POLLING_INTERVAL_MS = 50;
48
+ // Always populated: ChordAnalytics applies a `namespace` default before
49
+ // constructing the consent manager.
50
+ var namespace = options.namespace;
48
51
  var TIMEOUT_MS = options.awaitConsentTimeout
49
52
  ? options.awaitConsentTimeout
50
53
  : 60000;
@@ -96,31 +99,49 @@ var addCdpConsent = function (options) {
96
99
  });
97
100
  }); };
98
101
  /**
99
- * Tracks consent preference changes.
100
- * Note: Per-event consent stamping is handled in ChordAnalytics via stampConsent().
102
+ * Tracks consent preference *transitions* only, deduped against the last
103
+ * state we emitted (persisted, so the guard survives navigation).
104
+ * Per-event consent stamping is handled by configureConsentOnCdp().
101
105
  */
102
106
  var trackConsentUpdate = function (updatedConsent) { return __awaiter(void 0, void 0, void 0, function () {
103
- var chordAnalytics;
107
+ var chordAnalytics, error_1;
104
108
  return __generator(this, function (_a) {
105
109
  switch (_a.label) {
106
110
  case 0:
107
- if (lastProcessedConsent &&
108
- JSON.stringify(lastProcessedConsent) === JSON.stringify(updatedConsent)) {
111
+ chordAnalytics = window[namespace];
112
+ if (!chordAnalytics ||
113
+ typeof chordAnalytics.track !== 'function' ||
114
+ updatedConsent.consentCategories === undefined) {
109
115
  return [2 /*return*/];
110
116
  }
111
- lastProcessedConsent = updatedConsent;
112
- chordAnalytics = window[options.namespace];
113
- if (!(chordAnalytics &&
114
- typeof chordAnalytics.track === 'function' &&
115
- updatedConsent.consentCategories !== undefined)) return [3 /*break*/, 2];
116
- return [4 /*yield*/, chordAnalytics.track('Consent Preferences Updated', updatedConsent)];
117
+ if (!(0, persistence_js_1.claimConsentEmit)(namespace, updatedConsent)) {
118
+ if (options.enableLogging && options.debug) {
119
+ console.log('[Chord Analytics DEBUG]: Consent unchanged or emit already in flight, skipping');
120
+ }
121
+ return [2 /*return*/];
122
+ }
123
+ _a.label = 1;
117
124
  case 1:
125
+ _a.trys.push([1, 3, , 4]);
126
+ return [4 /*yield*/, chordAnalytics.track('Consent Preferences Updated', updatedConsent)
127
+ // Only recorded once delivery resolves. Committing before this would
128
+ // suppress the event permanently if it were queued and never flushed.
129
+ ];
130
+ case 2:
118
131
  _a.sent();
132
+ // Only recorded once delivery resolves. Committing before this would
133
+ // suppress the event permanently if it were queued and never flushed.
134
+ (0, persistence_js_1.commitConsentEmit)(namespace, updatedConsent);
119
135
  if (options.enableLogging && options.debug) {
120
136
  console.log('[Chord Analytics DEBUG]: Consent preferences updated, sent tracking event');
121
137
  }
122
- _a.label = 2;
123
- case 2: return [2 /*return*/];
138
+ return [3 /*break*/, 4];
139
+ case 3:
140
+ error_1 = _a.sent();
141
+ (0, persistence_js_1.releaseConsentEmit)(namespace, updatedConsent);
142
+ console.error('Error tracking consent preferences update:', error_1);
143
+ return [3 /*break*/, 4];
144
+ case 4: return [2 /*return*/];
124
145
  }
125
146
  });
126
147
  }); };
@@ -129,24 +150,19 @@ var addCdpConsent = function (options) {
129
150
  * Returns the initial consent so it can be tracked after CDP is fully ready (avoiding circular await).
130
151
  */
131
152
  var setupConsentManagement = function () { return __awaiter(void 0, void 0, void 0, function () {
132
- var initialConsent;
133
153
  return __generator(this, function (_a) {
134
- switch (_a.label) {
135
- case 0:
136
- if (!adapter)
137
- return [2 /*return*/, null
138
- // Set up the consent update handler first to catch any immediate updates
139
- ];
154
+ if (!adapter)
155
+ return [2 /*return*/, null
140
156
  // Set up the consent update handler first to catch any immediate updates
141
- adapter.handleConsentUpdates(function (updatedConsent) {
142
- trackConsentUpdate(updatedConsent);
143
- });
144
- return [4 /*yield*/, adapter.getCurrentConsent()];
145
- case 1:
146
- initialConsent = _a.sent();
147
- lastProcessedConsent = initialConsent;
148
- return [2 /*return*/, initialConsent];
149
- }
157
+ ];
158
+ // Set up the consent update handler first to catch any immediate updates
159
+ adapter.handleConsentUpdates(function (updatedConsent) {
160
+ trackConsentUpdate(updatedConsent);
161
+ });
162
+ // Return initial consent to be tracked after CDP is fully initialized.
163
+ // Not marked as processed here: whichever path emits first records the
164
+ // signature, and the other is then deduped against it.
165
+ return [2 /*return*/, adapter.getCurrentConsent()];
150
166
  });
151
167
  }); };
152
168
  /**
@@ -155,7 +171,7 @@ var addCdpConsent = function (options) {
155
171
  * or null if consent setup failed or was blocked.
156
172
  */
157
173
  var init = function () { return __awaiter(void 0, void 0, void 0, function () {
158
- var canBeTracked, error_1;
174
+ var canBeTracked, error_2;
159
175
  return __generator(this, function (_a) {
160
176
  switch (_a.label) {
161
177
  case 0:
@@ -185,8 +201,8 @@ var addCdpConsent = function (options) {
185
201
  return [4 /*yield*/, setupConsentManagement()];
186
202
  case 3: return [2 /*return*/, _a.sent()];
187
203
  case 4:
188
- error_1 = _a.sent();
189
- console.error('Error setting up consent management:', error_1);
204
+ error_2 = _a.sent();
205
+ console.error('Error setting up consent management:', error_2);
190
206
  return [2 /*return*/, null];
191
207
  case 5: return [2 /*return*/];
192
208
  }
@@ -0,0 +1,49 @@
1
+ import type { ConsentConfiguration } from '../../types/index.js';
2
+ /**
3
+ * Build a stable, order-independent signature for a consent configuration.
4
+ *
5
+ * Category keys are sorted so a change in the order the consent manager
6
+ * reports its groups does not read as a change in consent.
7
+ */
8
+ export declare const consentSignature: (consent: ConsentConfiguration) => string;
9
+ /**
10
+ * Whether this consent configuration differs from the last one we tracked.
11
+ *
12
+ * Returns true when nothing has been stored yet, so every visitor still gets
13
+ * one baseline `Consent Preferences Updated` event recording their starting
14
+ * state. Subsequent page loads with unchanged consent return false.
15
+ */
16
+ export declare const hasConsentChanged: (namespace: string | undefined, consent: ConsentConfiguration) => boolean;
17
+ /**
18
+ * Claim the right to emit a consent event, atomically.
19
+ *
20
+ * Returns false when this state was already tracked, or when an emit for the
21
+ * same state is already in flight. Callers that get true MUST follow with
22
+ * `commitConsentEmit` on success or `releaseConsentEmit` on failure.
23
+ *
24
+ * The claim is deliberately not persisted: it exists only to dedupe within a
25
+ * page load. If delivery never completes, the next page load starts clean and
26
+ * retries rather than suppressing the event forever.
27
+ */
28
+ export declare const claimConsentEmit: (namespace: string | undefined, consent: ConsentConfiguration) => boolean;
29
+ /**
30
+ * Record a consent configuration as tracked, once delivery has actually
31
+ * completed. Later page loads dedupe against it.
32
+ */
33
+ export declare const commitConsentEmit: (namespace: string | undefined, consent: ConsentConfiguration) => void;
34
+ /**
35
+ * Give up a claim without recording it, so a later attempt can retry. Takes
36
+ * the consent it claimed so a failing emit cannot clear another state's claim.
37
+ */
38
+ export declare const releaseConsentEmit: (namespace: string | undefined, consent: ConsentConfiguration) => void;
39
+ /**
40
+ * Clear the stored consent signature so the next init emits a fresh baseline.
41
+ * Called on reset(), where a new anonymous ID would otherwise never get a
42
+ * consent record of its own.
43
+ */
44
+ export declare const forgetConsent: (namespace: string | undefined) => void;
45
+ /**
46
+ * Drop all module-level state. Test-only: `localStorage` is cleared between
47
+ * tests, but these would otherwise persist and suppress expected events.
48
+ */
49
+ export declare const clearConsentMemory: () => void;
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearConsentMemory = exports.forgetConsent = exports.releaseConsentEmit = exports.commitConsentEmit = exports.claimConsentEmit = exports.hasConsentChanged = exports.consentSignature = void 0;
4
+ var LAST_CONSENT_STORAGE_KEY = 'chordcommerce/analytics/last-consent';
5
+ /**
6
+ * In-memory fallback for environments where localStorage is unavailable
7
+ * (SSR, private browsing, storage disabled). Keyed by namespace so multiple
8
+ * ChordAnalytics instances on one page don't clobber each other.
9
+ */
10
+ var memoryStore = {};
11
+ var storageKey = function (namespace) {
12
+ return "".concat(LAST_CONSENT_STORAGE_KEY, "/").concat(namespace !== null && namespace !== void 0 ? namespace : 'default');
13
+ };
14
+ /**
15
+ * Build a stable, order-independent signature for a consent configuration.
16
+ *
17
+ * Category keys are sorted so a change in the order the consent manager
18
+ * reports its groups does not read as a change in consent.
19
+ */
20
+ var consentSignature = function (consent) {
21
+ var _a;
22
+ var categories = (_a = consent.consentCategories) !== null && _a !== void 0 ? _a : {};
23
+ var sorted = Object.keys(categories)
24
+ .sort()
25
+ .reduce(function (acc, key) {
26
+ acc[key] = categories[key];
27
+ return acc;
28
+ }, {});
29
+ return JSON.stringify(sorted);
30
+ };
31
+ exports.consentSignature = consentSignature;
32
+ var readSignature = function (namespace) {
33
+ var _a;
34
+ var key = storageKey(namespace);
35
+ try {
36
+ if (typeof window !== 'undefined' && window.localStorage) {
37
+ return window.localStorage.getItem(key);
38
+ }
39
+ }
40
+ catch (_b) {
41
+ // localStorage can throw in private browsing; fall through to memory
42
+ }
43
+ return (_a = memoryStore[key]) !== null && _a !== void 0 ? _a : null;
44
+ };
45
+ var writeSignature = function (namespace, signature) {
46
+ var key = storageKey(namespace);
47
+ memoryStore[key] = signature;
48
+ try {
49
+ if (typeof window !== 'undefined' && window.localStorage) {
50
+ window.localStorage.setItem(key, signature);
51
+ }
52
+ }
53
+ catch (_a) {
54
+ // Quota or private browsing; the in-memory copy still dedupes within
55
+ // this page load.
56
+ }
57
+ };
58
+ /**
59
+ * Claims for emits currently mid-delivery, as `<storage key>::<signature>`.
60
+ * Delivery is awaited and a queued event can stay unresolved for a long time,
61
+ * so without this two callers in the same tick would both pass the guard and
62
+ * emit duplicates.
63
+ *
64
+ * Keyed by signature, not just namespace, so two different consent states can
65
+ * be in flight at once without clearing each other's claim.
66
+ */
67
+ var inFlight = new Set();
68
+ var inFlightKey = function (namespace, signature) {
69
+ return "".concat(storageKey(namespace), "::").concat(signature);
70
+ };
71
+ /**
72
+ * Whether this consent configuration differs from the last one we tracked.
73
+ *
74
+ * Returns true when nothing has been stored yet, so every visitor still gets
75
+ * one baseline `Consent Preferences Updated` event recording their starting
76
+ * state. Subsequent page loads with unchanged consent return false.
77
+ */
78
+ var hasConsentChanged = function (namespace, consent) { return readSignature(namespace) !== (0, exports.consentSignature)(consent); };
79
+ exports.hasConsentChanged = hasConsentChanged;
80
+ /**
81
+ * Claim the right to emit a consent event, atomically.
82
+ *
83
+ * Returns false when this state was already tracked, or when an emit for the
84
+ * same state is already in flight. Callers that get true MUST follow with
85
+ * `commitConsentEmit` on success or `releaseConsentEmit` on failure.
86
+ *
87
+ * The claim is deliberately not persisted: it exists only to dedupe within a
88
+ * page load. If delivery never completes, the next page load starts clean and
89
+ * retries rather than suppressing the event forever.
90
+ */
91
+ var claimConsentEmit = function (namespace, consent) {
92
+ var signature = (0, exports.consentSignature)(consent);
93
+ if (readSignature(namespace) === signature)
94
+ return false;
95
+ if (inFlight.has(inFlightKey(namespace, signature)))
96
+ return false;
97
+ inFlight.add(inFlightKey(namespace, signature));
98
+ return true;
99
+ };
100
+ exports.claimConsentEmit = claimConsentEmit;
101
+ /**
102
+ * Record a consent configuration as tracked, once delivery has actually
103
+ * completed. Later page loads dedupe against it.
104
+ */
105
+ var commitConsentEmit = function (namespace, consent) {
106
+ var signature = (0, exports.consentSignature)(consent);
107
+ inFlight.delete(inFlightKey(namespace, signature));
108
+ writeSignature(namespace, signature);
109
+ };
110
+ exports.commitConsentEmit = commitConsentEmit;
111
+ /**
112
+ * Give up a claim without recording it, so a later attempt can retry. Takes
113
+ * the consent it claimed so a failing emit cannot clear another state's claim.
114
+ */
115
+ var releaseConsentEmit = function (namespace, consent) {
116
+ inFlight.delete(inFlightKey(namespace, (0, exports.consentSignature)(consent)));
117
+ };
118
+ exports.releaseConsentEmit = releaseConsentEmit;
119
+ /**
120
+ * Clear the stored consent signature so the next init emits a fresh baseline.
121
+ * Called on reset(), where a new anonymous ID would otherwise never get a
122
+ * consent record of its own.
123
+ */
124
+ var forgetConsent = function (namespace) {
125
+ var key = storageKey(namespace);
126
+ delete memoryStore[key];
127
+ inFlight.forEach(function (claim) {
128
+ if (claim.startsWith("".concat(key, "::")))
129
+ inFlight.delete(claim);
130
+ });
131
+ try {
132
+ if (typeof window !== 'undefined' && window.localStorage) {
133
+ window.localStorage.removeItem(key);
134
+ }
135
+ }
136
+ catch (_a) {
137
+ // Nothing to do; the in-memory copy is already gone.
138
+ }
139
+ };
140
+ exports.forgetConsent = forgetConsent;
141
+ /**
142
+ * Drop all module-level state. Test-only: `localStorage` is cleared between
143
+ * tests, but these would otherwise persist and suppress expected events.
144
+ */
145
+ var clearConsentMemory = function () {
146
+ Object.keys(memoryStore).forEach(function (key) { return delete memoryStore[key]; });
147
+ inFlight.clear();
148
+ };
149
+ exports.clearConsentMemory = clearConsentMemory;
@@ -31,34 +31,10 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
31
31
  */
32
32
  private cdpReadyPromise;
33
33
  private consentConfig;
34
- private resetCompletePromise;
35
- /**
36
- * Queue for events waiting for consent to be ready
37
- */
38
- private consentEventQueue;
39
- /**
40
- * Whether we're currently waiting for consent and watching for it
41
- */
42
- private consentWatcherActive;
43
- /**
44
- * Promise that resolves when consent is ready (shared across all queued events)
45
- */
34
+ private operationQueue;
35
+ private resetting;
46
36
  private consentReadyPromise;
47
37
  constructor(options: ChordAnalyticsOptions<T>);
48
- /**
49
- * Persist the consent event queue to sessionStorage for recovery on instance recreation.
50
- * Validates queue size before storing to prevent QuotaExceededError.
51
- */
52
- private persistQueueToStorage;
53
- /**
54
- * Restore pending events from sessionStorage (keeps events less than 5 min old).
55
- * Called on new instance initialization.
56
- */
57
- private restoreQueueFromStorage;
58
- /**
59
- * Clear the persisted queue from sessionStorage after successful flush.
60
- */
61
- private clearQueueFromStorage;
62
38
  /**
63
39
  * Initialize CDP snippet and wait for it to be ready.
64
40
  * Fires initial consent track event after CDP is fully initialized to avoid circular await.
@@ -71,29 +47,17 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
71
47
  setDebugMode: (enabled: boolean) => void;
72
48
  cdp: () => AnyAnalytics;
73
49
  ccdp: () => AnyAnalytics;
50
+ private currentUser;
74
51
  logger: (message?: any, ...optionalParams: any[]) => void;
75
52
  isResetting: () => boolean;
53
+ private enqueue;
54
+ private withTimeout;
76
55
  private waitForReadyState;
77
56
  /**
78
57
  * Wait for consent manager to be ready (no timeout - queues indefinitely).
79
58
  * Uses a shared promise so multiple events share the same watcher.
80
59
  */
81
60
  private waitForConsent;
82
- /**
83
- * Queue an event to be sent when consent is ready.
84
- * Persists to sessionStorage for recovery on instance recreation.
85
- * Starts a consent watcher if not already active.
86
- */
87
- private queueEventForConsent;
88
- /**
89
- * Watch for consent to become ready, then flush the queue.
90
- */
91
- private watchForConsentAndFlush;
92
- /**
93
- * Flush all queued events now that consent is ready.
94
- * Clears sessionStorage after successful flush.
95
- */
96
- private flushConsentQueue;
97
61
  /**
98
62
  * Configure CDP with fresh consent categories before each event.
99
63
  * Uses Jitsu's configure() to update privacy.consentCategories.
@@ -152,7 +116,6 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
152
116
  */
153
117
  private sendPageEvent;
154
118
  reset: () => Promise<void>;
155
- private performReset;
156
119
  /**
157
120
  * Handler for postMessage events from the Shopify Web Pixel sandbox.
158
121
  * Bound to the instance for proper `this` context and cleanup.