@plitzi/sdk-shared 0.39.10 → 0.39.12

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/audiences/audiences.d.ts +64 -0
  3. package/dist/audiences/audiences.mjs +66 -0
  4. package/dist/audiences/index.d.ts +1 -0
  5. package/dist/audiences/index.mjs +1 -0
  6. package/dist/authoring/spaceCallbacks.mjs +46 -33
  7. package/dist/dataSource/globalSources.d.ts +4 -4
  8. package/dist/dataSource/globalSources.mjs +1 -0
  9. package/dist/experimentEvents/experimentEvents.d.ts +24 -0
  10. package/dist/experimentEvents/experimentEvents.mjs +28 -0
  11. package/dist/experimentEvents/index.d.ts +1 -0
  12. package/dist/experimentEvents/index.mjs +1 -0
  13. package/dist/experiments/experiments.d.ts +73 -0
  14. package/dist/experiments/experiments.mjs +74 -0
  15. package/dist/experiments/experimentsCookie.d.ts +26 -0
  16. package/dist/experiments/experimentsCookie.mjs +25 -0
  17. package/dist/experiments/forcedExperiments.d.ts +31 -0
  18. package/dist/experiments/forcedExperiments.mjs +41 -0
  19. package/dist/experiments/index.d.ts +3 -0
  20. package/dist/experiments/index.mjs +4 -0
  21. package/dist/flags/index.d.ts +1 -0
  22. package/dist/flags/index.mjs +6 -5
  23. package/dist/flags/react/index.d.ts +1 -0
  24. package/dist/flags/react/index.mjs +3 -1
  25. package/dist/flags/react/useFlagResolution.mjs +22 -19
  26. package/dist/flags/react/useRolloutBuckets.d.ts +9 -0
  27. package/dist/flags/react/useRolloutBuckets.mjs +19 -0
  28. package/dist/flags/resolveFlags.d.ts +10 -3
  29. package/dist/flags/resolveFlags.mjs +19 -9
  30. package/dist/flags/rollout.d.ts +41 -0
  31. package/dist/flags/rollout.mjs +53 -0
  32. package/dist/flags/schemaFlag.d.ts +3 -1
  33. package/dist/flags/schemaFlag.mjs +2 -2
  34. package/dist/helpers/index.mjs +17 -17
  35. package/dist/schema/serverElements.mjs +18 -18
  36. package/dist/schema/serverInput.mjs +6 -6
  37. package/dist/store/renderSettings.d.ts +1 -0
  38. package/dist/store/renderSettings.mjs +16 -12
  39. package/dist/store/storeHooks.d.ts +45 -45
  40. package/dist/timeWindows/index.d.ts +1 -0
  41. package/dist/timeWindows/index.mjs +1 -0
  42. package/dist/timeWindows/timeWindows.d.ts +16 -0
  43. package/dist/timeWindows/timeWindows.mjs +10 -0
  44. package/dist/types/DataSourceTypes.d.ts +1 -1
  45. package/dist/types/ElementTypes.d.ts +1 -1
  46. package/dist/types/PluginTypes.d.ts +5 -0
  47. package/dist/types/SchemaTypes.d.ts +11 -1
  48. package/dist/types/StoreTypes.d.ts +59 -0
  49. package/dist/variants/index.d.ts +1 -0
  50. package/dist/variants/index.mjs +1 -0
  51. package/dist/variants/variants.d.ts +23 -0
  52. package/dist/variants/variants.mjs +9 -0
  53. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @plitzi/sdk-shared
2
2
 
3
+ ## 0.39.12
4
+
5
+ ### Patch Changes
6
+
7
+ - 2f5b44d: Released with every `@plitzi` package at this version. What changed in each, and what to do when you upgrade, is in
8
+ `@plitzi/cli`'s `CHANGELOG.md`: one copy of the notes, not one per package.
9
+
10
+ ## 0.39.11
11
+
12
+ ### Patch Changes
13
+
14
+ - d0b5be6: Released with every `@plitzi` package at this version. What changed in each, and what to do when you upgrade, is in
15
+ `@plitzi/cli`'s `CHANGELOG.md`: one copy of the notes, not one per package.
16
+
3
17
  ## 0.39.10
4
18
 
5
19
  ### Patch Changes
