@mrrocket/sdk 0.1.0 → 0.3.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/core.js CHANGED
@@ -1,4 +1,46 @@
1
1
  // src/core.ts
2
+ var SDK_VERSION = "0.3.0";
3
+ var propOf = (person, name) => {
4
+ const want = name.trim().toLowerCase();
5
+ for (const [k, v] of Object.entries(person)) if (k.toLowerCase() === want) return v;
6
+ return void 0;
7
+ };
8
+ var same = (actual, value) => {
9
+ const a = actual.trim().toLowerCase();
10
+ const v = value.trim().toLowerCase();
11
+ return v.startsWith("@") ? a.endsWith(v) : a === v;
12
+ };
13
+ var matchesCondition = (c, person) => {
14
+ if (c.op === "in cohort") return false;
15
+ const raw = propOf(person, c.property);
16
+ const actual = raw === void 0 || raw === null ? "" : String(raw);
17
+ if (c.op === "\u2265") {
18
+ const a = Number(actual);
19
+ const b = Number(c.values[0]);
20
+ return actual !== "" && Number.isFinite(a) && Number.isFinite(b) && a >= b;
21
+ }
22
+ const hit = actual !== "" && c.values.some((v) => same(actual, v));
23
+ return c.op === "is" || c.op === "in" ? hit : !hit;
24
+ };
25
+ var evaluateFlag = (flag, distinctId, person = {}) => {
26
+ if (!flag.on) return false;
27
+ const subject = { id: distinctId, ...person };
28
+ const set = flag.sets.length ? flag.sets.find((s) => s.conditions.every((c) => matchesCondition(c, subject))) : { conditions: [], rollout: 100 };
29
+ if (!set) return false;
30
+ if (flagUnit(`${flag.key}:rollout:${distinctId}`) * 100 >= set.rollout) return false;
31
+ if (!flag.variants?.length) return true;
32
+ const u = flagUnit(`${flag.key}:variant:${distinctId}`) * 100;
33
+ let acc = 0;
34
+ for (const v of flag.variants) {
35
+ acc += v.split;
36
+ if (u < acc) return v.key;
37
+ }
38
+ return flag.variants[flag.variants.length - 1].key;
39
+ };
40
+ var flagFor = (key, config, distinctId, person = {}) => {
41
+ const flag = config?.flags?.find((f) => f.key === key);
42
+ return flag ? evaluateFlag(flag, distinctId, person) : false;
43
+ };
2
44
  var UTM_KEYS = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
3
45
  var PREVIEW_PARAM = "ck_preview";
4
46
  var EMBED_PARAM = "ck_embed";
@@ -22,6 +64,19 @@ var hashUnit = (input) => {
22
64
  }
23
65
  return (h >>> 0) / 4294967296;
24
66
  };
