@mrrocket/sdk 0.4.0 → 0.8.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +102 -7
  3. package/dist/api.d.ts +23 -3
  4. package/dist/api.js +7 -3
  5. package/dist/api.js.map +1 -1
  6. package/dist/browser/mrrocket-flows.js +64 -0
  7. package/dist/browser/mrrocket-push.js +1 -0
  8. package/dist/browser/mrrocket-replay.js +3 -3
  9. package/dist/browser/mrrocket-sw.js +1 -0
  10. package/dist/browser/mrrocket.js +2 -1
  11. package/dist/browser.d.ts +1 -1
  12. package/dist/browser.js +3 -1
  13. package/dist/browser.js.map +1 -1
  14. package/dist/client.d.ts +36 -1
  15. package/dist/client.js +161 -6
  16. package/dist/client.js.map +1 -1
  17. package/dist/context.d.ts +10 -0
  18. package/dist/context.js.map +1 -1
  19. package/dist/core.d.ts +64 -3
  20. package/dist/core.js +54 -2
  21. package/dist/core.js.map +1 -1
  22. package/dist/feedback.d.ts +19 -4
  23. package/dist/feedback.js +652 -190
  24. package/dist/feedback.js.map +1 -1
  25. package/dist/flows-ui.d.ts +23 -0
  26. package/dist/flows-ui.js +384 -0
  27. package/dist/flows-ui.js.map +1 -0
  28. package/dist/flows-web.d.ts +19 -0
  29. package/dist/flows-web.js +42 -0
  30. package/dist/flows-web.js.map +1 -0
  31. package/dist/flows.d.ts +222 -0
  32. package/dist/flows.js +409 -0
  33. package/dist/flows.js.map +1 -0
  34. package/dist/index.d.ts +5 -2
  35. package/dist/index.js +9 -1
  36. package/dist/index.js.map +1 -1
  37. package/dist/proxy.d.ts +34 -1
  38. package/dist/proxy.js +14 -1
  39. package/dist/proxy.js.map +1 -1
  40. package/dist/push-web.d.ts +43 -0
  41. package/dist/push-web.js +81 -0
  42. package/dist/push-web.js.map +1 -0
  43. package/dist/react-native/index.d.cts +305 -3
  44. package/dist/react-native/index.d.ts +305 -3
  45. package/dist/react-native/index.js +852 -31
  46. package/dist/react-native/index.js.map +1 -1
  47. package/dist/styles.d.ts +1 -1
  48. package/dist/styles.js +127 -24
  49. package/dist/styles.js.map +1 -1
  50. package/dist/ui.d.ts +3 -0
  51. package/dist/ui.js +12 -0
  52. package/dist/ui.js.map +1 -1
  53. package/package.json +10 -2
@@ -1,19 +1,200 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
3
 
