@mrrocket/sdk 0.23.1 → 0.25.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/CHANGELOG.md +16 -0
- package/README.md +16 -1
- package/dist/api-client.d.ts +7 -0
- package/dist/api-client.js +2 -0
- package/dist/api-client.js.map +1 -1
- package/dist/browser/mrrocket-flows.js +3 -3
- package/dist/browser/mrrocket-messenger.js +3 -3
- package/dist/browser/mrrocket-replay.js +3 -3
- package/dist/browser/mrrocket.js +2 -1
- package/dist/client.js +59 -25
- package/dist/client.js.map +1 -1
- package/dist/context.d.ts +5 -0
- package/dist/context.js.map +1 -1
- package/dist/core.d.ts +84 -13
- package/dist/core.js +95 -14
- package/dist/core.js.map +1 -1
- package/dist/flows.d.ts +12 -1
- package/dist/flows.js +12 -1
- package/dist/flows.js.map +1 -1
- package/dist/messenger-ui.d.ts +1 -1
- package/dist/messenger-ui.js +43 -9
- package/dist/messenger-ui.js.map +1 -1
- package/dist/paywall.d.ts +6 -3
- package/dist/paywall.js +5 -5
- package/dist/paywall.js.map +1 -1
- package/dist/proxy.d.ts +3 -2
- package/dist/proxy.js +1 -1
- package/dist/proxy.js.map +1 -1
- package/dist/react-native/index.d.cts +31 -2
- package/dist/react-native/index.d.ts +31 -2
- package/dist/react-native/index.js +114 -32
- package/dist/react-native/index.js.map +1 -1
- package/dist/server.d.ts +47 -7
- package/dist/server.js +74 -9
- package/dist/server.js.map +1 -1
- package/dist/user-hash.d.ts +1 -0
- package/dist/user-hash.js +1 -0
- package/dist/user-hash.js.map +1 -1
- package/dist/web-state.d.ts +2 -1
- package/dist/web-state.js +3 -0
- package/dist/web-state.js.map +1 -1
- package/package.json +1 -1
package/dist/paywall.d.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* - `{action: "purchased", plan}` after `onPurchase` went through (a test's purchase goal counts it), and
|
|
19
19
|
* `{action: "closed", index}` when the person closed it.
|
|
20
20
|
*/
|
|
21
|
-
import { type CockpitConfig, type VariantId } from "./core.js";
|
|
21
|
+
import { type CockpitConfig, type KeptVariants, type VariantId } from "./core.js";
|
|
22
22
|
export declare const PAYWALL_EVENT = "$paywall";
|
|
23
23
|
/** One page of a paywall. Plans pages list the plans and buy; the others say why and go on. */
|
|
24
24
|
export interface PaywallPage {
|
|
@@ -100,9 +100,12 @@ export interface PaywallView {
|
|
|
100
100
|
}
|
|
101
101
|
export interface PaywallEnv {
|
|
102
102
|
config: () => CockpitConfig | null;
|
|
103
|
-
|
|
103
|
+
/** The id tests assign by; null while it is not known yet (0.24.0): a running test then shows control and records nothing. */
|
|
104
|
+
distinctId: () => string | null;
|
|
104
105
|
/** Records an event under the SDK's consent. */
|
|
105
106
|
track: (event: string, props?: Record<string, unknown>) => void;
|
|
107
|
+
/** 0.24.0: the versions kept for this session (`assignKept`). */
|
|
108
|
+
kept?: KeptVariants;
|
|
106
109
|
}
|
|
107
110
|
export interface PaywallHost {
|
|
108
111
|
/** Opens the paywall of a placement; resolves when it is bought or closed (at once with "closed" when there is none). */
|
|
@@ -115,7 +118,7 @@ export interface PaywallHost {
|
|
|
115
118
|
close(): void;
|
|
116
119
|
}
|
|
117
120
|
/** The pages of a person's variant: the test's when it runs (or shipped), else control's. */
|
|
118
|
-
export declare const paywallPages: (config: CockpitConfig | null, w: PaywallConfig, distinctId: string) => {
|
|
121
|
+
export declare const paywallPages: (config: CockpitConfig | null, w: PaywallConfig, distinctId: string | null, kept?: KeptVariants) => {
|
|
119
122
|
variant: string;
|
|
120
123
|
pages: PaywallPage[];
|
|
121
124
|
test: string | undefined;
|
package/dist/paywall.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// src/paywall.ts
|
|
2
|
-
import {
|
|
2
|
+
import { resolveVariant } from "./core.js";
|
|
3
3
|
import { t } from "./i18n.js";
|
|
4
4
|
var PAYWALL_EVENT = "$paywall";
|
|
5
|
-
var paywallPages = (config, w, distinctId) => {
|
|
5
|
+
var paywallPages = (config, w, distinctId, kept) => {
|
|
6
6
|
const e = w.experiment ? config?.experiments.find((x) => x.key === w.experiment) : void 0;
|
|
7
|
-
const id = e ?
|
|
7
|
+
const id = e ? resolveVariant(e.key, config, distinctId, {}, kept) : w.variants[0]?.id;
|
|
8
8
|
const v = w.variants.find((x) => x.id === id) ?? w.variants[0];
|
|
9
|
-
return v ? { variant: v.id, pages: v.pages, test: e?.status === "running" && !e.winner ? e.key : void 0 } : null;
|
|
9
|
+
return v ? { variant: v.id, pages: v.pages, test: e?.status === "running" && !e.winner && distinctId !== null ? e.key : void 0 } : null;
|
|
10
10
|
};
|
|
11
11
|
var createPaywallHost = (env) => {
|
|
12
12
|
let run = null;
|
|
@@ -69,7 +69,7 @@ var createPaywallHost = (env) => {
|
|
|
69
69
|
if (run) return run.promise;
|
|
70
70
|
const config = env.config();
|
|
71
71
|
const w = config?.paywalls?.find((x) => x.placement === placement);
|
|
72
|
-
const picked = w ? paywallPages(config, w, env.distinctId()) : null;
|
|
72
|
+
const picked = w ? paywallPages(config, w, env.distinctId(), env.kept) : null;
|
|
73
73
|
if (!w || !picked?.pages.length) return Promise.resolve({ outcome: "closed", index: 0 });
|
|
74
74
|
const own = new Map((options.plans ?? []).map((p) => [p.id, p]));
|
|
75
75
|
const plans = w.plans.map((p) => own.get(p.id) ?? p);
|
package/dist/paywall.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/paywall.ts"],"sourcesContent":["/**\n * Paywalls with several pages (SDK 0.14.0): what MRRocket's config serves under `paywalls` (one per placement, built\n * in MRRocket › Conversion › Paywalls) and a small controller that walks the person through its pages, page after page,\n * until the last one hands the chosen plan to the app. No DOM and no React: the web renderer (`mountPaywall` in\n * flows-ui.ts, loaded only when a paywall opens) and the React Native one (native/flows-view.ts) draw `view()` and\n * call the actions.\n *\n * MRRocket never takes a payment. The last page's button calls the app's `onPurchase(plan)`, which buys through the\n * app's own store code (StoreKit or Play through RevenueCat or Adapty, or Stripe Checkout on the web); only when it\n * resolves is the purchase recorded. It throwing keeps the paywall open with an error, resolving `false` (the person\n * closed the store sheet) keeps it open without one.\n *\n * A running test on the paywall (`experiment`) gives each person their variant's pages, by the same stable hash as\n * every other test (`assign`), and records `$exposure` when the paywall opens. What it records, as normal events under\n * the SDK's analytics consent:\n * - `$paywall` `{paywall, placement, variant, page, index, action: \"page\"}` the first time each page shows in a run,\n * so MRRocket shows how many reach page 2 and 3;\n * - `{action: \"purchased\", plan}` after `onPurchase` went through (a test's purchase goal counts it), and\n * `{action: \"closed\", index}` when the person closed it.\n */\nimport { type CockpitConfig, type VariantId, assign } from \"./core\";\nimport { t } from \"./i18n\";\n\nexport const PAYWALL_EVENT = \"$paywall\";\n\n/** One page of a paywall. Plans pages list the plans and buy; the others say why and go on. */\nexport interface PaywallPage {\n id: string;\n /** \"benefits\": a list of what the person gets; \"proof\": quotes of happy customers; \"plans\": the plans and the buy button. */\n kind: \"benefits\" | \"proof\" | \"plans\";\n title: string;\n body?: string;\n /** Benefits: one line each. */\n items?: string[];\n /** Proof: what customers said, and who. */\n quotes?: { text: string; by?: string }[];\n /** The button: \"Continue\" on the way, the buy button on the plans page (\"Start free trial\"). */\n button?: string;\n}\n\n/** A plan the paywall offers. `id` is what the app buys: the store product id or the Stripe price, as set in MRRocket. */\nexport interface PaywallPlan {\n id: string;\n name: string;\n /** As shown (\"€59.99 / year\"). The app's own store prices win when it passes `plans`. */\n price: string;\n /** A line under it (\"€5 a month\", \"7 days free\"). */\n note?: string;\n}\n\n/** A published paywall as the config serves it. */\nexport interface PaywallConfig {\n id: string;\n /** The placement's key (`onboarding_end`), what the app opens: `paywall.open(\"onboarding_end\")`. */\n placement: string;\n /** The running test on it, a key of `experiments`; each of its variants has pages under `variants`. */\n experiment?: string;\n /** Control (the live paywall) first. */\n variants: { id: VariantId; pages: PaywallPage[] }[];\n plans: PaywallPlan[];\n /** The plan picked before the person picks one. */\n highlight?: string;\n}\n\nexport interface PaywallResult {\n /**\n * \"purchased\": `onPurchase` went through; \"chosen\": the person picked a plan and there is no `onPurchase`, so the app\n * buys it now (the store's purchase still counts for a test through RevenueCat or Adapty); \"closed\": the person\n * closed it (or there was no paywall).\n */\n outcome: \"purchased\" | \"chosen\" | \"closed\";\n plan?: PaywallPlan;\n paywall?: string;\n variant?: VariantId;\n /** The page the person was on, from 0. */\n index: number;\n}\n\nexport interface PaywallOptions {\n /**\n * Buy the plan in your code (RevenueCat `purchasePackage`, Adapty `makePurchase`, Stripe Checkout). Throwing keeps\n * the paywall open with an error; `false` keeps it open without one (the person cancelled the store sheet).\n */\n onPurchase?: (plan: PaywallPlan) => unknown;\n /** Your store's plans with their local prices, by the same ids; they replace what MRRocket has for them. */\n plans?: PaywallPlan[];\n onClose?: (result: PaywallResult) => void;\n}\n\nexport interface PaywallView {\n paywall: PaywallConfig;\n variant: VariantId;\n page: PaywallPage;\n index: number;\n total: number;\n last: boolean;\n plans: PaywallPlan[];\n /** The picked plan's id. */\n plan?: string;\n busy: boolean;\n error?: string;\n}\n\nexport interface PaywallEnv {\n config: () => CockpitConfig | null;\n distinctId: () => string;\n /** Records an event under the SDK's consent. */\n track: (event: string, props?: Record<string, unknown>) => void;\n}\n\nexport interface PaywallHost {\n /** Opens the paywall of a placement; resolves when it is bought or closed (at once with \"closed\" when there is none). */\n open(placement: string, options?: PaywallOptions): Promise<PaywallResult>;\n view(): PaywallView | null;\n subscribe(listener: () => void): () => void;\n next(): void;\n back(): void;\n pick(planId: string): void;\n close(): void;\n}\n\n/** The pages of a person's variant: the test's when it runs (or shipped), else control's. */\nexport const paywallPages = (config: CockpitConfig | null, w: PaywallConfig, distinctId: string) => {\n const e = w.experiment ? config?.experiments.find((x) => x.key === w.experiment) : undefined;\n const id = e ? assign(e, distinctId) : w.variants[0]?.id;\n const v = w.variants.find((x) => x.id === id) ?? w.variants[0];\n return v ? { variant: v.id, pages: v.pages, test: e?.status === \"running\" && !e.winner ? e.key : undefined } : null;\n};\n\nexport const createPaywallHost = (env: PaywallEnv): PaywallHost => {\n let run: {\n w: PaywallConfig;\n variant: VariantId;\n pages: PaywallPage[];\n plans: PaywallPlan[];\n index: number;\n plan?: string;\n busy: boolean;\n error?: string;\n seen: Set<number>;\n options: PaywallOptions;\n resolve: (r: PaywallResult) => void;\n promise: Promise<PaywallResult>;\n } | null = null;\n const listeners = new Set<() => void>();\n const emit = () => {\n for (const l of [...listeners]) {\n try {\n l();\n } catch {\n // One renderer must not stop another.\n }\n }\n };\n const base = () => (run ? { paywall: run.w.id, placement: run.w.placement, variant: run.variant } : {});\n const show = () => {\n if (run && !run.seen.has(run.index)) {\n run.seen.add(run.index);\n env.track(PAYWALL_EVENT, { ...base(), page: run.pages[run.index].id, index: run.index, action: \"page\" });\n }\n emit();\n };\n const end = (r: PaywallResult) => {\n const c = run;\n if (!c) return;\n run = null;\n if (r.outcome === \"closed\") c.options.onClose?.(r);\n c.resolve(r);\n emit();\n };\n const result = (outcome: PaywallResult[\"outcome\"]): PaywallResult => ({\n outcome,\n index: run?.index ?? 0,\n ...(run ? { paywall: run.w.id, variant: run.variant } : {}),\n ...(outcome !== \"closed\" && run ? { plan: run.plans.find((p) => p.id === run!.plan) } : {}),\n });\n\n const buy = () => {\n const c = run;\n const plan = c?.plans.find((p) => p.id === c.plan);\n if (!c || !plan || c.busy) return;\n if (!c.options.onPurchase) return end(result(\"chosen\"));\n c.busy = true;\n c.error = undefined;\n emit();\n Promise.resolve()\n .then(() => c.options.onPurchase?.(plan))\n .then(\n (ok) => {\n c.busy = false;\n if (run !== c) return;\n if (ok === false) return emit();\n env.track(PAYWALL_EVENT, { ...base(), page: c.pages[c.index].id, index: c.index, plan: plan.id, action: \"purchased\" });\n end(result(\"purchased\"));\n },\n () => {\n c.busy = false;\n c.error = t(\"The purchase did not go through. Please try again.\");\n emit();\n },\n );\n };\n\n return {\n open: (placement, options = {}) => {\n if (run) return run.promise;\n const config = env.config();\n const w = config?.paywalls?.find((x) => x.placement === placement);\n const picked = w ? paywallPages(config, w, env.distinctId()) : null;\n if (!w || !picked?.pages.length) return Promise.resolve({ outcome: \"closed\", index: 0 });\n // The app's store prices replace MRRocket's for the same plans.\n const own = new Map((options.plans ?? []).map((p) => [p.id, p]));\n const plans = w.plans.map((p) => own.get(p.id) ?? p);\n let resolve: (r: PaywallResult) => void = () => {};\n const promise = new Promise<PaywallResult>((r) => (resolve = r));\n const plan = plans.some((p) => p.id === w.highlight) ? w.highlight : plans[0]?.id;\n run = { w, variant: picked.variant, pages: picked.pages, plans, index: 0, plan, busy: false, seen: new Set(), options, resolve, promise };\n if (picked.test) env.track(\"$exposure\", { experiment: picked.test, variant: picked.variant });\n show();\n return promise;\n },\n view: () =>\n run\n ? {\n paywall: run.w,\n variant: run.variant,\n page: run.pages[run.index],\n index: run.index,\n total: run.pages.length,\n last: run.index === run.pages.length - 1,\n plans: run.plans,\n plan: run.plan,\n busy: run.busy,\n error: run.error,\n }\n : null,\n subscribe: (l) => {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n next: () => {\n if (!run || run.busy) return;\n // A plans page or the last page buys; any other page goes on.\n if (run.pages[run.index].kind === \"plans\" || run.index === run.pages.length - 1) return buy();\n run.index++;\n run.error = undefined;\n show();\n },\n back: () => {\n if (!run || run.busy || !run.index) return;\n run.index--;\n run.error = undefined;\n emit();\n },\n pick: (id) => {\n if (!run || run.busy || !run.plans.some((p) => p.id === id)) return;\n run.plan = id;\n run.error = undefined;\n emit();\n },\n close: () => {\n if (!run || run.busy) return;\n env.track(PAYWALL_EVENT, { ...base(), index: run.index, action: \"closed\" });\n end(result(\"closed\"));\n },\n };\n};\n"],"mappings":";AAoBA,SAA6C,cAAc;AAC3D,SAAS,SAAS;AAEX,IAAM,gBAAgB;AAmGtB,IAAM,eAAe,CAAC,QAA8B,GAAkB,eAAuB;AAChG,QAAM,IAAI,EAAE,aAAa,QAAQ,YAAY,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,UAAU,IAAI;AACnF,QAAM,KAAK,IAAI,OAAO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MAAM,SAAS,EAAG,QAAO,IAAI;AAC5F,UAAI;AACJ,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,MAAM,MAAM;AACR,UAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,IAAI,MAAO;AACpC,UAAI;AACJ,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,MAAM,CAAC,OAAO;AACV,UAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,IAAI,MAAM,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,EAAG;AAC7D,UAAI,OAAO;AACX,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,OAAO,MAAM;AACT,UAAI,CAAC,OAAO,IAAI,KAAM;AACtB,UAAI,MAAM,eAAe,EAAE,GAAG,KAAK,GAAG,OAAO,IAAI,OAAO,QAAQ,SAAS,CAAC;AAC1E,UAAI,OAAO,QAAQ,CAAC;AAAA,IACxB;AAAA,EACJ;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/paywall.ts"],"sourcesContent":["/**\n * Paywalls with several pages (SDK 0.14.0): what MRRocket's config serves under `paywalls` (one per placement, built\n * in MRRocket › Conversion › Paywalls) and a small controller that walks the person through its pages, page after page,\n * until the last one hands the chosen plan to the app. No DOM and no React: the web renderer (`mountPaywall` in\n * flows-ui.ts, loaded only when a paywall opens) and the React Native one (native/flows-view.ts) draw `view()` and\n * call the actions.\n *\n * MRRocket never takes a payment. The last page's button calls the app's `onPurchase(plan)`, which buys through the\n * app's own store code (StoreKit or Play through RevenueCat or Adapty, or Stripe Checkout on the web); only when it\n * resolves is the purchase recorded. It throwing keeps the paywall open with an error, resolving `false` (the person\n * closed the store sheet) keeps it open without one.\n *\n * A running test on the paywall (`experiment`) gives each person their variant's pages, by the same stable hash as\n * every other test (`assign`), and records `$exposure` when the paywall opens. What it records, as normal events under\n * the SDK's analytics consent:\n * - `$paywall` `{paywall, placement, variant, page, index, action: \"page\"}` the first time each page shows in a run,\n * so MRRocket shows how many reach page 2 and 3;\n * - `{action: \"purchased\", plan}` after `onPurchase` went through (a test's purchase goal counts it), and\n * `{action: \"closed\", index}` when the person closed it.\n */\nimport { type CockpitConfig, type KeptVariants, type VariantId, resolveVariant } from \"./core\";\nimport { t } from \"./i18n\";\n\nexport const PAYWALL_EVENT = \"$paywall\";\n\n/** One page of a paywall. Plans pages list the plans and buy; the others say why and go on. */\nexport interface PaywallPage {\n id: string;\n /** \"benefits\": a list of what the person gets; \"proof\": quotes of happy customers; \"plans\": the plans and the buy button. */\n kind: \"benefits\" | \"proof\" | \"plans\";\n title: string;\n body?: string;\n /** Benefits: one line each. */\n items?: string[];\n /** Proof: what customers said, and who. */\n quotes?: { text: string; by?: string }[];\n /** The button: \"Continue\" on the way, the buy button on the plans page (\"Start free trial\"). */\n button?: string;\n}\n\n/** A plan the paywall offers. `id` is what the app buys: the store product id or the Stripe price, as set in MRRocket. */\nexport interface PaywallPlan {\n id: string;\n name: string;\n /** As shown (\"€59.99 / year\"). The app's own store prices win when it passes `plans`. */\n price: string;\n /** A line under it (\"€5 a month\", \"7 days free\"). */\n note?: string;\n}\n\n/** A published paywall as the config serves it. */\nexport interface PaywallConfig {\n id: string;\n /** The placement's key (`onboarding_end`), what the app opens: `paywall.open(\"onboarding_end\")`. */\n placement: string;\n /** The running test on it, a key of `experiments`; each of its variants has pages under `variants`. */\n experiment?: string;\n /** Control (the live paywall) first. */\n variants: { id: VariantId; pages: PaywallPage[] }[];\n plans: PaywallPlan[];\n /** The plan picked before the person picks one. */\n highlight?: string;\n}\n\nexport interface PaywallResult {\n /**\n * \"purchased\": `onPurchase` went through; \"chosen\": the person picked a plan and there is no `onPurchase`, so the app\n * buys it now (the store's purchase still counts for a test through RevenueCat or Adapty); \"closed\": the person\n * closed it (or there was no paywall).\n */\n outcome: \"purchased\" | \"chosen\" | \"closed\";\n plan?: PaywallPlan;\n paywall?: string;\n variant?: VariantId;\n /** The page the person was on, from 0. */\n index: number;\n}\n\nexport interface PaywallOptions {\n /**\n * Buy the plan in your code (RevenueCat `purchasePackage`, Adapty `makePurchase`, Stripe Checkout). Throwing keeps\n * the paywall open with an error; `false` keeps it open without one (the person cancelled the store sheet).\n */\n onPurchase?: (plan: PaywallPlan) => unknown;\n /** Your store's plans with their local prices, by the same ids; they replace what MRRocket has for them. */\n plans?: PaywallPlan[];\n onClose?: (result: PaywallResult) => void;\n}\n\nexport interface PaywallView {\n paywall: PaywallConfig;\n variant: VariantId;\n page: PaywallPage;\n index: number;\n total: number;\n last: boolean;\n plans: PaywallPlan[];\n /** The picked plan's id. */\n plan?: string;\n busy: boolean;\n error?: string;\n}\n\nexport interface PaywallEnv {\n config: () => CockpitConfig | null;\n /** The id tests assign by; null while it is not known yet (0.24.0): a running test then shows control and records nothing. */\n distinctId: () => string | null;\n /** Records an event under the SDK's consent. */\n track: (event: string, props?: Record<string, unknown>) => void;\n /** 0.24.0: the versions kept for this session (`assignKept`). */\n kept?: KeptVariants;\n}\n\nexport interface PaywallHost {\n /** Opens the paywall of a placement; resolves when it is bought or closed (at once with \"closed\" when there is none). */\n open(placement: string, options?: PaywallOptions): Promise<PaywallResult>;\n view(): PaywallView | null;\n subscribe(listener: () => void): () => void;\n next(): void;\n back(): void;\n pick(planId: string): void;\n close(): void;\n}\n\n/** The pages of a person's variant: the test's when it runs (or shipped), else control's. */\nexport const paywallPages = (config: CockpitConfig | null, w: PaywallConfig, distinctId: string | null, kept?: KeptVariants) => {\n const e = w.experiment ? config?.experiments.find((x) => x.key === w.experiment) : undefined;\n const id = e ? resolveVariant(e.key, config, distinctId, {}, kept) : w.variants[0]?.id;\n const v = w.variants.find((x) => x.id === id) ?? w.variants[0];\n return v ? { variant: v.id, pages: v.pages, test: e?.status === \"running\" && !e.winner && distinctId !== null ? e.key : undefined } : null;\n};\n\nexport const createPaywallHost = (env: PaywallEnv): PaywallHost => {\n let run: {\n w: PaywallConfig;\n variant: VariantId;\n pages: PaywallPage[];\n plans: PaywallPlan[];\n index: number;\n plan?: string;\n busy: boolean;\n error?: string;\n seen: Set<number>;\n options: PaywallOptions;\n resolve: (r: PaywallResult) => void;\n promise: Promise<PaywallResult>;\n } | null = null;\n const listeners = new Set<() => void>();\n const emit = () => {\n for (const l of [...listeners]) {\n try {\n l();\n } catch {\n // One renderer must not stop another.\n }\n }\n };\n const base = () => (run ? { paywall: run.w.id, placement: run.w.placement, variant: run.variant } : {});\n const show = () => {\n if (run && !run.seen.has(run.index)) {\n run.seen.add(run.index);\n env.track(PAYWALL_EVENT, { ...base(), page: run.pages[run.index].id, index: run.index, action: \"page\" });\n }\n emit();\n };\n const end = (r: PaywallResult) => {\n const c = run;\n if (!c) return;\n run = null;\n if (r.outcome === \"closed\") c.options.onClose?.(r);\n c.resolve(r);\n emit();\n };\n const result = (outcome: PaywallResult[\"outcome\"]): PaywallResult => ({\n outcome,\n index: run?.index ?? 0,\n ...(run ? { paywall: run.w.id, variant: run.variant } : {}),\n ...(outcome !== \"closed\" && run ? { plan: run.plans.find((p) => p.id === run!.plan) } : {}),\n });\n\n const buy = () => {\n const c = run;\n const plan = c?.plans.find((p) => p.id === c.plan);\n if (!c || !plan || c.busy) return;\n if (!c.options.onPurchase) return end(result(\"chosen\"));\n c.busy = true;\n c.error = undefined;\n emit();\n Promise.resolve()\n .then(() => c.options.onPurchase?.(plan))\n .then(\n (ok) => {\n c.busy = false;\n if (run !== c) return;\n if (ok === false) return emit();\n env.track(PAYWALL_EVENT, { ...base(), page: c.pages[c.index].id, index: c.index, plan: plan.id, action: \"purchased\" });\n end(result(\"purchased\"));\n },\n () => {\n c.busy = false;\n c.error = t(\"The purchase did not go through. Please try again.\");\n emit();\n },\n );\n };\n\n return {\n open: (placement, options = {}) => {\n if (run) return run.promise;\n const config = env.config();\n const w = config?.paywalls?.find((x) => x.placement === placement);\n const picked = w ? paywallPages(config, w, env.distinctId(), env.kept) : null;\n if (!w || !picked?.pages.length) return Promise.resolve({ outcome: \"closed\", index: 0 });\n // The app's store prices replace MRRocket's for the same plans.\n const own = new Map((options.plans ?? []).map((p) => [p.id, p]));\n const plans = w.plans.map((p) => own.get(p.id) ?? p);\n let resolve: (r: PaywallResult) => void = () => {};\n const promise = new Promise<PaywallResult>((r) => (resolve = r));\n const plan = plans.some((p) => p.id === w.highlight) ? w.highlight : plans[0]?.id;\n run = { w, variant: picked.variant, pages: picked.pages, plans, index: 0, plan, busy: false, seen: new Set(), options, resolve, promise };\n if (picked.test) env.track(\"$exposure\", { experiment: picked.test, variant: picked.variant });\n show();\n return promise;\n },\n view: () =>\n run\n ? {\n paywall: run.w,\n variant: run.variant,\n page: run.pages[run.index],\n index: run.index,\n total: run.pages.length,\n last: run.index === run.pages.length - 1,\n plans: run.plans,\n plan: run.plan,\n busy: run.busy,\n error: run.error,\n }\n : null,\n subscribe: (l) => {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n next: () => {\n if (!run || run.busy) return;\n // A plans page or the last page buys; any other page goes on.\n if (run.pages[run.index].kind === \"plans\" || run.index === run.pages.length - 1) return buy();\n run.index++;\n run.error = undefined;\n show();\n },\n back: () => {\n if (!run || run.busy || !run.index) return;\n run.index--;\n run.error = undefined;\n emit();\n },\n pick: (id) => {\n if (!run || run.busy || !run.plans.some((p) => p.id === id)) return;\n run.plan = id;\n run.error = undefined;\n emit();\n },\n close: () => {\n if (!run || run.busy) return;\n env.track(PAYWALL_EVENT, { ...base(), index: run.index, action: \"closed\" });\n end(result(\"closed\"));\n },\n };\n};\n"],"mappings":";AAoBA,SAAgE,sBAAsB;AACtF,SAAS,SAAS;AAEX,IAAM,gBAAgB;AAsGtB,IAAM,eAAe,CAAC,QAA8B,GAAkB,YAA2B,SAAwB;AAC5H,QAAM,IAAI,EAAE,aAAa,QAAQ,YAAY,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,UAAU,IAAI;AACnF,QAAM,KAAK,IAAI,eAAe,EAAE,KAAK,QAAQ,YAAY,CAAC,GAAG,IAAI,IAAI,EAAE,SAAS,CAAC,GAAG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QAAO,IAAI;AAC5F,UAAI;AACJ,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,MAAM,MAAM;AACR,UAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,IAAI,MAAO;AACpC,UAAI;AACJ,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,MAAM,CAAC,OAAO;AACV,UAAI,CAAC,OAAO,IAAI,QAAQ,CAAC,IAAI,MAAM,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,EAAG;AAC7D,UAAI,OAAO;AACX,UAAI,QAAQ;AACZ,WAAK;AAAA,IACT;AAAA,IACA,OAAO,MAAM;AACT,UAAI,CAAC,OAAO,IAAI,KAAM;AACtB,UAAI,MAAM,eAAe,EAAE,GAAG,KAAK,GAAG,OAAO,IAAI,OAAO,QAAQ,SAAS,CAAC;AAC1E,UAAI,OAAO,QAAQ,CAAC;AAAA,IACxB;AAAA,EACJ;AACJ;","names":[]}
|
package/dist/proxy.d.ts
CHANGED
|
@@ -35,14 +35,15 @@ export declare const withCockpitId: (incoming: IncomingCookies, outgoing: Outgoi
|
|
|
35
35
|
* usual and the provider records it.
|
|
36
36
|
*
|
|
37
37
|
* A running test needs a stable visitor id to keep each person in one variant: the `ck_id` cookie (consent "cookies",
|
|
38
|
-
* with `withCockpitId` before this),
|
|
38
|
+
* with `withCockpitId` before this), your signed-in user id as `distinctId`, or (0.24.0, cookieless) the visitor's unit
|
|
39
|
+
* from a config read with the request's headers and `MRROCKET_SERVER_KEY`. Without any, the test is left to the
|
|
39
40
|
* browser (the provider redirects after the first paint); page redirects and shipped winners still answer here.
|
|
40
41
|
*
|
|
41
42
|
* import { NextResponse, type NextRequest } from "next/server";
|
|
42
43
|
* import { experimentRedirect, getCockpitConfig } from "@mrrocket/sdk/next/proxy";
|
|
43
44
|
*
|
|
44
45
|
* export async function proxy(request: NextRequest) {
|
|
45
|
-
* const to = experimentRedirect(request, await getCockpitConfig({ timeoutMs: 800 }));
|
|
46
|
+
* const to = experimentRedirect(request, await getCockpitConfig({ timeoutMs: 800, headers: request.headers }));
|
|
46
47
|
* return to ? NextResponse.redirect(to, 307) : NextResponse.next();
|
|
47
48
|
* }
|
|
48
49
|
* export const config = { matcher: ["/pricing", "/old-pricing"] };
|
package/dist/proxy.js
CHANGED
|
@@ -9,7 +9,7 @@ var withCockpitId = (incoming, outgoing) => {
|
|
|
9
9
|
outgoing.set(ID_COOKIE, id, { path: "/", maxAge: 60 * 60 * 24 * 365, sameSite: "lax" });
|
|
10
10
|
};
|
|
11
11
|
var experimentRedirect = (request, config, options = {}) => {
|
|
12
|
-
const id = options.distinctId ?? request.cookies?.get(ID_COOKIE)?.value ?? null;
|
|
12
|
+
const id = options.distinctId ?? request.cookies?.get(ID_COOKIE)?.value ?? config?.unit ?? null;
|
|
13
13
|
let preview = {};
|
|
14
14
|
try {
|
|
15
15
|
preview = parsePreview(new URL(request.url).searchParams.get(PREVIEW_PARAM));
|
package/dist/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { type CockpitConfig, ID_COOKIE, PREVIEW_PARAM, newDistinctId, parsePreview, redirectFor } from \"./core\";\n\nexport { getCockpitConfig } from \"./server\";\nexport { type SecurityOptions, reportSuspiciousRequest, watchRequest } from \"./security\";\n\n/** What `request.cookies` (Next's `RequestCookies`) offers: read, and set so that the page of this request sees the id. */\ninterface IncomingCookies {\n get(name: string): { value: string } | undefined;\n set(name: string, value: string): unknown;\n}\n\n/** What `response.cookies` (Next's `ResponseCookies`) offers: set with options. */\ninterface OutgoingCookies {\n set(name: string, value: string, options?: { path?: string; maxAge?: number; sameSite?: \"lax\" | \"strict\" | \"none\" }): unknown;\n}\n\n/**\n * For `proxy.ts`: gives a first-time visitor their `ck_id` before the page renders, so the server already assigns\n * the variant they will keep. It writes a cookie, so only use it with `consent=\"cookies\"` and only once the visitor\n * agreed; in the default cookieless mode leave it out.\n *\n * export function proxy(request: NextRequest) {\n * const response = NextResponse.next();\n * withCockpitId(request.cookies, response.cookies);\n * return response;\n * }\n */\nexport const withCockpitId = (incoming: IncomingCookies, outgoing: OutgoingCookies) => {\n if (incoming.get(ID_COOKIE)) return;\n const id = newDistinctId();\n incoming.set(ID_COOKIE, id);\n outgoing.set(ID_COOKIE, id, { path: \"/\", maxAge: 60 * 60 * 24 * 365, sameSite: \"lax\" });\n};\n\n/**\n * For `proxy.ts`: where to send this request, or null to render it. Page redirects set in MRRocket (Landing pages ›\n * Redirect) and redirect tests (Experiments) answer before the page renders, so nobody sees the old page flash. The\n * page the visitor lands on records the test's exposure (`<CockpitProvider>` reads `ck_x` there); control renders as\n * usual and the provider records it.\n *\n * A running test needs a stable visitor id to keep each person in one variant: the `ck_id` cookie (consent \"cookies\",\n * with `withCockpitId` before this),
|
|
1
|
+
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { type CockpitConfig, ID_COOKIE, PREVIEW_PARAM, newDistinctId, parsePreview, redirectFor } from \"./core\";\n\nexport { getCockpitConfig } from \"./server\";\nexport { type SecurityOptions, reportSuspiciousRequest, watchRequest } from \"./security\";\n\n/** What `request.cookies` (Next's `RequestCookies`) offers: read, and set so that the page of this request sees the id. */\ninterface IncomingCookies {\n get(name: string): { value: string } | undefined;\n set(name: string, value: string): unknown;\n}\n\n/** What `response.cookies` (Next's `ResponseCookies`) offers: set with options. */\ninterface OutgoingCookies {\n set(name: string, value: string, options?: { path?: string; maxAge?: number; sameSite?: \"lax\" | \"strict\" | \"none\" }): unknown;\n}\n\n/**\n * For `proxy.ts`: gives a first-time visitor their `ck_id` before the page renders, so the server already assigns\n * the variant they will keep. It writes a cookie, so only use it with `consent=\"cookies\"` and only once the visitor\n * agreed; in the default cookieless mode leave it out.\n *\n * export function proxy(request: NextRequest) {\n * const response = NextResponse.next();\n * withCockpitId(request.cookies, response.cookies);\n * return response;\n * }\n */\nexport const withCockpitId = (incoming: IncomingCookies, outgoing: OutgoingCookies) => {\n if (incoming.get(ID_COOKIE)) return;\n const id = newDistinctId();\n incoming.set(ID_COOKIE, id);\n outgoing.set(ID_COOKIE, id, { path: \"/\", maxAge: 60 * 60 * 24 * 365, sameSite: \"lax\" });\n};\n\n/**\n * For `proxy.ts`: where to send this request, or null to render it. Page redirects set in MRRocket (Landing pages ›\n * Redirect) and redirect tests (Experiments) answer before the page renders, so nobody sees the old page flash. The\n * page the visitor lands on records the test's exposure (`<CockpitProvider>` reads `ck_x` there); control renders as\n * usual and the provider records it.\n *\n * A running test needs a stable visitor id to keep each person in one variant: the `ck_id` cookie (consent \"cookies\",\n * with `withCockpitId` before this), your signed-in user id as `distinctId`, or (0.24.0, cookieless) the visitor's unit\n * from a config read with the request's headers and `MRROCKET_SERVER_KEY`. Without any, the test is left to the\n * browser (the provider redirects after the first paint); page redirects and shipped winners still answer here.\n *\n * import { NextResponse, type NextRequest } from \"next/server\";\n * import { experimentRedirect, getCockpitConfig } from \"@mrrocket/sdk/next/proxy\";\n *\n * export async function proxy(request: NextRequest) {\n * const to = experimentRedirect(request, await getCockpitConfig({ timeoutMs: 800, headers: request.headers }));\n * return to ? NextResponse.redirect(to, 307) : NextResponse.next();\n * }\n * export const config = { matcher: [\"/pricing\", \"/old-pricing\"] };\n *\n * Keep the matcher to the pages with a redirect or a test: each matched request waits for the config (at most\n * `timeoutMs`; MRRocket unreachable means no redirect).\n */\nexport const experimentRedirect = (\n request: { url: string; cookies?: { get(name: string): { value: string } | undefined } },\n config: CockpitConfig | null,\n options: { distinctId?: string } = {},\n): string | null => {\n // 0.24.0: a config read with the request's headers (`getCockpitConfig({ headers: request.headers })`) carries the\n // visitor's unit, so a cookieless visitor is sent on here too, the same way the browser would send them.\n const id = options.distinctId ?? request.cookies?.get(ID_COOKIE)?.value ?? config?.unit ?? null;\n let preview = {};\n try {\n preview = parsePreview(new URL(request.url).searchParams.get(PREVIEW_PARAM));\n } catch {\n return null;\n }\n return redirectFor(config, request.url, id, preview)?.to ?? null;\n};\n"],"mappings":";AAAA,SAA6B,WAAW,eAAe,eAAe,cAAc,mBAAmB;AAEvG,SAAS,wBAAwB;AACjC,SAA+B,yBAAyB,oBAAoB;AAwBrE,IAAM,gBAAgB,CAAC,UAA2B,aAA8B;AACnF,MAAI,SAAS,IAAI,SAAS,EAAG;AAC7B,QAAM,KAAK,cAAc;AACzB,WAAS,IAAI,WAAW,EAAE;AAC1B,WAAS,IAAI,WAAW,IAAI,EAAE,MAAM,KAAK,QAAQ,KAAK,KAAK,KAAK,KAAK,UAAU,MAAM,CAAC;AAC1F;AAyBO,IAAM,qBAAqB,CAC9B,SACA,QACA,UAAmC,CAAC,MACpB;AAGhB,QAAM,KAAK,QAAQ,cAAc,QAAQ,SAAS,IAAI,SAAS,GAAG,SAAS,QAAQ,QAAQ;AAC3F,MAAI,UAAU,CAAC;AACf,MAAI;AACA,cAAU,aAAa,IAAI,IAAI,QAAQ,GAAG,EAAE,aAAa,IAAI,aAAa,CAAC;AAAA,EAC/E,QAAQ;AACJ,WAAO;AAAA,EACX;AACA,SAAO,YAAY,QAAQ,QAAQ,KAAK,IAAI,OAAO,GAAG,MAAM;AAChE;","names":[]}
|
|
@@ -200,12 +200,15 @@ interface ChatSummary {
|
|
|
200
200
|
subject: string;
|
|
201
201
|
/** The newest message, its body cut to a preview. */
|
|
202
202
|
last: ChatMessage | null;
|
|
203
|
+
/** Up to when the visitor read it, on any device (servers from 0.24.0). */
|
|
204
|
+
seenAt?: string;
|
|
203
205
|
}
|
|
204
206
|
/** One chat (the one asked for, else the visitor's newest, open or closed) and its messages. */
|
|
205
207
|
interface ChatThread {
|
|
206
208
|
conversation: {
|
|
207
209
|
id: string;
|
|
208
210
|
status: "open" | "solved";
|
|
211
|
+
seenAt?: string;
|
|
209
212
|
} | null;
|
|
210
213
|
messages: ChatMessage[];
|
|
211
214
|
/** All the visitor's conversations, the latest activity first (0.19). */
|
|
@@ -278,6 +281,10 @@ declare const createApi: (c: Conn) => {
|
|
|
278
281
|
attachments?: string[];
|
|
279
282
|
context?: ChatContext;
|
|
280
283
|
}) => Promise<ChatThread>;
|
|
284
|
+
/** The visitor read a chat up to `at` (the newest message they saw): MRRocket counts it read on every device and shows "Seen" to the team (0.24.0). */
|
|
285
|
+
seenChat: (conversation: string, at: string) => Promise<{
|
|
286
|
+
seenAt: string | null;
|
|
287
|
+
}>;
|
|
281
288
|
/** Uploads one picked file for the next message (pictures and PDFs, at most 10 MB); answers its upload id. */
|
|
282
289
|
uploadChatFile: (file: ChatUpload, signal?: AbortSignal) => Promise<ChatFile>;
|
|
283
290
|
/** Where a sent file of this visitor's chat is read (an image's src, a PDF's link). */
|
|
@@ -541,6 +548,12 @@ interface OnboardingFlow {
|
|
|
541
548
|
when?: string;
|
|
542
549
|
/** 0.11.0+: the whole flow with its waits and conditions. Read instead of `steps` when it is there. */
|
|
543
550
|
nodes?: OnboardingNode[];
|
|
551
|
+
/**
|
|
552
|
+
* 0.24.0: the key of the A/B test running on this item (Conversion › Experiments). The SDK records the test's
|
|
553
|
+
* `$exposure` when the SDK shows the item (its "started" or "shown"); what each version changes is drawn by the app with
|
|
554
|
+
* `useVariant(experiment)` for now, since MRRocket's onboarding versions carry no content the SDK could draw yet.
|
|
555
|
+
*/
|
|
556
|
+
experiment?: string;
|
|
544
557
|
}
|
|
545
558
|
/** An in-app message a Lifecycle journey has for this person. */
|
|
546
559
|
interface InAppMessage {
|
|
@@ -829,7 +842,7 @@ interface PaywallHost {
|
|
|
829
842
|
|
|
830
843
|
type VariantId = string;
|
|
831
844
|
/** This build of the SDK. Sent with the config request so MRRocket knows what the site runs (error tracking, flags). */
|
|
832
|
-
declare const SDK_VERSION = "0.
|
|
845
|
+
declare const SDK_VERSION = "0.25.0";
|
|
833
846
|
/** One variant of an experiment. `changes` replaces texts that the page marks with `<Text id>`. */
|
|
834
847
|
interface VariantConfig {
|
|
835
848
|
id: VariantId;
|
|
@@ -894,6 +907,14 @@ interface CockpitConfig {
|
|
|
894
907
|
redirects?: RedirectConfig[];
|
|
895
908
|
/** Published paywalls with their pages, per placement (MRRocket › Conversion › Paywalls), for `paywall.open()`. SDK 0.14.0+. */
|
|
896
909
|
paywalls?: PaywallConfig[];
|
|
910
|
+
/**
|
|
911
|
+
* The visitor's unit for A/B tests (0.24.0): the same daily visitor hash MRRocket counts a cookieless visitor by
|
|
912
|
+
* (`v_…`, from the key, the day, the address and the browser, with a server secret). Without a signed-in id or the
|
|
913
|
+
* `ck_id` cookie, tests and flags assign by it, so a visitor keeps their version all day across reloads and between
|
|
914
|
+
* server render and browser, and their events carry it as `distinctId`. Held in memory only, never stored on the
|
|
915
|
+
* device (a React Native app leaves it out of its cached config). Missing from an older MRRocket.
|
|
916
|
+
*/
|
|
917
|
+
unit?: string;
|
|
897
918
|
}
|
|
898
919
|
/** One condition of a flag's audience, on a property of the person (`identify(id, props)`), the id or the page. */
|
|
899
920
|
interface FlagCondition {
|
|
@@ -920,6 +941,11 @@ interface FlagConfig {
|
|
|
920
941
|
split: number;
|
|
921
942
|
payload?: string;
|
|
922
943
|
}[];
|
|
944
|
+
/**
|
|
945
|
+
* The key of the A/B test the flag measures, while it runs (0.24.0). Reading the flag then records one `$exposure`
|
|
946
|
+
* per page view; `flagExposure` says which version.
|
|
947
|
+
*/
|
|
948
|
+
experiment?: string;
|
|
923
949
|
}
|
|
924
950
|
/**
|
|
925
951
|
* Session recordings of a product, from the config. Recording also needs the visitor's own replay consent
|
|
@@ -935,7 +961,10 @@ interface ReplayConfig {
|
|
|
935
961
|
/** One event as it goes over the wire to `POST /api/sdk/events`. */
|
|
936
962
|
interface CockpitEvent {
|
|
937
963
|
event: string;
|
|
938
|
-
/**
|
|
964
|
+
/**
|
|
965
|
+
* The visitor. In "cookieless" mode until identify() it is the config's `unit` (0.24.0, MRRocket's own daily visitor
|
|
966
|
+
* hash, held in memory), or missing before the config has one: the server then makes that hash itself.
|
|
967
|
+
*/
|
|
939
968
|
distinctId?: string;
|
|
940
969
|
/** Ends after 30 minutes without an event. */
|
|
941
970
|
sessionId?: string;
|
|
@@ -200,12 +200,15 @@ interface ChatSummary {
|
|
|
200
200
|
subject: string;
|
|
201
201
|
/** The newest message, its body cut to a preview. */
|
|
202
202
|
last: ChatMessage | null;
|
|
203
|
+
/** Up to when the visitor read it, on any device (servers from 0.24.0). */
|
|
204
|
+
seenAt?: string;
|
|
203
205
|
}
|
|
204
206
|
/** One chat (the one asked for, else the visitor's newest, open or closed) and its messages. */
|
|
205
207
|
interface ChatThread {
|
|
206
208
|
conversation: {
|
|
207
209
|
id: string;
|
|
208
210
|
status: "open" | "solved";
|
|
211
|
+
seenAt?: string;
|
|
209
212
|
} | null;
|
|
210
213
|
messages: ChatMessage[];
|
|
211
214
|
/** All the visitor's conversations, the latest activity first (0.19). */
|
|
@@ -278,6 +281,10 @@ declare const createApi: (c: Conn) => {
|
|
|
278
281
|
attachments?: string[];
|
|
279
282
|
context?: ChatContext;
|
|
280
283
|
}) => Promise<ChatThread>;
|
|
284
|
+
/** The visitor read a chat up to `at` (the newest message they saw): MRRocket counts it read on every device and shows "Seen" to the team (0.24.0). */
|
|
285
|
+
seenChat: (conversation: string, at: string) => Promise<{
|
|
286
|
+
seenAt: string | null;
|
|
287
|
+
}>;
|
|
281
288
|
/** Uploads one picked file for the next message (pictures and PDFs, at most 10 MB); answers its upload id. */
|
|
282
289
|
uploadChatFile: (file: ChatUpload, signal?: AbortSignal) => Promise<ChatFile>;
|
|
283
290
|
/** Where a sent file of this visitor's chat is read (an image's src, a PDF's link). */
|
|
@@ -541,6 +548,12 @@ interface OnboardingFlow {
|
|
|
541
548
|
when?: string;
|
|
542
549
|
/** 0.11.0+: the whole flow with its waits and conditions. Read instead of `steps` when it is there. */
|
|
543
550
|
nodes?: OnboardingNode[];
|
|
551
|
+
/**
|
|
552
|
+
* 0.24.0: the key of the A/B test running on this item (Conversion › Experiments). The SDK records the test's
|
|
553
|
+
* `$exposure` when the SDK shows the item (its "started" or "shown"); what each version changes is drawn by the app with
|
|
554
|
+
* `useVariant(experiment)` for now, since MRRocket's onboarding versions carry no content the SDK could draw yet.
|
|
555
|
+
*/
|
|
556
|
+
experiment?: string;
|
|
544
557
|
}
|
|
545
558
|
/** An in-app message a Lifecycle journey has for this person. */
|
|
546
559
|
interface InAppMessage {
|
|
@@ -829,7 +842,7 @@ interface PaywallHost {
|
|
|
829
842
|
|
|
830
843
|
type VariantId = string;
|
|
831
844
|
/** This build of the SDK. Sent with the config request so MRRocket knows what the site runs (error tracking, flags). */
|
|
832
|
-
declare const SDK_VERSION = "0.
|
|
845
|
+
declare const SDK_VERSION = "0.25.0";
|
|
833
846
|
/** One variant of an experiment. `changes` replaces texts that the page marks with `<Text id>`. */
|
|
834
847
|
interface VariantConfig {
|
|
835
848
|
id: VariantId;
|
|
@@ -894,6 +907,14 @@ interface CockpitConfig {
|
|
|
894
907
|
redirects?: RedirectConfig[];
|
|
895
908
|
/** Published paywalls with their pages, per placement (MRRocket › Conversion › Paywalls), for `paywall.open()`. SDK 0.14.0+. */
|
|
896
909
|
paywalls?: PaywallConfig[];
|
|
910
|
+
/**
|
|
911
|
+
* The visitor's unit for A/B tests (0.24.0): the same daily visitor hash MRRocket counts a cookieless visitor by
|
|
912
|
+
* (`v_…`, from the key, the day, the address and the browser, with a server secret). Without a signed-in id or the
|
|
913
|
+
* `ck_id` cookie, tests and flags assign by it, so a visitor keeps their version all day across reloads and between
|
|
914
|
+
* server render and browser, and their events carry it as `distinctId`. Held in memory only, never stored on the
|
|
915
|
+
* device (a React Native app leaves it out of its cached config). Missing from an older MRRocket.
|
|
916
|
+
*/
|
|
917
|
+
unit?: string;
|
|
897
918
|
}
|
|
898
919
|
/** One condition of a flag's audience, on a property of the person (`identify(id, props)`), the id or the page. */
|
|
899
920
|
interface FlagCondition {
|
|
@@ -920,6 +941,11 @@ interface FlagConfig {
|
|
|
920
941
|
split: number;
|
|
921
942
|
payload?: string;
|
|
922
943
|
}[];
|
|
944
|
+
/**
|
|
945
|
+
* The key of the A/B test the flag measures, while it runs (0.24.0). Reading the flag then records one `$exposure`
|
|
946
|
+
* per page view; `flagExposure` says which version.
|
|
947
|
+
*/
|
|
948
|
+
experiment?: string;
|
|
923
949
|
}
|
|
924
950
|
/**
|
|
925
951
|
* Session recordings of a product, from the config. Recording also needs the visitor's own replay consent
|
|
@@ -935,7 +961,10 @@ interface ReplayConfig {
|
|
|
935
961
|
/** One event as it goes over the wire to `POST /api/sdk/events`. */
|
|
936
962
|
interface CockpitEvent {
|
|
937
963
|
event: string;
|
|
938
|
-
/**
|
|
964
|
+
/**
|
|
965
|
+
* The visitor. In "cookieless" mode until identify() it is the config's `unit` (0.24.0, MRRocket's own daily visitor
|
|
966
|
+
* hash, held in memory), or missing before the config has one: the server then makes that hash itself.
|
|
967
|
+
*/
|
|
939
968
|
distinctId?: string;
|
|
940
969
|
/** Ends after 30 minutes without an event. */
|
|
941
970
|
sessionId?: string;
|