@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 +28 -2
- package/dist/cjs/client/index.d.ts +5 -42
- package/dist/cjs/client/index.js +302 -487
- package/dist/cjs/types/client.d.ts +14 -1
- package/dist/cjs/utils.d.ts +19 -0
- package/dist/cjs/utils.js +79 -1
- package/dist/client/index.d.ts +5 -42
- package/dist/client/index.js +277 -438
- package/dist/types/client.d.ts +14 -1
- package/dist/utils.d.ts +19 -0
- package/dist/utils.js +39 -0
- package/package.json +11 -1
|
@@ -88,10 +88,23 @@ type MiddlewarePayload = {
|
|
|
88
88
|
page: PageContext;
|
|
89
89
|
};
|
|
90
90
|
};
|
|
91
|
+
/**
|
|
92
|
+
* The current user and anonymous ids, read from the CDP at send time.
|
|
93
|
+
* Segment's middleware payload exposes these as `userId()`/`anonymousId()`
|
|
94
|
+
* methods on a facade; they are plain values here.
|
|
95
|
+
*/
|
|
96
|
+
userId?: string;
|
|
97
|
+
anonymousId?: string;
|
|
91
98
|
};
|
|
92
99
|
type MiddlewareArgs = {
|
|
93
100
|
payload: MiddlewarePayload;
|
|
94
101
|
next: <T>(arg: T) => T;
|
|
95
102
|
};
|
|
96
|
-
export type
|
|
103
|
+
export type MiddlewareResult = Partial<EventProperties> | void;
|
|
104
|
+
/**
|
|
105
|
+
* A source middleware may be synchronous or asynchronous. An asynchronous one
|
|
106
|
+
* is awaited before the event is dispatched, bounded by a timeout — see
|
|
107
|
+
* `addSourceMiddleware` in the README.
|
|
108
|
+
*/
|
|
109
|
+
export type AnalyticsMiddleware = (args: MiddlewareArgs) => MiddlewareResult | Promise<MiddlewareResult>;
|
|
97
110
|
export {};
|
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -1,2 +1,21 @@
|
|
|
1
1
|
import type { EventProperties } from './types/index.js';
|
|
2
2
|
export declare const pruneNullValues: (props: EventProperties) => EventProperties;
|
|
3
|
+
/**
|
|
4
|
+
* How long a single asynchronous source middleware may take before its
|
|
5
|
+
* contribution is abandoned.
|
|
6
|
+
*
|
|
7
|
+
* Middleware runs on the path to the CDP, so an unbounded wait would let one
|
|
8
|
+
* slow lookup hold up delivery indefinitely. `Order Completed` in particular
|
|
9
|
+
* fires on a confirmation page shoppers close, so a late event is a lost one.
|
|
10
|
+
*/
|
|
11
|
+
export declare const MIDDLEWARE_TIMEOUT_MS = 1000;
|
|
12
|
+
/** Returned by {@link withTimeout} when the promise did not settle in time. */
|
|
13
|
+
export declare const TIMED_OUT: unique symbol;
|
|
14
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Resolve `promise`, or {@link TIMED_OUT} if it has not settled within `ms`.
|
|
17
|
+
*
|
|
18
|
+
* The timer is cleared either way, so a promise that settles quickly does not
|
|
19
|
+
* leave a pending timeout behind.
|
|
20
|
+
*/
|
|
21
|
+
export declare const withTimeout: <T>(promise: Promise<T>, ms: number) => Promise<typeof TIMED_OUT | T>;
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.pruneNullValues = void 0;
|
|
39
|
+
exports.withTimeout = exports.isPromise = exports.TIMED_OUT = exports.MIDDLEWARE_TIMEOUT_MS = exports.pruneNullValues = void 0;
|
|
4
40
|
var pruneNullValues = function (props) {
|
|
5
41
|
if (typeof props !== 'object' || props === null)
|
|
6
42
|
return props;
|
|
@@ -23,3 +59,45 @@ var pruneNullValues = function (props) {
|
|
|
23
59
|
return props;
|
|
24
60
|
};
|
|
25
61
|
exports.pruneNullValues = pruneNullValues;
|
|
62
|
+
/**
|
|
63
|
+
* How long a single asynchronous source middleware may take before its
|
|
64
|
+
* contribution is abandoned.
|
|
65
|
+
*
|
|
66
|
+
* Middleware runs on the path to the CDP, so an unbounded wait would let one
|
|
67
|
+
* slow lookup hold up delivery indefinitely. `Order Completed` in particular
|
|
68
|
+
* fires on a confirmation page shoppers close, so a late event is a lost one.
|
|
69
|
+
*/
|
|
70
|
+
exports.MIDDLEWARE_TIMEOUT_MS = 1000;
|
|
71
|
+
/** Returned by {@link withTimeout} when the promise did not settle in time. */
|
|
72
|
+
exports.TIMED_OUT = Symbol('timed-out');
|
|
73
|
+
var isPromise = function (value) {
|
|
74
|
+
return typeof (value === null || value === void 0 ? void 0 : value.then) === 'function';
|
|
75
|
+
};
|
|
76
|
+
exports.isPromise = isPromise;
|
|
77
|
+
/**
|
|
78
|
+
* Resolve `promise`, or {@link TIMED_OUT} if it has not settled within `ms`.
|
|
79
|
+
*
|
|
80
|
+
* The timer is cleared either way, so a promise that settles quickly does not
|
|
81
|
+
* leave a pending timeout behind.
|
|
82
|
+
*/
|
|
83
|
+
var withTimeout = function (promise, ms) { return __awaiter(void 0, void 0, void 0, function () {
|
|
84
|
+
var timer, timeout;
|
|
85
|
+
return __generator(this, function (_a) {
|
|
86
|
+
switch (_a.label) {
|
|
87
|
+
case 0:
|
|
88
|
+
timeout = new Promise(function (resolve) {
|
|
89
|
+
timer = setTimeout(function () { return resolve(exports.TIMED_OUT); }, ms);
|
|
90
|
+
});
|
|
91
|
+
_a.label = 1;
|
|
92
|
+
case 1:
|
|
93
|
+
_a.trys.push([1, , 3, 4]);
|
|
94
|
+
return [4 /*yield*/, Promise.race([promise, timeout])];
|
|
95
|
+
case 2: return [2 /*return*/, _a.sent()];
|
|
96
|
+
case 3:
|
|
97
|
+
clearTimeout(timer);
|
|
98
|
+
return [7 /*endfinally*/];
|
|
99
|
+
case 4: return [2 /*return*/];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}); };
|
|
103
|
+
exports.withTimeout = withTimeout;
|
package/dist/client/index.d.ts
CHANGED
|
@@ -31,34 +31,10 @@ export declare class ChordAnalytics<T extends ObjectTypes = ObjectTypes> {
|
|
|
31
31
|
*/
|
|
32
32
|
private cdpReadyPromise;
|
|
33
33
|
private consentConfig;
|
|
34
|
-
private
|
|
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.
|