67
+ var flagUnit = (input) => {
68
+ let h = 2166136261;
69
+ for (let i = 0; i < input.length; i++) {
70
+ h ^= input.charCodeAt(i);
71
+ h = Math.imul(h, 16777619);
72
+ }
73
+ h ^= h >>> 16;
74
+ h = Math.imul(h, 2246822507);
75
+ h ^= h >>> 13;
76
+ h = Math.imul(h, 3266489909);
77
+ h ^= h >>> 16;
78
+ return (h >>> 0) / 4294967296;
79
+ };
25
80
  var assign = (experiment, distinctId) => {
26
81
  const control = experiment.variants[0]?.id ?? "control";
27
82
  if (experiment.winner) return experiment.winner;
@@ -86,12 +141,16 @@ export {
86
141
  EMBED_PARAM,
87
142
  ID_COOKIE,
88
143
  PREVIEW_PARAM,
144
+ SDK_VERSION,
89
145
  SESSION_COOKIE,
90
146
  SESSION_TIMEOUT_MS,
91
147
  UTM_KEYS,
92
148
  assign,
93
149
  envHost,
94
150
  envKey,
151
+ evaluateFlag,
152
+ flagFor,
153
+ flagUnit,
95
154
  hashUnit,
96
155
  newDistinctId,
97
156
  parsePreview,
package/dist/core.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * The part of the SDK that runs anywhere: experiment config, variant assignment, preview links and event shapes.\n * No React, no browser APIs, so the server helper, the proxy helper and the client share it.\n */\n\nexport type VariantId = string;\n\n/** One variant of an experiment. `changes` replaces texts that the page marks with `<Text id>`. */\nexport interface VariantConfig {\n id: VariantId;\n /** Share of traffic, 0 to 1. The weights of an experiment add up to 1. */\n weight: number;\n changes?: Record<string, string>;\n}\n\nexport interface ExperimentConfig {\n /** The experiment's key, the same as in Cockpit. */\n key: string;\n status: \"running\" | \"draft\" | \"stopped\";\n /** Control first. */\n variants: VariantConfig[];\n /** A winner that shipped: everyone gets it, and nobody is assigned anymore. */\n winner?: VariantId;\n}\n\nexport interface CockpitConfig {\n /** The product the public key belongs to. */\n project: string;\n experiments: ExperimentConfig[];\n /** The product's affiliate program: how many days a partner's referral is kept. Missing without a program. */\n referral?: { cookieDays: number };\n}\n\n/**\n * What the SDK may send and store.\n * - \"cookieless\" (default): no cookie, no localStorage. Events go without a visitor id; the server counts visitors\n * with a hash of key, date, IP and user agent that changes every day.\n * - \"cookies\": a persistent `ck_id` cookie identifies the visitor across visits, `ck_sid` holds the session.\n * - \"off\": nothing is sent and nothing is stored.\n */\nexport type ConsentMode = \"cookieless\" | \"cookies\" | \"off\";\n\n/** The campaign parameters the SDK reads from the landing URL. */\nexport const UTM_KEYS = [\"utm_source\", \"utm_medium\", \"utm_campaign\", \"utm_term\", \"utm_content\"] as const;\nexport type UtmKey = (typeof UTM_KEYS)[number];\n\n/** One event as it goes over the wire to `POST /api/sdk/events`. */\nexport interface CockpitEvent {\n event: string;\n /** The visitor. Missing in \"cookieless\" mode until identify(): the server then uses its daily visitor hash. */\n distinctId?: string;\n /** Ends after 30 minutes without an event. */\n sessionId?: string;\n props?: Record<string, unknown>;\n /** Milliseconds since the epoch. */\n ts: number;\n url?: string;\n /** `document.referrer` on the first page of a page load, the previous in-app URL after client-side navigation. */\n referrer?: string;\n utm_source?: string;\n utm_medium?: string;\n utm_campaign?: string;\n utm_term?: string;\n utm_content?: string;\n /** The screen size in CSS pixels. */\n screen?: { w: number; h: number };\n}\n\n/** The query parameter that forces variants, for previews in Cockpit: `?ck_preview=pricing-annual:a,home-hero:control`. */\nexport const PREVIEW_PARAM = \"ck_preview\";\n/** Set when Cockpit embeds the page: nothing is tracked, so previews never count as visits. */\nexport const EMBED_PARAM = \"ck_embed\";\n/** The cookie that keeps a visitor in the same variant across visits. Only written in \"cookies\" mode. */\nexport const ID_COOKIE = \"ck_id\";\n/** The cookie that holds the session id in \"cookies\" mode. */\nexport const SESSION_COOKIE = \"ck_sid\";\n/** A session ends after this long without an event. */\nexport const SESSION_TIMEOUT_MS = 30 * 60 * 1000;\n\n/** The UTM parameters of a query string, like `\"?utm_source=x\"`. Empty values are left out. */\nexport const readUtm = (search: string): Partial<Record<UtmKey, string>> => {\n const params = new URLSearchParams(search);\n const out: Partial<Record<UtmKey, string>> = {};\n for (const key of UTM_KEYS) {\n const value = params.get(key)?.trim();\n if (value) out[key] = value.slice(0, 150);\n }\n return out;\n};\n\n/** FNV-1a, 32 bit, mapped to [0, 1). Stable across server and browser. */\nexport const hashUnit = (input: string) => {\n let h = 0x811c9dc5;\n for (let i = 0; i < input.length; i++) {\n h ^= input.charCodeAt(i);\n h = Math.imul(h, 0x01000193);\n }\n return (h >>> 0) / 4294967296;\n};\n\n/** The variant a visitor sees: a shipped winner, control while the experiment does not run, else a stable bucket by visitor. */\nexport const assign = (experiment: ExperimentConfig, distinctId: string): VariantId => {\n const control = experiment.variants[0]?.id ?? \"control\";\n if (experiment.winner) return experiment.winner;\n if (experiment.status !== \"running\") return control;\n const u = hashUnit(`${experiment.key}:${distinctId}`);\n let acc = 0;\n for (const v of experiment.variants) {\n acc += v.weight;\n if (u < acc) return v.id;\n }\n return experiment.variants[experiment.variants.length - 1]?.id ?? control;\n};\n\n/** `\"a:b,c:d\"` → `{ a: \"b\", c: \"d\" }`. Ignores anything malformed. */\nexport const parsePreview = (value: string | string[] | null | undefined): Record<string, VariantId> => {\n const raw = Array.isArray(value) ? value[0] : value;\n if (!raw) return {};\n const out: Record<string, VariantId> = {};\n for (const pair of raw.split(\",\")) {\n const [key, variant] = pair.split(\":\");\n if (key && variant) out[key.trim()] = variant.trim();\n }\n return out;\n};\n\n/** A link that shows one variant of a page, for example in Cockpit's preview frames. */\nexport const previewHref = (url: string, forced: Record<string, VariantId>, embed = true) => {\n const [path, query = \"\"] = url.split(\"?\");\n const params = new URLSearchParams(query);\n const value = Object.entries(forced)\n .map(([k, v]) => `${k}:${v}`)\n .join(\",\");\n if (value) params.set(PREVIEW_PARAM, value);\n if (embed) params.set(EMBED_PARAM, \"1\");\n return `${path}?${params.toString()}`;\n};\n\n/** The variant for one experiment, from a forced preview first, then the config. Unknown experiments get control. */\nexport const resolveVariant = (key: string, config: CockpitConfig | null, distinctId: string, preview: Record<string, VariantId>): VariantId => {\n if (preview[key]) return preview[key];\n const experiment = config?.experiments.find((e) => e.key === key);\n return experiment ? assign(experiment, distinctId) : \"control\";\n};\n\n/**\n * Every text change that applies to this visitor, merged in config order. Control keeps the page's own words,\n * so only a non-control variant (assigned, forced in a preview, or a shipped winner) changes text.\n */\nexport const textChanges = (config: CockpitConfig | null, distinctId: string, preview: Record<string, VariantId>) => {\n const out: Record<string, string> = {};\n for (const experiment of config?.experiments ?? []) {\n const id = resolveVariant(experiment.key, config, distinctId, preview);\n if (id === (experiment.variants[0]?.id ?? \"control\")) continue;\n Object.assign(out, experiment.variants.find((v) => v.id === id)?.changes ?? {});\n }\n return out;\n};\n\n/**\n * The MRRocket API host from `NEXT_PUBLIC_MRROCKET_HOST`. Written as a literal property access on purpose: Next.js\n * inlines `process.env.NEXT_PUBLIC_*` at build time, in the browser bundle and in the server bundle (also on\n * Cloudflare Workers), and only replaces the literal form. Without a bundler (or a `process`) it is undefined.\n */\nexport const envHost = (): string | undefined => {\n try {\n return process.env.NEXT_PUBLIC_MRROCKET_HOST || undefined;\n } catch {\n return undefined;\n }\n};\n\n/** The public key from `NEXT_PUBLIC_MRROCKET_KEY`, the default for the `apiKey` prop. Same literal-access rule as `envHost`. */\nexport const envKey = (): string | undefined => {\n try {\n return process.env.NEXT_PUBLIC_MRROCKET_KEY || undefined;\n } catch {\n return undefined;\n }\n};\n\n/**\n * Where the API lives: the `host` prop, else `NEXT_PUBLIC_MRROCKET_HOST`, else \"\" (the page's own origin, which is how\n * MRRocket's own demo pages call it). Without a trailing slash.\n */\nexport const resolveHost = (host?: string) => (host ?? envHost() ?? \"\").trim().replace(/\\/+$/, \"\");\n\n/** A random visitor id. */\nexport const newDistinctId = () =>\n typeof crypto !== \"undefined\" && \"randomUUID\" in crypto ? crypto.randomUUID() : `ck_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;\n"],"mappings":";AA2CO,IAAM,WAAW,CAAC,cAAc,cAAc,gBAAgB,YAAY,aAAa;AA0BvF,IAAM,gBAAgB;AAEtB,IAAM,cAAc;AAEpB,IAAM,YAAY;AAElB,IAAM,iBAAiB;AAEvB,IAAM,qBAAqB,KAAK,KAAK;AAGrC,IAAM,UAAU,CAAC,WAAoD;AACxE,QAAM,SAAS,IAAI,gBAAgB,MAAM;AACzC,QAAM,MAAuC,CAAC;AAC9C,aAAW,OAAO,UAAU;AACxB,UAAM,QAAQ,OAAO,IAAI,GAAG,GAAG,KAAK;AACpC,QAAI,MAAO,KAAI,GAAG,IAAI,MAAM,MAAM,GAAG,GAAG;AAAA,EAC5C;AACA,SAAO;AACX;AAGO,IAAM,WAAW,CAAC,UAAkB;AACvC,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACnC,SAAK,MAAM,WAAW,CAAC;AACvB,QAAI,KAAK,KAAK,GAAG,QAAU;AAAA,EAC/B;AACA,UAAQ,MAAM,KAAK;AACvB;AAGO,IAAM,SAAS,CAAC,YAA8B,eAAkC;AACnF,QAAM,UAAU,WAAW,SAAS,CAAC,GAAG,MAAM;AAC9C,MAAI,WAAW,OAAQ,QAAO,WAAW;AACzC,MAAI,WAAW,WAAW,UAAW,QAAO;AAC5C,QAAM,IAAI,SAAS,GAAG,WAAW,GAAG,IAAI,UAAU,EAAE;AACpD,MAAI,MAAM;AACV,aAAW,KAAK,WAAW,UAAU;AACjC,WAAO,EAAE;AACT,QAAI,IAAI,IAAK,QAAO,EAAE;AAAA,EAC1B;AACA,SAAO,WAAW,SAAS,WAAW,SAAS,SAAS,CAAC,GAAG,MAAM;AACtE;AAGO,IAAM,eAAe,CAAC,UAA2E;AACpG,Q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EAAE,OAAO,EAAE,GAAG,WAAW,CAAC,CAAC;AAAA,EAClF;AACA,SAAO;AACX;AAOO,IAAM,UAAU,MAA0B;AAC7C,MAAI;AACA,WAAO,QAAQ,IAAI,6BAA6B;AAAA,EACpD,QAAQ;AACJ,WAAO;AAAA,EACX;AACJ;AAGO,IAAM,SAAS,MAA0B;AAC5C,MAAI;AACA,WAAO,QAAQ,IAAI,4BAA4B;AAAA,EACnD,QAAQ;AACJ,WAAO;AAAA,EACX;AACJ;AAMO,IAAM,cAAc,CAAC,UAAmB,QAAQ,QAAQ,KAAK,IAAI,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAG1F,IAAM,gBAAgB,MACzB,OAAO,WAAW,eAAe,gBAAgB,SAAS,OAAO,WAAW,IAAI,MAAM,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC;","names":[]}
1
+ {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * The part of the SDK that runs anywhere: experiment config, variant assignment, preview links and event shapes.\n * No React, no browser APIs, so the server helper, the proxy helper and the client share it.\n */\n\nexport type VariantId = string;\n\n/** This build of the SDK. Sent with the config request so MRRocket knows what the site runs (error tracking, flags). */\nexport const SDK_VERSION = \"0.3.0\";\n\n/** One variant of an experiment. `changes` replaces texts that the page marks with `<Text id>`. */\nexport interface VariantConfig {\n id: VariantId;\n /** Share of traffic, 0 to 1. The weights of an experiment add up to 1. */\n weight: number;\n changes?: Record<string, string>;\n}\n\nexport interface ExperimentConfig {\n /** The experiment's key, the same as in Cockpit. */\n key: string;\n status: \"running\" | \"draft\" | \"stopped\";\n /** Control first. */\n variants: VariantConfig[];\n /** A winner that shipped: everyone gets it, and nobody is assigned anymore. */\n winner?: VariantId;\n}\n\nexport interface CockpitConfig {\n /** The product the public key belongs to. */\n project: string;\n experiments: ExperimentConfig[];\n /** The product's affiliate program: how many days a partner's referral is kept. Missing without a program. */\n referral?: { cookieDays: number };\n /** Session recordings: present only while the product records (Cockpit › Sessions). See `ReplayConfig`. */\n replay?: ReplayConfig;\n /** Feature flags of the product in Production (MRRocket › Build › Feature flags), evaluated by `useFlag` and `getFlag`. */\n flags?: FlagConfig[];\n}\n\n/** One condition of a flag's audience, on a property of the person (`identify(id, props)`), the id or the page. */\nexport interface FlagCondition {\n /** \"Email\", \"Plan\", \"Country\", \"id\", \"path\" … Matched against the person's properties without regard to case. */\n property: string;\n op: \"is\" | \"is not\" | \"in\" | \"not in\" | \"≥\" | \"in cohort\";\n values: string[];\n}\n\n/** Users who match every condition, of whom `rollout` percent get the flag. No conditions: everyone. */\nexport interface FlagConditionSet {\n conditions: FlagCondition[];\n /** 0 to 100. */\n rollout: number;\n}\n\nexport interface FlagConfig {\n key: string;\n /** Off: everyone gets the off side. */\n on: boolean;\n /** The first set a person matches decides; none matched: off. */\n sets: FlagConditionSet[];\n /** Multivariate flags: the variants and their split in percent (control included). Missing for an on/off flag. */\n variants?: { key: string; split: number; payload?: string }[];\n}\n\n/** What a person is for flag conditions: their properties from identify(), their id and the page they are on. */\nexport type FlagPerson = Record<string, unknown>;\n\nconst propOf = (person: FlagPerson, name: string) => {\n const want = name.trim().toLowerCase();\n for (const [k, v] of Object.entries(person)) if (k.toLowerCase() === want) return v;\n return undefined;\n};\n\n/** `\"@acme.com\"` matches every address at that domain; anything else matches the whole value, without regard to case. */\nconst same = (actual: string, value: string) => {\n const a = actual.trim().toLowerCase();\n const v = value.trim().toLowerCase();\n return v.startsWith(\"@\") ? a.endsWith(v) : a === v;\n};\n\nconst matchesCondition = (c: FlagCondition, person: FlagPerson) => {\n // Cohorts live in MRRocket; a browser cannot know them, so a cohort condition never matches here.\n if (c.op === \"in cohort\") return false;\n const raw = propOf(person, c.property);\n const actual = raw === undefined || raw === null ? \"\" : String(raw);\n if (c.op === \"≥\") {\n const a = Number(actual);\n const b = Number(c.values[0]);\n return actual !== \"\" && Number.isFinite(a) && Number.isFinite(b) && a >= b;\n }\n const hit = actual !== \"\" && c.values.some((v) => same(actual, v));\n return c.op === \"is\" || c.op === \"in\" ? hit : !hit;\n};\n\n/**\n * The flag for one person: `false` (off), `true` (on, an on/off flag) or the variant's key (a multivariate flag). The\n * bucket is a stable hash of flag and person, so a person keeps their side as the rollout grows.\n */\nexport const evaluateFlag = (flag: FlagConfig, distinctId: string, person: FlagPerson = {}): boolean | string => {\n if (!flag.on) return false;\n const subject = { id: distinctId, ...person };\n const set = flag.sets.length ? flag.sets.find((s) => s.conditions.every((c) => matchesCondition(c, subject))) : { conditions: [], rollout: 100 };\n if (!set) return false;\n if (flagUnit(`${flag.key}:rollout:${distinctId}`) * 100 >= set.rollout) return false;\n if (!flag.variants?.length) return true;\n const u = flagUnit(`${flag.key}:variant:${distinctId}`) * 100;\n let acc = 0;\n for (const v of flag.variants) {\n acc += v.split;\n if (u < acc) return v.key;\n }\n return flag.variants[flag.variants.length - 1].key;\n};\n\n/** A flag of the config for this person; an unknown flag (or no config) is off. */\nexport const flagFor = (key: string, config: CockpitConfig | null, distinctId: string, person: FlagPerson = {}) => {\n const flag = config?.flags?.find((f) => f.key === key);\n return flag ? evaluateFlag(flag, distinctId, person) : false;\n};\n\n/** One JavaScript error as it goes over the wire to `POST /api/sdk/errors`. */\nexport interface ErrorReport {\n /** \"TypeError\", \"Error\" … */\n type: string;\n message: string;\n /** The raw stack, at most 8 KB. MRRocket parses it into frames. */\n stack?: string;\n /** \"onerror\" (uncaught), \"onunhandledrejection\" (a rejected promise nobody caught) or \"manual\" (captureError). */\n mechanism: \"onerror\" | \"onunhandledrejection\" | \"manual\";\n handled: boolean;\n url: string;\n ts: number;\n /** What happened just before: pages and clicks, newest last, at most 20. */\n crumbs?: { ts: number; type: \"navigation\" | \"click\" | \"lifecycle\"; message: string }[];\n /** Your release (a version or commit), from the provider's `release` prop. */\n release?: string;\n distinctId?: string;\n sessionId?: string;\n}\n\n/**\n * Session recordings of a product, from the config. Recording also needs the visitor's own replay consent\n * (`<CockpitProvider replay>` or `setConsent({ replay: true })`); without it nothing is recorded or buffered.\n */\nexport interface ReplayConfig {\n /**\n * Share of sessions uploaded from their start, 0 to 1 (default 0.1). The others keep only the last minute in\n * memory and upload it when something goes wrong: a JS error, a rage click, a `purchase…` or `checkout…` event.\n */\n sampleRate: number;\n}\n\n/**\n * What the SDK may send and store.\n * - \"cookieless\" (default): no cookie, no localStorage. Events go without a visitor id; the server counts visitors\n * with a hash of key, date, IP and user agent that changes every day.\n * - \"cookies\": a persistent `ck_id` cookie identifies the visitor across visits, `ck_sid` holds the session.\n * - \"off\": nothing is sent and nothing is stored.\n */\nexport type ConsentMode = \"cookieless\" | \"cookies\" | \"off\";\n\n/**\n * What `setConsent` takes: a mode as before, or the parts to change. `replay` is the separate consent for session\n * recordings (default false); it applies only while analytics is not \"off\".\n *\n * setConsent(\"cookies\");\n * setConsent({ replay: true });\n * setConsent({ analytics: \"cookieless\", replay: false });\n */\nexport type ConsentChoice = ConsentMode | { analytics?: ConsentMode; replay?: boolean };\n\n/** The campaign parameters the SDK reads from the landing URL. */\nexport const UTM_KEYS = [\"utm_source\", \"utm_medium\", \"utm_campaign\", \"utm_term\", \"utm_content\"] as const;\nexport type UtmKey = (typeof UTM_KEYS)[number];\n\n/** One event as it goes over the wire to `POST /api/sdk/events`. */\nexport interface CockpitEvent {\n event: string;\n /** The visitor. Missing in \"cookieless\" mode until identify(): the server then uses its daily visitor hash. */\n distinctId?: string;\n /** Ends after 30 minutes without an event. */\n sessionId?: string;\n props?: Record<string, unknown>;\n /** Milliseconds since the epoch. */\n ts: number;\n url?: string;\n /** `document.referrer` on the first page of a page load, the previous in-app URL after client-side navigation. */\n referrer?: string;\n utm_source?: string;\n utm_medium?: string;\n utm_campaign?: string;\n utm_term?: string;\n utm_content?: string;\n /** The screen size in CSS pixels. */\n screen?: { w: number; h: number };\n}\n\n/** The query parameter that forces variants, for previews in Cockpit: `?ck_preview=pricing-annual:a,home-hero:control`. */\nexport const PREVIEW_PARAM = \"ck_preview\";\n/** Set when Cockpit embeds the page: nothing is tracked, so previews never count as visits. */\nexport const EMBED_PARAM = \"ck_embed\";\n/** The cookie that keeps a visitor in the same variant across visits. Only written in \"cookies\" mode. */\nexport const ID_COOKIE = \"ck_id\";\n/** The cookie that holds the session id in \"cookies\" mode. */\nexport const SESSION_COOKIE = \"ck_sid\";\n/** A session ends after this long without an event. */\nexport const SESSION_TIMEOUT_MS = 30 * 60 * 1000;\n\n/** The UTM parameters of a query string, like `\"?utm_source=x\"`. Empty values are left out. */\nexport const readUtm = (search: string): Partial<Record<UtmKey, string>> => {\n const params = new URLSearchParams(search);\n const out: Partial<Record<UtmKey, string>> = {};\n for (const key of UTM_KEYS) {\n const value = params.get(key)?.trim();\n if (value) out[key] = value.slice(0, 150);\n }\n return out;\n};\n\n/** FNV-1a, 32 bit, mapped to [0, 1). Stable across server and browser. */\nexport const hashUnit = (input: string) => {\n let h = 0x811c9dc5;\n for (let i = 0; i < input.length; i++) {\n h ^= input.charCodeAt(i);\n h = Math.imul(h, 0x01000193);\n }\n return (h >>> 0) / 4294967296;\n};\n\n/**\n * FNV-1a with murmur3's finalizer, mapped to [0, 1): ids that differ only at the end (\"user-1\", \"user-2\") still spread\n * evenly, so a 30 % rollout reaches about 30 % of people. Flags only; experiments keep `hashUnit` so nobody switches.\n */\nexport const flagUnit = (input: string) => {\n let h = 0x811c9dc5;\n for (let i = 0; i < input.length; i++) {\n h ^= input.charCodeAt(i);\n h = Math.imul(h, 0x01000193);\n }\n h ^= h >>> 16;\n h = Math.imul(h, 0x85ebca6b);\n h ^= h >>> 13;\n h = Math.imul(h, 0xc2b2ae35);\n h ^= h >>> 16;\n return (h >>> 0) / 4294967296;\n};\n\n/** The variant a visitor sees: a shipped winner, control while the experiment does not run, else a stable bucket by visitor. */\nexport const assign = (experiment: ExperimentConfig, distinctId: string): VariantId => {\n const control = experiment.variants[0]?.id ?? \"control\";\n if (experiment.winner) return experiment.winner;\n if (experiment.status !== \"running\") return control;\n const u = hashUnit(`${experiment.key}:${distinctId}`);\n let acc = 0;\n for (const v of experiment.variants) {\n acc += v.weight;\n if (u < acc) return v.id;\n }\n return experiment.variants[experiment.variants.length - 1]?.id ?? control;\n};\n\n/** `\"a:b,c:d\"` → `{ a: \"b\", c: \"d\" }`. Ignores anything malformed. */\nexport const parsePreview = (value: string | string[] | null | undefined): Record<string, VariantId> => {\n const raw = Array.isArray(value) ? value[0] : value;\n if (!raw) return {};\n const out: Record<string, VariantId> = {};\n for (const pair of raw.split(\",\")) {\n const [key, variant] = pair.split(\":\");\n if (key && variant) out[key.trim()] = variant.trim();\n }\n return out;\n};\n\n/** A link that shows one variant of a page, for example in Cockpit's preview frames. */\nexport const previewHref = (url: string, forced: Record<string, VariantId>, embed = true) => {\n const [path, query = \"\"] = url.split(\"?\");\n const params = new URLSearchParams(query);\n const value = Object.entries(forced)\n .map(([k, v]) => `${k}:${v}`)\n .join(\",\");\n if (value) params.set(PREVIEW_PARAM, value);\n if (embed) params.set(EMBED_PARAM, \"1\");\n return `${path}?${params.toString()}`;\n};\n\n/** The variant for one experiment, from a forced preview first, then the config. Unknown experiments get control. */\nexport const resolveVariant = (key: string, config: CockpitConfig | null, distinctId: string, preview: Record<string, VariantId>): VariantId => {\n if (preview[key]) return preview[key];\n const experiment = config?.experiments.find((e) => e.key === key);\n return experiment ? assign(experiment, distinctId) : \"control\";\n};\n\n/**\n * Every text change that applies to this visitor, merged in config order. Control keeps the page's own words,\n * so only a non-control variant (assigned, forced in a preview, or a shipped winner) changes text.\n */\nexport const textChanges = (config: CockpitConfig | null, distinctId: string, preview: Record<string, VariantId>) => {\n const out: Record<string, string> = {};\n for (const experiment of config?.experiments ?? []) {\n const id = resolveVariant(experiment.key, config, distinctId, preview);\n if (id === (experiment.variants[0]?.id ?? \"control\")) continue;\n Object.assign(out, experiment.variants.find((v) => v.id === id)?.changes ?? {});\n }\n return out;\n};\n\n/**\n * The MRRocket API host from `NEXT_PUBLIC_MRROCKET_HOST`. Written as a literal property access on purpose: Next.js\n * inlines `process.env.NEXT_PUBLIC_*` at build time, in the browser bundle and in the server bundle (also on\n * Cloudflare Workers), and only replaces the literal form. Without a bundler (or a `process`) it is undefined.\n */\nexport const envHost = (): string | undefined => {\n try {\n return process.env.NEXT_PUBLIC_MRROCKET_HOST || undefined;\n } catch {\n return undefined;\n }\n};\n\n/** The public key from `NEXT_PUBLIC_MRROCKET_KEY`, the default for the `apiKey` prop. Same literal-access rule as `envHost`. */\nexport const envKey = (): string | undefined => {\n try {\n return process.env.NEXT_PUBLIC_MRROCKET_KEY || undefined;\n } catch {\n return undefined;\n }\n};\n\n/**\n * Where the API lives: the `host` prop, else `NEXT_PUBLIC_MRROCKET_HOST`, else \"\" (the page's own origin, which is how\n * MRRocket's own demo pages call it). Without a trailing slash.\n */\nexport const resolveHost = (host?: string) => (host ?? envHost() ?? \"\").trim().replace(/\\/+$/, \"\");\n\n/** A random visitor id. */\nexport const newDistinctId = () =>\n typeof crypto !== \"undefined\" && \"randomUUID\" in crypto ? crypto.randomUUID() : `ck_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;\n"],"mappings":";AAQO,IAAM,cAAc;AA4D3B,IAAM,SAAS,CAAC,QAAoB,SAAiB;AACjD,QAAM,OAAO,KAAK,KAAK,EAAE,YAAY;AACrC,aAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,MAAM,EAAG,KAAI,EAAE,YAAY,MAAM,KAAM,QAAO;AAClF,SAAO;AACX;AAGA,IAAM,OAAO,CAAC,QAAgB,UAAkB;AAC5C,QAAM,IAAI,OAAO,KAAK,EAAE,YAAY;AACpC,QAAM,IAAI,MAAM,KAAK,EAAE,YAAY;AACnC,SAAO,EAAE,WAAW,GAAG,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM;AACrD;AAEA,IAAM,mBAAmB,CAAC,GAAkB,WAAuB;AAE/D,MAAI,EAAE,OAAO,YAAa,QAAO;AACjC,QAAM,MAAM,OAAO,QAAQ,EAAE,QAAQ;A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AAc,CAAC,QAA8B,YAAoB,YAAuC;AACjH,QAAM,MAA8B,CAAC;AACrC,aAAW,cAAc,QAAQ,eAAe,CAAC,GAAG;AAChD,UAAM,KAAK,eAAe,WAAW,KAAK,QAAQ,YAAY,OAAO;AACrE,QAAI,QAAQ,WAAW,SAAS,CAAC,GAAG,MAAM,WAAY;AACtD,WAAO,OAAO,KAAK,WAAW,SAAS,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,WAAW,CAAC,CAAC;AAAA,EAClF;AACA,SAAO;AACX;AAOO,IAAM,UAAU,MAA0B;AAC7C,MAAI;AACA,WAAO,QAAQ,IAAI,6BAA6B;AAAA,EACpD,QAAQ;AACJ,WAAO;AAAA,EACX;AACJ;AAGO,IAAM,SAAS,MAA0B;AAC5C,MAAI;AACA,WAAO,QAAQ,IAAI,4BAA4B;AAAA,EACnD,QAAQ;AACJ,WAAO;AAAA,EACX;AACJ;AAMO,IAAM,cAAc,CAAC,UAAmB,QAAQ,QAAQ,KAAK,IAAI,KAAK,EAAE,QAAQ,QAAQ,EAAE;AAG1F,IAAM,gBAAgB,MACzB,OAAO,WAAW,eAAe,gBAAgB,SAAS,OAAO,WAAW,IAAI,MAAM,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC,CAAC,GAAG,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC;","names":[]}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Error tracking: uncaught errors and unhandled promise rejections of the page (and `captureError` for ones you
3
+ * catch), sent to `POST /api/sdk/errors`, where MRRocket groups them into issues for Build › Errors. Independent of
4
+ * session recordings. Only the error itself, the page URL and the last pages and clicks go along (no typed text, no
5
+ * network bodies), under the same consent as analytics.
6
+ *
7
+ * Limits on the page: the same error is sent once per page load, at most 20 errors per page load, in batches every
8
+ * 2 seconds and with `sendBeacon` when the page hides.
9
+ */
10
+ import type { ErrorReport } from "./core.js";
11
+ export interface ErrorCapture {
12
+ /** An error you caught yourself; `handled` is true. */
13
+ capture: (error: unknown, mechanism?: ErrorReport["mechanism"]) => void;
14
+ /** A page view or a click for the breadcrumbs. */
15
+ crumb: (type: "navigation" | "click" | "lifecycle", message: string) => void;
16
+ stop: () => void;
17
+ }
18
+ /** Type, message and stack of anything thrown. */
19
+ export declare const describeError: (error: unknown) => {
20
+ type: string;
21
+ message: string;
22
+ stack?: string;
23
+ };
24
+ export declare const startErrorCapture: (opts: {
25
+ host: string;
26
+ apiKey: string;
27
+ release?: string;
28
+ /** Read when an error happens: whether to send at all (consent), and who and which session. */
29
+ context: () => {
30
+ send: boolean;
31
+ distinctId?: string;
32
+ sessionId?: string;
33
+ };
34
+ warn: (message: string) => void;
35
+ }) => ErrorCapture;
package/dist/errors.js ADDED
@@ -0,0 +1,83 @@
1
+ // src/errors.ts
2
+ var MAX_STACK = 8 * 1024;
3
+ var MAX_PER_PAGE = 20;
4
+ var MAX_CRUMBS = 20;
5
+ var FLUSH_MS = 2e3;
6
+ var clip = (s, n) => s.length > n ? `${s.slice(0, n - 1)}\u2026` : s;
7
+ var describeError = (error) => {
8
+ if (error instanceof Error) return { type: error.name || "Error", message: clip(error.message || String(error), 1e3), stack: error.stack ? clip(error.stack, MAX_STACK) : void 0 };
9
+ if (typeof error === "string") return { type: "Error", message: clip(error, 1e3) };
10
+ try {
11
+ return { type: "Error", message: clip(JSON.stringify(error) ?? String(error), 1e3) };
12
+ } catch {
13
+ return { type: "Error", message: String(error) };
14
+ }
15
+ };
16
+ var startErrorCapture = (opts) => {
17
+ const queue = [];
18
+ const crumbs = [];
19
+ const seen = /* @__PURE__ */ new Set();
20
+ let sent = 0;
21
+ const url = `${opts.host}/api/sdk/errors`;
22
+ const flush = (beacon = false) => {
23
+ while (queue.length) {
24
+ const body = JSON.stringify({ key: opts.apiKey, errors: queue.splice(0, 4) });
25
+ const ok = beacon && typeof navigator !== "undefined" && "sendBeacon" in navigator && navigator.sendBeacon(url, body);
26
+ if (!ok)
27
+ fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body, keepalive: true }).then((r) => {
28
+ if (!r.ok && r.status !== 429) opts.warn(`Error reports were rejected with ${r.status}. Check the public key and NEXT_PUBLIC_MRROCKET_HOST.`);
29
+ }).catch(() => {
30
+ });
31
+ }
32
+ };
33
+ const capture = (error, mechanism = "manual") => {
34
+ const ctx = opts.context();
35
+ if (!ctx.send || sent >= MAX_PER_PAGE) return;
36
+ const d = describeError(error);
37
+ if (/\/api\/sdk\/errors/.test(d.message)) return;
38
+ const mark = `${d.type}:${d.message}`;
39
+ if (seen.has(mark)) return;
40
+ seen.add(mark);
41
+ sent++;
42
+ queue.push({
43
+ ...d,
44
+ mechanism,
45
+ handled: mechanism === "manual",
46
+ url: window.location.href,
47
+ ts: Date.now(),
48
+ crumbs: crumbs.slice(),
49
+ ...opts.release ? { release: opts.release } : {},
50
+ ...ctx.distinctId ? { distinctId: ctx.distinctId } : {},
51
+ ...ctx.sessionId ? { sessionId: ctx.sessionId } : {}
52
+ });
53
+ };
54
+ const onError = (e) => {
55
+ if (!e.error && (!e.message || e.message === "Script error.")) return;
56
+ capture(e.error ?? new Error(e.message), "onerror");
57
+ };
58
+ const onRejection = (e) => capture(e.reason ?? new Error("Unhandled promise rejection"), "onunhandledrejection");
59
+ const onHide = () => flush(true);
60
+ window.addEventListener("error", onError);
61
+ window.addEventListener("unhandledrejection", onRejection);
62
+ window.addEventListener("pagehide", onHide);
63
+ const timer = window.setInterval(() => flush(), FLUSH_MS);
64
+ return {
65
+ capture,
66
+ crumb: (type, message) => {
67
+ crumbs.push({ ts: Date.now(), type, message: clip(message, 120) });
68
+ if (crumbs.length > MAX_CRUMBS) crumbs.shift();
69
+ },
70
+ stop: () => {
71
+ window.removeEventListener("error", onError);
72
+ window.removeEventListener("unhandledrejection", onRejection);
73
+ window.removeEventListener("pagehide", onHide);
74
+ window.clearInterval(timer);
75
+ flush(true);
76
+ }
77
+ };
78
+ };
79
+ export {
80
+ describeError,
81
+ startErrorCapture
82
+ };
83
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts"],"sourcesContent":["/**\n * Error tracking: uncaught errors and unhandled promise rejections of the page (and `captureError` for ones you\n * catch), sent to `POST /api/sdk/errors`, where MRRocket groups them into issues for Build › Errors. Independent of\n * session recordings. Only the error itself, the page URL and the last pages and clicks go along (no typed text, no\n * network bodies), under the same consent as analytics.\n *\n * Limits on the page: the same error is sent once per page load, at most 20 errors per page load, in batches every\n * 2 seconds and with `sendBeacon` when the page hides.\n */\nimport type { ErrorReport } from \"./core\";\n\nconst MAX_STACK = 8 * 1024;\nconst MAX_PER_PAGE = 20;\nconst MAX_CRUMBS = 20;\nconst FLUSH_MS = 2000;\n\nexport interface ErrorCapture {\n /** An error you caught yourself; `handled` is true. */\n capture: (error: unknown, mechanism?: ErrorReport[\"mechanism\"]) => void;\n /** A page view or a click for the breadcrumbs. */\n crumb: (type: \"navigation\" | \"click\" | \"lifecycle\", message: string) => void;\n stop: () => void;\n}\n\nconst clip = (s: string, n: number) => (s.length > n ? `${s.slice(0, n - 1)}…` : s);\n\n/** Type, message and stack of anything thrown. */\nexport const describeError = (error: unknown): { type: string; message: string; stack?: string } => {\n if (error instanceof Error) return { type: error.name || \"Error\", message: clip(error.message || String(error), 1000), stack: error.stack ? clip(error.stack, MAX_STACK) : undefined };\n if (typeof error === \"string\") return { type: \"Error\", message: clip(error, 1000) };\n try {\n return { type: \"Error\", message: clip(JSON.stringify(error) ?? String(error), 1000) };\n } catch {\n return { type: \"Error\", message: String(error) };\n }\n};\n\nexport const startErrorCapture = (opts: {\n host: string;\n apiKey: string;\n release?: string;\n /** Read when an error happens: whether to send at all (consent), and who and which session. */\n context: () => { send: boolean; distinctId?: string; sessionId?: string };\n warn: (message: string) => void;\n}): ErrorCapture => {\n const queue: ErrorReport[] = [];\n const crumbs: NonNullable<ErrorReport[\"crumbs\"]> = [];\n const seen = new Set<string>();\n let sent = 0;\n const url = `${opts.host}/api/sdk/errors`;\n\n const flush = (beacon = false) => {\n // At most four reports per request: four full stacks stay under the server's 64 KB.\n while (queue.length) {\n const body = JSON.stringify({ key: opts.apiKey, errors: queue.splice(0, 4) });\n const ok = beacon && typeof navigator !== \"undefined\" && \"sendBeacon\" in navigator && navigator.sendBeacon(url, body);\n if (!ok)\n fetch(url, { method: \"POST\", headers: { \"content-type\": \"application/json\" }, body, keepalive: true })\n .then((r) => {\n if (!r.ok && r.status !== 429) opts.warn(`Error reports were rejected with ${r.status}. Check the public key and NEXT_PUBLIC_MRROCKET_HOST.`);\n })\n .catch(() => {});\n }\n };\n\n const capture = (error: unknown, mechanism: ErrorReport[\"mechanism\"] = \"manual\") => {\n const ctx = opts.context();\n if (!ctx.send || sent >= MAX_PER_PAGE) return;\n const d = describeError(error);\n // Our own reports failing must never report themselves.\n if (/\\/api\\/sdk\\/errors/.test(d.message)) return;\n const mark = `${d.type}:${d.message}`;\n if (seen.has(mark)) return;\n seen.add(mark);\n sent++;\n queue.push({\n ...d,\n mechanism,\n handled: mechanism === \"manual\",\n url: window.location.href,\n ts: Date.now(),\n crumbs: crumbs.slice(),\n ...(opts.release ? { release: opts.release } : {}),\n ...(ctx.distinctId ? { distinctId: ctx.distinctId } : {}),\n ...(ctx.sessionId ? { sessionId: ctx.sessionId } : {}),\n });\n };\n\n const onError = (e: ErrorEvent) => {\n // \"Script error.\" from a cross-origin script carries nothing useful.\n if (!e.error && (!e.message || e.message === \"Script error.\")) return;\n capture(e.error ?? new Error(e.message), \"onerror\");\n };\n const onRejection = (e: PromiseRejectionEvent) => capture(e.reason ?? new Error(\"Unhandled promise rejection\"), \"onunhandledrejection\");\n const onHide = () => flush(true);\n window.addEventListener(\"error\", onError);\n window.addEventListener(\"unhandledrejection\", onRejection);\n window.addEventListener(\"pagehide\", onHide);\n const timer = window.setInterval(() => flush(), FLUSH_MS);\n\n return {\n capture,\n crumb: (type, message) => {\n crumbs.push({ ts: Date.now(), type, message: clip(message, 120) });\n if (crumbs.length > MAX_CRUMBS) crumbs.shift();\n },\n stop: () => {\n window.removeEventListener(\"error\", onError);\n window.removeEventListener(\"unhandledrejection\", onRejection);\n window.removeEventListener(\"pagehide\", onHide);\n window.clearInterval(timer);\n flush(true);\n },\n };\n};\n"],"mappings":";AAWA,IAAM,YAAY,IAAI;AACtB,IAAM,eAAe;AACrB,IAAM,aAAa;AACnB,IAAM,WAAW;AAUjB,IAAM,OAAO,CAAC,GAAW,MAAe,EAAE,SAAS,IAAI,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC,WAAM;AAG1E,IAAM,gBAAgB,CAAC,UAAsE;AAChG,MAAI,iBAAiB,MAAO,QAAO,EAAE,MAAM,MAAM,QAAQ,SAAS,SAAS,KAAK,MAAM,WAAW,OAAO,KAAK,GAAG,GAAI,GAAG,OAAO,MAAM,QAAQ,KAAK,MAAM,OAAO,SAAS,IAAI,OAAU;AACrL,MAAI,OAAO,UAAU,SAAU,QAAO,EAAE,MAAM,SAAS,SAAS,KAAK,OAAO,GAAI,EAAE;AAClF,MAAI;AACA,WAAO,EAAE,MAAM,SAAS,SAAS,KAAK,KAAK,UAAU,KAAK,KAAK,OAAO,KAAK,GAAG,GAAI,EAAE;AAAA,EACxF,QAAQ;AACJ,WAAO,EAAE,MAAM,SAAS,SAAS,OAAO,KAAK,EAAE;AAAA,EACnD;AACJ;AAEO,IAAM,oBAAoB,CAAC,SAOd;AAChB,QAAM,QAAuB,CAAC;AAC9B,QAAM,SAA6C,CAAC;AACpD,QAAM,OAAO,oBAAI,IAAY;AAC7B,MAAI,OAAO;AACX,QAAM,MAAM,GAAG,KAAK,IAAI;AAExB,QAAM,QAAQ,CAAC,SAAS,UAAU;AAE9B,WAAO,MAAM,QAAQ;AACjB,YAAM,OAAO,KAAK,UAAU,EAAE,KAAK,KAAK,QAAQ,QAAQ,MAAM,OAAO,GAAG,CAAC,EAAE,CAAC;AAC5E,YAAM,KAAK,UAAU,OAAO,cAAc,eAAe,gBAAgB,aAAa,UAAU,WAAW,KAAK,I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UAAU,IAAI,MAAM,6BAA6B,GAAG,sBAAsB;AACtI,QAAM,SAAS,MAAM,MAAM,IAAI;AAC/B,SAAO,iBAAiB,SAAS,OAAO;AACxC,SAAO,iBAAiB,sBAAsB,WAAW;AACzD,SAAO,iBAAiB,YAAY,MAAM;AAC1C,QAAM,QAAQ,OAAO,YAAY,MAAM,MAAM,GAAG,QAAQ;AAExD,SAAO;AAAA,IACH;AAAA,IACA,OAAO,CAAC,MAAM,YAAY;AACtB,aAAO,KAAK,EAAE,IAAI,KAAK,IAAI,GAAG,MAAM,SAAS,KAAK,SAAS,GAAG,EAAE,CAAC;AACjE,UAAI,OAAO,SAAS,WAAY,QAAO,MAAM;AAAA,IACjD;AAAA,IACA,MAAM,MAAM;AACR,aAAO,oBAAoB,SAAS,OAAO;AAC3C,aAAO,oBAAoB,sBAAsB,WAAW;AAC5D,aAAO,oBAAoB,YAAY,MAAM;AAC7C,aAAO,cAAc,KAAK;AAC1B,YAAM,IAAI;AAAA,IACd;AAAA,EACJ;AACJ;","names":[]}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { type ChangelogEntry, type CockpitProviderProps, type CockpitUser, type FeedbackBoardInfo, type FeedbackBoardProps, type FeedbackComment, type FeedbackList, type FeedbackPost, type FeedbackRoadmap, type FeedbackWidgetProps, type PostStatus, type SurveyAnswer, type SurveyDefinition, type SurveyQuestion, type SurveyQuestionKind, CockpitProvider, Experiment, FeedbackBoard, FeedbackWidget, Survey, Text, WIDGET_CSS, createApi, useCockpit, useFeedback, useStyles, useVariant, } from "./browser.js";
2
- export { type CockpitConfig, type CockpitEvent, type ConsentMode, type ExperimentConfig, type VariantConfig, type VariantId, EMBED_PARAM, ID_COOKIE, PREVIEW_PARAM, SESSION_COOKIE, assign, parsePreview, previewHref, } from "./core.js";
1
+ export { type ChangelogEntry, type CockpitProviderProps, type CockpitUser, type FeedbackBoardInfo, type FeedbackBoardProps, type FeedbackComment, type FeedbackList, type FeedbackPost, type FeedbackRoadmap, type FeedbackWidgetProps, type PostStatus, type SurveyAnswer, type SurveyDefinition, type SurveyQuestion, type SurveyQuestionKind, CockpitProvider, Experiment, FeedbackBoard, FeedbackWidget, Survey, Text, WIDGET_CSS, createApi, useCockpit, useFeedback, useFlag, useFlagVariant, useStyles, useVariant, } from "./browser.js";
2
+ export { type CockpitConfig, type CockpitEvent, type ConsentChoice, type ConsentMode, type ReplayConfig, type ErrorReport, type ExperimentConfig, type FlagCondition, type FlagConditionSet, type FlagConfig, type FlagPerson, type VariantConfig, type VariantId, EMBED_PARAM, SDK_VERSION, ID_COOKIE, PREVIEW_PARAM, SESSION_COOKIE, assign, evaluateFlag, parsePreview, previewHref, } from "./core.js";
3
3
  export { type AttributionMode, type CheckoutReferral, type Referral, REFERRAL_COOKIE, getReferral, readReferralCookie, referralForCheckout, referralLink, } from "./referral.js";
