@chordcommerce/analytics 1.23.1 → 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
@@ -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.