@@ -0,0 +1,64 @@
1
+ import { FlagScope } from '../flags/resolveFlags';
2
+ import { Schema } from '../types';
3
+ export type VisitorDevice = 'mobile' | 'tablet' | 'desktop';
4
+ /**
5
+ * What an audience's rules know of the visitor beyond what a flag's do, read as `visitor.<fact>`: whether they came
6
+ * before, the language their browser asks for first (its primary subtag: `es`, `en`) and the kind of device.
7
+ */
8
+ export type VisitorFacts = {
9
+ returning: boolean;
10
+ language: string;
11
+ device: VisitorDevice;
12
+ };
13
+ export declare const VISITOR_FACTS: readonly ["returning", "language", "device"];
14
+ export type VisitorFact = (typeof VISITOR_FACTS)[number];
15
+ /** What an audience's rules are matched against: what a flag's are, and the visitor's facts. */
16
+ export type AudienceScope = FlagScope & {
17
+ visitor?: VisitorFacts;
18
+ };
19
+ /**
20
+ * The kind of device a user agent says, with the client hint `Sec-CH-UA-Mobile` (`?1`) first where the browser sends
21
+ * one. A tablet says so in its agent (`iPad`, `Tablet`) or is an Android one without `Mobile`.
22
+ */
23
+ export declare const deviceFromUserAgent: (userAgent: string | undefined, mobileHint?: string) => VisitorDevice;
24
+ /** The language a browser asks for first, by its primary subtag — `es` for `es-AR,es;q=0.9,en;q=0.8` — or none. */
25
+ export declare const languageFrom: (preferred: string | undefined) => string;
26
+ /**
27
+ * The cookie that says when a visitor was first seen, in seconds since the epoch (UTC): what makes them returning. Named
28
+ * with the port, as the others a page keeps for its visitor are.
29
+ */
30
+ export declare const seenCookieName: (host: string | undefined) => string;
31
+ /** How long a visit lasts, at most: a visitor first seen longer ago than this is returning. */
32
+ export declare const VISIT_LENGTH_S: number;
33
+ /** When a visitor was first seen, from a `Cookie` header or `document.cookie` — none for a first visit. */
34
+ export declare const firstSeenOf: (cookies: string | undefined, host: string | undefined) => number | undefined;
35
+ /** Whether a visitor first seen then is returning now (`now` in ms). */
36
+ export declare const isReturning: (firstSeen: number | undefined, now: number) => boolean;
37
+ /** The cookie's whole text, for `document.cookie`: first seen now. */
38
+ export declare const seenCookie: (host: string | undefined, now: number, { secure }: {
39
+ secure: boolean;
40
+ }) => string;
41
+ /** A visitor's facts, as a request says them: its cookies, its agent and its languages. */
42
+ export declare const visitorFactsOf: ({ cookies, host, userAgent, mobileHint, acceptLanguage, now }: {
43
+ cookies: string | undefined;
44
+ host: string | undefined;
45
+ userAgent: string | undefined;
46
+ mobileHint?: string;
47
+ acceptLanguage: string | undefined;
48
+ now: number;
49
+ }) => VisitorFacts;
50
+ /**
51
+ * The visitor facts a space's variants chosen by rules decide on, in order: what a page of it is drawn by besides the address and the
52
+ * session — so what its pages are kept apart by. Empty for a space with no audience, or none reading a fact.
53
+ */
54
+ export declare const audienceFactsUsed: (schema: Pick<Schema, "flat"> & Partial<Pick<Schema, "components">>) => VisitorFact[];
55
+ /** The facts a page is kept apart by, written as one part of a cache key: `device=mobile;language=es`. */
56
+ export declare const audienceKeyOf: (facts: VisitorFacts, used: readonly VisitorFact[]) => string;
57
+ /**
58
+ * Which of an audience's variants a visitor is shown: the first whose rule they match — one with no conditions matches
59
+ * everybody, the "everyone else" an audience ends with — or none (`-1`).
60
+ */
61
+ export declare const chooseAudienceVariant: (rules: readonly unknown[], scope: AudienceScope) => number;
62
+ export declare const browserVisitorFacts: () => VisitorFacts;
63
+ /** Says, for the visitor's next visits, when they were first seen — once: the cookie is never moved forward. */
64
+ export declare const keepFirstSeen: () => void;
@@ -0,0 +1,66 @@
1
+ import { hasConditions as e } from "../flags/resolveFlags.mjs";
2
+ import { evaluateRuleGroup as t } from "../conditions/evaluateRuleGroup.mjs";
3
+ import { cookieFromHeader as n } from "../helpers/cookies.mjs";
4
+ import { isRecord as r } from "../helpers/isRecord.mjs";
5
+ import { chooseByOf as i, variantItemsOf as a, variantsElementsOf as o } from "../variants/variants.mjs";
6
+ // src/audiences/audiences.ts
7
+ var s = [
8
+ "returning",
9
+ "language",
10
+ "device"
11
+ ], c = (e, t) => {
12
+ if (t === "?1") return "mobile";
13
+ let n = e ?? "";
14
+ return /iPad|Tablet|PlayBook|Silk|Android(?!.*Mobile)/i.test(n) ? "tablet" : /Mobi|iPhone|iPod|Android|Windows Phone/i.test(n) ? "mobile" : "desktop";
15
+ }, l = (e) => (e ?? "").split(",")[0]?.split(";")[0]?.split("-")[0]?.trim().toLowerCase() ?? "", u = (e) => {
16
+ let t = e?.split(":")[1];
17
+ return t ? `plitzi_seen_${t}` : "plitzi_seen";
18
+ }, d = 1800, f = 31536e3, p = (e, t) => {
19
+ let r = Number(n(e, u(t)));
20
+ return Number.isInteger(r) && r > 0 ? r : void 0;
21
+ }, m = (e, t) => e !== void 0 && t / 1e3 - e > 1800, h = (e, t, { secure: n }) => [
22
+ `${u(e)}=${String(Math.floor(t / 1e3))}`,
23
+ "Path=/",
24
+ `Max-Age=${String(f)}`,
25
+ "SameSite=Lax",
26
+ ...n ? ["Secure"] : []
27
+ ].join("; "), g = ({ cookies: e, host: t, userAgent: n, mobileHint: r, acceptLanguage: i, now: a }) => ({
28
+ returning: m(p(e, t), a),
29
+ language: l(i),
30
+ device: c(n, r)
31
+ }), _ = (e, t) => {
32
+ if (!r(e)) return;
33
+ let { field: n, rules: i } = e;
34
+ if (typeof n == "string" && n.startsWith("visitor.")) {
35
+ let e = s.find((e) => `visitor.${e}` === n);
36
+ e && t.add(e);
37
+ }
38
+ Array.isArray(i) && i.forEach((e) => _(e, t));
39
+ }, v = (e) => o(e).filter(({ element: e }) => i(e.attributes) === "rules").flatMap(({ element: e, flat: t }) => a(e, t)), y = (e) => {
40
+ let t = /* @__PURE__ */ new Set();
41
+ for (let n of v(e)) _(n.attributes.when, t);
42
+ return s.filter((e) => t.has(e));
43
+ }, b = (e, t) => t.map((t) => `${t}=${String(e[t])}`).join(";"), x = (n, r) => n.findIndex((n) => !e(n) || t(n, r)), S, C = () => {
44
+ if (!S) {
45
+ let e;
46
+ try {
47
+ e = document.cookie;
48
+ } catch {
49
+ e = void 0;
50
+ }
51
+ S = g({
52
+ cookies: e,
53
+ host: window.location.host,
54
+ userAgent: navigator.userAgent,
55
+ acceptLanguage: navigator.languages.join(",") || navigator.language,
56
+ now: Date.now()
57
+ });
58
+ }
59
+ return S;
60
+ }, w = () => {
61
+ try {
62
+ let e = window.location.host;
63
+ p(document.cookie, e) === void 0 && (document.cookie = h(e, Date.now(), { secure: window.location.protocol === "https:" }));
64
+ } catch {}
65
+ };
66
+ export { s as VISITOR_FACTS, d as VISIT_LENGTH_S, y as audienceFactsUsed, b as audienceKeyOf, C as browserVisitorFacts, x as chooseAudienceVariant, c as deviceFromUserAgent, p as firstSeenOf, m as isReturning, w as keepFirstSeen, l as languageFrom, h as seenCookie, u as seenCookieName, g as visitorFactsOf };
@@ -0,0 +1 @@
1
+ export * from './audiences';
@@ -0,0 +1 @@
1
+ export { VISITOR_FACTS, VISIT_LENGTH_S, audienceFactsUsed, audienceKeyOf, browserVisitorFacts, chooseAudienceVariant, deviceFromUserAgent, firstSeenOf, isReturning, keepFirstSeen, languageFrom, seenCookie, seenCookieName, visitorFactsOf } from "./audiences.mjs";
@@ -32,40 +32,53 @@ var e = [
32
32
  appearance: "success",
33
33
  autoDismiss: !0,
34
34
  autoDismissTimeout: 5e3
35
- }, i = { addNotification: {
36
- source: "space",
37
- title: "Add Notification",
38
- strictParams: !0,
39
- params: {
40
- content: {
41
- type: "textarea",
42
- description: "The notification text shown to the user — this is the message body. There is no separate title/message/type param.",
43
- default: "Content"
44
- },
45
- placement: {
46
- type: "select",
47
- description: "Where the toast appears on screen.",
48
- default: r.placement,
49
- options: [...e]
50
- },
51
- appearance: {
52
- type: "select",
53
- description: "Visual style of the notification: success, danger, warning or info.",
54
- default: r.appearance,
55
- options: [...t]
56
- },
57
- autoDismiss: {
58
- type: "boolean",
59
- description: "Whether the notification dismisses itself after a timeout.",
60
- default: r.autoDismiss,
35
+ }, i = {
36
+ reachGoal: {
37
+ source: "space",
38
+ title: "Reach Goal",
39
+ strictParams: !0,
40
+ params: { goal: {
41
+ type: "text",
42
+ description: "The goal reached — `signup`, `checkout`: counted in the space’s analytics as a conversion of each A/B variant the visitor is shown. Letters, digits, - and _.",
43
+ default: "signup",
61
44
  canBind: !1
62
- },
63
- autoDismissTimeout: {
64
- type: "number",
65
- description: "Milliseconds before auto-dismiss. Only applies when autoDismiss is true.",
66
- default: r.autoDismissTimeout,
67
- when: (e) => e.autoDismiss === !0
45
+ } }
46
+ },
47
+ addNotification: {
48
+ source: "space",
49
+ title: "Add Notification",
50
+ strictParams: !0,
51
+ params: {
52
+ content: {
53
+ type: "textarea",
54
+ description: "The notification text shown to the user — this is the message body. There is no separate title/message/type param.",
55
+ default: "Content"
56
+ },
57
+ placement: {
58
+ type: "select",
59
+ description: "Where the toast appears on screen.",
60
+ default: r.placement,
61
+ options: [...e]
62
+ },
63
+ appearance: {
64
+ type: "select",
65
+ description: "Visual style of the notification: success, danger, warning or info.",
66
+ default: r.appearance,
67
+ options: [...t]
68
+ },
69
+ autoDismiss: {
70
+ type: "boolean",
71
+ description: "Whether the notification dismisses itself after a timeout.",
72
+ default: r.autoDismiss,
73
+ canBind: !1
74
+ },
75
+ autoDismissTimeout: {
76
+ type: "number",
77
+ description: "Milliseconds before auto-dismiss. Only applies when autoDismiss is true.",
78
+ default: r.autoDismissTimeout,
79
+ when: (e) => e.autoDismiss === !0
80
+ }
68
81
  }
69
82
  }
70
- } };
83
+ };
71
84
  export { t as NOTIFICATION_APPEARANCES, r as NOTIFICATION_DEFAULTS, e as NOTIFICATION_PLACEMENTS, n as readNotificationOptions, i as spaceCallbacks };
@@ -6,19 +6,19 @@
6
6
  * refusing a binding to a name nothing answers to, an element that may not take one of these names as its id — so a
7
7
  * new global is a global everywhere at once.
8
8
  */
9
- export declare const GLOBAL_SOURCES: readonly ["variables", "navigation", "auth", "state", "host", "theme", "flags", "computed"];
9
+ export declare const GLOBAL_SOURCES: readonly ["variables", "navigation", "auth", "state", "host", "theme", "flags", "experiments", "computed"];
10
10
  export type GlobalSource = (typeof GLOBAL_SOURCES)[number];
11
11
  /**
12
12
  * What a computed value is evaluated over: every global but `state`, which a flow hands it live, and `computed`, which
13
13
  * is what is being evaluated.
14
14
  */
15
- export declare const COMPUTED_GLOBALS: ("auth" | "host" | "navigation" | "flags" | "variables" | "theme")[];
15
+ export declare const COMPUTED_GLOBALS: ("auth" | "host" | "navigation" | "flags" | "variables" | "theme" | "experiments")[];
16
16
  /**
17
17
  * The fields of the globals whose shape the platform decides, as `GlobalSources` publishes them: a read of any other
18
18
  * — `auth.authenticated` for `auth.isAuthenticated` — resolves to nothing, and the authoring linter says so. An `open`
19
19
  * field holds whatever the page puts in it (the route's params, the signed-in account's details), so nothing below it
20
- * is a mistake. The rest of the globals are the space's own (`variables`, `flags`, `computed`, held to what it
21
- * declares) or anybody's (`state`, `host`).
20
+ * is a mistake. The rest of the globals are the space's own (`variables`, `flags`, `experiments`, `computed`, held to
21
+ * what it declares) or anybody's (`state`, `host`).
22
22
  */
23
23
  export declare const GLOBAL_SOURCE_FIELDS: {
24
24
  readonly navigation: {
@@ -7,6 +7,7 @@ var e = [
7
7
  "host",
8
8
  "theme",
9
9
  "flags",
10
+ "experiments",
10
11
  "computed"
11
12
  ], t = e.filter((e) => e !== "state" && e !== "computed"), n = {
12
13
  navigation: {
@@ -0,0 +1,24 @@
1
+ import { ExperimentAssignments, ExperimentVariant } from '../experiments/experiments';
2
+ /**
3
+ * The analytics event a visitor's variant is reported with, once — when it is drawn for them: one visitor of that
4
+ * variant. Reserved (`$`): a space's own `plitzi.track` names never start with it.
5
+ */
6
+ export declare const EXPERIMENT_EXPOSURE_EVENT = "$experiment";
7
+ /** The analytics event a goal is reached with (the `reachGoal` step), carrying the visitor's variants. */
8
+ export declare const GOAL_EVENT = "$goal";
9
+ /** A goal's name as a step writes it and the console shows it: what a conversion is counted under. */
10
+ export declare const isGoalName: (name: unknown) => name is string;
11
+ export declare const exposureProps: (experiment: string, variant: ExperimentVariant) => Record<string, string>;
12
+ export declare const goalProps: (goal: string, assignments: ExperimentAssignments) => Record<string, string>;
13
+ type EventProps = Readonly<Record<string, string | number | boolean>> | undefined;
14
+ /** What an exposure event says — the experiment and the variant shown — or nothing it can read. */
15
+ export declare const exposureOf: (props: EventProps) => {
16
+ experiment: string;
17
+ variant: ExperimentVariant;
18
+ } | undefined;
19
+ /** What a goal event says: the goal, and the visitor's variant of each experiment they were in when they reached it. */
20
+ export declare const goalOf: (props: EventProps) => {
21
+ goal: string;
22
+ assignments: ExperimentAssignments;
23
+ } | undefined;
24
+ export {};
@@ -0,0 +1,28 @@
1
+ import { isExperimentName as e, isExperimentVariant as t } from "../experiments/experiments.mjs";
2
+ // src/experimentEvents/experimentEvents.ts
3
+ var n = "$experiment", r = "$goal", i = "ab.", a = (e) => typeof e == "string" && /^[A-Za-z0-9_-]{1,64}$/.test(e), o = (e, t) => ({
4
+ experiment: e,
5
+ variant: t
6
+ }), s = (e, t) => ({
7
+ goal: e,
8
+ ...Object.fromEntries(Object.entries(t).map(([e, t]) => [`${i}${e}`, t]))
9
+ }), c = (n) => {
10
+ let r = n?.experiment, i = n?.variant;
11
+ return typeof r == "string" && e(r) && t(i) ? {
12
+ experiment: r,
13
+ variant: i
14
+ } : void 0;
15
+ }, l = (n) => {
16
+ let r = n?.goal;
17
+ if (!a(r)) return;
18
+ let o = {};
19
+ for (let [r, a] of Object.entries(n ?? {})) {
20
+ let n = r.slice(i.length);
21
+ r.startsWith(i) && e(n) && t(a) && (o[n] = a);
22
+ }
23
+ return {
24
+ goal: r,
25
+ assignments: o
26
+ };
27
+ };
28
+ export { n as EXPERIMENT_EXPOSURE_EVENT, r as GOAL_EVENT, c as exposureOf, o as exposureProps, l as goalOf, s as goalProps, a as isGoalName };
@@ -0,0 +1 @@
1
+ export * from './experimentEvents';
@@ -0,0 +1 @@
1
+ export { EXPERIMENT_EXPOSURE_EVENT, GOAL_EVENT, exposureOf, exposureProps, goalOf, goalProps, isGoalName } from "./experimentEvents.mjs";
@@ -0,0 +1,73 @@
1
+ import { Element, Schema } from '../types';
2
+ /** Every variant an experiment may have, by its place among the variants: A first. */
3
+ export declare const EXPERIMENT_VARIANTS: readonly ["a", "b", "c", "d", "e"];
4
+ export type ExperimentVariant = (typeof EXPERIMENT_VARIANTS)[number];
5
+ /** The variant each experiment showed this visitor, by the experiment's name — what `{{ experiments.<name> }}` reads. */
6
+ export type ExperimentAssignments = Record<string, ExperimentVariant>;
7
+ /**
8
+ * An experiment as the space declares it: each variant's weight, in order — its share of the visitors, against the
9
+ * others' — and the variant the builder's canvas shows in its place.
10
+ */
11
+ export type DeclaredExperiment = {
12
+ weights: number[];
13
+ builderVariant: ExperimentVariant;
14
+ };
15
+ /**
16
+ * An experiment's name: what `{{ experiments.<name> }}` reads, so — like a flag's — a valid key in a template path, and
17
+ * what the visitor's cookie keeps their variant under.
18
+ */
19
+ export declare const isExperimentName: (name: string) => boolean;
20
+ export declare const isExperimentVariant: (value: unknown) => value is ExperimentVariant;
21
+ /** A variant's weight as an element may hold one: a whole number from 0 to 100. 0 shows it to nobody. */
22
+ export declare const isExperimentWeight: (value: unknown) => value is number;
23
+ /** A variant's weight when it says none: every variant left as it was gets the same share. */
24
+ export declare const DEFAULT_VARIANT_WEIGHT = 50;
25
+ /** The variant at a place among the variants: A at 0. */
26
+ export declare const variantAt: (index: number) => ExperimentVariant | undefined;
27
+ /** A variant's place among the variants: 0 for A. */
28
+ export declare const indexOfVariant: (variant: ExperimentVariant) => number;
29
+ /** A variant's weight as it is read: its own, or the default share. */
30
+ export declare const weightOf: (variant: Element) => number;
31
+ /**
32
+ * Shares made whole percents that add up to 100, each in proportion to what it was: `[1, 1, 1]` is `[34, 33, 33]`,
33
+ * `[60, 30, 30]` is `[50, 25, 25]` — the points left after rounding down going to the largest remainders, the first
34
+ * first. All at 0 stay so: nothing is said of shares that give nobody anything.
35
+ */
36
+ export declare const sharesToHundred: (weights: readonly number[]) => number[];
37
+ /**
38
+ * The experiment a `variants` element chosen by chance runs: the name it is given — or, until it is given one, the
39
+ * element's own, made a name (`pricing-test` runs `pricing_test`), so a test runs as soon as it is placed. None for a
40
+ * name it was given that is not one (the linter says so).
41
+ */
42
+ export declare const experimentNameOf: (element: Pick<Element, "id" | "attributes">) => string | undefined;
43
+ /** What a `variants` element chosen by chance runs: its experiment's name and its variants' weights, A first. */
44
+ export declare const experimentOf: (element: Element, flat: Schema["flat"]) => ({
45
+ name: string;
46
+ } & DeclaredExperiment) | undefined;
47
+ /**
48
+ * Every experiment a space runs, by name — on its pages and in its components. Two elements naming the same experiment
49
+ * share it, so a visitor sees the same variant of both; the first one met gives the weights (the linter warns when
50
+ * they disagree). One answer for the server assigning variants before it draws and the browser assigning those it was
51
+ * not handed.
52
+ */
53
+ export declare const declaredExperiments: (schema: Pick<Schema, "flat"> & Partial<Pick<Schema, "components">>) => Record<string, DeclaredExperiment>;
54
+ export type ExperimentAssignment = {
55
+ /** Every declared experiment's variant for this visitor. */
56
+ assignments: ExperimentAssignments;
57
+ /**
58
+ * What the visitor keeps from now on: the variants drawn at random, the ones kept before among them. Nothing of an
59
+ * experiment decided for everybody — one variant left with a weight — which needs nothing kept.
60
+ */
61
+ kept: ExperimentAssignments;
62
+ /** The experiments drawn for this visitor just now — a page drawn with any is theirs alone, and they keep them. */
63
+ drawn: string[];
64
+ };
65
+ /**
66
+ * The variant of each experiment for one visitor. The one they kept, while it still has a weight — what they were
67
+ * shown is what their visit is measured on, so a change of weights does not move them; a new one drawn in proportion to
68
+ * the weights when they have none, or theirs was taken out (weight 0). An experiment with one variant left with a
69
+ * weight is ended: everybody sees that one (A when none has any).
70
+ */
71
+ export declare const assignExperiments: (declared: Record<string, DeclaredExperiment>, kept: ExperimentAssignments, random?: () => number) => ExperimentAssignment;
72
+ /** What the builder's canvas shows of each experiment: the variant its settings name, never a visitor's. */
73
+ export declare const builderExperiments: (declared: Record<string, DeclaredExperiment>) => ExperimentAssignments;
@@ -0,0 +1,74 @@
1
+ import { isFlagName as e } from "../flags/schemaFlag.mjs";
2
+ import { builderVariantOf as t, chooseByOf as n, variantItemsOf as r, variantsElementsOf as i } from "../variants/variants.mjs";
3
+ // src/experiments/experiments.ts
4
+ var a = [
5
+ "a",
6
+ "b",
7
+ "c",
8
+ "d",
9
+ "e"
10
+ ], o = (t) => e(t), s = (e) => a.some((t) => t === e), c = (e) => typeof e == "number" && Number.isInteger(e) && e >= 0 && e <= 100, l = 50, u = (e) => a[e], d = (e) => a.indexOf(e), f = (e) => c(e.attributes.weight) ? e.attributes.weight : 50, p = (e) => {
11
+ let t = e.reduce((e, t) => e + Math.max(0, t), 0);
12
+ if (t === 0) return e.map(() => 0);
13
+ let n = e.map((e) => Math.max(0, e) / t * 100), r = n.map(Math.floor), i = 100 - r.reduce((e, t) => e + t, 0), a = n.map((e, t) => ({
14
+ index: t,
15
+ remainder: e - Math.floor(e)
16
+ })).sort((e, t) => t.remainder - e.remainder || e.index - t.index);
17
+ for (let { index: e } of a.slice(0, i)) r[e] += 1;
18
+ return r;
19
+ }, m = (e) => {
20
+ let { experiment: t } = e.attributes;
21
+ if (t !== void 0 && t !== "") return typeof t == "string" && o(t) ? t : void 0;
22
+ let n = e.id.replace(/[^A-Za-z0-9_]/g, "_"), r = /^[0-9]/.test(n) ? `_${n}` : n;
23
+ return o(r) ? r : void 0;
24
+ }, h = (e, i) => {
25
+ if (e.definition.type !== "variants" || n(e.attributes) !== "chance") return;
26
+ let o = m(e);
27
+ if (o === void 0) return;
28
+ let s = r(e, i).slice(0, a.length).map(f);
29
+ return {
30
+ name: o,
31
+ weights: s,
32
+ builderVariant: u(t(e.attributes, s.length)) ?? "a"
33
+ };
34
+ }, g = (e) => {
35
+ let t = {};
36
+ for (let { element: n, flat: r } of i(e)) {
37
+ let e = h(n, r);
38
+ e && !(e.name in t) && (t[e.name] = {
39
+ weights: e.weights,
40
+ builderVariant: e.builderVariant
41
+ });
42
+ }
43
+ return t;
44
+ }, _ = (e) => e.flatMap((e, t) => {
45
+ let n = u(t);
46
+ return n && e > 0 ? [{
47
+ variant: n,
48
+ weight: e
49
+ }] : [];
50
+ }), v = (e, t) => {
51
+ let n = e.reduce((e, { weight: t }) => e + t, 0), r = t() * n;
52
+ for (let { variant: t, weight: n } of e) {
53
+ if (r < n) return t;
54
+ r -= n;
55
+ }
56
+ return e[e.length - 1]?.variant ?? "a";
57
+ }, y = (e, t, n = Math.random) => {
58
+ let r = {}, i = {}, a = [];
59
+ for (let [o, { weights: s }] of Object.entries(e)) {
60
+ let e = _(s);
61
+ if (e.length <= 1) {
62
+ r[o] = e[0]?.variant ?? "a";
63
+ continue;
64
+ }
65
+ let c = Object.hasOwn(t, o) ? t[o] : void 0, l = e.some(({ variant: e }) => e === c) ? c : void 0, u = l ?? v(e, n);
66
+ l === void 0 && a.push(o), r[o] = u, i[o] = u;
67
+ }
68
+ return {
69
+ assignments: r,
70
+ kept: i,
71
+ drawn: a
72
+ };
73
+ }, b = (e) => Object.fromEntries(Object.entries(e).map(([e, { builderVariant: t }]) => [e, t]));
74
+ export { l as DEFAULT_VARIANT_WEIGHT, a as EXPERIMENT_VARIANTS, y as assignExperiments, b as builderExperiments, g as declaredExperiments, m as experimentNameOf, h as experimentOf, d as indexOfVariant, o as isExperimentName, s as isExperimentVariant, c as isExperimentWeight, p as sharesToHundred, u as variantAt, f as weightOf };
@@ -0,0 +1,26 @@
1
+ import { ExperimentAssignments } from './experiments';
2
+ /**
3
+ * The cookie a visitor's variants are kept in, so a reload — or the next visit — shows them the same variant of every
4
+ * experiment.
5
+ *
6
+ * A cookie and not web storage for the reason the theme is one: the server draws the variant, so it has to know it
7
+ * before the page exists. Named with the port, like the flags a tester forces, because a cookie's scope has none.
8
+ */
9
+ export declare const experimentsCookieName: (host: string | undefined) => string;
10
+ /** How long a visitor keeps their variants: long enough to outlast an experiment, as a measurement needs. */
11
+ export declare const EXPERIMENTS_COOKIE_MAX_AGE_S: number;
12
+ /** `heroTest:b,pricing:a` — an experiment's name and its variant; nothing else survives the parse. */
13
+ export declare const parseExperimentList: (value: string | undefined) => ExperimentAssignments;
14
+ export declare const serializeExperimentList: (assignments: ExperimentAssignments) => string;
15
+ /** The variants a visitor kept, from a `Cookie` header or `document.cookie`. */
16
+ export declare const keptExperiments: (cookies: string | undefined, host: string | undefined) => ExperimentAssignments;
17
+ /**
18
+ * The cookie's whole text, as a `Set-Cookie` header and `document.cookie` take it: one writer for the server and the
19
+ * browser, so the two never keep it under different terms. Readable by the page (no `HttpOnly`) — the browser assigns
20
+ * what a page reached without the server never drew.
21
+ */
22
+ export declare const experimentsCookie: (host: string | undefined, assignments: ExperimentAssignments, { secure }: {
23
+ secure: boolean;
24
+ }) => string;
25
+ /** Keeps a visitor's variants in the browser — what a page assigned that the server did not draw. */
26
+ export declare const writeExperiments: (assignments: ExperimentAssignments) => void;
@@ -0,0 +1,25 @@
1
+ import { isExperimentName as e, isExperimentVariant as t } from "./experiments.mjs";
2
+ import { cookieFromHeader as n } from "../helpers/cookies.mjs";
3
+ // src/experiments/experimentsCookie.ts
4
+ var r = (e) => {
5
+ let t = e?.split(":")[1];
6
+ return t ? `plitzi_ab_${t}` : "plitzi_ab";
7
+ }, i = 15552e3, a = (n) => {
8
+ let r = {};
9
+ for (let i of n?.split(",") ?? []) {
10
+ let [n = "", a] = i.split(":");
11
+ e(n) && t(a) && (r[n] = a);
12
+ }
13
+ return r;
14
+ }, o = (n) => Object.entries(n).filter(([n, r]) => e(n) && t(r)).sort(([e], [t]) => e.localeCompare(t)).map(([e, t]) => `${e}:${t}`).join(","), s = (e, t) => a(n(e, r(t))), c = (e, t, { secure: n }) => [
15
+ `${r(e)}=${encodeURIComponent(o(t))}`,
16
+ "Path=/",
17
+ `Max-Age=${i}`,
18
+ "SameSite=Lax",
19
+ ...n ? ["Secure"] : []
20
+ ].join("; "), l = (e) => {
21
+ try {
22
+ document.cookie = c(window.location.host, e, { secure: window.location.protocol === "https:" });
23
+ } catch {}
24
+ };
25
+ export { i as EXPERIMENTS_COOKIE_MAX_AGE_S, c as experimentsCookie, r as experimentsCookieName, s as keptExperiments, a as parseExperimentList, o as serializeExperimentList, l as writeExperiments };
@@ -0,0 +1,31 @@
1
+ import { DeclaredExperiment, ExperimentAssignments, ExperimentVariant } from './experiments';
2
+ /**
3
+ * The cookie a tester forces variants with — what the dev tools' Experiments tab and an address's `?ab=` write.
4
+ *
5
+ * Apart from the visitor's own (`plitzi_ab`): forcing shows a variant without touching the one the visitor keeps, so
6
+ * a tester's visit is never measured as a side it was not drawn. A session cookie, as the flags a tester forces are,
7
+ * and only honoured where debugging is authorized — the server reads it only then, as the dev tools appear only then.
8
+ */
9
+ export declare const forcedExperimentsCookieName: (host: string | undefined) => string;
10
+ /** The address parameter a tester forces variants with: `?ab=heroTest:b,pricing:c`. */
11
+ export declare const FORCED_EXPERIMENTS_PARAM = "ab";
12
+ /** The variants a tester forced, from a `Cookie` header or `document.cookie`. */
13
+ export declare const forcedExperimentsFromCookies: (cookies: string | undefined, host: string | undefined) => ExperimentAssignments;
14
+ /** The variants an address forces (`?ab=`), over the ones the cookie forced before it. */
15
+ export declare const forcedExperimentsOf: (cookies: string | undefined, host: string | undefined, param: unknown) => ExperimentAssignments;
16
+ /** What is forced once one experiment is forced to `variant` — or, with `undefined`, once it is no longer forced. */
17
+ export declare const withForcedExperiment: (forced: ExperimentAssignments, name: string, variant: ExperimentVariant | undefined) => ExperimentAssignments;
18
+ export type ForcedExperiments = {
19
+ /** The forced variants this space can show: an experiment it runs, a variant its test has. */
20
+ applied: ExperimentAssignments;
21
+ /** What was forced and cannot be shown, each with why — said to the tester, never applied. */
22
+ ignored: {
23
+ name: string;
24
+ variant: ExperimentVariant;
25
+ reason: 'undeclared' | 'no-such-variant';
26
+ }[];
27
+ };
28
+ /** What a tester forced, held to the experiments the space runs and the variants each has. */
29
+ export declare const forcedExperimentsFor: (declared: Record<string, DeclaredExperiment>, forced: ExperimentAssignments) => ForcedExperiments;
30
+ /** Keeps what a tester forced for the next page this browser asks for; nothing forced removes the cookie. */
31
+ export declare const writeForcedExperiments: (forced: ExperimentAssignments) => void;
@@ -0,0 +1,41 @@
1
+ import { indexOfVariant as e } from "./experiments.mjs";
2
+ import { parseExperimentList as t, serializeExperimentList as n } from "./experimentsCookie.mjs";
3
+ import { cookieFromHeader as r } from "../helpers/cookies.mjs";
4
+ // src/experiments/forcedExperiments.ts
5
+ var i = (e) => {
6
+ let t = e?.split(":")[1];
7
+ return t ? `plitzi_ab_qa_${t}` : "plitzi_ab_qa";
8
+ }, a = "ab", o = (e, n) => t(r(e, i(n))), s = (e, n, r) => ({
9
+ ...o(e, n),
10
+ ...typeof r == "string" ? t(r) : {}
11
+ }), c = (e, t, n) => {
12
+ let { [t]: r, ...i } = e;
13
+ return n === void 0 ? i : {
14
+ ...i,
15
+ [t]: n
16
+ };
17
+ }, l = (t, n) => {
18
+ let r = {}, i = [];
19
+ for (let [a, o] of Object.entries(n)) {
20
+ let n = Object.hasOwn(t, a) ? t[a] : void 0;
21
+ n ? e(o) >= n.weights.length ? i.push({
22
+ name: a,
23
+ variant: o,
24
+ reason: "no-such-variant"
25
+ }) : r[a] = o : i.push({
26
+ name: a,
27
+ variant: o,
28
+ reason: "undeclared"
29
+ });
30
+ }
31
+ return {
32
+ applied: r,
33
+ ignored: i
34
+ };
35
+ }, u = (e) => {
36
+ let t = i(window.location.host), r = n(e);
37
+ try {
38
+ document.cookie = r ? `${t}=${encodeURIComponent(r)};path=/;SameSite=Lax` : `${t}=;path=/;max-age=0;SameSite=Lax`;
39
+ } catch {}
40
+ };
41
+ export { a as FORCED_EXPERIMENTS_PARAM, i as forcedExperimentsCookieName, l as forcedExperimentsFor, o as forcedExperimentsFromCookies, s as forcedExperimentsOf, c as withForcedExperiment, u as writeForcedExperiments };
@@ -0,0 +1,3 @@
1
+ export * from './experiments';
2
+ export * from './experimentsCookie';
3
+ export * from './forcedExperiments';
@@ -0,0 +1,4 @@
1
+ import { DEFAULT_VARIANT_WEIGHT as e, EXPERIMENT_VARIANTS as t, assignExperiments as n, builderExperiments as r, declaredExperiments as i, experimentNameOf as a, experimentOf as o, indexOfVariant as s, isExperimentName as c, isExperimentVariant as l, isExperimentWeight as u, sharesToHundred as d, variantAt as f, weightOf as p } from "./experiments.mjs";
2
+ import { EXPERIMENTS_COOKIE_MAX_AGE_S as m, experimentsCookie as h, experimentsCookieName as g, keptExperiments as _, parseExperimentList as v, serializeExperimentList as y, writeExperiments as b } from "./experimentsCookie.mjs";
3
+ import { FORCED_EXPERIMENTS_PARAM as x, forcedExperimentsCookieName as S, forcedExperimentsFor as C, forcedExperimentsFromCookies as w, forcedExperimentsOf as T, withForcedExperiment as E, writeForcedExperiments as D } from "./forcedExperiments.mjs";
4
+ export { e as DEFAULT_VARIANT_WEIGHT, m as EXPERIMENTS_COOKIE_MAX_AGE_S, t as EXPERIMENT_VARIANTS, x as FORCED_EXPERIMENTS_PARAM, n as assignExperiments, r as builderExperiments, i as declaredExperiments, a as experimentNameOf, o as experimentOf, h as experimentsCookie, g as experimentsCookieName, S as forcedExperimentsCookieName, C as forcedExperimentsFor, w as forcedExperimentsFromCookies, T as forcedExperimentsOf, s as indexOfVariant, c as isExperimentName, l as isExperimentVariant, u as isExperimentWeight, _ as keptExperiments, v as parseExperimentList, y as serializeExperimentList, d as sharesToHundred, f as variantAt, p as weightOf, E as withForcedExperiment, b as writeExperiments, D as writeForcedExperiments };
@@ -2,3 +2,4 @@ export * from './schemaFlag';
2
2
  export * from './resolveFlags';
3
3
  export * from './flagsCookie';
4
4
  export * from './overrides';
5
+ export * from './rollout';