package/dist/index.js CHANGED
@@ -10,15 +10,19 @@ import {
10
10
  createApi,
11
11
  useCockpit,
12
12
  useFeedback,
13
+ useFlag,
14
+ useFlagVariant,
13
15
  useStyles,
14
16
  useVariant
15
17
  } from "./browser.js";
16
18
  import {
17
19
  EMBED_PARAM,
20
+ SDK_VERSION,
18
21
  ID_COOKIE,
19
22
  PREVIEW_PARAM,
20
23
  SESSION_COOKIE,
21
24
  assign,
25
+ evaluateFlag,
22
26
  parsePreview,
23
27
  previewHref
24
28
  } from "./core.js";
@@ -38,12 +42,14 @@ export {
38
42
  ID_COOKIE,
39
43
  PREVIEW_PARAM,
40
44
  REFERRAL_COOKIE,
45
+ SDK_VERSION,
41
46
  SESSION_COOKIE,
42
47
  Survey,
43
48
  Text,
44
49
  WIDGET_CSS,
45
50
  assign,
46
51
  createApi,
52
+ evaluateFlag,
47
53
  getReferral,
48
54
  parsePreview,
49
55
  previewHref,
@@ -52,6 +58,8 @@ export {
52
58
  referralLink,
53
59
  useCockpit,
54
60
  useFeedback,
61
+ useFlag,
62
+ useFlagVariant,
55
63
  useStyles,
56
64
  useVariant
57
65
  };
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export {\n type ChangelogEntry,\n type CockpitProviderProps,\n type CockpitUser,\n type FeedbackBoardInfo,\n type FeedbackBoardProps,\n type FeedbackComment,\n type FeedbackList,\n type FeedbackPost,\n type FeedbackRoadmap,\n type FeedbackWidgetProps,\n type PostStatus,\n type SurveyAnswer,\n type SurveyDefinition,\n type SurveyQuestion,\n type SurveyQuestionKind,\n CockpitProvider,\n Experiment,\n FeedbackBoard,\n FeedbackWidget,\n Survey,\n Text,\n WIDGET_CSS,\n createApi,\n useCockpit,\n useFeedback,\n useStyles,\n useVariant,\n} from \"./browser\";\nexport {\n type CockpitConfig,\n type CockpitEvent,\n type ConsentMode,\n type ExperimentConfig,\n type VariantConfig,\n type VariantId,\n EMBED_PARAM,\n ID_COOKIE,\n PREVIEW_PARAM,\n SESSION_COOKIE,\n assign,\n parsePreview,\n previewHref,\n} from \"./core\";\nexport {\n type AttributionMode,\n type CheckoutReferral,\n type Referral,\n REFERRAL_COOKIE,\n getReferral,\n readReferralCookie,\n referralForCheckout,\n referralLink,\n} from \"./referral\";\n"],"mappings":";AAAA;AAAA,EAgBI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP;AAAA,EAOI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP;AAAA,EAII;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["export {\n type ChangelogEntry,\n type CockpitProviderProps,\n type CockpitUser,\n type FeedbackBoardInfo,\n type FeedbackBoardProps,\n type FeedbackComment,\n type FeedbackList,\n type FeedbackPost,\n type FeedbackRoadmap,\n type FeedbackWidgetProps,\n type PostStatus,\n type SurveyAnswer,\n type SurveyDefinition,\n type SurveyQuestion,\n type SurveyQuestionKind,\n CockpitProvider,\n Experiment,\n FeedbackBoard,\n FeedbackWidget,\n Survey,\n Text,\n WIDGET_CSS,\n createApi,\n useCockpit,\n useFeedback,\n useFlag,\n useFlagVariant,\n useStyles,\n useVariant,\n} from \"./browser\";\nexport {\n type CockpitConfig,\n type CockpitEvent,\n type ConsentChoice,\n type ConsentMode,\n type ReplayConfig,\n type ErrorReport,\n type ExperimentConfig,\n type FlagCondition,\n type FlagConditionSet,\n type FlagConfig,\n type FlagPerson,\n type VariantConfig,\n type VariantId,\n EMBED_PARAM,\n SDK_VERSION,\n ID_COOKIE,\n PREVIEW_PARAM,\n SESSION_COOKIE,\n assign,\n evaluateFlag,\n parsePreview,\n previewHref,\n} from \"./core\";\nexport {\n type AttributionMode,\n type CheckoutReferral,\n type Referral,\n REFERRAL_COOKIE,\n getReferral,\n readReferralCookie,\n referralForCheckout,\n referralLink,\n} from \"./referral\";\n"],"mappings":";AAAA;AAAA,EAgBI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP;AAAA,EAcI;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;AACP;AAAA,EAII;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACG;","names":[]}
@@ -0,0 +1,93 @@
1
+ export declare const DEFAULT_SAMPLE_RATE = 0.1;
2
+ /** A recording stops after 30 minutes. */
3
+ export declare const MAX_RECORDING_MS: number;
4
+ /** A visit outside the sample keeps about this much in memory, from the last full snapshot on. */
5
+ export declare const BUFFER_MS: number;
6
+ /** How often a buffering visit takes a new full snapshot, so the buffer can drop what is older. */
7
+ export declare const CHECKOUT_MS: number;
8
+ /** Upload a chunk every 5 seconds or once it holds about 64 KB of events, whichever comes first. */
9
+ export declare const FLUSH_MS: number;
10
+ export declare const FLUSH_BYTES: number;
11
+ /** The buffer never holds more than this; the oldest snapshot goes first. */
12
+ export declare const BUFFER_MAX_BYTES: number;
13
+ /** The server takes at most 1 MB per request; a chunk that would not fit ends the recording. */
14
+ export declare const MAX_REQUEST_BYTES: number;
15
+ /** Why a recording was uploaded. */
16
+ export type ReplayTrigger = "sample" | "error" | "rage" | "purchase" | "checkout";
17
+ /** Never recorded: password, hidden and card fields, payment iframes, and whatever the site marks `data-ck-block`. */
18
+ export declare const BLOCK_SELECTOR: string;
19
+ export declare const MASK_TEXT_SELECTOR = "[data-ck-mask]";
20
+ /** Whether this session uploads from its start. Stable per session id, so every page of a session agrees. */
21
+ export declare const inSample: (sessionId: string, rate: number) => boolean;
22
+ /** A tracked event that uploads the buffered minute: names starting with `purchase` or `checkout`. */
23
+ export declare const triggerOfEvent: (event: string) => "purchase" | "checkout" | null;
24
+ /** Typed text never reaches MRRocket: every input value becomes stars, at most 12. */
25
+ export declare const maskInput: (text: string) => string;
26
+ /**
27
+ * rrweb's record options: all inputs masked, blocked fields left out, text in `data-ck-mask` masked, no canvas, video,
28
+ * fonts or inlined images, scripts and head clutter dropped. `emit` receives the events.
29
+ */
30
+ export declare const recordOptions: <E>(emit: (e: E) => void) => {
31
+ emit: (e: E) => void;
32
+ maskAllInputs: boolean;
33
+ maskInputOptions: {
34
+ color: boolean;
35
+ date: boolean;
36
+ "datetime-local": boolean;
37
+ email: boolean;
38
+ month: boolean;
39
+ number: boolean;
40
+ range: boolean;
41
+ search: boolean;
42
+ tel: boolean;
43
+ text: boolean;
44
+ time: boolean;
45
+ url: boolean;
46
+ week: boolean;
47
+ textarea: boolean;
48
+ select: boolean;
49
+ password: boolean;
50
+ };
51
+ maskInputFn: (text: string) => string;
52
+ maskTextSelector: string;
53
+ blockSelector: string;
54
+ recordCanvas: boolean;
55
+ recordCrossOriginIframes: boolean;
56
+ collectFonts: boolean;
57
+ inlineImages: boolean;
58
+ slimDOMOptions: "all";
59
+ sampling: {
60
+ mousemove: number;
61
+ scroll: number;
62
+ media: number;
63
+ input: "last";
64
+ };
65
+ };
66
+ /** The few rrweb fields this module reads. Type 4 is the Meta event that starts each full snapshot. */
67
+ export interface ReplayEvent {
68
+ type: number;
69
+ timestamp: number;
70
+ }
71
+ /**
72
+ * The last minute of a visit that is not uploaded (yet). Events are kept in segments that each start at a full
73
+ * snapshot; a segment is dropped once the next one already covers the whole window, so whatever is drained starts
74
+ * with a full snapshot and holds at least the last `windowMs`.
75
+ */
76
+ export declare class RingBuffer<E extends ReplayEvent = ReplayEvent> {
77
+ private readonly windowMs;
78
+ private readonly maxBytes;
79
+ private segments;
80
+ constructor(windowMs?: number, maxBytes?: number);
81
+ push(e: E, bytes: number): void;
82
+ get bytes(): number;
83
+ get size(): number;
84
+ /** The oldest event kept, or null when empty. */
85
+ get first(): number;
86
+ /** Everything kept, oldest first, and empties the buffer. */
87
+ drain(): E[];
88
+ clear(): void;
89
+ }
90
+ /** Whether a chunk is due: enough bytes, or enough time since the last upload. */
91
+ export declare const chunkDue: (bytes: number, sinceFlushMs: number) => boolean;
92
+ /** Base64 of bytes, in slices so a large chunk does not overflow the call stack. */
93
+ export declare const toBase64: (bytes: Uint8Array) => string;
@@ -0,0 +1,122 @@
1
+ // src/replay-core.ts
2
+ import { BLOCK_ATTR_SELECTOR, MASK_SELECTOR } from "./capture.js";
3
+ import { hashUnit } from "./core.js";
4
+ var DEFAULT_SAMPLE_RATE = 0.1;
5
+ var MAX_RECORDING_MS = 30 * 60 * 1e3;
6
+ var BUFFER_MS = 60 * 1e3;
7
+ var CHECKOUT_MS = 30 * 1e3;
8
+ var FLUSH_MS = 5 * 1e3;
9
+ var FLUSH_BYTES = 64 * 1024;
10
+ var BUFFER_MAX_BYTES = 4 * 1024 * 1024;
11
+ var MAX_REQUEST_BYTES = 1024 * 1024;
12
+ var BLOCK_SELECTOR = [
13
+ BLOCK_ATTR_SELECTOR,
14
+ 'input[type="password"]',
15
+ 'input[type="hidden"]',
16
+ '[autocomplete^="cc-"]',
17
+ 'input[name*="card" i]',
18
+ 'input[name*="cvc" i]',
19
+ 'input[name*="cvv" i]',
20
+ 'iframe[src*="js.stripe.com"]',
21
+ 'iframe[src*="paypal.com"]'
22
+ ].join(",");
23
+ var MASK_TEXT_SELECTOR = MASK_SELECTOR;
24
+ var inSample = (sessionId, rate) => hashUnit(`replay:${sessionId}`) < Math.min(1, Math.max(0, rate));
25
+ var triggerOfEvent = (event) => {
26
+ const e = event.toLowerCase().replace(/^\$/, "");
27
+ return e.startsWith("purchase") ? "purchase" : e.startsWith("checkout") ? "checkout" : null;
28
+ };
29
+ var maskInput = (text) => "*".repeat(Math.min(12, text.length));
30
+ var recordOptions = (emit) => ({
31
+ emit,
32
+ maskAllInputs: true,
33
+ maskInputOptions: {
34
+ color: true,
35
+ date: true,
36
+ "datetime-local": true,
37
+ email: true,
38
+ month: true,
39
+ number: true,
40
+ range: true,
41
+ search: true,
42
+ tel: true,
43
+ text: true,
44
+ time: true,
45
+ url: true,
46
+ week: true,
47
+ textarea: true,
48
+ select: true,
49
+ password: true
50
+ },
51
+ maskInputFn: maskInput,
52
+ maskTextSelector: MASK_TEXT_SELECTOR,
53
+ blockSelector: BLOCK_SELECTOR,
54
+ recordCanvas: false,
55
+ recordCrossOriginIframes: false,
56
+ collectFonts: false,
57
+ inlineImages: false,
58
+ slimDOMOptions: "all",
59
+ sampling: { mousemove: 50, scroll: 150, media: 800, input: "last" }
60
+ });
61
+ var RingBuffer = class {
62
+ constructor(windowMs = BUFFER_MS, maxBytes = BUFFER_MAX_BYTES) {
63
+ this.windowMs = windowMs;
64
+ this.maxBytes = maxBytes;
65
+ }
66
+ windowMs;
67
+ maxBytes;
68
+ segments = [];
69
+ push(e, bytes) {
70
+ if (e.type === 4 || !this.segments.length) this.segments.push({ events: [], bytes: 0 });
71
+ const seg = this.segments[this.segments.length - 1];
72
+ seg.events.push(e);
73
+ seg.bytes += bytes;
74
+ const from = e.timestamp - this.windowMs;
75
+ while (this.segments.length > 1 && (this.segments[1].events[0].timestamp <= from || this.bytes > this.maxBytes)) this.segments.shift();
76
+ }
77
+ get bytes() {
78
+ return this.segments.reduce((s, x) => s + x.bytes, 0);
79
+ }
80
+ get size() {
81
+ return this.segments.reduce((s, x) => s + x.events.length, 0);
82
+ }
83
+ /** The oldest event kept, or null when empty. */
84
+ get first() {
85
+ return this.segments[0]?.events[0]?.timestamp ?? null;
86
+ }
87
+ /** Everything kept, oldest first, and empties the buffer. */
88
+ drain() {
89
+ const out = this.segments.flatMap((s) => s.events);
90
+ this.segments = [];
91
+ return out;
92
+ }
93
+ clear() {
94
+ this.segments = [];
95
+ }
96
+ };
97
+ var chunkDue = (bytes, sinceFlushMs) => bytes >= FLUSH_BYTES || bytes > 0 && sinceFlushMs >= FLUSH_MS;
98
+ var toBase64 = (bytes) => {
99
+ let s = "";
100
+ for (let i = 0; i < bytes.length; i += 32768) s += String.fromCharCode(...bytes.subarray(i, i + 32768));
101
+ return btoa(s);
102
+ };
103
+ export {
104
+ BLOCK_SELECTOR,
105
+ BUFFER_MAX_BYTES,
106
+ BUFFER_MS,
107
+ CHECKOUT_MS,
108
+ DEFAULT_SAMPLE_RATE,
109
+ FLUSH_BYTES,
110
+ FLUSH_MS,
111
+ MASK_TEXT_SELECTOR,
112
+ MAX_RECORDING_MS,
113
+ MAX_REQUEST_BYTES,
114
+ RingBuffer,
115
+ chunkDue,
116
+ inSample,
117
+ maskInput,
118
+ recordOptions,
119
+ toBase64,
120
+ triggerOfEvent
121
+ };
122
+ //# sourceMappingURL=replay-core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/replay-core.ts"],"sourcesContent":["/**\n * The rules of session recording, without the browser: which visits upload from the start, what a trigger is, how the\n * last minute is kept in memory, how events become upload chunks, and the privacy defaults handed to rrweb. The\n * recorder itself (./replay.ts) is loaded only when a visit records; this file is shared with MRRocket's own checks.\n */\nimport { BLOCK_ATTR_SELECTOR, MASK_SELECTOR } from \"./capture\";\nimport { hashUnit } from \"./core\";\n\nexport const DEFAULT_SAMPLE_RATE = 0.1;\n/** A recording stops after 30 minutes. */\nexport const MAX_RECORDING_MS = 30 * 60 * 1000;\n/** A visit outside the sample keeps about this much in memory, from the last full snapshot on. */\nexport const BUFFER_MS = 60 * 1000;\n/** How often a buffering visit takes a new full snapshot, so the buffer can drop what is older. */\nexport const CHECKOUT_MS = 30 * 1000;\n/** Upload a chunk every 5 seconds or once it holds about 64 KB of events, whichever comes first. */\nexport const FLUSH_MS = 5 * 1000;\nexport const FLUSH_BYTES = 64 * 1024;\n/** The buffer never holds more than this; the oldest snapshot goes first. */\nexport const BUFFER_MAX_BYTES = 4 * 1024 * 1024;\n/** The server takes at most 1 MB per request; a chunk that would not fit ends the recording. */\nexport const MAX_REQUEST_BYTES = 1024 * 1024;\n\n/** Why a recording was uploaded. */\nexport type ReplayTrigger = \"sample\" | \"error\" | \"rage\" | \"purchase\" | \"checkout\";\n\n/** Never recorded: password, hidden and card fields, payment iframes, and whatever the site marks `data-ck-block`. */\nexport const BLOCK_SELECTOR = [\n BLOCK_ATTR_SELECTOR,\n 'input[type=\"password\"]',\n 'input[type=\"hidden\"]',\n '[autocomplete^=\"cc-\"]',\n 'input[name*=\"card\" i]',\n 'input[name*=\"cvc\" i]',\n 'input[name*=\"cvv\" i]',\n 'iframe[src*=\"js.stripe.com\"]',\n 'iframe[src*=\"paypal.com\"]',\n].join(\",\");\nexport const MASK_TEXT_SELECTOR = MASK_SELECTOR;\n\n/** Whether this session uploads from its start. Stable per session id, so every page of a session agrees. */\nexport const inSample = (sessionId: string, rate: number) => hashUnit(`replay:${sessionId}`) < Math.min(1, Math.max(0, rate));\n\n/** A tracked event that uploads the buffered minute: names starting with `purchase` or `checkout`. */\nexport const triggerOfEvent = (event: string): \"purchase\" | \"checkout\" | null => {\n const e = event.toLowerCase().replace(/^\\$/, \"\");\n return e.startsWith(\"purchase\") ? \"purchase\" : e.startsWith(\"checkout\") ? \"checkout\" : null;\n};\n\n/** Typed text never reaches MRRocket: every input value becomes stars, at most 12. */\nexport const maskInput = (text: string) => \"*\".repeat(Math.min(12, text.length));\n\n/**\n * rrweb's record options: all inputs masked, blocked fields left out, text in `data-ck-mask` masked, no canvas, video,\n * fonts or inlined images, scripts and head clutter dropped. `emit` receives the events.\n */\nexport const recordOptions = <E>(emit: (e: E) => void) => ({\n emit,\n maskAllInputs: true,\n maskInputOptions: {\n color: true,\n date: true,\n \"datetime-local\": true,\n email: true,\n month: true,\n number: true,\n range: true,\n search: true,\n tel: true,\n text: true,\n time: true,\n url: true,\n week: true,\n textarea: true,\n select: true,\n password: true,\n },\n maskInputFn: maskInput,\n maskTextSelector: MASK_TEXT_SELECTOR,\n blockSelector: BLOCK_SELECTOR,\n recordCanvas: false,\n recordCrossOriginIframes: false,\n collectFonts: false,\n inlineImages: false,\n slimDOMOptions: \"all\" as const,\n sampling: { mousemove: 50, scroll: 150, media: 800, input: \"last\" as const },\n});\n\n/** The few rrweb fields this module reads. Type 4 is the Meta event that starts each full snapshot. */\nexport interface ReplayEvent {\n type: number;\n timestamp: number;\n}\n\n/**\n * The last minute of a visit that is not uploaded (yet). Events are kept in segments that each start at a full\n * snapshot; a segment is dropped once the next one already covers the whole window, so whatever is drained starts\n * with a full snapshot and holds at least the last `windowMs`.\n */\nexport class RingBuffer<E extends ReplayEvent = ReplayEvent> {\n private segments: { events: E[]; bytes: number }[] = [];\n constructor(\n private readonly windowMs = BUFFER_MS,\n private readonly maxBytes = BUFFER_MAX_BYTES,\n ) {}\n\n push(e: E, bytes: number) {\n if (e.type === 4 || !this.segments.length) this.segments.push({ events: [], bytes: 0 });\n const seg = this.segments[this.segments.length - 1];\n seg.events.push(e);\n seg.bytes += bytes;\n const from = e.timestamp - this.windowMs;\n while (this.segments.length > 1 && (this.segments[1].events[0].timestamp <= from || this.bytes > this.maxBytes)) this.segments.shift();\n }\n\n get bytes() {\n return this.segments.reduce((s, x) => s + x.bytes, 0);\n }\n\n get size() {\n return this.segments.reduce((s, x) => s + x.events.length, 0);\n }\n\n /** The oldest event kept, or null when empty. */\n get first() {\n return this.segments[0]?.events[0]?.timestamp ?? null;\n }\n\n /** Everything kept, oldest first, and empties the buffer. */\n drain(): E[] {\n const out = this.segments.flatMap((s) => s.events);\n this.segments = [];\n return out;\n }\n\n clear() {\n this.segments = [];\n }\n}\n\n/** Whether a chunk is due: enough bytes, or enough time since the last upload. */\nexport const chunkDue = (bytes: number, sinceFlushMs: number) => bytes >= FLUSH_BYTES || (bytes > 0 && sinceFlushMs >= FLUSH_MS);\n\n/** Base64 of bytes, in slices so a large chunk does not overflow the call stack. */\nexport const toBase64 = (bytes: Uint8Array) => {\n let s = \"\";\n for (let i = 0; i < bytes.length; i += 0x8000) s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));\n return btoa(s);\n};\n"],"mappings":";AAKA,SAAS,qBAAqB,qBAAqB;AACnD,SAAS,gBAAgB;AAElB,IAAM,sBAAsB;AAE5B,IAAM,mBAAmB,KAAK,KAAK;AAEnC,IAAM,YAAY,KAAK;AAEvB,IAAM,cAAc,KAAK;AAEzB,IAAM,WAAW,IAAI;AACrB,IAAM,cAAc,KAAK;AAEzB,IAAM,mBAAmB,IAAI,OAAO;AAEpC,IAAM,oBAAoB,OAAO;AAMjC,IAAM,iBAAiB;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ,EAAE,KAAK,GAAG;AACH,IAAM,qBAAqB;AAG3B,IAAM,WAAW,CAAC,WAAmB,SAAiB,SAAS,UAAU,SAAS,EAAE,IAAI,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,IAAI,CAAC;AAGrH,IAAM,iBAAiB,CAAC,UAAkD;AAC7E,QAAM,IAAI,MAAM,YAAY,EAA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SAAS,MAAM,KAAK,SAAS,CAAC,EAAE,OAAO,CAAC,EAAE,aAAa,QAAQ,KAAK,QAAQ,KAAK,UAAW,MAAK,SAAS,MAAM;AAAA,EACzI;AAAA,EAEA,IAAI,QAAQ;AACR,WAAO,KAAK,SAAS,OAAO,CAAC,GAAG,MAAM,IAAI,EAAE,OAAO,CAAC;AAAA,EACxD;AAAA,EAEA,IAAI,OAAO;AACP,WAAO,KAAK,SAAS,OAAO,CAAC,GAAG,MAAM,IAAI,EAAE,OAAO,QAAQ,CAAC;AAAA,EAChE;AAAA;AAAA,EAGA,IAAI,QAAQ;AACR,WAAO,KAAK,SAAS,CAAC,GAAG,OAAO,CAAC,GAAG,aAAa;AAAA,EACrD;AAAA;AAAA,EAGA,QAAa;AACT,UAAM,MAAM,KAAK,SAAS,QAAQ,CAAC,MAAM,EAAE,MAAM;AACjD,SAAK,WAAW,CAAC;AACjB,WAAO;AAAA,EACX;AAAA,EAEA,QAAQ;AACJ,SAAK,WAAW,CAAC;AAAA,EACrB;AACJ;AAGO,IAAM,WAAW,CAAC,OAAe,iBAAyB,SAAS,eAAgB,QAAQ,KAAK,gBAAgB;AAGhH,IAAM,WAAW,CAAC,UAAsB;AAC3C,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,MAAQ,MAAK,OAAO,aAAa,GAAG,MAAM,SAAS,GAAG,IAAI,KAAM,CAAC;AACxG,SAAO,KAAK,CAAC;AACjB;","names":[]}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * The session recorder. The provider imports this file (and rrweb's recorder with it) only when a visit records: the
3
+ * product has recordings on and the visitor gave replay consent. Pages without recording never load it.
4
+ *
5
+ * A visit in the product's sample uploads from its start. Any other visit keeps the last minute in memory and uploads
6
+ * it only when something happens worth watching: a JS error, a rage click, or a `purchase…` / `checkout…` event; from
7
+ * then on it uploads like a sampled one. Chunks go every 5 seconds or 64 KB, gzip'd where the browser can, to
8
+ * `POST {host}/api/sdk/replay`; the last one leaves with sendBeacon when the page hides. A recording ends after 30
9
+ * minutes. Withdrawing consent stops it and drops what was not sent.
10
+ */
11
+ import type { ClickInfo } from "./capture.js";
12
+ export interface ReplayOptions {
13
+ host: string;
14
+ apiKey: string;
15
+ /** The SDK's session id; every chunk carries it, so the recording joins the session's events. */
16
+ sessionId: string;
17
+ /** The identify() id or the cookie id; missing cookieless until identify(). */
18
+ distinctId?: string;
19
+ /** Whether `distinctId` comes from identify() (a person) rather than the visitor cookie. */
20
+ identified?: boolean;
21
+ sampleRate: number;
22
+ warn: (message: string) => void;
23
+ }
24
+ export interface ReplayHandle {
25
+ /** A tracked event; `purchase…` and `checkout…` upload the buffered minute. */
26
+ event(name: string): void;
27
+ click(c: ClickInfo, rage: boolean): void;
28
+ /** A client-side navigation. */
29
+ page(path: string): void;
30
+ identify(id: string): void;
31
+ /** Stops recording. `discard` (consent withdrawn) drops everything not sent yet. */
32
+ stop(discard?: boolean): void;
33
+ }
34
+ export declare const startReplay: (o: ReplayOptions) => Promise<ReplayHandle | null>;