4
+ /**
5
+ * The cancel flow, onboarding and in-app messages (SDK 0.7.0): what MRRocket's config and `GET /api/sdk/inapp` serve,
6
+ * and one controller that decides what shows when and records what the person did. No DOM and no React: the web
7
+ * renderer (flows-ui.ts, loaded only when something shows) and the React Native one (native/flows-view.ts) draw
8
+ * `view()` and call the actions.
9
+ *
10
+ * What it records, as normal events under the SDK's analytics consent (nothing with consent "off"):
11
+ * - `$cancel_flow` `{action: "shown" | "answered" | "offer" | "finished", run, version, …}`. MRRocket turns a
12
+ * `finished` with `outcome: "saved"` into a save and with `outcome: "cancelled"` into a cancellation on the Churn page.
13
+ * MRRocket never changes a subscription: an accepted offer goes to the app's `onSave`, a confirmed cancel to its
14
+ * `onCancel`, and only after they succeed is the outcome recorded.
15
+ * - `$onboarding` `{flow, step?, action: "started" | "shown" | "completed" | "dismissed"}`.
16
+ * - `$inapp` `{message, journey, action: "shown" | "clicked" | "dismissed"}`; the journey moves the person on.
17
+ */
18
+
19
+ type CancelOfferKind = "discount" | "pause" | "downgrade" | "contact";
20
+ /** What one answer of the survey offers before the confirmation. An answer without one goes straight to it. */
21
+ interface CancelOffer {
22
+ kind: CancelOfferKind;
23
+ headline: string;
24
+ /** Discount: percent off. */
25
+ percent?: number;
26
+ /** Discount: for how many months; pause: up to how many months. */
27
+ months?: number;
28
+ /** Downgrade: the plan to switch to, as MRRocket knows it from the payment provider. */
29
+ plan?: string;
30
+ planName?: string;
31
+ }
32
+ /** The product's live cancel flow (MRRocket › Churn › Cancel flow). */
33
+ interface CancelFlowConfig {
34
+ /** Grows with every publish. */
35
+ version: number;
36
+ question: string;
37
+ /** The survey's answers, in order. */
38
+ reasons: {
39
+ id: string;
40
+ label: string;
41
+ }[];
42
+ /** The offer per answer id. */
43
+ offers: Record<string, CancelOffer>;
44
+ confirm: {
45
+ title: string;
46
+ body: string;
47
+ button: string;
48
+ };
49
+ /**
50
+ * Who gets the survey and the offers: business customers only. Anyone else (a consumer, or a customer the app does
51
+ * not say) gets the plain confirmation, with nothing in the way of cancelling (§ 312k BGB).
52
+ */
53
+ offersFor: "business";
54
+ }
55
+ type OnboardingStepKind = "tour" | "checklist" | "modal";
56
+ interface OnboardingStep {
57
+ id: string;
58
+ kind: OnboardingStepKind;
59
+ title: string;
60
+ body?: string;
61
+ button?: string;
62
+ /** Tour steps: the element they point at, marked `data-ck-target="<target>"` in the app. Without it: a card. */
63
+ target?: string;
64
+ /** Checklist items: the event that ticks them (`track("<event>")`). Without it the person ticks it themselves. */
65
+ event?: string;
66
+ }
67
+ /** A live onboarding flow's in-app steps (MRRocket › Conversion › Onboarding); its emails go out from MRRocket. */
68
+ interface OnboardingFlow {
69
+ id: string;
70
+ name: string;
71
+ /** When it starts: an event's words ("Signed up", "First export"), or "First launch" / "First visit". */
72
+ trigger: string;
73
+ steps: OnboardingStep[];
74
+ }
75
+ /** An in-app message a Lifecycle journey has for this person. */
76
+ interface InAppMessage {
77
+ id: string;
78
+ journey?: string;
79
+ title: string;
80
+ body?: string;
81
+ button?: string;
82
+ /** Where the button goes (http(s) or a path of the app). */
83
+ url?: string;
84
+ style: "banner" | "modal";
85
+ }
86
+ interface CancelFlowResult {
87
+ /** "cancelled": confirmed; "saved": took the offer; "kept": closed the dialog without cancelling. */
88
+ outcome: "cancelled" | "saved" | "kept";
89
+ reason?: string;
90
+ reasonLabel?: string;
91
+ offer?: CancelOffer;
92
+ /** What the customer wrote on the confirmation, if anything. */
93
+ comment?: string;
94
+ /** This run's id, the same as in the events. */
95
+ run: string;
96
+ }
97
+ interface CancelFlowOptions {
98
+ /**
99
+ * "business" shows the survey and the offers; anything else (the default) only the plain confirmation, so a
100
+ * consumer can always cancel in one step (§ 312k BGB).
101
+ */
102
+ customer?: "business" | "consumer";
103
+ /** The customer's current price, to show what a discount costs (`{ amount: 29, currency: "EUR" }`). */
104
+ price?: {
105
+ amount: number;
106
+ currency: string;
107
+ interval?: "month" | "year";
108
+ };
109
+ /** The customer confirmed: cancel the subscription in your code. Throwing keeps the dialog open with an error. */
110
+ onCancel?: (result: CancelFlowResult) => unknown;
111
+ /** The customer took an offer: apply it in your code (coupon, pause, plan change). Throwing keeps the dialog open. */
112
+ onSave?: (result: CancelFlowResult) => unknown;
113
+ /** Closed without cancelling or taking an offer. */
114
+ onClose?: (result: CancelFlowResult) => void;
115
+ }
116
+ /** What a renderer draws. */
117
+ type FlowView = {
118
+ type: "cancel";
119
+ screen: "survey" | "offer" | "confirm";
120
+ flow: CancelFlowConfig;
121
+ /** The answer picked on the survey. */
122
+ reason?: string;
123
+ offer?: CancelOffer;
124
+ price?: CancelFlowOptions["price"];
125
+ busy: boolean;
126
+ error?: string;
127
+ /** The step for the dots, 1 to `steps`. */
128
+ step: number;
129
+ steps: number;
130
+ } | {
131
+ type: "step";
132
+ flow: OnboardingFlow;
133
+ step: OnboardingStep;
134
+ index: number;
135
+ total: number;
136
+ last: boolean;
137
+ } | {
138
+ type: "checklist";
139
+ flow: OnboardingFlow;
140
+ items: {
141
+ step: OnboardingStep;
142
+ done: boolean;
143
+ }[];
144
+ } | {
145
+ type: "message";
146
+ message: InAppMessage;
147
+ };
148
+ interface FlowsController {
149
+ setConfig(config: CockpitConfig | null): void;
150
+ /** The person changed (identify, reset, consent): loads their messages and onboarding progress. */
151
+ refresh(): Promise<void>;
152
+ /** An event the app recorded: starts flows whose trigger it is and ticks checklist items. */
153
+ onEvent(name: string): void;
154
+ openCancelFlow(options?: CancelFlowOptions): Promise<CancelFlowResult>;
155
+ /** Starts an onboarding flow now (the first live one without an id). False when there is none to start. */
156
+ startOnboarding(id?: string): boolean;
157
+ view(): FlowView | null;
158
+ subscribe(listener: () => void): () => void;
159
+ /** `accept(comment)`: a "Talk to us" offer sends what the customer wrote with it. */
160
+ cancel: {
161
+ pick(reason: string): void;
162
+ next(): void;
163
+ accept(comment?: string): void;
164
+ decline(): void;
165
+ confirm(comment?: string): void;
166
+ close(): void;
167
+ };
168
+ onboarding: {
169
+ next(): void;
170
+ check(stepId: string): void;
171
+ dismiss(): void;
172
+ };
173
+ message: {
174
+ click(): void;
175
+ dismiss(): void;
176
+ };
177
+ }
178
+
4
179
  /**
5
180
  * The part of the SDK that runs anywhere: experiment config, variant assignment, preview links and event shapes.
6
181
  * No React, no browser APIs, so the server helper, the proxy helper and the client share it.
7
182
  */
