@wellth-app/experiments 0.0.0-stage → 0.1.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.
- package/README.md +223 -2
- package/dist/cjs/index.js +10 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/react.js +317 -0
- package/dist/cjs/react.js.map +1 -0
- package/dist/cjs/registry-pg5aaOm-.js +537 -0
- package/dist/cjs/registry-pg5aaOm-.js.map +1 -0
- package/dist/cjs/web.js +87 -0
- package/dist/cjs/web.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/react.js +290 -0
- package/dist/esm/react.js.map +1 -0
- package/dist/esm/registry-B73fwn9J.js +534 -0
- package/dist/esm/registry-B73fwn9J.js.map +1 -0
- package/dist/esm/web.js +85 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/events.d.ts +65 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/react/context.d.ts +37 -0
- package/dist/react/context.d.ts.map +1 -0
- package/dist/react/guard.d.ts +11 -0
- package/dist/react/guard.d.ts.map +1 -0
- package/dist/react/index.d.ts +12 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/provider.d.ts +38 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/use-analytics.d.ts +12 -0
- package/dist/react/use-analytics.d.ts.map +1 -0
- package/dist/react/use-experiment-qa.d.ts +14 -0
- package/dist/react/use-experiment-qa.d.ts.map +1 -0
- package/dist/react/use-experiment.d.ts +30 -0
- package/dist/react/use-experiment.d.ts.map +1 -0
- package/dist/react/use-feature-flag.d.ts +16 -0
- package/dist/react/use-feature-flag.d.ts.map +1 -0
- package/dist/react/use-feature-value.d.ts +16 -0
- package/dist/react/use-feature-value.d.ts.map +1 -0
- package/dist/registry.d.ts +143 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/types.d.ts +111 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/uuid.d.ts +6 -0
- package/dist/uuid.d.ts.map +1 -0
- package/dist/web/batched-event-sender.d.ts +35 -0
- package/dist/web/batched-event-sender.d.ts.map +1 -0
- package/dist/web/index.d.ts +7 -0
- package/dist/web/index.d.ts.map +1 -0
- package/package.json +127 -4
- package/src/events.ts +90 -0
- package/src/index.ts +37 -0
- package/src/react/context.ts +62 -0
- package/src/react/guard.ts +25 -0
- package/src/react/index.ts +17 -0
- package/src/react/provider.tsx +109 -0
- package/src/react/use-analytics.ts +17 -0
- package/src/react/use-experiment-qa.ts +26 -0
- package/src/react/use-experiment.ts +72 -0
- package/src/react/use-feature-flag.ts +35 -0
- package/src/react/use-feature-value.ts +42 -0
- package/src/registry.ts +715 -0
- package/src/types.ts +120 -0
- package/src/uuid.ts +14 -0
- package/src/web/batched-event-sender.ts +111 -0
- package/src/web/index.ts +9 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public contract of the experiment SDK. Call sites bind to these types
|
|
3
|
+
* only — never to the evaluation engine's own types.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The parameter payload configured on one experiment arm. Genuinely dynamic JSON
|
|
8
|
+
* owned by the experiment config, so the shape is open; call sites narrow what
|
|
9
|
+
* they read (e.g. `params["button_color"] === "blue"`).
|
|
10
|
+
*/
|
|
11
|
+
export type ExperimentParams = Record<string, unknown>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Targeting attributes an experiment's conditions evaluate against before
|
|
15
|
+
* bucketing (platform, program, appVersion, …). The attribute catalogue is owned
|
|
16
|
+
* by the experiment config, so the map is open. Never consulted for bucketing —
|
|
17
|
+
* the hash input is always the subject id alone. The registry always supplies a
|
|
18
|
+
* `date` attribute (the current ISO date) so configs can enforce time windows;
|
|
19
|
+
* callers may override it (tests).
|
|
20
|
+
*/
|
|
21
|
+
export type TargetingAttributes = Record<string, unknown>;
|
|
22
|
+
|
|
23
|
+
/** Who the assignment is for, plus the attributes targeting gates on. */
|
|
24
|
+
export interface ExperimentContext {
|
|
25
|
+
/**
|
|
26
|
+
* The stable member id — the only input the bucketing hash ever sees.
|
|
27
|
+
* Normalized (trim + lowercase) inside the evaluator, so casing/whitespace
|
|
28
|
+
* drift at call sites never scrambles cohorts. Defaults to the session's.
|
|
29
|
+
*/
|
|
30
|
+
subjectId?: string;
|
|
31
|
+
attributes?: TargetingAttributes;
|
|
32
|
+
/**
|
|
33
|
+
* "auto" (default): exposure fires when the assigned variant renders.
|
|
34
|
+
* "manual": the hook returns a `markExposed` trigger instead, for call sites
|
|
35
|
+
* that evaluate now but render the experience later.
|
|
36
|
+
*/
|
|
37
|
+
exposure?: "auto" | "manual";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What `useExperiment` returns: the assigned arm's key and its configured payload. */
|
|
41
|
+
export interface ExperimentAssignment {
|
|
42
|
+
/** The assigned arm's key from the experiment config (e.g. "control" | "treatment"). */
|
|
43
|
+
variant: string;
|
|
44
|
+
/** The assigned arm's configured parameter payload. */
|
|
45
|
+
params: ExperimentParams;
|
|
46
|
+
/**
|
|
47
|
+
* Present when called with `exposure: "manual"`: records the exposure when the
|
|
48
|
+
* experience actually renders. No-ops for an assignment that isn't exposed
|
|
49
|
+
* (not enrolled, stopped, QA-pinned).
|
|
50
|
+
*/
|
|
51
|
+
markExposed?: () => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A QA/debug pin forcing one variant for one subject on the current session.
|
|
56
|
+
* Pinned evaluations never emit exposure.
|
|
57
|
+
*/
|
|
58
|
+
export interface QaPin {
|
|
59
|
+
experimentKey: string;
|
|
60
|
+
subjectId: string;
|
|
61
|
+
variant: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A cutover assignment map imported at migration time, so no member flips arms
|
|
66
|
+
* mid-experiment: per experiment, subject id to the variant key they were
|
|
67
|
+
* already on. The app bundles the map and hands it to the registry — it is
|
|
68
|
+
* config, not a service. A plain variant string is a migration entry; the full
|
|
69
|
+
* entry form adds an optional expiry and kind.
|
|
70
|
+
*/
|
|
71
|
+
export type Overrides = Record<string, Record<string, string | OverrideEntry>>;
|
|
72
|
+
|
|
73
|
+
export interface OverrideEntry {
|
|
74
|
+
/** The forced variant key. */
|
|
75
|
+
variant: string;
|
|
76
|
+
/** ISO timestamp after which the override is ignored. Overrides always expire. */
|
|
77
|
+
expiresAt?: string;
|
|
78
|
+
/**
|
|
79
|
+
* "migration" (default): a real assignment — exposure logs normally and
|
|
80
|
+
* analysis keeps the member. "qa": a debug/support forcing — exposure logs
|
|
81
|
+
* with `override: true` so analysis drops it by default.
|
|
82
|
+
*/
|
|
83
|
+
kind?: "migration" | "qa";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The registry's resolution, including whether the assignment may log exposure. */
|
|
87
|
+
export interface ResolvedAssignment extends ExperimentAssignment {
|
|
88
|
+
/**
|
|
89
|
+
* Present only when this assignment is a real in-experiment allocation that
|
|
90
|
+
* becomes an exposure once rendered. Null for defaults, stopped experiments,
|
|
91
|
+
* targeting failures, and QA pins.
|
|
92
|
+
*/
|
|
93
|
+
exposure: ExposureInput | null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** An allocation that became visible — one exposure event when rendered. */
|
|
97
|
+
export interface ExposureInput {
|
|
98
|
+
experimentKey: string;
|
|
99
|
+
subjectId: string;
|
|
100
|
+
variant: string;
|
|
101
|
+
/**
|
|
102
|
+
* The config version the assignment was evaluated against — captured when the
|
|
103
|
+
* exposure is built, so a config refresh between compute and render can never
|
|
104
|
+
* re-stamp the event. Null when unknown.
|
|
105
|
+
*/
|
|
106
|
+
configVersion: string | null;
|
|
107
|
+
/** True when a QA-kind override produced the assignment — flagged on the event. */
|
|
108
|
+
override?: boolean;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A minimal key/value store for persisting the config cache (AsyncStorage on
|
|
113
|
+
* React Native, a localStorage-compatible shim elsewhere). Both methods may be
|
|
114
|
+
* async. When provided, the evaluation client caches the config here, so cold
|
|
115
|
+
* starts evaluate offline from the last fetch.
|
|
116
|
+
*/
|
|
117
|
+
export interface KeyValueStorage {
|
|
118
|
+
getItem(key: string): string | null | Promise<string | null>;
|
|
119
|
+
setItem(key: string, value: string): void | Promise<void>;
|
|
120
|
+
}
|
package/src/uuid.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A v4 UUID for idempotency keys — uniqueness, not security. Uses the platform
|
|
3
|
+
* crypto when it has one (web, modern Hermes), a Math.random fallback otherwise.
|
|
4
|
+
*/
|
|
5
|
+
export function newId(): string {
|
|
6
|
+
const cryptoApi = globalThis.crypto;
|
|
7
|
+
if (cryptoApi?.randomUUID) {
|
|
8
|
+
return cryptoApi.randomUUID();
|
|
9
|
+
}
|
|
10
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
11
|
+
const r = Math.floor(Math.random() * 16);
|
|
12
|
+
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { EventStream, StreamEvent } from "../events";
|
|
2
|
+
|
|
3
|
+
export interface BatchedEventSender extends EventStream {
|
|
4
|
+
/** Push every queued event now. Best-effort: a failed batch is dropped. */
|
|
5
|
+
flush(): Promise<void>;
|
|
6
|
+
/** Stop the flush timer and drop anything still queued. */
|
|
7
|
+
destroy(): void;
|
|
8
|
+
/** Events queued but not yet flushed. */
|
|
9
|
+
pendingCount(): number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface BatchedEventSenderOptions {
|
|
13
|
+
url: string;
|
|
14
|
+
authorization?: string;
|
|
15
|
+
/** Flush as soon as this many events are queued. Default 20. */
|
|
16
|
+
batchSize?: number;
|
|
17
|
+
/** Flush interval while the page is alive. Default 5000ms. */
|
|
18
|
+
flushIntervalMs?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Where a serialized batch goes. Defaults to sendBeacon while the page is
|
|
21
|
+
* hidden (a mid-unload fetch can die) and a keepalive POST otherwise.
|
|
22
|
+
*/
|
|
23
|
+
transport?: (batch: readonly StreamEvent[]) => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An EventStream that buffers events and flushes them in batches — the same
|
|
28
|
+
* stream every product event uses. Batches are best-effort: analytics events
|
|
29
|
+
* ride the network's happy path and a failed batch is dropped, never retried
|
|
30
|
+
* into a duplicate.
|
|
31
|
+
*
|
|
32
|
+
* This transport is the web flavor (document visibility + sendBeacon). Other
|
|
33
|
+
* runtimes supply their own transport through the `transport` option or their
|
|
34
|
+
* own EventStream entirely.
|
|
35
|
+
*/
|
|
36
|
+
export function createBatchedEventSender({
|
|
37
|
+
url,
|
|
38
|
+
authorization,
|
|
39
|
+
batchSize = 20,
|
|
40
|
+
flushIntervalMs = 5000,
|
|
41
|
+
transport = defaultTransport(url, authorization),
|
|
42
|
+
}: BatchedEventSenderOptions): BatchedEventSender {
|
|
43
|
+
let queue: StreamEvent[] = [];
|
|
44
|
+
let inFlight = false;
|
|
45
|
+
|
|
46
|
+
const flush = async (): Promise<void> => {
|
|
47
|
+
if (inFlight || queue.length === 0) return;
|
|
48
|
+
const batch = queue;
|
|
49
|
+
queue = [];
|
|
50
|
+
inFlight = true;
|
|
51
|
+
try {
|
|
52
|
+
await transport(batch);
|
|
53
|
+
} catch {
|
|
54
|
+
// Best-effort stream: a dropped batch must never break the app.
|
|
55
|
+
} finally {
|
|
56
|
+
inFlight = false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const onHide = (): void => {
|
|
61
|
+
void flush();
|
|
62
|
+
};
|
|
63
|
+
const onVisibilityChange = (): void => {
|
|
64
|
+
if (document.visibilityState === "hidden") onHide();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const timer = setInterval(() => {
|
|
68
|
+
void flush();
|
|
69
|
+
}, flushIntervalMs);
|
|
70
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
71
|
+
window.addEventListener("pagehide", onHide);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
send(event: StreamEvent): void {
|
|
75
|
+
queue.push(event);
|
|
76
|
+
if (queue.length >= batchSize) void flush();
|
|
77
|
+
},
|
|
78
|
+
flush,
|
|
79
|
+
destroy(): void {
|
|
80
|
+
clearInterval(timer);
|
|
81
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
82
|
+
window.removeEventListener("pagehide", onHide);
|
|
83
|
+
queue = [];
|
|
84
|
+
},
|
|
85
|
+
pendingCount(): number {
|
|
86
|
+
return queue.length;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function defaultTransport(
|
|
92
|
+
url: string,
|
|
93
|
+
authorization?: string,
|
|
94
|
+
): (batch: readonly StreamEvent[]) => Promise<void> {
|
|
95
|
+
return async (batch) => {
|
|
96
|
+
const body = JSON.stringify(batch);
|
|
97
|
+
if (!authorization && document.visibilityState === "hidden") {
|
|
98
|
+
if (navigator.sendBeacon?.(url, new Blob([body], { type: "application/json" }))) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
|
103
|
+
if (authorization) headers.Authorization = authorization;
|
|
104
|
+
await fetch(url, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
body,
|
|
107
|
+
headers,
|
|
108
|
+
keepalive: true,
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
}
|
package/src/web/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@wellth-app/experiments/web` — the web event transport: a batching sender over
|
|
3
|
+
* fetch/sendBeacon for browsers. React Native and other runtimes supply their
|
|
4
|
+
* own EventStream instead.
|
|
5
|
+
*/
|
|
6
|
+
export {
|
|
7
|
+
createBatchedEventSender,
|
|
8
|
+
type BatchedEventSender,
|
|
9
|
+
} from "./batched-event-sender";
|