@mrrocket/sdk 0.10.0 → 0.12.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/dist/flows.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/flows.ts"],"sourcesContent":["/**\n * The cancel flow, onboarding and in-app messages (SDK 0.7.0): what MRRocket's config and `GET /api/sdk/inapp` serve,\n * and one controller that decides what shows when and records what the person did. No DOM and no React: the web\n * renderer (flows-ui.ts, loaded only when something shows) and the React Native one (native/flows-view.ts) draw\n * `view()` and call the actions.\n *\n * What it records, as normal events under the SDK's analytics consent (nothing with consent \"off\"):\n * - `$cancel_flow` `{action: \"shown\" | \"answered\" | \"offer\" | \"finished\", run, version, …}`. MRRocket turns a\n * `finished` with `outcome: \"saved\"` into a save and with `outcome: \"cancelled\"` into a cancellation on the Churn page.\n * MRRocket never changes a subscription: an accepted offer goes to the app's `onSave`, a confirmed cancel to its\n * `onCancel`, and only after they succeed is the outcome recorded.\n * - `$onboarding` `{flow, step?, action: \"started\" | \"shown\" | \"completed\" | \"dismissed\"}`.\n * - `$inapp` `{message, journey, action: \"shown\" | \"clicked\" | \"dismissed\"}`; the journey moves the person on.\n */\nimport type { CockpitConfig } from \"./core\";\n\nexport const CANCEL_EVENT = \"$cancel_flow\";\nexport const ONBOARDING_EVENT = \"$onboarding\";\nexport const INAPP_EVENT = \"$inapp\";\n\nexport type CancelOfferKind = \"discount\" | \"pause\" | \"downgrade\" | \"contact\";\n\n/** What one answer of the survey offers before the confirmation. An answer without one goes straight to it. */\nexport interface CancelOffer {\n kind: CancelOfferKind;\n headline: string;\n /** Discount: percent off. */\n percent?: number;\n /** Discount: for how many months; pause: up to how many months. */\n months?: number;\n /** Downgrade: the plan to switch to, as MRRocket knows it from the payment provider. */\n plan?: string;\n planName?: string;\n}\n\n/** The product's live cancel flow (MRRocket › Churn › Cancel flow). */\nexport interface CancelFlowConfig {\n /** Grows with every publish. */\n version: number;\n question: string;\n /** The survey's answers, in order. */\n reasons: { id: string; label: string }[];\n /** The offer per answer id. */\n offers: Record<string, CancelOffer>;\n confirm: { title: string; body: string; button: string };\n /**\n * Who gets the survey and the offers: business customers only. Anyone else (a consumer, or a customer the app does\n * not say) gets the plain confirmation, with nothing in the way of cancelling (§ 312k BGB).\n */\n offersFor: \"business\";\n}\n\nexport type OnboardingStepKind = \"tour\" | \"checklist\" | \"modal\";\n\nexport interface OnboardingStep {\n id: string;\n kind: OnboardingStepKind;\n title: string;\n body?: string;\n button?: string;\n /** Tour steps: the element they point at, marked `data-ck-target=\"<target>\"` in the app. Without it: a card. */\n target?: string;\n /** Checklist items: the event that ticks them (`track(\"<event>\")`). Without it the person ticks it themselves. */\n event?: string;\n}\n\n/** A live onboarding flow's in-app steps (MRRocket › Conversion › Onboarding); its emails go out from MRRocket. */\nexport interface OnboardingFlow {\n id: string;\n name: string;\n /** When it starts: an event's words (\"Signed up\", \"First export\"), or \"First launch\" / \"First visit\". */\n trigger: string;\n steps: OnboardingStep[];\n}\n\n/** An in-app message a Lifecycle journey has for this person. */\nexport interface InAppMessage {\n id: string;\n journey?: string;\n title: string;\n body?: string;\n button?: string;\n /** Where the button goes (http(s) or a path of the app). */\n url?: string;\n style: \"banner\" | \"modal\";\n}\n\n/** What `GET /api/sdk/inapp` answers for one signed-in person. */\nexport interface InAppState {\n /** Progress per onboarding flow: the steps done and whether the person closed it. */\n onboarding: Record<string, { done: string[]; dismissed?: boolean }>;\n messages: InAppMessage[];\n}\n\nexport interface CancelFlowResult {\n /** \"cancelled\": confirmed; \"saved\": took the offer; \"kept\": closed the dialog without cancelling. */\n outcome: \"cancelled\" | \"saved\" | \"kept\";\n reason?: string;\n reasonLabel?: string;\n offer?: CancelOffer;\n /** What the customer wrote on the confirmation, if anything. */\n comment?: string;\n /** This run's id, the same as in the events. */\n run: string;\n}\n\nexport interface CancelFlowOptions {\n /**\n * \"business\" shows the survey and the offers; anything else (the default) only the plain confirmation, so a\n * consumer can always cancel in one step (§ 312k BGB).\n */\n customer?: \"business\" | \"consumer\";\n /** The customer's current price, to show what a discount costs (`{ amount: 29, currency: \"EUR\" }`). */\n price?: { amount: number; currency: string; interval?: \"month\" | \"year\" };\n /** The customer confirmed: cancel the subscription in your code. Throwing keeps the dialog open with an error. */\n onCancel?: (result: CancelFlowResult) => unknown;\n /** The customer took an offer: apply it in your code (coupon, pause, plan change). Throwing keeps the dialog open. */\n onSave?: (result: CancelFlowResult) => unknown;\n /** Closed without cancelling or taking an offer. */\n onClose?: (result: CancelFlowResult) => void;\n}\n\n/** What a renderer draws. */\nexport type FlowView =\n | {\n type: \"cancel\";\n screen: \"survey\" | \"offer\" | \"confirm\";\n flow: CancelFlowConfig;\n /** The answer picked on the survey. */\n reason?: string;\n offer?: CancelOffer;\n price?: CancelFlowOptions[\"price\"];\n busy: boolean;\n error?: string;\n /** The step for the dots, 1 to `steps`. */\n step: number;\n steps: number;\n }\n | { type: \"step\"; flow: OnboardingFlow; step: OnboardingStep; index: number; total: number; last: boolean }\n | { type: \"checklist\"; flow: OnboardingFlow; items: { step: OnboardingStep; done: boolean }[] }\n | { type: \"message\"; message: InAppMessage };\n\nexport interface FlowsEnv {\n /** Records an event under the SDK's consent (the normal queue). */\n track(event: string, props?: Record<string, unknown>): void;\n /** Whose messages and progress these are: the identify() id; undefined while nobody is signed in. */\n personId(): string | undefined;\n /** `GET /api/sdk/inapp` for that person; null when it fails. */\n fetchInApp(personId: string): Promise<InAppState | null>;\n /** Keeps onboarding progress on the device; only while the consent lets the SDK store something. */\n storage?: () => { get(key: string): string | null | Promise<string | null>; set(key: string, value: string): void } | null;\n /** Opens a message's link. */\n openUrl?: (url: string) => void;\n /** Start onboarding flows by their trigger on its own (default true). */\n auto?: boolean;\n}\n\nexport interface FlowsController {\n setConfig(config: CockpitConfig | null): void;\n /** The person changed (identify, reset, consent): loads their messages and onboarding progress. */\n refresh(): Promise<void>;\n /** An event the app recorded: starts flows whose trigger it is and ticks checklist items. */\n onEvent(name: string): void;\n openCancelFlow(options?: CancelFlowOptions): Promise<CancelFlowResult>;\n /** Starts an onboarding flow now (the first live one without an id). False when there is none to start. */\n startOnboarding(id?: string): boolean;\n view(): FlowView | null;\n subscribe(listener: () => void): () => void;\n /** `accept(comment)`: a \"Talk to us\" offer sends what the customer wrote with it. */\n cancel: { pick(reason: string): void; next(): void; accept(comment?: string): void; decline(): void; confirm(comment?: string): void; close(): void };\n onboarding: { next(): void; check(stepId: string): void; dismiss(): void };\n message: { click(): void; dismiss(): void };\n}\n\n/** The plain confirmation, when the product has no live cancel flow (or MRRocket was not reachable). */\nexport const PLAIN_CANCEL: CancelFlowConfig = {\n version: 0,\n question: \"\",\n reasons: [],\n offers: {},\n confirm: { title: \"Cancel your subscription?\", body: \"You keep access until the end of the billing period.\", button: \"Cancel subscription\" },\n offersFor: \"business\",\n};\n\nconst FAILED = \"Something went wrong. Please try again.\";\nconst PROGRESS_KEY = \"mrrocket:onboarding\";\n\n/** `\"Signed up\"` → `signed_up`, the way MRRocket names events from words. */\nexport const eventSlug = (s: string) =>\n s\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"_\")\n .replace(/^_+|_+$/g, \"\");\n\nconst SIGNUP = new Set([\"signed_up\", \"signup\", \"sign_up\", \"registered\", \"account_created\", \"user_signed_up\"]);\n\n/** Whether recording `event` is the trigger's moment (\"Signed up\" is any of the usual signup event names). */\nexport const matchesTrigger = (trigger: string, event: string) => {\n const t = eventSlug(trigger);\n const e = eventSlug(event);\n if (!t || !e) return false;\n return t === e || (SIGNUP.has(t) && SIGNUP.has(e));\n};\n\n/** Triggers that mean \"the first time the person opens the app\": the flow starts on load until it is done. */\nexport const startsOnLoad = (trigger: string) =>\n /^(first (launch|visit|open|session|login|sign ?in|app open)|app (opened|launched)|on load|every visit|always)?$/i.test(trigger.trim());\n\n/** A random id for one cancel flow run. */\nexport const runId = () => `cf_${Math.random().toString(36).slice(2, 10)}${Date.now().toString(36)}`;\n\n/** The money a discount leaves: `29 EUR` at 30 % → `20.30`. */\nexport const discounted = (amount: number, percent: number) => Math.round(amount * (1 - percent / 100) * 100) / 100;\n\n/** \"€20.30\" in the customer's own currency, without a library. */\nexport const money = (amount: number, currency: string) => {\n try {\n return new Intl.NumberFormat(undefined, { style: \"currency\", currency }).format(amount);\n } catch {\n return `${amount.toFixed(2)} ${currency}`;\n }\n};\n\n/** The button of an offer. */\nexport const offerButton = (o: CancelOffer) =>\n o.kind === \"contact\" ? \"Send to the team\" : o.kind === \"pause\" ? \"Pause instead\" : o.kind === \"downgrade\" ? \"Switch plan\" : \"Accept offer\";\n\n/** A link a message may open: http(s), or a path of the app. Nothing else (no `javascript:`). */\nexport const safeUrl = (url: string | undefined) => {\n const u = (url ?? \"\").trim();\n return /^https?:\\/\\//i.test(u) || /^\\/(?!\\/)/.test(u) ? u : undefined;\n};\n\ninterface Progress {\n done: Set<string>;\n dismissed: boolean;\n}\n\ninterface CancelRun {\n flow: CancelFlowConfig;\n options: CancelFlowOptions;\n plain: boolean;\n screen: \"survey\" | \"offer\" | \"confirm\";\n reason?: string;\n run: string;\n busy: boolean;\n error?: string;\n resolve: (r: CancelFlowResult) => void;\n promise: Promise<CancelFlowResult>;\n}\n\nexport const createFlows = (env: FlowsEnv): FlowsController => {\n let config: CockpitConfig | null = null;\n let cancel: CancelRun | null = null;\n let active: string | null = null;\n let messages: InAppMessage[] = [];\n let known = false;\n let loading: Promise<void> | null = null;\n let loadedFor: string | undefined;\n let progressFor: string | undefined | null = null;\n const progress = new Map<string, Progress>();\n const handled = new Set<string>();\n const shown = new Set<string>();\n const listeners = new Set<() => void>();\n const auto = env.auto !== false;\n\n const flows = () => config?.onboarding ?? [];\n const flowById = (id: string | null) => (id ? flows().find((f) => f.id === id) : undefined);\n const prog = (id: string) => {\n let p = progress.get(id);\n if (!p) progress.set(id, (p = { done: new Set(), dismissed: false }));\n return p;\n };\n const finished = (f: OnboardingFlow) => {\n const p = progress.get(f.id);\n return !!p && (p.dismissed || f.steps.every((s) => p.done.has(s.id)));\n };\n\n const keyOf = (pid: string | undefined) => `${PROGRESS_KEY}:${pid ?? \"visitor\"}`;\n\n const save = () => {\n const store = env.storage?.();\n if (!store) return;\n const out: Record<string, { done: string[]; dismissed?: boolean }> = {};\n for (const [id, p] of progress) out[id] = { done: [...p.done], ...(p.dismissed ? { dismissed: true } : {}) };\n try {\n store.set(keyOf(env.personId()), JSON.stringify(out));\n } catch {\n // Storage full or gone: progress stays in memory.\n }\n };\n\n const merge = (state: InAppState[\"onboarding\"] | undefined) => {\n for (const [id, s] of Object.entries(state ?? {})) {\n const p = prog(id);\n for (const d of s.done ?? []) p.done.add(d);\n if (s.dismissed) p.dismissed = true;\n }\n };\n\n // ---------- What shows ----------\n\n const onboardingView = (): FlowView | null => {\n const flow = flowById(active);\n if (!flow) return null;\n const p = prog(flow.id);\n const at = flow.steps.findIndex((s) => !p.done.has(s.id));\n if (at < 0 || p.dismissed) return null;\n const step = flow.steps[at];\n if (step.kind === \"checklist\") {\n let from = at;\n while (from > 0 && flow.steps[from - 1].kind === \"checklist\") from--;\n let to = at;\n while (to + 1 < flow.steps.length && flow.steps[to + 1].kind === \"checklist\") to++;\n return { type: \"checklist\", flow, items: flow.steps.slice(from, to + 1).map((s) => ({ step: s, done: p.done.has(s.id) })) };\n }\n return { type: \"step\", flow, step, index: at, total: flow.steps.length, last: at === flow.steps.length - 1 };\n };\n\n const view = (): FlowView | null => {\n if (cancel) {\n const offer = cancel.reason ? cancel.flow.offers[cancel.reason] : undefined;\n return {\n type: \"cancel\",\n screen: cancel.screen,\n flow: cancel.flow,\n reason: cancel.reason,\n offer: cancel.screen === \"offer\" ? offer : undefined,\n price: cancel.options.price,\n busy: cancel.busy,\n error: cancel.error,\n step: cancel.plain ? 1 : cancel.screen === \"survey\" ? 1 : cancel.screen === \"offer\" ? 2 : 3,\n steps: cancel.plain ? 1 : 3,\n };\n }\n const ob = onboardingView();\n if (ob) return ob;\n // A flow that just ended lets the next thing show.\n if (active) active = null;\n const m = messages.find((x) => !handled.has(x.id));\n return m ? { type: \"message\", message: m } : null;\n };\n\n /** Tells the renderers, and records what became visible (once per step or message per page load). */\n const emit = () => {\n const v = view();\n if (v?.type === \"step\" && !shown.has(`s:${v.step.id}`)) {\n shown.add(`s:${v.step.id}`);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: v.step.id, kind: v.step.kind, action: \"shown\" });\n } else if (v?.type === \"checklist\") {\n for (const i of v.items)\n if (!i.done && !shown.has(`s:${i.step.id}`)) {\n shown.add(`s:${i.step.id}`);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: i.step.id, kind: \"checklist\", action: \"shown\" });\n }\n } else if (v?.type === \"message\" && !shown.has(`m:${v.message.id}`)) {\n shown.add(`m:${v.message.id}`);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"shown\" });\n }\n for (const l of [...listeners]) {\n try {\n l();\n } catch {\n // One renderer must not stop another.\n }\n }\n };\n\n const start = (flow: OnboardingFlow) => {\n if (active === flow.id) return true;\n if (active && flowById(active) && !finished(flowById(active)!)) return false;\n active = flow.id;\n env.track(ONBOARDING_EVENT, { flow: flow.id, action: \"started\" });\n emit();\n return true;\n };\n\n /** Flows whose trigger is \"the first launch\" start once their progress is known (never twice for a done flow). */\n const autoStart = () => {\n if (!auto || active || !known) return;\n // Without an id or storage a \"first launch\" could not be told from any other: no automatic start then.\n if (!env.personId() && !env.storage?.()) return;\n const next = flows().find((f) => startsOnLoad(f.trigger) && f.steps.length && !finished(f));\n if (next) start(next);\n };\n\n const refresh = () => {\n const pid = env.personId();\n // Another person on this device: nothing of the one before carries over.\n if (progressFor !== null && progressFor !== pid) {\n progress.clear();\n handled.clear();\n active = null;\n loadedFor = undefined;\n }\n progressFor = pid;\n const run = (async () => {\n const store = env.storage?.();\n if (store) {\n try {\n const raw = await store.get(keyOf(pid));\n if (raw) merge(JSON.parse(raw) as InAppState[\"onboarding\"]);\n } catch {\n // A damaged entry is ignored.\n }\n }\n const wants = flows().length > 0 || !!config?.messages;\n if (pid && wants && loadedFor !== pid) {\n loadedFor = pid;\n const state = await env.fetchInApp(pid).catch(() => null);\n if (state && env.personId() === pid) {\n merge(state.onboarding);\n messages = (state.messages ?? []).filter((m) => m && m.id && m.title);\n }\n }\n if (!pid) messages = [];\n known = true;\n autoStart();\n emit();\n })();\n loading = run.finally(() => {\n if (loading === run) loading = null;\n });\n return loading;\n };\n\n // ---------- Cancel flow ----------\n\n const result = (c: CancelRun, outcome: CancelFlowResult[\"outcome\"], comment?: string): CancelFlowResult => {\n const offer = c.reason ? c.flow.offers[c.reason] : undefined;\n return {\n outcome,\n run: c.run,\n ...(c.reason ? { reason: c.reason, reasonLabel: c.flow.reasons.find((r) => r.id === c.reason)?.label } : {}),\n ...(outcome === \"saved\" && offer ? { offer } : {}),\n ...(comment?.trim() ? { comment: comment.trim().slice(0, 500) } : {}),\n };\n };\n\n const finish = (c: CancelRun, r: CancelFlowResult) => {\n env.track(CANCEL_EVENT, {\n action: \"finished\",\n run: c.run,\n version: c.flow.version,\n outcome: r.outcome,\n customer: c.options.customer ?? \"consumer\",\n ...(r.reason ? { reason: r.reason } : {}),\n ...(r.offer ? { offer: r.offer.kind } : {}),\n ...(r.comment ? { comment: r.comment } : {}),\n });\n if (cancel === c) cancel = null;\n c.resolve(r);\n emit();\n };\n\n /** Runs the app's callback; the outcome is recorded only once it went through. */\n const settle = (c: CancelRun, outcome: \"saved\" | \"cancelled\", comment?: string) => {\n if (c.busy) return;\n const r = result(c, outcome, comment);\n const fn = outcome === \"saved\" ? c.options.onSave : c.options.onCancel;\n c.busy = true;\n c.error = undefined;\n emit();\n Promise.resolve()\n .then(() => fn?.(r))\n .then(\n () => finish(c, r),\n () => {\n c.busy = false;\n c.error = FAILED;\n emit();\n },\n );\n };\n\n const openCancelFlow = (options: CancelFlowOptions = {}) => {\n if (cancel) return cancel.promise;\n const flow = config?.cancelFlow ?? PLAIN_CANCEL;\n const plain = options.customer !== \"business\" || flow.reasons.length < 2;\n let resolve: (r: CancelFlowResult) => void = () => {};\n const promise = new Promise<CancelFlowResult>((r) => (resolve = r));\n const c: CancelRun = { flow, options, plain, screen: plain ? \"confirm\" : \"survey\", run: runId(), busy: false, resolve, promise };\n cancel = c;\n env.track(CANCEL_EVENT, { action: \"shown\", run: c.run, version: flow.version, customer: options.customer ?? \"consumer\", plain });\n emit();\n return promise;\n };\n\n const controller: FlowsController = {\n setConfig: (c) => {\n config = c;\n if (active && !flowById(active)) active = null;\n emit();\n },\n refresh,\n onEvent: (name) => {\n if (!name || name.startsWith(\"$\")) return;\n const flow = flowById(active);\n let ticked = false;\n if (flow) {\n const p = prog(flow.id);\n for (const s of flow.steps)\n if (s.kind === \"checklist\" && s.event && !p.done.has(s.id) && matchesTrigger(s.event, name)) {\n p.done.add(s.id);\n ticked = true;\n env.track(ONBOARDING_EVENT, { flow: flow.id, step: s.id, kind: s.kind, action: \"completed\" });\n }\n }\n if (ticked) {\n save();\n emit();\n }\n // Like the first-launch start: only for someone whose progress is kept, so nobody gets a flow twice.\n if (!auto || (!env.personId() && !env.storage?.())) return;\n const next = flows().find((f) => f.steps.length && matchesTrigger(f.trigger, name) && !finished(f));\n if (next) start(next);\n },\n openCancelFlow,\n startOnboarding: (id) => {\n const f = id ? flowById(id) : flows().find((x) => x.steps.length && !finished(x));\n if (!f || !f.steps.length) return false;\n const p = prog(f.id);\n // Started by hand: a flow the person closed before shows again.\n p.dismissed = false;\n return start(f);\n },\n view,\n subscribe: (l) => {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n cancel: {\n pick: (reason) => {\n if (!cancel || cancel.busy || !cancel.flow.reasons.some((r) => r.id === reason)) return;\n cancel.reason = reason;\n cancel.error = undefined;\n emit();\n },\n next: () => {\n const c = cancel;\n if (!c || c.screen !== \"survey\" || !c.reason) return;\n env.track(CANCEL_EVENT, { action: \"answered\", run: c.run, version: c.flow.version, reason: c.reason });\n c.screen = c.flow.offers[c.reason] ? \"offer\" : \"confirm\";\n emit();\n },\n accept: (comment) => {\n const c = cancel;\n if (!c || c.screen !== \"offer\" || !c.reason) return;\n const offer = c.flow.offers[c.reason];\n if (!c.busy)\n env.track(CANCEL_EVENT, {\n action: \"offer\",\n run: c.run,\n version: c.flow.version,\n reason: c.reason,\n offer: offer?.kind,\n outcome: \"accepted\",\n });\n settle(c, \"saved\", comment);\n },\n decline: () => {\n const c = cancel;\n if (!c || c.screen !== \"offer\" || c.busy) return;\n env.track(CANCEL_EVENT, {\n action: \"offer\",\n run: c.run,\n version: c.flow.version,\n reason: c.reason,\n offer: c.reason ? c.flow.offers[c.reason]?.kind : undefined,\n outcome: \"declined\",\n });\n c.screen = \"confirm\";\n c.error = undefined;\n emit();\n },\n confirm: (comment) => {\n const c = cancel;\n if (!c || c.screen !== \"confirm\") return;\n settle(c, \"cancelled\", comment);\n },\n close: () => {\n const c = cancel;\n if (!c || c.busy) return;\n const r = result(c, \"kept\");\n c.options.onClose?.(r);\n finish(c, r);\n },\n },\n onboarding: {\n next: () => {\n const v = onboardingView();\n if (v?.type !== \"step\") return;\n prog(v.flow.id).done.add(v.step.id);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: v.step.id, kind: v.step.kind, action: \"completed\" });\n save();\n emit();\n },\n check: (stepId) => {\n const v = onboardingView();\n if (v?.type !== \"checklist\") return;\n const item = v.items.find((i) => i.step.id === stepId);\n if (!item || item.done) return;\n prog(v.flow.id).done.add(stepId);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: stepId, kind: \"checklist\", action: \"completed\" });\n save();\n emit();\n },\n dismiss: () => {\n const v = onboardingView();\n if (!v || (v.type !== \"step\" && v.type !== \"checklist\")) return;\n prog(v.flow.id).dismissed = true;\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, ...(v.type === \"step\" ? { step: v.step.id } : {}), action: \"dismissed\" });\n active = null;\n save();\n emit();\n },\n },\n message: {\n click: () => {\n const v = view();\n if (v?.type !== \"message\") return;\n handled.add(v.message.id);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"clicked\" });\n const url = safeUrl(v.message.url);\n emit();\n if (url) env.openUrl?.(url);\n },\n dismiss: () => {\n const v = view();\n if (v?.type !== \"message\") return;\n handled.add(v.message.id);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"dismissed\" });\n emit();\n },\n },\n };\n return controller;\n};\n"],"mappings":";AAgBO,IAAM,eAAe;AACrB,IAAM,mBAAmB;AACzB,IAAM,cAAc;AA6JpB,IAAM,eAAiC;AAAA,EAC1C,SAAS;AAAA,EACT,UAAU;AAAA,EACV,SAAS,CAAC;AAAA,EACV,QAAQ,CAAC;AAAA,EACT,SAAS,EAAE,OAAO,6BAA6B,MAAM,wDAAwD,QAAQ,sBAAsB;AAAA,EAC3I,WAAW;AACf;AAEA,IAAM,SAAS;AACf,IAAM,eAAe;AAGd,IAAM,YAAY,CAAC,MACtB,EACK,KAAK,EACL,YAAY,EACZ,QAAQ,eAAe,GAAG,EAC1B,QAAQ,YAAY,EAAE;AAE/B,IAAM,SAAS,oBAAI,IAAI,CAAC,aAAa,UAAU,WAAW,cAAc,mBAAmB,gBAAgB,CAAC;AAGrG,IAAM,iBAAiB,CAAC,SAAiB,UAAkB;AAC9D,QAAM,IAAI,UAAU,OAAO;AAC3B,QAAM,IAAI,UAAU,KAAK;AACzB,MAAI,CAAC,KAAK,CAAC,EAAG,QAAO;AACrB,SAAO,MAAM,KAAM,OAAO,IAAI,CAAC,KAAK,OAAO,IAAI,CAAC;AACpD;AAGO,IAAM,eAAe,CAAC,YACzB,mHAAmH,KAAK,QAAQ,KAAK,CAAC;AAGnI,IAAM,QAAQ,MAAM,MAAM,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,GAAG,EAAE,CAAC,GAAG,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC;AAG3F,IAAM,aAAa,CAAC,QAAgB,YAAoB,KAAK,MAAM,UAAU,IAAI,UAAU,OAAO,GAAG,I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AQ,GAAG;AACjC,aAAK;AACL,YAAI,IAAK,KAAI,UAAU,GAAG;AAAA,MAC9B;AAAA,MACA,SAAS,MAAM;AACX,cAAM,IAAI,KAAK;AACf,YAAI,GAAG,SAAS,UAAW;AAC3B,gBAAQ,IAAI,EAAE,QAAQ,EAAE;AACxB,YAAI,MAAM,aAAa,EAAE,SAAS,EAAE,QAAQ,IAAI,SAAS,EAAE,QAAQ,SAAS,QAAQ,YAAY,CAAC;AACjG,aAAK;AAAA,MACT;AAAA,IACJ;AAAA,EACJ;AACA,SAAO;AACX;","names":[]}
1
+ {"version":3,"sources":["../src/flows.ts"],"sourcesContent":["/**\n * The cancel flow, onboarding and in-app messages (SDK 0.7.0): what MRRocket's config and `GET /api/sdk/inapp` serve,\n * and one controller that decides what shows when and records what the person did. No DOM and no React: the web\n * renderer (flows-ui.ts, loaded only when something shows) and the React Native one (native/flows-view.ts) draw\n * `view()` and call the actions.\n *\n * What it records, as normal events under the SDK's analytics consent (nothing with consent \"off\"):\n * - `$cancel_flow` `{action: \"shown\" | \"answered\" | \"offer\" | \"finished\", run, version, …}`. MRRocket turns a\n * `finished` with `outcome: \"saved\"` into a save and with `outcome: \"cancelled\"` into a cancellation on the Churn page.\n * MRRocket never changes a subscription: an accepted offer goes to the app's `onSave`, a confirmed cancel to its\n * `onCancel`, and only after they succeed is the outcome recorded.\n * - `$onboarding` `{flow, step?, action: \"started\" | \"shown\" | \"completed\" | \"dismissed\"}`; since 0.11.0 a wait records\n * \"shown\" when the person reaches it and \"completed\" when it is over, a condition `{action: \"answered\", answer: \"yes\"\n * | \"no\"}` once it is decided.\n * - `$inapp` `{message, journey, action: \"shown\" | \"clicked\" | \"dismissed\"}`; the journey moves the person on.\n */\nimport type { CockpitConfig } from \"./core\";\n\nexport const CANCEL_EVENT = \"$cancel_flow\";\nexport const ONBOARDING_EVENT = \"$onboarding\";\nexport const INAPP_EVENT = \"$inapp\";\n\nexport type CancelOfferKind = \"discount\" | \"pause\" | \"downgrade\" | \"contact\";\n\n/** What one answer of the survey offers before the confirmation. An answer without one goes straight to it. */\nexport interface CancelOffer {\n kind: CancelOfferKind;\n headline: string;\n /** Discount: percent off. */\n percent?: number;\n /** Discount: for how many months; pause: up to how many months. */\n months?: number;\n /** Downgrade: the plan to switch to, as MRRocket knows it from the payment provider. */\n plan?: string;\n planName?: string;\n}\n\n/** The product's live cancel flow (MRRocket › Churn › Cancel flow). */\nexport interface CancelFlowConfig {\n /** Grows with every publish. */\n version: number;\n question: string;\n /** The survey's answers, in order. */\n reasons: { id: string; label: string }[];\n /** The offer per answer id. */\n offers: Record<string, CancelOffer>;\n confirm: { title: string; body: string; button: string };\n /**\n * Who gets the survey and the offers: business customers only. Anyone else (a consumer, or a customer the app does\n * not say) gets the plain confirmation, with nothing in the way of cancelling (§ 312k BGB).\n */\n offersFor: \"business\";\n}\n\nexport type OnboardingStepKind = \"tour\" | \"checklist\" | \"modal\";\n\nexport interface OnboardingStep {\n id: string;\n kind: OnboardingStepKind;\n title: string;\n body?: string;\n button?: string;\n /** Tour steps: the element they point at, marked `data-ck-target=\"<target>\"` in the app. Without it: a card. */\n target?: string;\n /** Checklist items: the event that ticks them (`track(\"<event>\")`). Without it the person ticks it themselves. */\n event?: string;\n}\n\n/** A pause in a flow (0.11.0): the next step shows this long after the one before was done. */\nexport interface OnboardingWait {\n id: string;\n kind: \"wait\";\n amount: number;\n unit: \"hours\" | \"days\";\n}\n\n/**\n * A fork in a flow (0.11.0): \"Did <event> happen?\" (the person's events, here and on MRRocket) or \"Is on <page>?\" (the\n * page they are on when they reach it; `*` at the end for everything below). Decided once, when the person reaches it;\n * like a journey's branch it ends its line, the rest of the flow lives in its two paths.\n */\nexport interface OnboardingCondition {\n id: string;\n kind: \"condition\";\n event?: string;\n page?: string;\n yes: OnboardingNode[];\n no: OnboardingNode[];\n}\n\nexport type OnboardingNode = OnboardingStep | OnboardingWait | OnboardingCondition;\n\n/** A live onboarding flow's in-app steps (MRRocket › Conversion › Onboarding); its emails go out from MRRocket. */\nexport interface OnboardingFlow {\n id: string;\n name: string;\n /** When it starts: an event's words (\"Signed up\", \"First export\"), or \"First launch\" / \"First visit\". */\n trigger: string;\n /** What every version shows: with `nodes`, only the in-app steps before the first wait or condition. */\n steps: OnboardingStep[];\n /** 0.11.0+: the whole flow with its waits and conditions. Read instead of `steps` when it is there. */\n nodes?: OnboardingNode[];\n}\n\n/** An in-app message a Lifecycle journey has for this person. */\nexport interface InAppMessage {\n id: string;\n journey?: string;\n title: string;\n body?: string;\n button?: string;\n /** Where the button goes (http(s) or a path of the app). */\n url?: string;\n style: \"banner\" | \"modal\";\n}\n\n/** One flow's progress: the steps done, whether the person closed it, and (0.11.0) its decided conditions and times. */\nexport interface FlowProgress {\n done: string[];\n dismissed?: boolean;\n /** Each decided condition's path. */\n answers?: Record<string, \"yes\" | \"no\">;\n /** When each step was done and each wait was reached (ms since 1970). */\n at?: Record<string, number>;\n reached?: Record<string, number>;\n}\n\n/** What `GET /api/sdk/inapp` answers for one signed-in person. */\nexport interface InAppState {\n /** Progress per onboarding flow: the steps done and whether the person closed it. */\n onboarding: Record<string, FlowProgress>;\n messages: InAppMessage[];\n /** 0.11.0: the events the flows' conditions ask about that this person did (as `eventSlug`s). */\n events?: string[];\n}\n\nexport interface CancelFlowResult {\n /** \"cancelled\": confirmed; \"saved\": took the offer; \"kept\": closed the dialog without cancelling. */\n outcome: \"cancelled\" | \"saved\" | \"kept\";\n reason?: string;\n reasonLabel?: string;\n offer?: CancelOffer;\n /** What the customer wrote on the confirmation, if anything. */\n comment?: string;\n /** This run's id, the same as in the events. */\n run: string;\n}\n\nexport interface CancelFlowOptions {\n /**\n * \"business\" shows the survey and the offers; anything else (the default) only the plain confirmation, so a\n * consumer can always cancel in one step (§ 312k BGB).\n */\n customer?: \"business\" | \"consumer\";\n /** The customer's current price, to show what a discount costs (`{ amount: 29, currency: \"EUR\" }`). */\n price?: { amount: number; currency: string; interval?: \"month\" | \"year\" };\n /** The customer confirmed: cancel the subscription in your code. Throwing keeps the dialog open with an error. */\n onCancel?: (result: CancelFlowResult) => unknown;\n /** The customer took an offer: apply it in your code (coupon, pause, plan change). Throwing keeps the dialog open. */\n onSave?: (result: CancelFlowResult) => unknown;\n /** Closed without cancelling or taking an offer. */\n onClose?: (result: CancelFlowResult) => void;\n}\n\n/** What a renderer draws. */\nexport type FlowView =\n | {\n type: \"cancel\";\n screen: \"survey\" | \"offer\" | \"confirm\";\n flow: CancelFlowConfig;\n /** The answer picked on the survey. */\n reason?: string;\n offer?: CancelOffer;\n price?: CancelFlowOptions[\"price\"];\n busy: boolean;\n error?: string;\n /** The step for the dots, 1 to `steps`. */\n step: number;\n steps: number;\n }\n | { type: \"step\"; flow: OnboardingFlow; step: OnboardingStep; index: number; total: number; last: boolean }\n | { type: \"checklist\"; flow: OnboardingFlow; items: { step: OnboardingStep; done: boolean }[] }\n | { type: \"message\"; message: InAppMessage };\n\nexport interface FlowsEnv {\n /** Records an event under the SDK's consent (the normal queue). */\n track(event: string, props?: Record<string, unknown>): void;\n /** Whose messages and progress these are: the identify() id; undefined while nobody is signed in. */\n personId(): string | undefined;\n /** `GET /api/sdk/inapp` for that person; null when it fails. */\n fetchInApp(personId: string): Promise<InAppState | null>;\n /** Keeps onboarding progress on the device; only while the consent lets the SDK store something. */\n storage?: () => { get(key: string): string | null | Promise<string | null>; set(key: string, value: string): void } | null;\n /** Opens a message's link. */\n openUrl?: (url: string) => void;\n /** Start onboarding flows by their trigger on its own (default true). */\n auto?: boolean;\n /** The page the person is on (a path), for \"Is on <page>?\" conditions; without it they answer no. */\n path?: () => string | undefined;\n /** The clock, for tests. */\n now?: () => number;\n}\n\nexport interface FlowsController {\n setConfig(config: CockpitConfig | null): void;\n /** The person changed (identify, reset, consent): loads their messages and onboarding progress. */\n refresh(): Promise<void>;\n /** An event the app recorded: starts flows whose trigger it is and ticks checklist items. */\n onEvent(name: string): void;\n openCancelFlow(options?: CancelFlowOptions): Promise<CancelFlowResult>;\n /** Starts an onboarding flow now (the first live one without an id). False when there is none to start. */\n startOnboarding(id?: string): boolean;\n view(): FlowView | null;\n subscribe(listener: () => void): () => void;\n /** `accept(comment)`: a \"Talk to us\" offer sends what the customer wrote with it. */\n cancel: { pick(reason: string): void; next(): void; accept(comment?: string): void; decline(): void; confirm(comment?: string): void; close(): void };\n onboarding: { next(): void; check(stepId: string): void; dismiss(): void };\n message: { click(): void; dismiss(): void };\n}\n\n/** The plain confirmation, when the product has no live cancel flow (or MRRocket was not reachable). */\nexport const PLAIN_CANCEL: CancelFlowConfig = {\n version: 0,\n question: \"\",\n reasons: [],\n offers: {},\n confirm: { title: \"Cancel your subscription?\", body: \"You keep access until the end of the billing period.\", button: \"Cancel subscription\" },\n offersFor: \"business\",\n};\n\nconst FAILED = \"Something went wrong. Please try again.\";\nconst PROGRESS_KEY = \"mrrocket:onboarding\";\n\n/** `\"Signed up\"` → `signed_up`, the way MRRocket names events from words. */\nexport const eventSlug = (s: string) =>\n s\n .trim()\n .toLowerCase()\n .replace(/[^a-z0-9]+/g, \"_\")\n .replace(/^_+|_+$/g, \"\");\n\nconst SIGNUP = new Set([\"signed_up\", \"signup\", \"sign_up\", \"registered\", \"account_created\", \"user_signed_up\"]);\n\n/** Whether recording `event` is the trigger's moment (\"Signed up\" is any of the usual signup event names). */\nexport const matchesTrigger = (trigger: string, event: string) => {\n const t = eventSlug(trigger);\n const e = eventSlug(event);\n if (!t || !e) return false;\n return t === e || (SIGNUP.has(t) && SIGNUP.has(e));\n};\n\n/** Triggers that mean \"the first time the person opens the app\": the flow starts on load until it is done. */\nexport const startsOnLoad = (trigger: string) =>\n /^(first (launch|visit|open|session|login|sign ?in|app open)|app (opened|launched)|on load|every visit|always)?$/i.test(trigger.trim());\n\n/** A random id for one cancel flow run. */\nexport const runId = () => `cf_${Math.random().toString(36).slice(2, 10)}${Date.now().toString(36)}`;\n\n/** The money a discount leaves: `29 EUR` at 30 % → `20.30`. */\nexport const discounted = (amount: number, percent: number) => Math.round(amount * (1 - percent / 100) * 100) / 100;\n\n/** \"€20.30\" in the customer's own currency, without a library. */\nexport const money = (amount: number, currency: string) => {\n try {\n return new Intl.NumberFormat(undefined, { style: \"currency\", currency }).format(amount);\n } catch {\n return `${amount.toFixed(2)} ${currency}`;\n }\n};\n\n/** The button of an offer. */\nexport const offerButton = (o: CancelOffer) =>\n o.kind === \"contact\" ? \"Send to the team\" : o.kind === \"pause\" ? \"Pause instead\" : o.kind === \"downgrade\" ? \"Switch plan\" : \"Accept offer\";\n\n/** A link a message may open: http(s), or a path of the app. Nothing else (no `javascript:`). */\nexport const safeUrl = (url: string | undefined) => {\n const u = (url ?? \"\").trim();\n return /^https?:\\/\\//i.test(u) || /^\\/(?!\\/)/.test(u) ? u : undefined;\n};\n\ninterface Progress {\n done: Set<string>;\n dismissed: boolean;\n answers: Map<string, boolean>;\n at: Map<string, number>;\n reached: Map<string, number>;\n}\n\n/** The flow's nodes: the whole tree (0.11.0 config), or the plain steps of an older one. */\nexport const nodesOf = (f: OnboardingFlow): OnboardingNode[] => f.nodes ?? f.steps;\n\n/** How long a wait lasts. */\nexport const waitMs = (w: Pick<OnboardingWait, \"amount\" | \"unit\">) => Math.max(0, w.amount) * (w.unit === \"hours\" ? 3_600_000 : 86_400_000);\n\n/** Whether a path matches a condition's page: the same path, or below it for a pattern ending in `*`. */\nexport const pageMatches = (pattern: string, path: string) => {\n const p = pattern.trim();\n const at = (path.split(/[?#]/)[0] || \"/\").replace(/\\/+$/, \"\") || \"/\";\n if (!p) return false;\n if (p.endsWith(\"*\")) {\n const base = p.slice(0, -1).replace(/\\/+$/, \"\");\n return !base || at === base || at.startsWith(`${base}/`);\n }\n return at === (p.replace(/\\/+$/, \"\") || \"/\");\n};\n\n/** Every in-app step of a tree, in reading order (for ticking checklist items by their event). */\nconst stepsIn = (nodes: OnboardingNode[]): OnboardingStep[] =>\n nodes.flatMap((n) => (n.kind === \"condition\" ? [...stepsIn(n.yes), ...stepsIn(n.no)] : n.kind === \"wait\" ? [] : [n]));\n\ntype Walked =\n | { at: \"step\"; step: OnboardingStep; line: OnboardingNode[]; index: number; before: number }\n | { at: \"wait\"; until: number }\n | { at: \"pending\" }\n | { at: \"end\" };\n\ninterface CancelRun {\n flow: CancelFlowConfig;\n options: CancelFlowOptions;\n plain: boolean;\n screen: \"survey\" | \"offer\" | \"confirm\";\n reason?: string;\n run: string;\n busy: boolean;\n error?: string;\n resolve: (r: CancelFlowResult) => void;\n promise: Promise<CancelFlowResult>;\n}\n\nexport const createFlows = (env: FlowsEnv): FlowsController => {\n let config: CockpitConfig | null = null;\n let cancel: CancelRun | null = null;\n let active: string | null = null;\n let messages: InAppMessage[] = [];\n let known = false;\n let loading: Promise<void> | null = null;\n let loadedFor: string | undefined;\n let progressFor: string | undefined | null = null;\n const progress = new Map<string, Progress>();\n const handled = new Set<string>();\n const shown = new Set<string>();\n const listeners = new Set<() => void>();\n const auto = env.auto !== false;\n /** Events the person did (as slugs), for \"Did <event> happen?\": from MRRocket and from this device. */\n const seenEvents = new Set<string>();\n const now = () => env.now?.() ?? Date.now();\n let timer: ReturnType<typeof setTimeout> | null = null;\n\n const flows = () => config?.onboarding ?? [];\n const flowById = (id: string | null) => (id ? flows().find((f) => f.id === id) : undefined);\n const prog = (id: string) => {\n let p = progress.get(id);\n if (!p) progress.set(id, (p = { done: new Set(), dismissed: false, answers: new Map(), at: new Map(), reached: new Map() }));\n return p;\n };\n\n let dirty = false;\n const record = (props: Record<string, unknown>) => {\n env.track(ONBOARDING_EVENT, props);\n dirty = true;\n };\n const markDone = (p: Progress, id: string) => {\n p.done.add(id);\n p.at.set(id, now());\n };\n\n /** A condition's answer for this person, or undefined while what it asks is not known yet. */\n const decide = (c: OnboardingCondition): boolean | undefined => {\n if (c.page?.trim()) {\n const path = env.path?.();\n return path === undefined ? false : pageMatches(c.page, path);\n }\n if (c.event?.trim()) {\n // The person's events from MRRocket arrive with their progress: before that, wait.\n if (!known) return undefined;\n for (const e of seenEvents) if (matchesTrigger(c.event, e)) return true;\n return false;\n }\n return false;\n };\n\n /**\n * Where the person is in a line of the flow. `live`: it is their flow now, so reaching a wait starts it, a decided\n * condition is kept and both are recorded; otherwise nothing changes (to ask whether a flow is finished).\n */\n const walkLine = (flow: OnboardingFlow, line: OnboardingNode[], p: Progress, live: boolean, before = 0, last?: number): Walked => {\n for (let i = 0; i < line.length; i++) {\n const n = line[i];\n if (n.kind === \"wait\") {\n if (p.done.has(n.id)) {\n last = p.at.get(n.id) ?? last;\n continue;\n }\n let since = p.reached.get(n.id);\n if (since === undefined) {\n if (!live) return { at: \"pending\" };\n // Counted from when the step before was done; a flow that opens with a wait counts from now.\n since = last ?? now();\n p.reached.set(n.id, since);\n record({ flow: flow.id, step: n.id, kind: \"wait\", action: \"shown\" });\n }\n const until = since + waitMs(n);\n if (now() < until) return { at: \"wait\", until };\n if (!live) return { at: \"pending\" };\n markDone(p, n.id);\n record({ flow: flow.id, step: n.id, kind: \"wait\", action: \"completed\" });\n last = until;\n continue;\n }\n if (n.kind === \"condition\") {\n let answer = p.answers.get(n.id);\n if (answer === undefined) {\n if (!live) return { at: \"pending\" };\n answer = decide(n);\n if (answer === undefined) return { at: \"pending\" };\n p.answers.set(n.id, answer);\n record({ flow: flow.id, step: n.id, kind: \"condition\", action: \"answered\", answer: answer ? \"yes\" : \"no\" });\n }\n return walkLine(flow, answer ? n.yes : n.no, p, live, before, last);\n }\n if (!p.done.has(n.id)) return { at: \"step\", step: n, line, index: i, before };\n last = p.at.get(n.id) ?? last;\n before++;\n }\n return { at: \"end\" };\n };\n\n const finished = (f: OnboardingFlow) => {\n const p = progress.get(f.id);\n return !!p && (p.dismissed || walkLine(f, nodesOf(f), p, false).at === \"end\");\n };\n\n const keyOf = (pid: string | undefined) => `${PROGRESS_KEY}:${pid ?? \"visitor\"}`;\n const eventsKeyOf = (pid: string | undefined) => `${PROGRESS_KEY}:events:${pid ?? \"visitor\"}`;\n\n const save = () => {\n dirty = false;\n const store = env.storage?.();\n if (!store) return;\n const out: Record<string, FlowProgress> = {};\n for (const [id, p] of progress)\n out[id] = {\n done: [...p.done],\n ...(p.dismissed ? { dismissed: true } : {}),\n ...(p.answers.size ? { answers: Object.fromEntries([...p.answers].map(([k, v]) => [k, v ? \"yes\" : \"no\"])) } : {}),\n ...(p.at.size ? { at: Object.fromEntries(p.at) } : {}),\n ...(p.reached.size ? { reached: Object.fromEntries(p.reached) } : {}),\n };\n try {\n store.set(keyOf(env.personId()), JSON.stringify(out));\n if (seenEvents.size) store.set(eventsKeyOf(env.personId()), JSON.stringify([...seenEvents].slice(-200)));\n } catch {\n // Storage full or gone: progress stays in memory.\n }\n };\n\n const merge = (state: InAppState[\"onboarding\"] | undefined) => {\n for (const [id, s] of Object.entries(state ?? {})) {\n const p = prog(id);\n for (const d of s.done ?? []) p.done.add(d);\n if (s.dismissed) p.dismissed = true;\n // A condition decided elsewhere keeps its path; the earliest time wins for waits.\n for (const [k, v] of Object.entries(s.answers ?? {})) if (!p.answers.has(k)) p.answers.set(k, v === \"yes\");\n for (const [k, v] of Object.entries(s.at ?? {})) if (typeof v === \"number\" && !p.at.has(k)) p.at.set(k, v);\n for (const [k, v] of Object.entries(s.reached ?? {})) if (typeof v === \"number\" && (!p.reached.has(k) || v < p.reached.get(k)!)) p.reached.set(k, v);\n }\n };\n\n // ---------- What shows ----------\n\n /** Comes back when a short wait is over, so the next step shows without a reload (long waits wait for the next visit). */\n const wakeAt = (until: number) => {\n if (timer) clearTimeout(timer);\n timer = null;\n const ms = until - now();\n if (ms > 0 && ms <= 86_400_000) {\n timer = setTimeout(() => ((timer = null), emit()), ms + 50);\n // Never what keeps a process alive (tests, server rendering).\n (timer as { unref?: () => void }).unref?.();\n }\n };\n\n /** The active flow waits (a wait step, or a condition whose answer is not known yet): it stays the active one. */\n let holding = false;\n\n const onboardingView = (): FlowView | null => {\n holding = false;\n const flow = flowById(active);\n if (!flow) return null;\n const p = prog(flow.id);\n if (p.dismissed) return null;\n const w = walkLine(flow, nodesOf(flow), p, true);\n if (dirty) save();\n if (w.at === \"wait\") wakeAt(w.until);\n holding = w.at === \"wait\" || w.at === \"pending\";\n if (w.at !== \"step\") return null;\n const { step, line, index } = w;\n const isStep = (n: OnboardingNode | undefined): n is OnboardingStep => !!n && n.kind !== \"wait\" && n.kind !== \"condition\";\n if (step.kind === \"checklist\") {\n let from = index;\n while (from > 0 && isStep(line[from - 1]) && line[from - 1].kind === \"checklist\") from--;\n let to = index;\n while (to + 1 < line.length && isStep(line[to + 1]) && line[to + 1].kind === \"checklist\") to++;\n const items = line.slice(from, to + 1).filter(isStep);\n for (const i of items)\n if (!p.done.has(i.id) && !shown.has(`s:${i.id}`)) {\n shown.add(`s:${i.id}`);\n env.track(ONBOARDING_EVENT, { flow: flow.id, step: i.id, kind: \"checklist\", action: \"shown\" });\n }\n return { type: \"checklist\", flow, items: items.map((s) => ({ step: s, done: p.done.has(s.id) })) };\n }\n if (!shown.has(`s:${step.id}`)) {\n shown.add(`s:${step.id}`);\n env.track(ONBOARDING_EVENT, { flow: flow.id, step: step.id, kind: step.kind, action: \"shown\" });\n }\n // The dots: the steps behind on the person's path, this one, and the ones left on its line before any fork.\n const rest = line.slice(index + 1);\n const fork = rest.findIndex((n) => n.kind === \"condition\");\n const ahead = (fork < 0 ? rest : rest.slice(0, fork)).filter(isStep).length;\n return { type: \"step\", flow, step, index: w.before, total: w.before + 1 + ahead, last: !ahead && fork < 0 };\n };\n\n const view = (): FlowView | null => {\n if (cancel) {\n const offer = cancel.reason ? cancel.flow.offers[cancel.reason] : undefined;\n return {\n type: \"cancel\",\n screen: cancel.screen,\n flow: cancel.flow,\n reason: cancel.reason,\n offer: cancel.screen === \"offer\" ? offer : undefined,\n price: cancel.options.price,\n busy: cancel.busy,\n error: cancel.error,\n step: cancel.plain ? 1 : cancel.screen === \"survey\" ? 1 : cancel.screen === \"offer\" ? 2 : 3,\n steps: cancel.plain ? 1 : 3,\n };\n }\n const ob = onboardingView();\n if (ob) return ob;\n // A flow that just ended lets the next thing show; one that waits keeps its place.\n if (active && !holding) active = null;\n const m = messages.find((x) => !handled.has(x.id));\n return m ? { type: \"message\", message: m } : null;\n };\n\n /** Tells the renderers, and records what became visible (once per step or message per page load). */\n const emit = () => {\n // Onboarding steps record their \"shown\" where they are worked out (onboardingView), so a step a wait let through\n // counts also when nothing else changed.\n const v = view();\n if (v?.type === \"message\" && !shown.has(`m:${v.message.id}`)) {\n shown.add(`m:${v.message.id}`);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"shown\" });\n }\n for (const l of [...listeners]) {\n try {\n l();\n } catch {\n // One renderer must not stop another.\n }\n }\n };\n\n const start = (flow: OnboardingFlow) => {\n if (active === flow.id) return true;\n if (active && flowById(active) && !finished(flowById(active)!)) return false;\n active = flow.id;\n env.track(ONBOARDING_EVENT, { flow: flow.id, action: \"started\" });\n emit();\n return true;\n };\n\n /** Flows whose trigger is \"the first launch\" start once their progress is known (never twice for a done flow). */\n const autoStart = () => {\n if (!auto || active || !known) return;\n // Without an id or storage a \"first launch\" could not be told from any other: no automatic start then.\n if (!env.personId() && !env.storage?.()) return;\n const next = flows().find((f) => startsOnLoad(f.trigger) && nodesOf(f).length && !finished(f));\n if (next) start(next);\n };\n\n const refresh = () => {\n const pid = env.personId();\n // Another person on this device: nothing of the one before carries over.\n if (progressFor !== null && progressFor !== pid) {\n progress.clear();\n seenEvents.clear();\n handled.clear();\n active = null;\n loadedFor = undefined;\n }\n progressFor = pid;\n const run = (async () => {\n const store = env.storage?.();\n if (store) {\n try {\n const raw = await store.get(keyOf(pid));\n if (raw) merge(JSON.parse(raw) as InAppState[\"onboarding\"]);\n const seen = await store.get(eventsKeyOf(pid));\n if (seen) for (const e of JSON.parse(seen) as string[]) if (typeof e === \"string\") seenEvents.add(e);\n } catch {\n // A damaged entry is ignored.\n }\n }\n const wants = flows().length > 0 || !!config?.messages;\n if (pid && wants && loadedFor !== pid) {\n loadedFor = pid;\n const state = await env.fetchInApp(pid).catch(() => null);\n if (state && env.personId() === pid) {\n merge(state.onboarding);\n for (const e of state.events ?? []) if (typeof e === \"string\") seenEvents.add(eventSlug(e));\n messages = (state.messages ?? []).filter((m) => m && m.id && m.title);\n }\n }\n if (!pid) messages = [];\n known = true;\n autoStart();\n emit();\n })();\n loading = run.finally(() => {\n if (loading === run) loading = null;\n });\n return loading;\n };\n\n // ---------- Cancel flow ----------\n\n const result = (c: CancelRun, outcome: CancelFlowResult[\"outcome\"], comment?: string): CancelFlowResult => {\n const offer = c.reason ? c.flow.offers[c.reason] : undefined;\n return {\n outcome,\n run: c.run,\n ...(c.reason ? { reason: c.reason, reasonLabel: c.flow.reasons.find((r) => r.id === c.reason)?.label } : {}),\n ...(outcome === \"saved\" && offer ? { offer } : {}),\n ...(comment?.trim() ? { comment: comment.trim().slice(0, 500) } : {}),\n };\n };\n\n const finish = (c: CancelRun, r: CancelFlowResult) => {\n env.track(CANCEL_EVENT, {\n action: \"finished\",\n run: c.run,\n version: c.flow.version,\n outcome: r.outcome,\n customer: c.options.customer ?? \"consumer\",\n ...(r.reason ? { reason: r.reason } : {}),\n ...(r.offer ? { offer: r.offer.kind } : {}),\n ...(r.comment ? { comment: r.comment } : {}),\n });\n if (cancel === c) cancel = null;\n c.resolve(r);\n emit();\n };\n\n /** Runs the app's callback; the outcome is recorded only once it went through. */\n const settle = (c: CancelRun, outcome: \"saved\" | \"cancelled\", comment?: string) => {\n if (c.busy) return;\n const r = result(c, outcome, comment);\n const fn = outcome === \"saved\" ? c.options.onSave : c.options.onCancel;\n c.busy = true;\n c.error = undefined;\n emit();\n Promise.resolve()\n .then(() => fn?.(r))\n .then(\n () => finish(c, r),\n () => {\n c.busy = false;\n c.error = FAILED;\n emit();\n },\n );\n };\n\n const openCancelFlow = (options: CancelFlowOptions = {}) => {\n if (cancel) return cancel.promise;\n const flow = config?.cancelFlow ?? PLAIN_CANCEL;\n const plain = options.customer !== \"business\" || flow.reasons.length < 2;\n let resolve: (r: CancelFlowResult) => void = () => {};\n const promise = new Promise<CancelFlowResult>((r) => (resolve = r));\n const c: CancelRun = { flow, options, plain, screen: plain ? \"confirm\" : \"survey\", run: runId(), busy: false, resolve, promise };\n cancel = c;\n env.track(CANCEL_EVENT, { action: \"shown\", run: c.run, version: flow.version, customer: options.customer ?? \"consumer\", plain });\n emit();\n return promise;\n };\n\n const controller: FlowsController = {\n setConfig: (c) => {\n config = c;\n if (active && !flowById(active)) active = null;\n emit();\n },\n refresh,\n onEvent: (name) => {\n if (!name || name.startsWith(\"$\")) return;\n // Remembered for \"Did <event> happen?\" conditions the person reaches later.\n const slug = eventSlug(name);\n const fresh = !!slug && !seenEvents.has(slug);\n if (fresh) seenEvents.add(slug);\n const flow = flowById(active);\n let ticked = false;\n if (flow) {\n const p = prog(flow.id);\n for (const s of stepsIn(nodesOf(flow)))\n if (s.kind === \"checklist\" && s.event && !p.done.has(s.id) && matchesTrigger(s.event, name)) {\n markDone(p, s.id);\n ticked = true;\n env.track(ONBOARDING_EVENT, { flow: flow.id, step: s.id, kind: s.kind, action: \"completed\" });\n }\n }\n if (ticked || (fresh && flow)) {\n save();\n emit();\n } else if (fresh) save();\n // Like the first-launch start: only for someone whose progress is kept, so nobody gets a flow twice.\n if (!auto || (!env.personId() && !env.storage?.())) return;\n const next = flows().find((f) => nodesOf(f).length && matchesTrigger(f.trigger, name) && !finished(f));\n if (next) start(next);\n },\n openCancelFlow,\n startOnboarding: (id) => {\n const f = id ? flowById(id) : flows().find((x) => nodesOf(x).length && !finished(x));\n if (!f || !nodesOf(f).length) return false;\n const p = prog(f.id);\n // Started by hand: a flow the person closed before shows again.\n p.dismissed = false;\n return start(f);\n },\n view,\n subscribe: (l) => {\n listeners.add(l);\n return () => listeners.delete(l);\n },\n cancel: {\n pick: (reason) => {\n if (!cancel || cancel.busy || !cancel.flow.reasons.some((r) => r.id === reason)) return;\n cancel.reason = reason;\n cancel.error = undefined;\n emit();\n },\n next: () => {\n const c = cancel;\n if (!c || c.screen !== \"survey\" || !c.reason) return;\n env.track(CANCEL_EVENT, { action: \"answered\", run: c.run, version: c.flow.version, reason: c.reason });\n c.screen = c.flow.offers[c.reason] ? \"offer\" : \"confirm\";\n emit();\n },\n accept: (comment) => {\n const c = cancel;\n if (!c || c.screen !== \"offer\" || !c.reason) return;\n const offer = c.flow.offers[c.reason];\n if (!c.busy)\n env.track(CANCEL_EVENT, {\n action: \"offer\",\n run: c.run,\n version: c.flow.version,\n reason: c.reason,\n offer: offer?.kind,\n outcome: \"accepted\",\n });\n settle(c, \"saved\", comment);\n },\n decline: () => {\n const c = cancel;\n if (!c || c.screen !== \"offer\" || c.busy) return;\n env.track(CANCEL_EVENT, {\n action: \"offer\",\n run: c.run,\n version: c.flow.version,\n reason: c.reason,\n offer: c.reason ? c.flow.offers[c.reason]?.kind : undefined,\n outcome: \"declined\",\n });\n c.screen = \"confirm\";\n c.error = undefined;\n emit();\n },\n confirm: (comment) => {\n const c = cancel;\n if (!c || c.screen !== \"confirm\") return;\n settle(c, \"cancelled\", comment);\n },\n close: () => {\n const c = cancel;\n if (!c || c.busy) return;\n const r = result(c, \"kept\");\n c.options.onClose?.(r);\n finish(c, r);\n },\n },\n onboarding: {\n next: () => {\n const v = onboardingView();\n if (v?.type !== \"step\") return;\n markDone(prog(v.flow.id), v.step.id);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: v.step.id, kind: v.step.kind, action: \"completed\" });\n save();\n emit();\n },\n check: (stepId) => {\n const v = onboardingView();\n if (v?.type !== \"checklist\") return;\n const item = v.items.find((i) => i.step.id === stepId);\n if (!item || item.done) return;\n markDone(prog(v.flow.id), stepId);\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, step: stepId, kind: \"checklist\", action: \"completed\" });\n save();\n emit();\n },\n dismiss: () => {\n const v = onboardingView();\n if (!v || (v.type !== \"step\" && v.type !== \"checklist\")) return;\n prog(v.flow.id).dismissed = true;\n env.track(ONBOARDING_EVENT, { flow: v.flow.id, ...(v.type === \"step\" ? { step: v.step.id } : {}), action: \"dismissed\" });\n active = null;\n save();\n emit();\n },\n },\n message: {\n click: () => {\n const v = view();\n if (v?.type !== \"message\") return;\n handled.add(v.message.id);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"clicked\" });\n const url = safeUrl(v.message.url);\n emit();\n if (url) env.openUrl?.(url);\n },\n dismiss: () => {\n const v = view();\n if (v?.type !== \"message\") return;\n handled.add(v.message.id);\n env.track(INAPP_EVENT, { message: v.message.id, journey: v.message.journey, action: \"dismissed\" });\n emit();\n },\n },\n };\n return controller;\n};\n"],"mappings":";AAkBO,IAAM,eAAe;AACrB,IAAM,mBAAmB;AACzB,IAAM,cAAc;AAyMpB,IAAM,eAAiC;AAAA,EAC1C,SAAS;AAAA,EACT,UAAU;AAAA,EACV,SAAS,CAAC;AAAA,EACV,QAAQ,CAAC;AAAA,EACT,SAAS,EAAE,OAAO,6BAA6B,MAAM,wDAAwD,QAAQ,sBAAsB;AAAA,EAC3I,WAAW;AACf;AAEA,IAAM,SAAS;AACf,IAAM,eAAe;AAGd,IAAM,YAAY,CAAC,MACtB,EACK,KAAK,EACL,YAAY,EACZ,QAAQ,eAAe,GAAG,EAC1B,QAAQ,YAAY,EAAE;AAE/B,IAAM,SAAS,oBAAI,IAAI,CAAC,aAAa,UAAU,WAAW,cAAc,mBAAmB,gBAAgB,CAAC;AAGrG,IAAM,iBAAiB,CAAC,SAAiB,UAAkB;AAC9D,QAAM,IAAI,UAAU,OAAO;AAC3B,QAAM,IAAI,UAAU,KAAK;AACzB,MAAI,CAAC,KAAK,CAAC,EAAG,QAAO;AACrB,SAAO,MAAM,KAAM,OAAO,IAAI,CAAC,KAAK,OAAO,IAAI,CAAC;AACpD;AAGO,IAAM,eAAe,CAAC,YACzB,mHAAmH,KAAK,QAAQ,KAAK,CAAC;AAGnI,IAAM,QAAQ,MAAM,MAAM,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,GAAG,EAAE,CAAC,GAAG,KAAK,IAAI,EAAE,SAAS,EAAE,CA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AI,eAAe;AACzB,YAAI,CAAC,KAAM,EAAE,SAAS,UAAU,EAAE,SAAS,YAAc;AACzD,aAAK,EAAE,KAAK,EAAE,EAAE,YAAY;AAC5B,YAAI,MAAM,kBAAkB,EAAE,MAAM,EAAE,KAAK,IAAI,GAAI,EAAE,SAAS,SAAS,EAAE,MAAM,EAAE,KAAK,GAAG,IAAI,CAAC,GAAI,QAAQ,YAAY,CAAC;AACvH,iBAAS;AACT,aAAK;AACL,aAAK;AAAA,MACT;AAAA,IACJ;AAAA,IACA,SAAS;AAAA,MACL,OAAO,MAAM;AACT,cAAM,IAAI,KAAK;AACf,YAAI,GAAG,SAAS,UAAW;AAC3B,gBAAQ,IAAI,EAAE,QAAQ,EAAE;AACxB,YAAI,MAAM,aAAa,EAAE,SAAS,EAAE,QAAQ,IAAI,SAAS,EAAE,QAAQ,SAAS,QAAQ,UAAU,CAAC;AAC/F,cAAM,MAAM,QAAQ,EAAE,QAAQ,GAAG;AACjC,aAAK;AACL,YAAI,IAAK,KAAI,UAAU,GAAG;AAAA,MAC9B;AAAA,MACA,SAAS,MAAM;AACX,cAAM,IAAI,KAAK;AACf,YAAI,GAAG,SAAS,UAAW;AAC3B,gBAAQ,IAAI,EAAE,QAAQ,EAAE;AACxB,YAAI,MAAM,aAAa,EAAE,SAAS,EAAE,QAAQ,IAAI,SAAS,EAAE,QAAQ,SAAS,QAAQ,YAAY,CAAC;AACjG,aAAK;AAAA,MACT;AAAA,IACJ;AAAA,EACJ;AACA,SAAO;AACX;","names":[]}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Form leads (0.12.0): a form on the customer's site becomes a contact in MRRocket when it is submitted. Forms marked
3
+ * `data-mrrocket-form` (its value names the form, e.g. `data-mrrocket-form="Demo request"`) are captured; with
4
+ * auto-detection on (`data-forms="auto"` on the script tag, `forms="auto"` on the provider) also any form with an email
5
+ * field, except forms with a password field (sign-in and sign-up) and forms marked `data-mrrocket-ignore`.
6
+ *
7
+ * What is sent: the email (required), the name, and the other fields a person typed, never passwords, card fields,
8
+ * hidden fields, files or fields whose name looks sensitive (`SENSITIVE`); a field or form part marked
9
+ * `data-mrrocket-ignore` stays out too. Plus the form's name, the page, the referrer, UTM, and the state of a checkbox
10
+ * marked `data-mrrocket-consent` (the visitor's own yes to hear from the site). MRRocket records consent as the contact's
11
+ * legal basis only when that box was ticked.
12
+ *
13
+ * No React and no globals beyond the DOM, so the script tag (`sdk-forms.js`, loaded only on pages with a form to
14
+ * capture) and the Next.js provider (a dynamic import) share it. `fieldsOf` is pure for the tests.
15
+ */
16
+ /** Field names that never leave the page, whatever the form says. */
17
+ export declare const SENSITIVE: RegExp;
18
+ export declare const EMAIL_RE: RegExp;
19
+ /** Limits: what a lead may carry, also checked again by the server. */
20
+ export declare const MAX_FIELDS = 30;
21
+ export declare const MAX_VALUE = 1000;
22
+ /** One control of a form as the filter sees it. */
23
+ export interface FieldInput {
24
+ name: string;
25
+ type: string;
26
+ value: string;
27
+ autocomplete?: string;
28
+ /** A checkbox or radio that is not ticked. */
29
+ unchecked?: boolean;
30
+ /** Marked `data-mrrocket-ignore` (itself or a part of the form around it). */
31
+ ignored?: boolean;
32
+ /** Marked `data-mrrocket-consent`. */
33
+ consent?: boolean;
34
+ }
35
+ export interface FormFields {
36
+ email: string | null;
37
+ name: string | null;
38
+ /** The other fields, by name; checkbox groups joined with ", ". */
39
+ fields: Record<string, string>;
40
+ /** True or false when the form has a checkbox marked `data-mrrocket-consent`; undefined without one. */
41
+ consent?: boolean;
42
+ }
43
+ /** Whether a field's value may be sent: not a password, card, hidden or sensitive-looking field. */
44
+ export declare const sendable: (f: Pick<FieldInput, "name" | "type" | "autocomplete" | "ignored">) => boolean;
45
+ /**
46
+ * The lead in a form's controls: the email (first valid one), the name (a full-name field, or first and last), the
47
+ * consent box's state, and the other sendable fields. Pure.
48
+ */
49
+ export declare const fieldsOf: (inputs: FieldInput[]) => FormFields;
50
+ /** What the page sends to `POST /api/sdk/forms`. */
51
+ export interface FormLead extends FormFields {
52
+ key: string;
53
+ form: string;
54
+ url: string;
55
+ title?: string;
56
+ referrer?: string;
57
+ utm?: Record<string, string>;
58
+ distinctId?: string;
59
+ }
60
+ /** The form's name: the value of `data-mrrocket-form`, else its name, id or label, else "Form on /path". */
61
+ export declare const formName: (form: HTMLFormElement) => string;
62
+ /** Whether a form is captured: marked, or (auto) one with an email field and no password field. */
63
+ export declare const captures: (form: HTMLFormElement, auto: boolean) => boolean;
64
+ /** The controls of a form, read for `fieldsOf`. */
65
+ export declare const inputsOf: (form: HTMLFormElement) => FieldInput[];
66
+ export interface WatchFormsOptions {
67
+ host: string;
68
+ apiKey: string;
69
+ /** Also forms with an email field that are not marked (default false). */
70
+ auto?: boolean;
71
+ /** The visitor id to link the lead to, while analytics consent allows one. */
72
+ distinctId: () => string | undefined;
73
+ /** Whether the site already called identify(). */
74
+ identified: () => boolean;
75
+ /** Links this visitor's history to the person (the SDK's own identify). */
76
+ identify: (id: string, props: Record<string, unknown>) => void;
77
+ /** Whether analytics consent lets the SDK identify the visitor (not "off"). */
78
+ tracking: () => boolean;
79
+ referrer?: () => string | undefined;
80
+ utm?: () => Record<string, string | undefined> | undefined;
81
+ }
82
+ /**
83
+ * Captures submits from now on (one listener on the document, capture phase, so it sees forms that render later and
84
+ * forms whose own handler stops the event). The lead goes out with `sendBeacon`, which survives the page leaving. Returns
85
+ * a function that stops it. Calling it twice keeps one listener.
86
+ */
87
+ export declare const watchForms: (o: WatchFormsOptions) => (() => void);
package/dist/forms.js ADDED
@@ -0,0 +1,124 @@
1
+ // src/forms.ts
2
+ var SENSITIVE = /pass(word|wd|code)?|pwd|secret|token|csrf|xsrf|nonce|captcha|honeypot|card|^cc([-_ ]|num|exp|$)|cvv|cvc|csc|expir|iban|(^|[^a-z])bic([^a-z]|$)|swift|routing|account[-_ ]?(no|num)|sort[-_ ]?code|(^|[^a-z])ssn([^a-z]|$)|social[-_ ]?sec|tax[-_ ]?id|vat[-_ ]?id|steuer|passport|(^|[^a-z])(pin|otp|pw)([^a-z]|$)|one[-_ ]?time|auth(?!or)|api[-_ ]?key|session|signature/i;
3
+ var SKIP_TYPES = /* @__PURE__ */ new Set(["password", "hidden", "file", "submit", "button", "reset", "image"]);
4
+ var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
5
+ var MAX_FIELDS = 30;
6
+ var MAX_VALUE = 1e3;
7
+ var norm = (s) => s.toLowerCase().replace(/[\s_-]+/g, "");
8
+ var sendable = (f) => {
9
+ if (f.ignored || SKIP_TYPES.has(f.type)) return false;
10
+ const ac = (f.autocomplete ?? "").toLowerCase();
11
+ if (/^cc-|password|one-time-code/.test(ac)) return false;
12
+ return !SENSITIVE.test(f.name);
13
+ };
14
+ var isEmailField = (f) => f.type === "email" || /e-?mail/i.test(f.name) || f.autocomplete === "email";
15
+ var fieldsOf = (inputs) => {
16
+ let email = null;
17
+ let full = null;
18
+ let first = null;
19
+ let last = null;
20
+ let consent;
21
+ const fields = {};
22
+ for (const f of inputs) {
23
+ if (f.consent) {
24
+ consent = (consent ?? false) || !f.unchecked;
25
+ continue;
26
+ }
27
+ if (!f.name || !sendable(f) || f.unchecked) continue;
28
+ const value = f.value.trim().slice(0, MAX_VALUE);
29
+ if (!value) continue;
30
+ const n = norm(f.name);
31
+ const ac = (f.autocomplete ?? "").toLowerCase();
32
+ if (!email && isEmailField(f) && EMAIL_RE.test(value)) {
33
+ email = value.toLowerCase();
34
+ continue;
35
+ }
36
+ if (!full && (ac === "name" || ["name", "fullname", "yourname", "contactname"].includes(n))) {
37
+ full = value.slice(0, 100);
38
+ continue;
39
+ }
40
+ if (!first && (ac === "given-name" || ["firstname", "first", "vorname", "givenname", "fname"].includes(n))) {
41
+ first = value.slice(0, 60);
42
+ continue;
43
+ }
44
+ if (!last && (ac === "family-name" || ["lastname", "last", "nachname", "surname", "familyname", "lname"].includes(n))) {
45
+ last = value.slice(0, 60);
46
+ continue;
47
+ }
48
+ const key = f.name.trim().slice(0, 60);
49
+ if (key in fields) fields[key] = `${fields[key]}, ${value}`.slice(0, MAX_VALUE);
50
+ else if (Object.keys(fields).length < MAX_FIELDS) fields[key] = value;
51
+ }
52
+ const name = full ?? ([first, last].filter(Boolean).join(" ") || null);
53
+ return { email, name, fields, ...consent === void 0 ? {} : { consent } };
54
+ };
55
+ var formName = (form) => (form.getAttribute("data-mrrocket-form") || form.getAttribute("name") || form.id || form.getAttribute("aria-label") || `Form on ${window.location.pathname}`).trim().slice(0, 100);
56
+ var captures = (form, auto) => {
57
+ if (form.closest("[data-mrrocket-ignore]")) return false;
58
+ if (form.hasAttribute("data-mrrocket-form")) return true;
59
+ return auto && !!form.querySelector('input[type="email"], input[name*="email" i]') && !form.querySelector('input[type="password"]');
60
+ };
61
+ var inputsOf = (form) => Array.from(form.elements).flatMap((el) => {
62
+ if (!(el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLTextAreaElement)) return [];
63
+ if (el.disabled) return [];
64
+ const type = el instanceof HTMLInputElement ? el.type.toLowerCase() : el instanceof HTMLSelectElement ? "select" : "textarea";
65
+ const check = type === "checkbox" || type === "radio";
66
+ const value = el instanceof HTMLSelectElement && el.multiple ? Array.from(el.selectedOptions, (o) => o.value).join(", ") : check && el.value === "on" ? "yes" : el.value;
67
+ return [
68
+ {
69
+ name: el.name || el.id || "",
70
+ type,
71
+ value,
72
+ autocomplete: el.getAttribute("autocomplete") ?? void 0,
73
+ unchecked: check && !el.checked,
74
+ ignored: !!el.closest("[data-mrrocket-ignore]"),
75
+ consent: el.hasAttribute("data-mrrocket-consent")
76
+ }
77
+ ];
78
+ });
79
+ var watchForms = (o) => {
80
+ const w = window;
81
+ w.__mrrocketFormsOff?.();
82
+ const onSubmit = (e) => {
83
+ const form = e.target;
84
+ if (!(form instanceof HTMLFormElement) || !captures(form, !!o.auto)) return;
85
+ const read = fieldsOf(inputsOf(form));
86
+ if (!read.email) return;
87
+ const utm = Object.fromEntries(Object.entries(o.utm?.() ?? {}).filter((x) => typeof x[1] === "string" && !!x[1]));
88
+ const lead = {
89
+ key: o.apiKey,
90
+ form: formName(form),
91
+ url: window.location.href.slice(0, 500),
92
+ title: document.title.slice(0, 200) || void 0,
93
+ referrer: o.referrer?.() || document.referrer || void 0,
94
+ ...Object.keys(utm).length ? { utm } : {},
95
+ distinctId: o.distinctId(),
96
+ ...read
97
+ };
98
+ const body = JSON.stringify(lead);
99
+ const url = `${o.host}/api/sdk/forms`;
100
+ const sent = "sendBeacon" in navigator && navigator.sendBeacon(url, new Blob([body], { type: "text/plain" }));
101
+ if (!sent) void fetch(url, { method: "POST", headers: { "content-type": "text/plain" }, body, keepalive: true }).catch(() => void 0);
102
+ if (o.tracking() && !o.identified()) o.identify(read.email, { email: read.email, ...read.name ? { name: read.name } : {} });
103
+ };
104
+ document.addEventListener("submit", onSubmit, true);
105
+ const off = () => {
106
+ document.removeEventListener("submit", onSubmit, true);
107
+ if (w.__mrrocketFormsOff === off) delete w.__mrrocketFormsOff;
108
+ };
109
+ w.__mrrocketFormsOff = off;
110
+ return off;
111
+ };
112
+ export {
113
+ EMAIL_RE,
114
+ MAX_FIELDS,
115
+ MAX_VALUE,
116
+ SENSITIVE,
117
+ captures,
118
+ fieldsOf,
119
+ formName,
120
+ inputsOf,
121
+ sendable,
122
+ watchForms
123
+ };
124
+ //# sourceMappingURL=forms.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/forms.ts"],"sourcesContent":["/**\n * Form leads (0.12.0): a form on the customer's site becomes a contact in MRRocket when it is submitted. Forms marked\n * `data-mrrocket-form` (its value names the form, e.g. `data-mrrocket-form=\"Demo request\"`) are captured; with\n * auto-detection on (`data-forms=\"auto\"` on the script tag, `forms=\"auto\"` on the provider) also any form with an email\n * field, except forms with a password field (sign-in and sign-up) and forms marked `data-mrrocket-ignore`.\n *\n * What is sent: the email (required), the name, and the other fields a person typed, never passwords, card fields,\n * hidden fields, files or fields whose name looks sensitive (`SENSITIVE`); a field or form part marked\n * `data-mrrocket-ignore` stays out too. Plus the form's name, the page, the referrer, UTM, and the state of a checkbox\n * marked `data-mrrocket-consent` (the visitor's own yes to hear from the site). MRRocket records consent as the contact's\n * legal basis only when that box was ticked.\n *\n * No React and no globals beyond the DOM, so the script tag (`sdk-forms.js`, loaded only on pages with a form to\n * capture) and the Next.js provider (a dynamic import) share it. `fieldsOf` is pure for the tests.\n */\n\n/** Field names that never leave the page, whatever the form says. */\nexport const SENSITIVE =\n /pass(word|wd|code)?|pwd|secret|token|csrf|xsrf|nonce|captcha|honeypot|card|^cc([-_ ]|num|exp|$)|cvv|cvc|csc|expir|iban|(^|[^a-z])bic([^a-z]|$)|swift|routing|account[-_ ]?(no|num)|sort[-_ ]?code|(^|[^a-z])ssn([^a-z]|$)|social[-_ ]?sec|tax[-_ ]?id|vat[-_ ]?id|steuer|passport|(^|[^a-z])(pin|otp|pw)([^a-z]|$)|one[-_ ]?time|auth(?!or)|api[-_ ]?key|session|signature/i;\n\n/** Input types whose value is never sent. */\nconst SKIP_TYPES = new Set([\"password\", \"hidden\", \"file\", \"submit\", \"button\", \"reset\", \"image\"]);\n\nexport const EMAIL_RE = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$/;\n\n/** Limits: what a lead may carry, also checked again by the server. */\nexport const MAX_FIELDS = 30;\nexport const MAX_VALUE = 1000;\n\n/** One control of a form as the filter sees it. */\nexport interface FieldInput {\n name: string;\n type: string;\n value: string;\n autocomplete?: string;\n /** A checkbox or radio that is not ticked. */\n unchecked?: boolean;\n /** Marked `data-mrrocket-ignore` (itself or a part of the form around it). */\n ignored?: boolean;\n /** Marked `data-mrrocket-consent`. */\n consent?: boolean;\n}\n\nexport interface FormFields {\n email: string | null;\n name: string | null;\n /** The other fields, by name; checkbox groups joined with \", \". */\n fields: Record<string, string>;\n /** True or false when the form has a checkbox marked `data-mrrocket-consent`; undefined without one. */\n consent?: boolean;\n}\n\nconst norm = (s: string) => s.toLowerCase().replace(/[\\s_-]+/g, \"\");\n\n/** Whether a field's value may be sent: not a password, card, hidden or sensitive-looking field. */\nexport const sendable = (f: Pick<FieldInput, \"name\" | \"type\" | \"autocomplete\" | \"ignored\">) => {\n if (f.ignored || SKIP_TYPES.has(f.type)) return false;\n const ac = (f.autocomplete ?? \"\").toLowerCase();\n if (/^cc-|password|one-time-code/.test(ac)) return false;\n return !SENSITIVE.test(f.name);\n};\n\nconst isEmailField = (f: FieldInput) => f.type === \"email\" || /e-?mail/i.test(f.name) || f.autocomplete === \"email\";\n\n/**\n * The lead in a form's controls: the email (first valid one), the name (a full-name field, or first and last), the\n * consent box's state, and the other sendable fields. Pure.\n */\nexport const fieldsOf = (inputs: FieldInput[]): FormFields => {\n let email: string | null = null;\n let full: string | null = null;\n let first: string | null = null;\n let last: string | null = null;\n let consent: boolean | undefined;\n const fields: Record<string, string> = {};\n for (const f of inputs) {\n if (f.consent) {\n consent = (consent ?? false) || !f.unchecked;\n continue;\n }\n if (!f.name || !sendable(f) || f.unchecked) continue;\n const value = f.value.trim().slice(0, MAX_VALUE);\n if (!value) continue;\n const n = norm(f.name);\n const ac = (f.autocomplete ?? \"\").toLowerCase();\n if (!email && isEmailField(f) && EMAIL_RE.test(value)) {\n email = value.toLowerCase();\n continue;\n }\n if (!full && (ac === \"name\" || [\"name\", \"fullname\", \"yourname\", \"contactname\"].includes(n))) {\n full = value.slice(0, 100);\n continue;\n }\n if (!first && (ac === \"given-name\" || [\"firstname\", \"first\", \"vorname\", \"givenname\", \"fname\"].includes(n))) {\n first = value.slice(0, 60);\n continue;\n }\n if (!last && (ac === \"family-name\" || [\"lastname\", \"last\", \"nachname\", \"surname\", \"familyname\", \"lname\"].includes(n))) {\n last = value.slice(0, 60);\n continue;\n }\n const key = f.name.trim().slice(0, 60);\n if (key in fields) fields[key] = `${fields[key]}, ${value}`.slice(0, MAX_VALUE);\n else if (Object.keys(fields).length < MAX_FIELDS) fields[key] = value;\n }\n const name = full ?? ([first, last].filter(Boolean).join(\" \") || null);\n return { email, name, fields, ...(consent === undefined ? {} : { consent }) };\n};\n\n/** What the page sends to `POST /api/sdk/forms`. */\nexport interface FormLead extends FormFields {\n key: string;\n form: string;\n url: string;\n title?: string;\n referrer?: string;\n utm?: Record<string, string>;\n distinctId?: string;\n}\n\n/** The form's name: the value of `data-mrrocket-form`, else its name, id or label, else \"Form on /path\". */\nexport const formName = (form: HTMLFormElement) =>\n (form.getAttribute(\"data-mrrocket-form\") || form.getAttribute(\"name\") || form.id || form.getAttribute(\"aria-label\") || `Form on ${window.location.pathname}`)\n .trim()\n .slice(0, 100);\n\n/** Whether a form is captured: marked, or (auto) one with an email field and no password field. */\nexport const captures = (form: HTMLFormElement, auto: boolean) => {\n if (form.closest(\"[data-mrrocket-ignore]\")) return false;\n if (form.hasAttribute(\"data-mrrocket-form\")) return true;\n return auto && !!form.querySelector('input[type=\"email\"], input[name*=\"email\" i]') && !form.querySelector('input[type=\"password\"]');\n};\n\n/** The controls of a form, read for `fieldsOf`. */\nexport const inputsOf = (form: HTMLFormElement): FieldInput[] =>\n Array.from(form.elements).flatMap((el) => {\n if (!(el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLTextAreaElement)) return [];\n if (el.disabled) return [];\n const type = el instanceof HTMLInputElement ? el.type.toLowerCase() : el instanceof HTMLSelectElement ? \"select\" : \"textarea\";\n const check = type === \"checkbox\" || type === \"radio\";\n const value = el instanceof HTMLSelectElement && el.multiple ? Array.from(el.selectedOptions, (o) => o.value).join(\", \") : check && el.value === \"on\" ? \"yes\" : el.value;\n return [\n {\n name: el.name || el.id || \"\",\n type,\n value,\n autocomplete: el.getAttribute(\"autocomplete\") ?? undefined,\n unchecked: check && !(el as HTMLInputElement).checked,\n ignored: !!el.closest(\"[data-mrrocket-ignore]\"),\n consent: el.hasAttribute(\"data-mrrocket-consent\"),\n },\n ];\n });\n\nexport interface WatchFormsOptions {\n host: string;\n apiKey: string;\n /** Also forms with an email field that are not marked (default false). */\n auto?: boolean;\n /** The visitor id to link the lead to, while analytics consent allows one. */\n distinctId: () => string | undefined;\n /** Whether the site already called identify(). */\n identified: () => boolean;\n /** Links this visitor's history to the person (the SDK's own identify). */\n identify: (id: string, props: Record<string, unknown>) => void;\n /** Whether analytics consent lets the SDK identify the visitor (not \"off\"). */\n tracking: () => boolean;\n referrer?: () => string | undefined;\n utm?: () => Record<string, string | undefined> | undefined;\n}\n\n/**\n * Captures submits from now on (one listener on the document, capture phase, so it sees forms that render later and\n * forms whose own handler stops the event). The lead goes out with `sendBeacon`, which survives the page leaving. Returns\n * a function that stops it. Calling it twice keeps one listener.\n */\nexport const watchForms = (o: WatchFormsOptions): (() => void) => {\n const w = window as Window & { __mrrocketFormsOff?: () => void };\n w.__mrrocketFormsOff?.();\n const onSubmit = (e: Event) => {\n const form = e.target;\n if (!(form instanceof HTMLFormElement) || !captures(form, !!o.auto)) return;\n const read = fieldsOf(inputsOf(form));\n if (!read.email) return;\n const utm = Object.fromEntries(Object.entries(o.utm?.() ?? {}).filter((x): x is [string, string] => typeof x[1] === \"string\" && !!x[1]));\n const lead: FormLead = {\n key: o.apiKey,\n form: formName(form),\n url: window.location.href.slice(0, 500),\n title: document.title.slice(0, 200) || undefined,\n referrer: o.referrer?.() || document.referrer || undefined,\n ...(Object.keys(utm).length ? { utm } : {}),\n distinctId: o.distinctId(),\n ...read,\n };\n const body = JSON.stringify(lead);\n const url = `${o.host}/api/sdk/forms`;\n // text/plain keeps it a simple request (no CORS preflight), which sendBeacon needs anyway.\n const sent = \"sendBeacon\" in navigator && navigator.sendBeacon(url, new Blob([body], { type: \"text/plain\" }));\n if (!sent) void fetch(url, { method: \"POST\", headers: { \"content-type\": \"text/plain\" }, body, keepalive: true }).catch(() => undefined);\n // Their visits before the form and after it belong to the same person from now on.\n if (o.tracking() && !o.identified()) o.identify(read.email, { email: read.email, ...(read.name ? { name: read.name } : {}) });\n };\n document.addEventListener(\"submit\", onSubmit, true);\n const off = () => {\n document.removeEventListener(\"submit\", onSubmit, true);\n if (w.__mrrocketFormsOff === off) delete w.__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ACvI,UAAM,OAAiB;AAAA,MACnB,KAAK,EAAE;AAAA,MACP,MAAM,SAAS,IAAI;AAAA,MACnB,KAAK,OAAO,SAAS,KAAK,MAAM,GAAG,GAAG;AAAA,MACtC,OAAO,SAAS,MAAM,MAAM,GAAG,GAAG,KAAK;AAAA,MACvC,UAAU,EAAE,WAAW,KAAK,SAAS,YAAY;AAAA,MACjD,GAAI,OAAO,KAAK,GAAG,EAAE,SAAS,EAAE,IAAI,IAAI,CAAC;AAAA,MACzC,YAAY,EAAE,WAAW;AAAA,MACzB,GAAG;AAAA,IACP;AACA,UAAM,OAAO,KAAK,UAAU,IAAI;AAChC,UAAM,MAAM,GAAG,EAAE,IAAI;AAErB,UAAM,OAAO,gBAAgB,aAAa,UAAU,WAAW,KAAK,IAAI,KAAK,CAAC,IAAI,GAAG,EAAE,MAAM,aAAa,CAAC,CAAC;AAC5G,QAAI,CAAC,KAAM,MAAK,MAAM,KAAK,EAAE,QAAQ,QAAQ,SAAS,EAAE,gBAAgB,aAAa,GAAG,MAAM,WAAW,KAAK,CAAC,EAAE,MAAM,MAAM,MAAS;AAEtI,QAAI,EAAE,SAAS,KAAK,CAAC,EAAE,WAAW,EAAG,GAAE,SAAS,KAAK,OAAO,EAAE,OAAO,KAAK,OAAO,GAAI,KAAK,OAAO,EAAE,MAAM,KAAK,KAAK,IAAI,CAAC,EAAG,CAAC;AAAA,EAChI;AACA,WAAS,iBAAiB,UAAU,UAAU,IAAI;AAClD,QAAM,MAAM,MAAM;AACd,aAAS,oBAAoB,UAAU,UAAU,IAAI;AACrD,QAAI,EAAE,uBAAuB,IAAK,QAAO,EAAE;AAAA,EAC/C;AACA,IAAE,qBAAqB;AACvB,SAAO;AACX;","names":[]}
package/dist/pick.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * "Pick on your site" (0.11.0). MRRocket's onboarding editor opens the site in a frame with
3
+ * `?ck_embed=1&ck_pick=1&ck_origin=<the editor's origin>`: nothing is tracked (ck_embed), the element under the pointer
4
+ * gets an outline with its selector, and a click picks it instead of doing what it does on the page. The selector goes to
5
+ * the editor by postMessage, to that origin only; Esc tells it the person stopped. Only inside a frame, only with the
6
+ * parameter: a normal visit never sees any of this.
7
+ *
8
+ * Messages to the editor: `{ type: "mrrocket:pick-ready" }` once listening, `{ type: "mrrocket:picked", selector, text }`
9
+ * on a click, `{ type: "mrrocket:pick-cancel" }` on Esc.
10
+ */
11
+ export declare const PICK_PARAM = "ck_pick";
12
+ export declare const PICK_ORIGIN_PARAM = "ck_origin";
13
+ /** A short CSS selector that finds this element, and only it, on the page. */
14
+ export declare const pickSelector: (el: Element) => string;
15
+ /** Starts picking when the page was opened for it; returns how to stop, or null when it was not. */
16
+ export declare const startPicker: () => (() => void) | null;
package/dist/pick.js ADDED
@@ -0,0 +1,117 @@
1
+ // src/pick.ts
2
+ var PICK_PARAM = "ck_pick";
3
+ var PICK_ORIGIN_PARAM = "ck_origin";
4
+ var esc = (s) => typeof CSS !== "undefined" && CSS.escape ? CSS.escape(s) : s.replace(/[^a-zA-Z0-9_-]/g, (c) => `\\${c}`);
5
+ var stableClasses = (el) => [...el.classList].filter((c) => c.length < 30 && !/\d{2,}|[_-][a-z0-9]{5,}$|^(is|has)-|hover|active|focus|open|selected|:|\[/.test(c)).slice(0, 2);
6
+ var pickSelector = (el) => {
7
+ const doc = el.ownerDocument;
8
+ const unique = (sel) => {
9
+ try {
10
+ return doc.querySelectorAll(sel).length === 1;
11
+ } catch {
12
+ return false;
13
+ }
14
+ };
15
+ const named = el.getAttribute("data-ck-target");
16
+ if (named) return `[data-ck-target="${named.replace(/"/g, '\\"')}"]`;
17
+ if (el.id && !/\d{3,}/.test(el.id) && unique(`#${esc(el.id)}`)) return `#${esc(el.id)}`;
18
+ const parts = [];
19
+ let node = el;
20
+ while (node && node !== doc.documentElement && parts.length < 6) {
21
+ if (node !== el && node.id && !/\d{3,}/.test(node.id)) {
22
+ parts.unshift(`#${esc(node.id)}`);
23
+ break;
24
+ }
25
+ const tag = node.tagName.toLowerCase();
26
+ const cls = stableClasses(node).map((c) => `.${esc(c)}`).join("");
27
+ const parent = node.parentElement;
28
+ const same = parent ? [...parent.children].filter((c) => c.tagName === node.tagName) : [];
29
+ const nth = same.length > 1 ? `:nth-of-type(${same.indexOf(node) + 1})` : "";
30
+ parts.unshift(`${tag}${cls}${nth}`);
31
+ const sel = parts.join(" > ");
32
+ if (unique(sel)) return sel;
33
+ node = parent;
34
+ }
35
+ return parts.join(" > ");
36
+ };
37
+ var startPicker = () => {
38
+ if (typeof window === "undefined" || window.top === window) return null;
39
+ const params = new URLSearchParams(window.location.search);
40
+ if (params.get(PICK_PARAM) !== "1") return null;
41
+ let origin;
42
+ try {
43
+ origin = new URL(params.get(PICK_ORIGIN_PARAM) ?? "").origin;
44
+ } catch {
45
+ return null;
46
+ }
47
+ if (!/^https?:\/\//.test(origin)) return null;
48
+ const send = (data) => window.parent.postMessage(data, origin);
49
+ const box = document.createElement("div");
50
+ box.setAttribute("aria-hidden", "true");
51
+ Object.assign(box.style, {
52
+ position: "fixed",
53
+ zIndex: "2147483647",
54
+ pointerEvents: "none",
55
+ border: "2px solid #0ea5e9",
56
+ background: "rgba(14,165,233,.12)",
57
+ borderRadius: "6px",
58
+ transition: "all 60ms linear",
59
+ display: "none"
60
+ });
61
+ const tag = document.createElement("div");
62
+ Object.assign(tag.style, {
63
+ position: "absolute",
64
+ left: "-2px",
65
+ bottom: "calc(100% + 4px)",
66
+ font: "600 11px/1.4 system-ui, sans-serif",
67
+ color: "#fff",
68
+ background: "#0c4a6e",
69
+ padding: "2px 6px",
70
+ borderRadius: "4px",
71
+ whiteSpace: "nowrap",
72
+ maxWidth: "360px",
73
+ overflow: "hidden",
74
+ textOverflow: "ellipsis"
75
+ });
76
+ box.appendChild(tag);
77
+ document.body.appendChild(box);
78
+ let current = null;
79
+ const show = (el) => {
80
+ current = el && el !== box && !box.contains(el) ? el : null;
81
+ if (!current) {
82
+ box.style.display = "none";
83
+ return;
84
+ }
85
+ const r = current.getBoundingClientRect();
86
+ Object.assign(box.style, { display: "block", left: `${r.left - 2}px`, top: `${r.top - 2}px`, width: `${r.width + 4}px`, height: `${r.height + 4}px` });
87
+ tag.textContent = pickSelector(current);
88
+ };
89
+ const move = (e) => show(document.elementFromPoint(e.clientX, e.clientY));
90
+ const click = (e) => {
91
+ e.preventDefault();
92
+ e.stopPropagation();
93
+ const el = document.elementFromPoint(e.clientX, e.clientY) ?? current;
94
+ if (!el || el === box) return;
95
+ send({ type: "mrrocket:picked", selector: pickSelector(el), text: (el.textContent ?? "").trim().replace(/\s+/g, " ").slice(0, 80) });
96
+ };
97
+ const key = (e) => {
98
+ if (e.key === "Escape") send({ type: "mrrocket:pick-cancel" });
99
+ };
100
+ window.addEventListener("mousemove", move, true);
101
+ window.addEventListener("click", click, true);
102
+ window.addEventListener("keydown", key, true);
103
+ send({ type: "mrrocket:pick-ready" });
104
+ return () => {
105
+ window.removeEventListener("mousemove", move, true);
106
+ window.removeEventListener("click", click, true);
107
+ window.removeEventListener("keydown", key, true);
108
+ box.remove();
109
+ };
110
+ };
111
+ export {
112
+ PICK_ORIGIN_PARAM,
113
+ PICK_PARAM,
114
+ pickSelector,
115
+ startPicker
116
+ };
117
+ //# sourceMappingURL=pick.js.map