183
+
8
184
  type VariantId = string;
9
185
  /** This build of the SDK. Sent with the config request so MRRocket knows what the site runs (error tracking, flags). */
10
- declare const SDK_VERSION = "0.4.0";
186
+ declare const SDK_VERSION = "0.8.0";
11
187
  /** One variant of an experiment. `changes` replaces texts that the page marks with `<Text id>`. */
12
188
  interface VariantConfig {
13
189
  id: VariantId;
14
190
  /** Share of traffic, 0 to 1. The weights of an experiment add up to 1. */
15
191
  weight: number;
16
192
  changes?: Record<string, string>;
193
+ /**
194
+ * A redirect test: where this variant sends the visitor instead of the test's page (a path like "/pricing-b" or a
195
+ * full address). Control has none and stays. SDK 0.7.0+.
196
+ */
197
+ url?: string;
17
198
  }
18
199
  interface ExperimentConfig {
19
200
  /** The experiment's key, the same as in Cockpit. */
@@ -23,6 +204,15 @@ interface ExperimentConfig {
23
204
  variants: VariantConfig[];
24
205
  /** A winner that shipped: everyone gets it, and nobody is assigned anymore. */
25
206
  winner?: VariantId;
207
+ /** A redirect test: the path of the page it runs on ("/pricing"). Visitors there are sent to their variant's `url`. */
208
+ path?: string;
209
+ }
210
+ /** A page redirect set in MRRocket (Conversion › Landing pages › Redirect): everyone on `from` goes to `to`. SDK 0.7.0+. */
211
+ interface RedirectConfig {
212
+ /** A path of the site, like "/old-pricing". */
213
+ from: string;
214
+ /** A path or a full address. */
215
+ to: string;
26
216
  }
27
217
  interface CockpitConfig {
28
218
  /** The product the public key belongs to. */
@@ -36,6 +226,20 @@ interface CockpitConfig {
36
226
  replay?: ReplayConfig;
37
227
  /** Feature flags of the product in Production (MRRocket › Build › Feature flags), evaluated by `useFlag` and `getFlag`. */
38
228
  flags?: FlagConfig[];
229
+ /**
230
+ * Published copy for texts the site marks (`<Text id>`, `data-ck-text`): a landing page's headline, subheadline and
231
+ * button (`pricing.headline` …) and a paywall's (`paywall.<placement>.headline` …) as published in MRRocket. A
232
+ * running test's variant changes them on top. Missing when nothing is published. SDK 0.5.0+.
233
+ */
234
+ texts?: Record<string, string>;
235
+ /** The product's live cancel flow (MRRocket › Churn › Cancel flow), for `cancelFlow.open()`. SDK 0.7.0+. */
236
+ cancelFlow?: CancelFlowConfig;
237
+ /** The in-app steps of live onboarding flows (tours, checklists, modals). SDK 0.7.0+. */
238
+ onboarding?: OnboardingFlow[];
239
+ /** A live Lifecycle journey has in-app messages: signed-in people ask `/api/sdk/inapp` for theirs. SDK 0.7.0+. */
240
+ messages?: boolean;
241
+ /** Page redirects set in MRRocket; the SDK sends visitors on before the page shows (`redirectFor`). SDK 0.7.0+. */
242
+ redirects?: RedirectConfig[];
39
243
  }
40
244
  /** One condition of a flag's audience, on a property of the person (`identify(id, props)`), the id or the page. */
41
245
  interface FlagCondition {
@@ -147,6 +351,8 @@ interface MRRocketOptions {
147
351
  maxQueue?: number;
148
352
  /** Log what the SDK does to the console. */
149
353
  debug?: boolean;
354
+ /** Onboarding flows start on their own when the person does their trigger (default true). False: only `startOnboarding()`. */
355
+ onboarding?: boolean;
150
356
  }
151
357
  /** AsyncStorage's shape. */
152
358
  interface KeyValueStorage {
@@ -154,6 +360,20 @@ interface KeyValueStorage {
154
360
  setItem(key: string, value: string): Promise<void>;
155
361
  removeItem(key: string): Promise<void>;
156
362
  }
363
+ interface RegisterForPushOptions {
364
+ /** Ask for the permission when it was not decided yet (default true). False: only register when already allowed. */
365
+ ask?: boolean;
366
+ /** Your EAS project id; default: the one in your app config (`expo-constants`). */
367
+ projectId?: string;
368
+ }
369
+ type NativePushStatus = "registered" | "denied" | "not-asked" | "unavailable" | "not-signed-in" | "error";
370
+ interface NativePushResult {
371
+ status: NativePushStatus;
372
+ /** The Expo push token, once registered. */
373
+ token?: string;
374
+ /** Why it is unavailable or failed. */
375
+ reason?: string;
376
+ }
157
377
  /** What the app (and the React hooks) use. */
158
378
  interface MRRocketClient {
159
379
  track(event: string, props?: Record<string, unknown>): void;
@@ -172,6 +392,35 @@ interface MRRocketClient {
172
392
  peekVariant(key: string): VariantId;
173
393
  /** Records that the person saw `variant` of `key`, once per screen. */
174
394
  expose(key: string, variant: VariantId): void;
395
+ /**
396
+ * The copy for text `id` as published in MRRocket (a paywall's `paywall.<placement>.headline`, an onboarding title …)
397
+ * with this person's experiment variant applied on top, the same rules as `<Text id>` on the web; `fallback` (your
398
+ * own words) while nothing is published or the config is unknown. Records the exposure to every test that changes
399
+ * `id`, once per screen.
400
+ */
401
+ getText(id: string, fallback: string): string;
402
+ /** The text without recording anything (render code); undefined when MRRocket has none. Pair it with `exposeText`. */
403
+ peekText(id: string): string | undefined;
404
+ /** Records that the person saw text `id`: an exposure to each test that changes it, once per screen. */
405
+ exposeText(id: string): void;
406
+ /**
407
+ * Shows the cancel flow built in MRRocket › Churn (drawn by `<MRRocketProvider>`), resolves with what the customer
408
+ * did. MRRocket never changes the subscription: your `onCancel` and `onSave` do.
409
+ */
410
+ openCancelFlow(options?: CancelFlowOptions): Promise<CancelFlowResult>;
411
+ /** Starts an onboarding flow now (the first live one without an id). False when there is none. */
412
+ startOnboarding(id?: string): boolean;
413
+ /**
414
+ * Push notifications from your Lifecycle journeys for the signed-in person (call `identify` first). Asks the
415
+ * permission (unless `ask: false`), gets the Expo push token and sends it to MRRocket. Needs `expo-notifications`.
416
+ * Safe to call on every start: an existing token is sent again without asking. Opens of MRRocket's notifications
417
+ * are counted from then on.
418
+ */
419
+ registerForPush(options?: RegisterForPushOptions): Promise<NativePushResult>;
420
+ /** Removes this device's token for the signed-in person (sign-out does it on its own in `reset`). */
421
+ unregisterForPush(): Promise<boolean>;
422
+ /** The cancel flow, onboarding and messages for a renderer (the provider's, or your own). */
423
+ flows: FlowsController;
175
424
  /** Sends what is queued now. */
176
425
  flush(): Promise<void>;
177
426
  /** Resolves once stored state and the config (cached or fetched) are known. */
@@ -192,6 +441,15 @@ interface MRRocketClient {
192
441
  /** The name of the screen a React Navigation state shows: the focused route, followed into nested navigators. */
193
442
  declare const activeRouteName: (state: unknown) => string | undefined;
194
443
 
444
+ interface NativeAppearance {
445
+ theme?: "light" | "dark" | "auto";
446
+ accent?: string;
447
+ }
448
+ declare const MRRocketFlows: ({ flows, appearance }: {
449
+ flows: FlowsController;
450
+ appearance?: NativeAppearance;
451
+ }) => react.FunctionComponentElement<Record<string, unknown>> | null;
452
+
195
453
  /**
196
454
  * The SDK's client outside React (a background task, a store, an API layer): the one the provider made, or a new one.
197
455
  * One client per app; a second call returns the first.
@@ -208,6 +466,13 @@ interface MRRocketProviderProps extends Partial<MRRocketOptions> {
208
466
  storage?: KeyValueStorage | null;
209
467
  /** A client you made with createMRRocket; the other props are then ignored. */
210
468
  client?: MRRocketClient;
469
+ /**
470
+ * Draw the cancel flow, onboarding and in-app messages (default true). False: draw `client.flows` yourself, or
471
+ * place `<MRRocketFlows flows={client.flows} />` where you want it.
472
+ */
473
+ flows?: boolean;
474
+ /** The look of those dialogs: `{ theme: "light" | "dark" | "auto", accent: "#0f766e" }`. */
475
+ appearance?: NativeAppearance;
211
476
  children?: ReactNode;
212
477
  }
213
478
  /**
@@ -215,7 +480,7 @@ interface MRRocketProviderProps extends Partial<MRRocketOptions> {
215
480
  * starts the SDK: lifecycle events, the event queue, flags and experiments, error capture. Changing `consent` later
216
481
  * calls setConsent.
217
482
  */
218
- declare const MRRocketProvider: ({ client: given, children, consent, ...options }: MRRocketProviderProps) => react.FunctionComponentElement<react.ProviderProps<MRRocketClient | null>>;
483
+ declare const MRRocketProvider: ({ client: given, children, consent, flows, appearance, ...options }: MRRocketProviderProps) => react.FunctionComponentElement<react.ProviderProps<MRRocketClient | null>>;
219
484
  /**
220
485
  * `track`, `screen`, `identify`, `reset`, `setConsent`, `captureError`, `flush`, the visitor id, the consent and whether
221
486
  * flags and experiments have loaded.
@@ -232,6 +497,33 @@ declare const useMRRocket: () => {
232
497
  consent: NativeConsent;
233
498
  ready: boolean;
234
499
  };
500
+ /**
501
+ * The cancel flow built in MRRocket › Churn › Cancel flow. Call `open` from your Cancel button; the provider draws it.
502
+ * MRRocket never changes the subscription: your `onCancel` and `onSave` do (RevenueCat, your server, the store's
503
+ * subscription page).
504
+ *
505
+ * const cancelFlow = useCancelFlow();
506
+ * <Button title="Cancel subscription" onPress={() => cancelFlow.open({ customer: "consumer", onCancel: openStoreSubscriptions })} />
507
+ */
508
+ declare const useCancelFlow: () => {
509
+ open: (options?: CancelFlowOptions) => Promise<CancelFlowResult>;
510
+ };
511
+ /**
512
+ * Push notifications from your Lifecycle journeys (needs `expo-notifications`). After sign-in (`identify`), call
513
+ * `register()`: it asks the permission, gets the Expo push token and sends it to MRRocket. Sign-out (`reset`) takes the
514
+ * token back on its own.
515
+ *
516
+ * const push = usePush();
517
+ * useEffect(() => { if (user) void push.register(); }, [user]);
518
+ */
519
+ declare const usePush: () => {
520
+ register: MRRocketClient["registerForPush"];
521
+ unregister: MRRocketClient["unregisterForPush"];
522
+ };
523
+ /** Starts an onboarding flow now (the first live one without an id), whatever its trigger. */
524
+ declare const useOnboarding: () => {
525
+ start: (id?: string) => boolean;
526
+ };
235
527
  /** A feature flag from MRRocket › Build › Feature flags: true while it is on for this person; false while off, unknown or loading. */
236
528
  declare const useFlag: (key: string) => boolean;
237
529
  /** The variant of a multivariate flag ("control", "test" …), or null while it is off. */
@@ -241,6 +533,16 @@ declare const useFlagVariant: (key: string) => string | null;
241
533
  * cold start, so the first screen is already stable). Records the exposure once per screen.
242
534
  */
243
535
  declare const useVariant: (key: string) => VariantId;
536
+ /**
537
+ * Copy MRRocket can change without an app update: the text published for `id` (a paywall's
538
+ * `paywall.<placement>.headline`, `.body`, `.cta`), with this person's variant of a running text test on top,
539
+ * the same rules as `<Text id>` on the web. Write your own words as `fallback`: they show while nothing is published
540
+ * and on a first start before the config arrives. Records the exposure to every test that changes `id`, once per screen.
541
+ *
542
+ * const headline = useText("paywall.onboarding.headline", "Unlock everything");
543
+ * <Text style={styles.title}>{headline}</Text>
544
+ */
545
+ declare const useText: (id: string, fallback: string) => string;
244
546
  /**
245
547
  * Screen views for Expo Router: one `$screen` per path ("/settings", "/product/[id]" as its real path). Call it in a
246
548
  * component inside both the router and MRRocketProvider, for example the root layout's child.
@@ -256,4 +558,4 @@ declare const useExpoRouterTracking: () => void;
256
558
  */
257
559
  declare const useNavigationTracking: () => (state: unknown) => void;
258
560
 
259
- export { type AppContext, type CockpitConfig, type FlagConfig, type KeyValueStorage, type MRRocketClient, type MRRocketOptions, MRRocketProvider, type MRRocketProviderProps, type NativeConsent, SDK_VERSION, type VariantId, activeRouteName, createMRRocket, getMRRocket, useExpoRouterTracking, useFlag, useFlagVariant, useMRRocket, useNavigationTracking, useVariant };
561
+ export { type AppContext, type CancelFlowConfig, type CancelFlowOptions, type CancelFlowResult, type CancelOffer, type CockpitConfig, type FlagConfig, type FlowView, type InAppMessage, type KeyValueStorage, type MRRocketClient, MRRocketFlows, type MRRocketOptions, MRRocketProvider, type MRRocketProviderProps, type NativeAppearance, type NativeConsent, type NativePushResult, type NativePushStatus, type OnboardingFlow, type OnboardingStep, type RegisterForPushOptions, SDK_VERSION, type VariantId, activeRouteName, createMRRocket, getMRRocket, useCancelFlow, useExpoRouterTracking, useFlag, useFlagVariant, useMRRocket, useNavigationTracking, useOnboarding, usePush, useText, useVariant };