@shotik/react-native 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.2
2
+
3
+ - Purchase-sheet outcomes: ShotikAnalytics.sheet({ product, outcome, priceString, currency, code, storefront }) records shown, confirmed, cancelled, failed, pending and not_shown on the purchase event (new statuses shown, pending, not_shown; props ps, code, sf). trackRevenueCat emits them automatically, with the store price string, the error code and the storefront (Purchases.getStorefront when present).
4
+
1
5
  ## 0.2.0
2
6
 
3
7
  Analytics
@@ -45,6 +45,8 @@ export type Entitled = {
45
45
  first?: number;
46
46
  trial?: boolean;
47
47
  };
48
+ export type SheetOutcome = 'shown' | 'confirmed' | 'cancelled' | 'failed' | 'pending' | 'not_shown';
49
+ export declare const SHEET_STATUS: Record<SheetOutcome, 'shown' | 'done' | 'cancel' | 'fail' | 'pending' | 'not_shown'>;
48
50
  export type Gesture = {
49
51
  k: 'long' | 'swipe' | 'pan' | 'pinch';
50
52
  dir?: 'left' | 'right' | 'up' | 'down' | 'in' | 'out';
@@ -67,7 +69,7 @@ export type Scrolled = {
67
69
  ts: number;
68
70
  screen?: string;
69
71
  };
70
- export declare const SDK_VERSION = "0.2.0";
72
+ export declare const SDK_VERSION = "0.2.2";
71
73
  export declare function netTarget(url: string): {
72
74
  host: string;
73
75
  path: string;
@@ -155,7 +157,7 @@ export declare class Analytics {
155
157
  error(e: unknown, fatal?: boolean): void;
156
158
  purchase(p: {
157
159
  product: string;
158
- status: 'start' | 'done' | 'cancel' | 'fail';
160
+ status: 'start' | 'done' | 'cancel' | 'fail' | 'shown' | 'pending' | 'not_shown';
159
161
  price?: number;
160
162
  currency?: string;
161
163
  store?: string;
@@ -163,6 +165,18 @@ export declare class Analytics {
163
165
  renew?: boolean;
164
166
  conv?: boolean;
165
167
  at?: number;
168
+ ps?: string;
169
+ code?: string;
170
+ sf?: string;
171
+ }): void;
172
+ sheet(p: {
173
+ product: string;
174
+ outcome: SheetOutcome;
175
+ priceString?: string;
176
+ currency?: string;
177
+ code?: string;
178
+ storefront?: string;
179
+ store?: string;
166
180
  }): void;
167
181
  entitled(list: Entitled[], quiet?: boolean, store?: string): void;
168
182
  wantsSnapshot(screen: string): boolean;
@@ -3,6 +3,7 @@
3
3
  import { cleanProps, redact } from './privacy';
4
4
  import { Replay } from './replay';
5
5
  import { pack } from './wire';
6
+ export const SHEET_STATUS = { shown: 'shown', confirmed: 'done', cancelled: 'cancel', failed: 'fail', pending: 'pending', not_shown: 'not_shown' };
6
7
  const K = { anon: 'shotik.support.anon', state: 'shotik.analytics.state', queue: 'shotik.analytics.queue', consent: 'shotik.analytics.consent', purchases: 'shotik.analytics.purchases', replay: 'shotik.analytics.replay' };
7
8
  const MAX_QUEUE = 2000;
8
9
  const BATCH = 300;
@@ -22,7 +23,7 @@ const p95 = (a) => {
22
23
  return Math.round(s[Math.ceil(s.length * 0.95) - 1] * 1000) / 1000;
23
24
  };
24
25
  // Sent as `sdk: 'rn/<version>'` in the device context. Keep equal to package.json (a unit test checks it).
25
- export const SDK_VERSION = '0.2.0';
26
+ export const SDK_VERSION = '0.2.2';
26
27
  // A request URL as host + path template: no scheme, credentials, query or fragment; ids, UUIDs, hex, digit runs, tokens
27
28
  // of 8+ characters with 2+ digits, emails, anything redact() changes and percent-encoded or non-ASCII segments become :id
28
29
  // (a file extension stays). A plain-word segment (a username) can't be detected and stays. Plain string work, because React Native's URL polyfill lacks host/pathname.
@@ -425,7 +426,14 @@ export class Analytics {
425
426
  }
426
427
  purchase(p) {
427
428
  this.touch();
428
- this.push('purchase', p.product, { product: p.product, status: p.status, price: p.price, currency: p.currency, store: p.store, trial: p.trial ? 1 : undefined, renew: p.renew ? 1 : undefined, conv: p.conv ? 1 : undefined, at: p.at });
429
+ this.push('purchase', p.product, { product: p.product, status: p.status, price: p.price, currency: p.currency, store: p.store, trial: p.trial ? 1 : undefined, renew: p.renew ? 1 : undefined, conv: p.conv ? 1 : undefined, at: p.at, ps: p.ps?.slice(0, 32), code: p.code?.slice(0, 40), sf: p.sf?.slice(0, 3) });
430
+ }
431
+ // What happened at the store's purchase sheet for one buy tap; outcomes map onto the purchase statuses.
432
+ sheet(p) {
433
+ const status = SHEET_STATUS[p.outcome];
434
+ if (!status)
435
+ return;
436
+ this.purchase({ product: p.product, status, currency: p.currency, store: p.store, ps: p.priceString, code: p.code, sf: p.storefront });
429
437
  }
430
438
  // Store truth: each entitlement's latest purchase against a per-product watermark. A newer one is a done (renew unless it
431
439
  // is the subscription's first purchase, conv on the first paid period after a trial this install saw). Purchases from
@@ -120,6 +120,7 @@ export declare const ShotikAnalytics: {
120
120
  screen: typeof screen;
121
121
  identify: (userId: string | null, traits?: Record<string, unknown>) => void | undefined;
122
122
  purchase: (p: Parameters<Analytics["purchase"]>[0]) => void | undefined;
123
+ sheet: (p: Parameters<Analytics["sheet"]>[0]) => void | undefined;
123
124
  setConsent: (c: Consent) => void | undefined;
124
125
  optOut: () => void | undefined;
125
126
  optIn: () => void | undefined;
@@ -799,6 +799,18 @@ const startedTrial = (r, product) => {
799
799
  const pid = base(res?.productIdentifier ?? product);
800
800
  return entitlementsOf(res?.customerInfo).some((e) => e.trial && e.product === pid);
801
801
  };
802
+ let storefront;
803
+ const storefrontOf = async (P) => {
804
+ if (storefront || typeof P.getStorefront !== 'function')
805
+ return storefront;
806
+ try {
807
+ const s = (await P.getStorefront());
808
+ return (storefront = typeof s?.countryCode === 'string' ? s.countryCode : undefined);
809
+ }
810
+ catch {
811
+ return undefined;
812
+ }
813
+ };
802
814
  const buying = new Set();
803
815
  let restoring = 0;
804
816
  function trackRevenueCat(P) {
@@ -809,9 +821,10 @@ function trackRevenueCat(P) {
809
821
  const w = async (...a) => {
810
822
  const it = a[0];
811
823
  const prod = (typeof it === 'object' && (it.product ?? it)) || {};
812
- const info = { product: String(prod.identifier ?? it ?? 'unknown'), price: prod.price, currency: prod.currencyCode, store: 'revenuecat' };
824
+ const info = { product: String(prod.identifier ?? it ?? 'unknown'), price: prod.price, currency: prod.currencyCode, store: 'revenuecat', ps: prod.priceString, sf: await storefrontOf(P) };
813
825
  const pid = base(info.product);
814
826
  client?.purchase({ ...info, status: 'start' });
827
+ client?.purchase({ ...info, status: 'shown' });
815
828
  buying.add(pid);
816
829
  try {
817
830
  const r = await orig.apply(P, a);
@@ -821,7 +834,10 @@ function trackRevenueCat(P) {
821
834
  return r;
822
835
  }
823
836
  catch (e) {
824
- client?.purchase({ ...info, status: e?.userCancelled ? 'cancel' : 'fail' });
837
+ const err = e;
838
+ const code = String(err?.readableErrorCode ?? err?.code ?? 'unknown');
839
+ const pending = code === 'PAYMENT_PENDING_ERROR' || code === '20';
840
+ client?.purchase({ ...info, status: err?.userCancelled ? 'cancel' : pending ? 'pending' : 'fail', code: err?.userCancelled || pending ? undefined : code });
825
841
  throw e;
826
842
  }
827
843
  finally {
@@ -1000,6 +1016,7 @@ export const ShotikAnalytics = {
1000
1016
  screen,
1001
1017
  identify: (userId, traits) => client?.identify(userId, traits),
1002
1018
  purchase: (p) => client?.purchase(p),
1019
+ sheet: (p) => client?.sheet(p),
1003
1020
  setConsent: (c) => client?.setConsent(c),
1004
1021
  optOut: () => client?.optOut(),
1005
1022
  optIn: () => client?.optIn(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shotik/react-native",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Shotik SDK for React Native and Expo: in-app support chat answered from Shotik, and product analytics.",
5
5
  "keywords": [
6
6
  "react-native",
@@ -62,6 +62,9 @@ interface Pending {
62
62
  h: Held
63
63
  }
64
64
 
65
+ export type SheetOutcome = 'shown' | 'confirmed' | 'cancelled' | 'failed' | 'pending' | 'not_shown'
66
+ export const SHEET_STATUS: Record<SheetOutcome, 'shown' | 'done' | 'cancel' | 'fail' | 'pending' | 'not_shown'> = { shown: 'shown', confirmed: 'done', cancelled: 'cancel', failed: 'fail', pending: 'pending', not_shown: 'not_shown' }
67
+
65
68
  const K = { anon: 'shotik.support.anon', state: 'shotik.analytics.state', queue: 'shotik.analytics.queue', consent: 'shotik.analytics.consent', purchases: 'shotik.analytics.purchases', replay: 'shotik.analytics.replay' }
66
69
  const MAX_QUEUE = 2000
67
70
  const BATCH = 300
@@ -85,7 +88,7 @@ const p95 = (a: number[]) => {
85
88
  }
86
89
 
87
90
  // Sent as `sdk: 'rn/<version>'` in the device context. Keep equal to package.json (a unit test checks it).
88
- export const SDK_VERSION = '0.2.0'
91
+ export const SDK_VERSION = '0.2.2'
89
92
 
90
93
  // A request URL as host + path template: no scheme, credentials, query or fragment; ids, UUIDs, hex, digit runs, tokens
91
94
  // of 8+ characters with 2+ digits, emails, anything redact() changes and percent-encoded or non-ASCII segments become :id
@@ -483,9 +486,16 @@ export class Analytics {
483
486
  if (out.length) this.st?.set(K.replay, JSON.stringify({ a: this.anon, why: 'crash', chunks: out.map((b) => b.c) })).catch(() => {})
484
487
  }
485
488
 
486
- purchase(p: { product: string; status: 'start' | 'done' | 'cancel' | 'fail'; price?: number; currency?: string; store?: string; trial?: boolean; renew?: boolean; conv?: boolean; at?: number }): void {
489
+ purchase(p: { product: string; status: 'start' | 'done' | 'cancel' | 'fail' | 'shown' | 'pending' | 'not_shown'; price?: number; currency?: string; store?: string; trial?: boolean; renew?: boolean; conv?: boolean; at?: number; ps?: string; code?: string; sf?: string }): void {
487
490
  this.touch()
488
- this.push('purchase', p.product, { product: p.product, status: p.status, price: p.price, currency: p.currency, store: p.store, trial: p.trial ? 1 : undefined, renew: p.renew ? 1 : undefined, conv: p.conv ? 1 : undefined, at: p.at })
491
+ this.push('purchase', p.product, { product: p.product, status: p.status, price: p.price, currency: p.currency, store: p.store, trial: p.trial ? 1 : undefined, renew: p.renew ? 1 : undefined, conv: p.conv ? 1 : undefined, at: p.at, ps: p.ps?.slice(0, 32), code: p.code?.slice(0, 40), sf: p.sf?.slice(0, 3) })
492
+ }
493
+
494
+ // What happened at the store's purchase sheet for one buy tap; outcomes map onto the purchase statuses.
495
+ sheet(p: { product: string; outcome: SheetOutcome; priceString?: string; currency?: string; code?: string; storefront?: string; store?: string }): void {
496
+ const status = SHEET_STATUS[p.outcome]
497
+ if (!status) return
498
+ this.purchase({ product: p.product, status, currency: p.currency, store: p.store, ps: p.priceString, code: p.code, sf: p.storefront })
489
499
  }
490
500
 
491
501
  // Store truth: each entitlement's latest purchase against a per-product watermark. A newer one is a done (renew unless it
@@ -769,6 +769,16 @@ const startedTrial = (r: unknown, product: string): boolean => {
769
769
  const pid = base(res?.productIdentifier ?? product)
770
770
  return entitlementsOf(res?.customerInfo).some((e) => e.trial && e.product === pid)
771
771
  }
772
+ let storefront: string | undefined
773
+ const storefrontOf = async (P: Record<string, unknown>): Promise<string | undefined> => {
774
+ if (storefront || typeof P.getStorefront !== 'function') return storefront
775
+ try {
776
+ const s = (await (P.getStorefront as Fn)()) as { countryCode?: string } | null | undefined
777
+ return (storefront = typeof s?.countryCode === 'string' ? s.countryCode : undefined)
778
+ } catch {
779
+ return undefined
780
+ }
781
+ }
772
782
  const buying = new Set<string>()
773
783
  let restoring = 0
774
784
  function trackRevenueCat(P: Record<string, unknown>): void {
@@ -778,9 +788,10 @@ function trackRevenueCat(P: Record<string, unknown>): void {
778
788
  const w: Fn & { __shotik?: 1 } = async (...a) => {
779
789
  const it = a[0] as { product?: Record<string, unknown>; identifier?: string; price?: number; currencyCode?: string } | string
780
790
  const prod = (typeof it === 'object' && (it.product ?? it)) || {}
781
- const info = { product: String((prod as { identifier?: string }).identifier ?? it ?? 'unknown'), price: (prod as { price?: number }).price, currency: (prod as { currencyCode?: string }).currencyCode, store: 'revenuecat' }
791
+ const info = { product: String((prod as { identifier?: string }).identifier ?? it ?? 'unknown'), price: (prod as { price?: number }).price, currency: (prod as { currencyCode?: string }).currencyCode, store: 'revenuecat', ps: (prod as { priceString?: string }).priceString, sf: await storefrontOf(P) }
782
792
  const pid = base(info.product)
783
793
  client?.purchase({ ...info, status: 'start' })
794
+ client?.purchase({ ...info, status: 'shown' })
784
795
  buying.add(pid)
785
796
  try {
786
797
  const r = await orig.apply(P, a)
@@ -789,7 +800,10 @@ function trackRevenueCat(P: Record<string, unknown>): void {
789
800
  client?.entitled(mine, true)
790
801
  return r
791
802
  } catch (e) {
792
- client?.purchase({ ...info, status: (e as { userCancelled?: boolean })?.userCancelled ? 'cancel' : 'fail' })
803
+ const err = e as { userCancelled?: boolean; code?: unknown; readableErrorCode?: string } | null
804
+ const code = String(err?.readableErrorCode ?? err?.code ?? 'unknown')
805
+ const pending = code === 'PAYMENT_PENDING_ERROR' || code === '20'
806
+ client?.purchase({ ...info, status: err?.userCancelled ? 'cancel' : pending ? 'pending' : 'fail', code: err?.userCancelled || pending ? undefined : code })
793
807
  throw e
794
808
  } finally {
795
809
  buying.delete(pid)
@@ -962,6 +976,7 @@ export const ShotikAnalytics = {
962
976
  screen,
963
977
  identify: (userId: string | null, traits?: Record<string, unknown>) => client?.identify(userId, traits),
964
978
  purchase: (p: Parameters<Analytics['purchase']>[0]) => client?.purchase(p),
979
+ sheet: (p: Parameters<Analytics['sheet']>[0]) => client?.sheet(p),
965
980
  setConsent: (c: Consent) => client?.setConsent(c),
966
981
  optOut: () => client?.optOut(),
967
982
  optIn: () => client?.optIn(),
@@ -36,7 +36,9 @@ export interface DeviceContext {
36
36
  // tap { el, x, y, nx, ny (0..1000), id, label, dead: 1, input: 1, resp (ms to the first response) } rage { el, id, label, n, x, y }
37
37
  // id is the stable element key (testID, nativeID or a structural hash); el and label are for display; x, y, nx, ny absent when masked
38
38
  // error { msg, fatal: 1, stack (top frames, no values) } slow { ms, kind: 'ready' | 'stall' }
39
- // purchase { product, status: start | done | cancel | fail, price, currency, store, trial: 1, renew: 1, conv: 1, at (store purchase ms) }
39
+ // purchase { product, status: start | shown | done | cancel | pending | fail | not_shown, price, currency, store, trial: 1, renew: 1, conv: 1, at (store purchase ms),
40
+ // ps (the store's localized price string as shown, <= 32), code (error code on fail / not_shown, <= 40), sf (storefront country, ISO 3166 alpha-2 or alpha-3) }
41
+ // shown = the store sheet was presented; done = confirmed; cancel = the user cancelled; pending = Ask to Buy / SCA; not_shown = the product didn't load, so no sheet
40
42
  // custom free-form (no PII by contract) identify { traits }
41
43
  // net { m (method), host, path (template, no query), s (status, 0 = network error), ms }: failures and slow calls only
42
44
  // field { id, el, ms (focused), edits (<= 20, none on secure inputs), filled: 1 }: one per focus span, never a value or length