@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
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import type { QaPin } from "../types";
|
|
3
|
+
import { useExperiments } from "./context";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The QA override surface, for the dev/debug menu: force a variant for the
|
|
7
|
+
* current subject and session. Pinned assignments render the pinned arm but are
|
|
8
|
+
* never recorded as exposure.
|
|
9
|
+
*/
|
|
10
|
+
export function useExperimentQa(): {
|
|
11
|
+
pins: readonly QaPin[];
|
|
12
|
+
pin: (pin: QaPin) => void;
|
|
13
|
+
unpin: (input: { experimentKey: string }) => void;
|
|
14
|
+
} {
|
|
15
|
+
const { registry } = useExperiments();
|
|
16
|
+
useSyncExternalStore(registry.subscribe, registry.getVersion);
|
|
17
|
+
return {
|
|
18
|
+
pins: registry.getPins(),
|
|
19
|
+
pin: (pin: QaPin): void => {
|
|
20
|
+
registry.pin(pin);
|
|
21
|
+
},
|
|
22
|
+
unpin: (input: { experimentKey: string }): void => {
|
|
23
|
+
registry.unpin(input);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useEffect, useSyncExternalStore } from "react";
|
|
2
|
+
import type { ExperimentAssignment, ExperimentContext } from "../types";
|
|
3
|
+
import { useExperiments, useInternalRegistry } from "./context";
|
|
4
|
+
import { assertHookKeyKind } from "./guard";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read the experiment's assignment for the current subject:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const { variant, params } = useExperiment("brick_wall");
|
|
11
|
+
* // variant: the assigned arm key from the experiment config
|
|
12
|
+
* // params: the configured parameter payload for the assigned arm
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Pass `context` to evaluate for a different subject or with extra targeting
|
|
16
|
+
* attributes; both default to the session's. Targeting gates eligibility before
|
|
17
|
+
* bucketing — a member who fails targeting gets the default arm with no
|
|
18
|
+
* exposure. The hash input is always the subject id.
|
|
19
|
+
*
|
|
20
|
+
* Exactly one exposure is recorded, when the assigned variant is actually
|
|
21
|
+
* rendered (post-commit) — never on compute, and never for a member who was
|
|
22
|
+
* bucketed but not shown the experience. Defaults, stopped experiments,
|
|
23
|
+
* targeting failures, and QA pins never emit exposure.
|
|
24
|
+
*
|
|
25
|
+
* With `exposure: "manual"` the hook instead returns a `markExposed` trigger —
|
|
26
|
+
* for call sites that evaluate now but render the experience later. Call it
|
|
27
|
+
* when the experience actually renders; it no-ops on an unexposed assignment.
|
|
28
|
+
*
|
|
29
|
+
* Misuse guard: calling this on a feature-flag key throws a clear error in
|
|
30
|
+
* development (flags go through useFeatureFlag/useFeatureValue); production
|
|
31
|
+
* reads fail closed to control.
|
|
32
|
+
*/
|
|
33
|
+
export function useExperiment(
|
|
34
|
+
experimentKey: string,
|
|
35
|
+
context?: ExperimentContext,
|
|
36
|
+
): ExperimentAssignment {
|
|
37
|
+
const { subjectId: sessionSubjectId, attributes: sessionAttributes } =
|
|
38
|
+
useExperiments();
|
|
39
|
+
// The real registry, trackExposure included — the internal channel, so the
|
|
40
|
+
// context's facade can never emit an exposure outside this hook.
|
|
41
|
+
const registry = useInternalRegistry();
|
|
42
|
+
// Dev-only misuse guard: a flag key here throws; production fails closed.
|
|
43
|
+
assertHookKeyKind(registry, experimentKey, "experiment", "useExperiment");
|
|
44
|
+
// Subscribed so a config refresh or pin change re-evaluates this assignment.
|
|
45
|
+
useSyncExternalStore(registry.subscribe, registry.getVersion);
|
|
46
|
+
const subjectId = context?.subjectId ?? sessionSubjectId;
|
|
47
|
+
const attributes = context?.attributes ?? sessionAttributes;
|
|
48
|
+
const assignment = registry.evaluateExperiment({
|
|
49
|
+
experimentKey,
|
|
50
|
+
subjectId,
|
|
51
|
+
attributes,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const exposure = assignment.exposure;
|
|
55
|
+
const manual = context?.exposure === "manual";
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (manual || exposure === null) return;
|
|
58
|
+
registry.trackExposure(exposure);
|
|
59
|
+
}, [registry, exposure, manual]);
|
|
60
|
+
|
|
61
|
+
if (!manual) {
|
|
62
|
+
return { variant: assignment.variant, params: assignment.params };
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
variant: assignment.variant,
|
|
66
|
+
params: assignment.params,
|
|
67
|
+
markExposed: () => {
|
|
68
|
+
if (exposure === null) return;
|
|
69
|
+
registry.trackExposure(exposure);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import type { ExperimentContext } from "../types";
|
|
3
|
+
import { useExperiments } from "./context";
|
|
4
|
+
import { assertHookKeyKind } from "./guard";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read a boolean feature flag for the current subject:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const enabled = useFeatureFlag("dark_mode_checkout");
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Targeting conditions and percentage rollouts evaluate locally against the
|
|
14
|
+
* same deterministic hash the experiments use. Flags never log exposure. An
|
|
15
|
+
* unknown flag is off; an experiment key read here throws a clear error in
|
|
16
|
+
* development — experiments go through useExperiment, the exposure-aware path
|
|
17
|
+
* (production reads fail closed to the config's default).
|
|
18
|
+
*/
|
|
19
|
+
export function useFeatureFlag(
|
|
20
|
+
featureKey: string,
|
|
21
|
+
context?: ExperimentContext,
|
|
22
|
+
): boolean {
|
|
23
|
+
const {
|
|
24
|
+
registry,
|
|
25
|
+
subjectId: sessionSubjectId,
|
|
26
|
+
attributes: sessionAttributes,
|
|
27
|
+
} = useExperiments();
|
|
28
|
+
// Dev-only misuse guard: an experiment key here throws; production reads the config's default.
|
|
29
|
+
assertHookKeyKind(registry, featureKey, "flag", "useFeatureFlag");
|
|
30
|
+
// Subscribed so a config refresh re-evaluates this flag.
|
|
31
|
+
useSyncExternalStore(registry.subscribe, registry.getVersion);
|
|
32
|
+
const subjectId = context?.subjectId ?? sessionSubjectId;
|
|
33
|
+
const attributes = context?.attributes ?? sessionAttributes;
|
|
34
|
+
return registry.evaluateFlag({ featureKey, subjectId, attributes }).on;
|
|
35
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
import type { ExperimentContext } from "../types";
|
|
3
|
+
import { useExperiments } from "./context";
|
|
4
|
+
import { assertHookKeyKind } from "./guard";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read a valued feature flag (string/number/JSON remote config) for the current
|
|
8
|
+
* subject, falling back when the flag is unknown or has no value:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const headline = useFeatureValue("checkout_headline", "Control headline");
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The fallback declares the flag's type. Flags never log exposure; an
|
|
15
|
+
* experiment key read here throws a clear error in development — experiments
|
|
16
|
+
* go through useExperiment, the exposure-aware path (production reads fail
|
|
17
|
+
* closed to the config's default).
|
|
18
|
+
*/
|
|
19
|
+
export function useFeatureValue<T>(
|
|
20
|
+
featureKey: string,
|
|
21
|
+
fallback: T,
|
|
22
|
+
context?: ExperimentContext,
|
|
23
|
+
): T {
|
|
24
|
+
const {
|
|
25
|
+
registry,
|
|
26
|
+
subjectId: sessionSubjectId,
|
|
27
|
+
attributes: sessionAttributes,
|
|
28
|
+
} = useExperiments();
|
|
29
|
+
// Dev-only misuse guard: an experiment key here throws; production reads the config's default.
|
|
30
|
+
assertHookKeyKind(registry, featureKey, "flag", "useFeatureValue");
|
|
31
|
+
// Subscribed so a config refresh re-evaluates this flag.
|
|
32
|
+
useSyncExternalStore(registry.subscribe, registry.getVersion);
|
|
33
|
+
const subjectId = context?.subjectId ?? sessionSubjectId;
|
|
34
|
+
const attributes = context?.attributes ?? sessionAttributes;
|
|
35
|
+
const { value } = registry.evaluateFlag({
|
|
36
|
+
featureKey,
|
|
37
|
+
subjectId,
|
|
38
|
+
attributes,
|
|
39
|
+
});
|
|
40
|
+
// The engine's value is untyped; the caller's fallback declares the flag's type.
|
|
41
|
+
return value === null || value === undefined ? fallback : (value as T);
|
|
42
|
+
}
|