@shotik/react-native 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +193 -2
- package/README.ru.md +192 -0
- package/dist/analytics/core.d.ts +104 -0
- package/dist/analytics/core.js +345 -0
- package/dist/analytics/index.d.ts +5 -0
- package/dist/analytics/index.js +3 -0
- package/dist/analytics/rn.d.ts +84 -0
- package/dist/analytics/rn.js +413 -0
- package/dist/analytics/wire.d.ts +53 -0
- package/dist/analytics/wire.js +56 -0
- package/dist/client.d.ts +153 -0
- package/dist/client.js +595 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +6 -0
- package/dist/native.d.ts +3 -0
- package/dist/native.js +77 -0
- package/dist/parts.d.ts +103 -0
- package/dist/parts.js +157 -0
- package/dist/react.d.ts +51 -0
- package/dist/react.js +88 -0
- package/dist/sheet.d.ts +12 -0
- package/dist/sheet.js +273 -0
- package/dist/support.d.ts +12 -0
- package/dist/support.js +60 -0
- package/dist/theme.d.ts +72 -0
- package/dist/theme.js +95 -0
- package/guide.json +136 -0
- package/package.json +114 -4
- package/recipe.json +263 -0
- package/src/analytics/README.md +32 -0
- package/src/analytics/core.ts +380 -0
- package/src/analytics/index.ts +7 -0
- package/src/analytics/recipe.json +263 -0
- package/src/analytics/rn.tsx +425 -0
- package/src/analytics/wire.ts +123 -0
- package/src/client.ts +685 -0
- package/src/index.ts +10 -0
- package/src/native.ts +90 -0
- package/src/parts.tsx +243 -0
- package/src/react.tsx +140 -0
- package/src/sheet.tsx +538 -0
- package/src/support.ts +55 -0
- package/src/theme.ts +141 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
// Shotik analytics core: sessions, screens and time on screen, taps with rage detection, scroll depth, stalls, the
|
|
2
|
+
// offline queue and batched upload. Platform-free (no React Native imports), so it runs and is tested under plain Node.
|
|
3
|
+
|
|
4
|
+
import { pack } from './wire'
|
|
5
|
+
import type { DeviceContext, EventType, WireEvent, Wireframe } from './wire'
|
|
6
|
+
|
|
7
|
+
export interface Storage {
|
|
8
|
+
get(key: string): Promise<string | null>
|
|
9
|
+
set(key: string, value: string): Promise<void>
|
|
10
|
+
remove(key: string): Promise<void>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Consent = 'granted' | 'denied' | 'pending'
|
|
14
|
+
|
|
15
|
+
export interface AnalyticsConfig {
|
|
16
|
+
appKey: string
|
|
17
|
+
serverUrl: string
|
|
18
|
+
storage?: Storage
|
|
19
|
+
device?: () => DeviceContext
|
|
20
|
+
// pending: events are kept on the device and sent only after setConsent('granted'). Default granted.
|
|
21
|
+
consent?: Consent
|
|
22
|
+
// Share of sessions that record wireframes (0..1). Default 0.2.
|
|
23
|
+
snapshots?: number
|
|
24
|
+
flushMs?: number
|
|
25
|
+
sessionTimeoutMs?: number
|
|
26
|
+
debug?: boolean
|
|
27
|
+
fetch?: typeof fetch
|
|
28
|
+
now?: () => number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Pending {
|
|
32
|
+
el: string
|
|
33
|
+
id?: string
|
|
34
|
+
label?: string
|
|
35
|
+
x: number
|
|
36
|
+
y: number
|
|
37
|
+
t: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const K = { anon: 'shotik.support.anon', state: 'shotik.analytics.state', queue: 'shotik.analytics.queue', consent: 'shotik.analytics.consent' }
|
|
41
|
+
const MAX_QUEUE = 2000
|
|
42
|
+
const BATCH = 300
|
|
43
|
+
const RAGE_N = 3
|
|
44
|
+
const RAGE_MS = 800
|
|
45
|
+
const RAGE_PT = 40
|
|
46
|
+
|
|
47
|
+
const rnd = () => Math.random().toString(36).slice(2, 10)
|
|
48
|
+
const uid = () => Date.now().toString(36) + rnd() + rnd()
|
|
49
|
+
|
|
50
|
+
export class Analytics {
|
|
51
|
+
readonly cfg: AnalyticsConfig
|
|
52
|
+
private st: Storage | null
|
|
53
|
+
private now: () => number
|
|
54
|
+
private queue: WireEvent[] = []
|
|
55
|
+
private snaps: Wireframe[] = []
|
|
56
|
+
private anon = ''
|
|
57
|
+
private userId: string | null = null
|
|
58
|
+
private firstSeen = 0
|
|
59
|
+
private sessionId = ''
|
|
60
|
+
private lastActive = 0
|
|
61
|
+
private seq = 0
|
|
62
|
+
private sampled = false
|
|
63
|
+
private consent: Consent
|
|
64
|
+
private screen: string | undefined
|
|
65
|
+
private screenAt = 0
|
|
66
|
+
private screenStats = { depth: 0, drag: 0, stalls: 0, maxStall: 0 }
|
|
67
|
+
private taps: Pending[] = []
|
|
68
|
+
private snapped = new Set<string>()
|
|
69
|
+
private timer: ReturnType<typeof setTimeout> | null = null
|
|
70
|
+
private saveTimer: ReturnType<typeof setTimeout> | null = null
|
|
71
|
+
private sending = false
|
|
72
|
+
private inflight: Promise<void> | null = null
|
|
73
|
+
private backoff = 0
|
|
74
|
+
private ready: Promise<void>
|
|
75
|
+
private active = true
|
|
76
|
+
private loaded = false
|
|
77
|
+
private fresh = true
|
|
78
|
+
private storeIssue: 'none' | 'error' | undefined
|
|
79
|
+
|
|
80
|
+
constructor(cfg: AnalyticsConfig) {
|
|
81
|
+
this.cfg = cfg
|
|
82
|
+
this.st = cfg.storage ?? null
|
|
83
|
+
this.now = cfg.now ?? Date.now
|
|
84
|
+
this.consent = cfg.consent ?? 'granted'
|
|
85
|
+
this.ready = this.load()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private async load(): Promise<void> {
|
|
89
|
+
const st = this.st
|
|
90
|
+
if (st) {
|
|
91
|
+
try {
|
|
92
|
+
const [anon, raw, q, c] = await Promise.all([st.get(K.anon), st.get(K.state), st.get(K.queue), st.get(K.consent)])
|
|
93
|
+
this.anon = anon ?? ''
|
|
94
|
+
const s = raw ? JSON.parse(raw) : null
|
|
95
|
+
if (s) {
|
|
96
|
+
this.fresh = false
|
|
97
|
+
this.firstSeen = s.f ?? 0
|
|
98
|
+
this.sessionId = s.s ?? ''
|
|
99
|
+
this.lastActive = s.l ?? 0
|
|
100
|
+
this.seq = s.q ?? 0
|
|
101
|
+
this.sampled = !!s.p
|
|
102
|
+
this.userId = this.userId ?? s.u ?? null
|
|
103
|
+
}
|
|
104
|
+
if (q) this.queue = (JSON.parse(q) as WireEvent[]).concat(this.queue).slice(-MAX_QUEUE)
|
|
105
|
+
if (c === 'granted' || c === 'denied') this.consent = c
|
|
106
|
+
} catch {
|
|
107
|
+
this.storeIssue = 'error'
|
|
108
|
+
}
|
|
109
|
+
} else this.storeIssue = 'none'
|
|
110
|
+
if (!this.anon) {
|
|
111
|
+
this.anon = 'anon_' + uid()
|
|
112
|
+
st?.set(K.anon, this.anon).catch(() => {})
|
|
113
|
+
}
|
|
114
|
+
if (!this.firstSeen) this.firstSeen = this.now()
|
|
115
|
+
this.loaded = true
|
|
116
|
+
const pre = this.queue.filter((e) => !e.sessionId)
|
|
117
|
+
const n = this.queue.length
|
|
118
|
+
this.touch(pre[0]?.ts)
|
|
119
|
+
const at = pre.length && this.queue.length > n ? this.queue.indexOf(pre[0]) : -1
|
|
120
|
+
if (at >= 0) {
|
|
121
|
+
const [s] = this.queue.splice(this.queue.length - 1, 1)
|
|
122
|
+
this.queue.splice(at, 0, { ...s, screen: pre[0].screen })
|
|
123
|
+
}
|
|
124
|
+
for (const ev of pre) Object.assign(ev, { sessionId: this.sessionId, seq: ++this.seq })
|
|
125
|
+
if (pre.length) this.lastActive = Math.max(this.lastActive, pre[pre.length - 1].ts)
|
|
126
|
+
this.schedule()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Starts a new session after sessionTimeoutMs (30 min) of inactivity. at: when the activity happened (events queued before storage loaded).
|
|
130
|
+
private touch(at?: number): void {
|
|
131
|
+
const t = at ?? this.now()
|
|
132
|
+
if (!this.loaded) return
|
|
133
|
+
if (!this.sessionId || t - this.lastActive > (this.cfg.sessionTimeoutMs ?? 30 * 60_000)) {
|
|
134
|
+
const first = this.fresh
|
|
135
|
+
this.fresh = false
|
|
136
|
+
this.sessionId = 's_' + uid()
|
|
137
|
+
this.seq = 0
|
|
138
|
+
this.sampled = Math.random() < (this.cfg.snapshots ?? 0.2)
|
|
139
|
+
this.snapped.clear()
|
|
140
|
+
this.lastActive = t
|
|
141
|
+
const props = { ...(first && { first: 1 }), ...(this.storeIssue && { store: this.storeIssue }) }
|
|
142
|
+
this.push('session', undefined, Object.keys(props).length ? props : undefined, t)
|
|
143
|
+
}
|
|
144
|
+
this.lastActive = t
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private push(type: EventType, name?: string, props?: Record<string, unknown>, ts?: number): void {
|
|
148
|
+
if (this.consent === 'denied') return
|
|
149
|
+
this.queue.push({ seq: ++this.seq, ts: ts ?? this.now(), type, sessionId: this.sessionId, screen: this.screen, name, props })
|
|
150
|
+
if (this.queue.length > MAX_QUEUE) this.queue.splice(0, this.queue.length - MAX_QUEUE)
|
|
151
|
+
if (this.cfg.debug) console.log('[shotik]', type, name ?? '', this.screen ?? '', props ?? '')
|
|
152
|
+
this.save()
|
|
153
|
+
if (this.queue.length >= BATCH) this.flush()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private save(): void {
|
|
157
|
+
if (!this.st || this.saveTimer) return
|
|
158
|
+
this.saveTimer = setTimeout(() => {
|
|
159
|
+
this.saveTimer = null
|
|
160
|
+
this.persist()
|
|
161
|
+
}, 1000)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private persist(): void {
|
|
165
|
+
const st = this.st
|
|
166
|
+
if (!st) return
|
|
167
|
+
st.set(K.state, JSON.stringify({ f: this.firstSeen, s: this.sessionId, l: this.lastActive, q: this.seq, p: this.sampled ? 1 : 0, u: this.userId })).catch(() => {})
|
|
168
|
+
st.set(K.queue, JSON.stringify(this.queue)).catch(() => {})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private schedule(): void {
|
|
172
|
+
if (this.timer) clearTimeout(this.timer)
|
|
173
|
+
this.timer = setTimeout(() => {
|
|
174
|
+
this.timer = null
|
|
175
|
+
this.flush().finally(() => this.active && this.schedule())
|
|
176
|
+
}, (this.cfg.flushMs ?? 10_000) * Math.pow(2, this.backoff))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Public API
|
|
180
|
+
|
|
181
|
+
track(name: string, props?: Record<string, unknown>): void {
|
|
182
|
+
this.touch()
|
|
183
|
+
this.push('custom', name, props)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
identify(userId: string | null, traits?: Record<string, unknown>): void {
|
|
187
|
+
this.userId = userId
|
|
188
|
+
this.push('identify', undefined, traits ? { traits } : undefined)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Screen change: closes the previous screen with its time and scroll depth.
|
|
192
|
+
screenView(name: string): void {
|
|
193
|
+
if (name === this.screen) return
|
|
194
|
+
this.touch()
|
|
195
|
+
const from = this.screen
|
|
196
|
+
this.leave()
|
|
197
|
+
this.screen = name
|
|
198
|
+
this.screenAt = this.now()
|
|
199
|
+
this.push('screen', name, from ? { from } : undefined)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private leave(): void {
|
|
203
|
+
if (!this.screen) return
|
|
204
|
+
const s = this.screenStats
|
|
205
|
+
const p: Record<string, unknown> = { dur: this.now() - this.screenAt }
|
|
206
|
+
if (s.depth) p.depth = Math.round(s.depth * 100) / 100
|
|
207
|
+
if (s.drag) p.drag = Math.round(s.drag)
|
|
208
|
+
if (s.stalls) {
|
|
209
|
+
p.stalls = s.stalls
|
|
210
|
+
p.maxStall = s.maxStall
|
|
211
|
+
}
|
|
212
|
+
this.push('leave', this.screen, p)
|
|
213
|
+
this.screenStats = { depth: 0, drag: 0, stalls: 0, maxStall: 0 }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
get currentScreen(): string | undefined {
|
|
217
|
+
return this.screen
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
screenReady(ms: number): void {
|
|
221
|
+
if (ms > 700) this.push('slow', this.screen, { ms: Math.round(ms), kind: 'ready' })
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
stall(ms: number): void {
|
|
225
|
+
const s = this.screenStats
|
|
226
|
+
s.stalls++
|
|
227
|
+
s.maxStall = Math.max(s.maxStall, Math.round(ms))
|
|
228
|
+
if (ms > 1000) this.push('slow', this.screen, { ms: Math.round(ms), kind: 'stall' })
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// A tap with its element identity. dead = nothing on the touched element handles presses.
|
|
232
|
+
tap(t: { el: string; id?: string; label?: string; x: number; y: number; nx: number; ny: number; dead?: boolean; input?: boolean; ts?: number }): void {
|
|
233
|
+
this.touch()
|
|
234
|
+
const now = t.ts ?? this.now()
|
|
235
|
+
const p: Record<string, unknown> = { el: t.el, x: Math.round(t.x), y: Math.round(t.y), nx: t.nx, ny: t.ny }
|
|
236
|
+
if (t.id) p.id = t.id
|
|
237
|
+
if (t.label) p.label = t.label
|
|
238
|
+
if (t.dead) p.dead = 1
|
|
239
|
+
if (t.input) p.input = 1
|
|
240
|
+
this.push('tap', undefined, p, now)
|
|
241
|
+
this.taps = this.taps.filter((q) => now - q.t < RAGE_MS && Math.hypot(q.x - t.x, q.y - t.y) < RAGE_PT)
|
|
242
|
+
this.taps.push({ el: t.el, id: t.id, label: t.label, x: t.x, y: t.y, t: now })
|
|
243
|
+
if (this.taps.length === RAGE_N) this.push('rage', undefined, { el: t.el, id: t.id, label: t.label, n: RAGE_N, x: Math.round(t.x), y: Math.round(t.y) }, now)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
scroll(depth: number | null, drag: number): void {
|
|
247
|
+
const s = this.screenStats
|
|
248
|
+
if (depth != null) s.depth = Math.max(s.depth, Math.min(1, depth))
|
|
249
|
+
s.drag += Math.abs(drag)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
error(e: unknown, fatal = false): void {
|
|
253
|
+
const err = e instanceof Error ? e : new Error(String(e))
|
|
254
|
+
const stack = (err.stack ?? '')
|
|
255
|
+
.split('\n')
|
|
256
|
+
.slice(1, 6)
|
|
257
|
+
.map((l) => l.trim().replace(/https?:\/\/[^\s)]+\//g, '').slice(0, 120))
|
|
258
|
+
this.push('error', err.name, { msg: err.message.slice(0, 200), fatal: fatal ? 1 : undefined, stack })
|
|
259
|
+
if (fatal) this.persist()
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
purchase(p: { product: string; status: 'start' | 'done' | 'cancel' | 'fail'; price?: number; currency?: string; store?: string; trial?: boolean }): void {
|
|
263
|
+
this.touch()
|
|
264
|
+
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 })
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Wireframes are recorded once per screen per sampled session.
|
|
268
|
+
wantsSnapshot(screen: string): boolean {
|
|
269
|
+
return this.sampled && this.consent === 'granted' && !this.snapped.has(screen)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
snapshot(w: Omit<Wireframe, 'sessionId' | 'ts'>): void {
|
|
273
|
+
if (!this.wantsSnapshot(w.screen)) return
|
|
274
|
+
this.snapped.add(w.screen)
|
|
275
|
+
this.snaps.push({ ...w, sessionId: this.sessionId, ts: this.now() })
|
|
276
|
+
this.push('snapshot', w.screen, { n: w.nodes.length })
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
foreground(): void {
|
|
280
|
+
this.touch()
|
|
281
|
+
this.push('open')
|
|
282
|
+
if (this.screen) {
|
|
283
|
+
this.screenAt = this.now()
|
|
284
|
+
this.push('screen', this.screen, { resume: 1 })
|
|
285
|
+
}
|
|
286
|
+
this.active = true
|
|
287
|
+
this.schedule()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
background(): void {
|
|
291
|
+
this.leave()
|
|
292
|
+
this.push('close')
|
|
293
|
+
this.active = false
|
|
294
|
+
this.persist()
|
|
295
|
+
this.flush()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
setConsent(c: Consent): void {
|
|
299
|
+
this.consent = c
|
|
300
|
+
this.st?.set(K.consent, c).catch(() => {})
|
|
301
|
+
if (c === 'denied') {
|
|
302
|
+
this.queue = []
|
|
303
|
+
this.snaps = []
|
|
304
|
+
this.persist()
|
|
305
|
+
} else if (c === 'granted') this.flush()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
optOut(): void {
|
|
309
|
+
this.setConsent('denied')
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
optIn(): void {
|
|
313
|
+
this.setConsent('granted')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Sign-out: a new anonymous id and session, nothing of the old user kept.
|
|
317
|
+
async reset(): Promise<void> {
|
|
318
|
+
await this.flush()
|
|
319
|
+
this.userId = null
|
|
320
|
+
this.anon = 'anon_' + uid()
|
|
321
|
+
this.sessionId = ''
|
|
322
|
+
this.firstSeen = this.now()
|
|
323
|
+
await this.st?.set(K.anon, this.anon).catch(() => {})
|
|
324
|
+
this.touch()
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
flush(): Promise<void> {
|
|
328
|
+
if (this.inflight) return this.inflight.then(() => (this.queue.length && !this.backoff ? this.flush() : undefined))
|
|
329
|
+
this.inflight = this.send().finally(() => (this.inflight = null))
|
|
330
|
+
return this.inflight
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private async send(): Promise<void> {
|
|
334
|
+
await this.ready
|
|
335
|
+
if (this.sending || this.consent !== 'granted' || (!this.queue.length && !this.snaps.length)) return
|
|
336
|
+
this.sending = true
|
|
337
|
+
const events = this.queue.slice(0, BATCH)
|
|
338
|
+
const snaps = this.snaps.splice(0, 4)
|
|
339
|
+
const f = this.cfg.fetch ?? fetch
|
|
340
|
+
const headers = { 'content-type': 'application/json', 'x-shotik-key': this.cfg.appKey }
|
|
341
|
+
try {
|
|
342
|
+
if (events.length) {
|
|
343
|
+
const body = JSON.stringify(pack({ anonymousId: this.anon, userId: this.userId, firstSeen: this.firstSeen, device: this.cfg.device?.(), events }))
|
|
344
|
+
const r = await f(this.cfg.serverUrl + '/analytics/v1/events', { method: 'POST', headers, body })
|
|
345
|
+
if (!r.ok && r.status < 500 && r.status !== 429) {
|
|
346
|
+
if (this.cfg.debug) console.warn('[shotik] dropped batch', r.status)
|
|
347
|
+
} else if (!r.ok) throw new Error(String(r.status))
|
|
348
|
+
this.queue.splice(0, events.length)
|
|
349
|
+
}
|
|
350
|
+
for (const s of snaps) await f(this.cfg.serverUrl + '/analytics/v1/snapshots', { method: 'POST', headers, body: JSON.stringify({ anonymousId: this.anon, ...s }) })
|
|
351
|
+
this.backoff = 0
|
|
352
|
+
this.persist()
|
|
353
|
+
} catch {
|
|
354
|
+
this.snaps.unshift(...snaps)
|
|
355
|
+
this.backoff = Math.min(this.backoff + 1, 5)
|
|
356
|
+
} finally {
|
|
357
|
+
this.sending = false
|
|
358
|
+
}
|
|
359
|
+
if (this.queue.length >= BATCH && !this.backoff) return this.send()
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
stop(): void {
|
|
363
|
+
this.active = false
|
|
364
|
+
if (this.timer) clearTimeout(this.timer)
|
|
365
|
+
if (this.saveTimer) clearTimeout(this.saveTimer)
|
|
366
|
+
this.persist()
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
get ids(): { anonymousId: string; sessionId: string; userId: string | null } {
|
|
370
|
+
return { anonymousId: this.anon, sessionId: this.sessionId, userId: this.userId }
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
get pending(): number {
|
|
374
|
+
return this.queue.length
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
whenReady(): Promise<void> {
|
|
378
|
+
return this.ready
|
|
379
|
+
}
|
|
380
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// @shotik/react-native/analytics: behavioral analytics for React Native and Expo, answered by Shotik insights.
|
|
2
|
+
|
|
3
|
+
export { ShotikAnalytics, ShotikAnalyticsRoot, ShotikMask, useShotikExpoRouter, useShotikScreen, describeTarget } from './rn'
|
|
4
|
+
export type { ShotikAnalyticsConfig, NavigationRefLike } from './rn'
|
|
5
|
+
export { Analytics } from './core'
|
|
6
|
+
export type { AnalyticsConfig, Consent, Storage } from './core'
|
|
7
|
+
export type { Batch, DeviceContext, EventType, WireEvent, Wireframe, WireNode } from './wire'
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schema": "shotik.recipe/1",
|
|
3
|
+
"id": "analytics-react-native",
|
|
4
|
+
"title": "Shotik analytics in a React Native / Expo app",
|
|
5
|
+
"package": "@shotik/react-native/analytics",
|
|
6
|
+
"rules": [
|
|
7
|
+
"Follow the repo's own conventions (AGENTS.md, CLAUDE.md, lint, import aliases, file layout) when placing code.",
|
|
8
|
+
"Edit only the files a step names or detects; never change native projects (ios/, android/): the SDK has no native code.",
|
|
9
|
+
"Replace {{VARS}} before writing. Keep edits minimal and idempotent: skip a step whose `done` check already passes.",
|
|
10
|
+
"Privacy: never pass text input values, emails, names or tokens in track() props. Wrap any view showing personal data (names, health numbers, messages) in <ShotikMask>.",
|
|
11
|
+
"Analytics must not change behavior or look: no new UI, no new screens. If something here would, stop and ask."
|
|
12
|
+
],
|
|
13
|
+
"variables": {
|
|
14
|
+
"APP_KEY": {
|
|
15
|
+
"required": true,
|
|
16
|
+
"desc": "Public SDK key from Shotik (Support → SDK), spk_…; the same key as Support, safe to ship"
|
|
17
|
+
},
|
|
18
|
+
"SERVER_URL": {
|
|
19
|
+
"required": true,
|
|
20
|
+
"desc": "Shotik server URL the phone can reach (simulator: http://127.0.0.1:4590)"
|
|
21
|
+
},
|
|
22
|
+
"SNAPSHOTS": {
|
|
23
|
+
"required": false,
|
|
24
|
+
"default": "0.2",
|
|
25
|
+
"desc": "Share of sessions that record privacy-safe wireframes (0..1)"
|
|
26
|
+
},
|
|
27
|
+
"METRO_PORT": {
|
|
28
|
+
"required": false,
|
|
29
|
+
"default": "8081",
|
|
30
|
+
"desc": "Port for the bundle check"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"detect": [
|
|
34
|
+
{
|
|
35
|
+
"id": "legacy",
|
|
36
|
+
"how": "package.json dependencies has \"@shotik/support-react-native\" (an older vendored copy, file:./vendor/…) → true",
|
|
37
|
+
"result": "true | false"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"id": "installed",
|
|
41
|
+
"how": "package.json dependencies has \"@shotik/react-native\" (Support already integrated) → true"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"id": "packageManager",
|
|
45
|
+
"how": "bun.lock or bun.lockb → bun; pnpm-lock.yaml → pnpm; yarn.lock → yarn; else npm"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"id": "router",
|
|
49
|
+
"how": "\"expo-router\" in dependencies → expo-router; \"@react-navigation/native\" → react-navigation; else none"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"id": "rootComponent",
|
|
53
|
+
"how": "expo-router: the root _layout.tsx; if it only re-exports, follow it to the file that renders the tree. Otherwise the component passed to registerRootComponent / AppRegistry."
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"id": "providers",
|
|
57
|
+
"how": "Outermost wrappers returned by rootComponent (GestureHandlerRootView, SafeAreaProvider, ShotikSupportProvider, …), outermost first"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"id": "navigationRef",
|
|
61
|
+
"when": "router == react-navigation",
|
|
62
|
+
"how": "The ref passed to <NavigationContainer ref=…>, or none"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": "purchases",
|
|
66
|
+
"how": "\"react-native-purchases\" in dependencies → the module where Purchases.configure(...) is called",
|
|
67
|
+
"optional": true
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"id": "auth",
|
|
71
|
+
"how": "Where the app learns the signed-in user and its sign-out path",
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"id": "keyActions",
|
|
76
|
+
"how": "Up to 5 core user actions worth naming (e.g. start a workout, finish a lesson, share): the handler where each completes",
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"id": "sensitive",
|
|
81
|
+
"how": "Views that render personal data (user name, email, health values, private messages)",
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"id": "typecheck",
|
|
86
|
+
"how": "package.json scripts.typecheck → \"<pm> run typecheck\"; else \"npx tsc --noEmit\" when tsconfig.json exists"
|
|
87
|
+
}
|
|
88
|
+
],
|
|
89
|
+
"steps": [
|
|
90
|
+
{
|
|
91
|
+
"id": "dependency",
|
|
92
|
+
"title": "Add the package",
|
|
93
|
+
"edits": [
|
|
94
|
+
{
|
|
95
|
+
"op": "json-set",
|
|
96
|
+
"file": "package.json",
|
|
97
|
+
"path": [
|
|
98
|
+
"dependencies",
|
|
99
|
+
"@shotik/react-native"
|
|
100
|
+
],
|
|
101
|
+
"value": "^0.1.0"
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
"run": {
|
|
105
|
+
"bun": "bun install",
|
|
106
|
+
"pnpm": "pnpm install",
|
|
107
|
+
"yarn": "yarn install",
|
|
108
|
+
"npm": "npm install"
|
|
109
|
+
},
|
|
110
|
+
"note": "One package for Support and Analytics: when detect.installed is true this step is already done.",
|
|
111
|
+
"done": "test -f node_modules/@shotik/react-native/dist/analytics/index.js",
|
|
112
|
+
"migrate": "When detect.legacy is true: delete dependencies[\"@shotik/support-react-native\"] and the vendor/shotik-support-react-native folder, and change every import from '@shotik/support-react-native' to '@shotik/react-native' (same exports, same subpaths)."
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"id": "init",
|
|
116
|
+
"title": "Start analytics once, at module scope of the root",
|
|
117
|
+
"file": "{{detect.rootComponent}}",
|
|
118
|
+
"edits": [
|
|
119
|
+
{
|
|
120
|
+
"op": "add-import",
|
|
121
|
+
"code": "import { ShotikAnalytics, ShotikAnalyticsRoot } from '@shotik/react-native/analytics';"
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"op": "insert-after",
|
|
125
|
+
"anchor": "the imports",
|
|
126
|
+
"code": "ShotikAnalytics.init({ appKey: '{{APP_KEY}}', serverUrl: '{{SERVER_URL}}', snapshots: {{SNAPSHOTS}} });"
|
|
127
|
+
}
|
|
128
|
+
],
|
|
129
|
+
"done": "grep -q 'ShotikAnalytics.init' {{detect.rootComponent}}"
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
"id": "root",
|
|
133
|
+
"title": "Wrap the tree for taps and wireframes",
|
|
134
|
+
"file": "{{detect.rootComponent}}",
|
|
135
|
+
"edits": [
|
|
136
|
+
{
|
|
137
|
+
"op": "wrap-jsx",
|
|
138
|
+
"target": "The children of the outermost element in detect.providers (inside GestureHandlerRootView), or the whole returned tree when there is none",
|
|
139
|
+
"open": "<ShotikAnalyticsRoot>",
|
|
140
|
+
"close": "</ShotikAnalyticsRoot>"
|
|
141
|
+
}
|
|
142
|
+
],
|
|
143
|
+
"done": "grep -q '<ShotikAnalyticsRoot>' {{detect.rootComponent}}"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"id": "screens",
|
|
147
|
+
"title": "Screen tracking",
|
|
148
|
+
"edits": [
|
|
149
|
+
{
|
|
150
|
+
"op": "add-import",
|
|
151
|
+
"file": "{{detect.rootComponent}}",
|
|
152
|
+
"when": "router == expo-router",
|
|
153
|
+
"code": "import { useShotikExpoRouter } from '@shotik/react-native/analytics';"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
"op": "insert-after",
|
|
157
|
+
"file": "{{detect.rootComponent}}",
|
|
158
|
+
"when": "router == expo-router",
|
|
159
|
+
"anchor": "the first line inside the root layout component body (a hook call at top level, before any early return)",
|
|
160
|
+
"code": "useShotikExpoRouter();"
|
|
161
|
+
}
|
|
162
|
+
],
|
|
163
|
+
"otherwise": "react-navigation: after the container is ready call ShotikAnalytics.trackNavigation(detect.navigationRef); no router: ShotikAnalytics.screen('<name>') in each screen's mount effect",
|
|
164
|
+
"done": "grep -rqE 'useShotikExpoRouter\\(|trackNavigation\\(' {{detect.rootComponent}}"
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
"id": "purchases",
|
|
168
|
+
"title": "Purchase events",
|
|
169
|
+
"when": "detect.purchases found",
|
|
170
|
+
"file": "{{detect.purchases}}",
|
|
171
|
+
"edits": [
|
|
172
|
+
{
|
|
173
|
+
"op": "add-import",
|
|
174
|
+
"code": "import { ShotikAnalytics } from '@shotik/react-native/analytics';"
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"op": "insert-after",
|
|
178
|
+
"anchor": "Purchases.configure(...)",
|
|
179
|
+
"code": "ShotikAnalytics.trackRevenueCat(Purchases as unknown as Record<string, unknown>);"
|
|
180
|
+
}
|
|
181
|
+
],
|
|
182
|
+
"done": "grep -q 'trackRevenueCat' {{detect.purchases}}"
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"id": "identify",
|
|
186
|
+
"title": "Identify after sign-in",
|
|
187
|
+
"when": "detect.auth found",
|
|
188
|
+
"file": "{{detect.auth}}",
|
|
189
|
+
"edits": [
|
|
190
|
+
{
|
|
191
|
+
"op": "add-import",
|
|
192
|
+
"code": "import { ShotikAnalytics } from '@shotik/react-native/analytics';"
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
"op": "insert-after",
|
|
196
|
+
"anchor": "the point where a signed-in user id is known",
|
|
197
|
+
"code": "ShotikAnalytics.identify(user.id);"
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
"op": "insert-after",
|
|
201
|
+
"anchor": "the sign-out path",
|
|
202
|
+
"code": "void ShotikAnalytics.reset();"
|
|
203
|
+
}
|
|
204
|
+
]
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
"id": "events",
|
|
208
|
+
"title": "Name the core actions",
|
|
209
|
+
"when": "detect.keyActions found",
|
|
210
|
+
"edits": [
|
|
211
|
+
{
|
|
212
|
+
"op": "insert-after",
|
|
213
|
+
"anchor": "where each key action completes",
|
|
214
|
+
"code": "ShotikAnalytics.track('<snake_case_action>', { <non-personal props, e.g. minutes: 10> });"
|
|
215
|
+
}
|
|
216
|
+
],
|
|
217
|
+
"note": "At most 5. Names are snake_case verbs (walk_started, plan_completed). No personal data in props."
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
"id": "mask",
|
|
221
|
+
"title": "Mask personal data",
|
|
222
|
+
"when": "detect.sensitive found",
|
|
223
|
+
"edits": [
|
|
224
|
+
{
|
|
225
|
+
"op": "wrap-jsx",
|
|
226
|
+
"target": "each view in detect.sensitive",
|
|
227
|
+
"open": "<ShotikMask>",
|
|
228
|
+
"close": "</ShotikMask>",
|
|
229
|
+
"import": "import { ShotikMask } from '@shotik/react-native/analytics';"
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
}
|
|
233
|
+
],
|
|
234
|
+
"verify": [
|
|
235
|
+
{
|
|
236
|
+
"id": "wired",
|
|
237
|
+
"run": "grep -rlE 'ShotikAnalytics\\.(init|track|trackRevenueCat)|ShotikAnalyticsRoot|useShotikExpoRouter' --include=*.ts --include=*.tsx --exclude-dir=node_modules --exclude-dir=vendor .",
|
|
238
|
+
"expect": "lists the root component (and the purchases/auth files when found)"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
"id": "typecheck",
|
|
242
|
+
"run": "{{detect.typecheck}}",
|
|
243
|
+
"expect": "exit 0, or no new errors in the files this recipe touched"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"id": "key",
|
|
247
|
+
"run": "curl -s -o /dev/null -w '%{http_code}' -X POST -H 'x-shotik-key: {{APP_KEY}}' -H 'content-type: application/json' -d '{\"anonymousId\":\"recipe-check\",\"events\":[]}' {{SERVER_URL}}/analytics/v1/events",
|
|
248
|
+
"expect": "200; 401 means a wrong key"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"id": "bundle",
|
|
252
|
+
"run": "curl -sf 'http://127.0.0.1:{{METRO_PORT}}/{{detect.entry}}.bundle?platform=ios&dev=true' | grep -c '/analytics/v1/events'",
|
|
253
|
+
"expect": "≥ 1",
|
|
254
|
+
"needs": "Metro running: npx expo start --dev-client --port {{METRO_PORT}}"
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
"id": "device",
|
|
258
|
+
"run": "open the app on the shared simulator (device broker) and tap through two screens",
|
|
259
|
+
"expect": "Shotik → Insights shows the app's users and sessions within a minute",
|
|
260
|
+
"manual": true
|
|
261
|
+
}
|
|
262
|
+
]
|
|
263
|
+
}
|