@shotik/react-native 0.1.0 → 0.2.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/CHANGELOG.md +16 -0
- package/LICENSE +26 -0
- package/README.md +39 -3
- package/README.ru.md +40 -3
- package/dist/analytics/core.d.ts +96 -11
- package/dist/analytics/core.js +295 -22
- package/dist/analytics/privacy.d.ts +4 -0
- package/dist/analytics/privacy.js +46 -0
- package/dist/analytics/replay.d.ts +41 -0
- package/dist/analytics/replay.js +192 -0
- package/dist/analytics/rn.d.ts +52 -2
- package/dist/analytics/rn.js +669 -62
- package/dist/analytics/wire.d.ts +54 -2
- package/dist/analytics/wire.js +44 -4
- package/dist/client.d.ts +94 -1
- package/dist/client.js +254 -17
- package/dist/flow.d.ts +48 -0
- package/dist/flow.js +56 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/parts.d.ts +42 -1
- package/dist/parts.js +72 -14
- package/dist/react.d.ts +24 -3
- package/dist/react.js +42 -15
- package/dist/sheet.d.ts +2 -1
- package/dist/sheet.js +207 -16
- package/dist/support.js +2 -2
- package/dist/theme.d.ts +43 -0
- package/dist/theme.js +90 -4
- package/package.json +15 -2
- package/recipe.json +1 -1
- package/src/analytics/README.md +10 -6
- package/src/analytics/core.ts +314 -25
- package/src/analytics/privacy.ts +50 -0
- package/src/analytics/recipe.json +1 -1
- package/src/analytics/replay.ts +190 -0
- package/src/analytics/rn.tsx +620 -63
- package/src/analytics/wire.ts +99 -8
- package/src/client.ts +344 -19
- package/src/flow.ts +100 -0
- package/src/index.ts +4 -4
- package/src/parts.tsx +102 -14
- package/src/react.tsx +57 -16
- package/src/sheet.tsx +423 -20
- package/src/support.ts +2 -2
- package/src/theme.ts +90 -4
package/dist/analytics/rn.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
// React Native glue for Shotik analytics: automatic capture with no native code. Taps come from one capture-phase
|
|
2
2
|
// touch listener on a root View (element identity from the React fiber of the touched view), screens from React
|
|
3
3
|
// Navigation / Expo Router state, app open and close from AppState, crashes from the global error handler, stalls
|
|
4
|
-
// from a 500 ms timer's drift, purchases from RevenueCat, and wireframes from the host tree's DOM API.
|
|
4
|
+
// from a 500 ms timer's drift, purchases from RevenueCat, and wireframes from the host tree's DOM API. Non-tap touches
|
|
5
|
+
// become gestures or settled scroll offsets, and wireframe replay diffs the host tree after interactions (PROTOCOL.md 4.7).
|
|
5
6
|
// Every handler is O(depth) and allocation-light; uploads happen off the touch path.
|
|
6
7
|
import { Component, createElement, useEffect } from 'react';
|
|
7
8
|
import { AppState, Dimensions, Linking, Platform, StyleSheet, View } from 'react-native';
|
|
8
|
-
import { Analytics } from './core';
|
|
9
|
+
import { Analytics, SDK_VERSION } from './core';
|
|
10
|
+
import { idWords, redact } from './privacy';
|
|
11
|
+
import { registerAnalytics } from '../client';
|
|
9
12
|
let client = null;
|
|
13
|
+
let labelMode = 'a11y';
|
|
10
14
|
const off = [];
|
|
11
15
|
const opt = (load) => {
|
|
12
16
|
try {
|
|
@@ -47,6 +51,7 @@ function storage() {
|
|
|
47
51
|
function device() {
|
|
48
52
|
let d = null;
|
|
49
53
|
let a = null;
|
|
54
|
+
let u = null;
|
|
50
55
|
try {
|
|
51
56
|
d = require('expo-device');
|
|
52
57
|
}
|
|
@@ -55,9 +60,33 @@ function device() {
|
|
|
55
60
|
a = require('expo-application');
|
|
56
61
|
}
|
|
57
62
|
catch { }
|
|
63
|
+
try {
|
|
64
|
+
u = require('expo-updates');
|
|
65
|
+
}
|
|
66
|
+
catch { }
|
|
58
67
|
const o = Intl.DateTimeFormat().resolvedOptions();
|
|
59
68
|
const { width, height } = Dimensions.get('window');
|
|
60
|
-
|
|
69
|
+
const ota = u && !u.isEmbeddedLaunch && typeof u.updateId === 'string' ? u.updateId : undefined;
|
|
70
|
+
return { platform: Platform.OS, os: d?.osVersion ?? String(Platform.Version), model: d?.modelName ?? undefined, app: a?.nativeApplicationVersion ?? undefined, build: a?.nativeBuildVersion ?? undefined, locale: o.locale, tz: o.timeZone, w: Math.round(width), h: Math.round(height), ota, ch: u?.channel || undefined, rt: u?.runtimeVersion || undefined, sdk: 'rn/' + SDK_VERSION, tzo: -new Date().getTimezoneOffset(), dev: globalThis.__DEV__ ? 1 : undefined };
|
|
71
|
+
}
|
|
72
|
+
// Wi-Fi only replay uploads (PROTOCOL.md 4.7): @react-native-community/netinfo, else expo-network; neither installed: never.
|
|
73
|
+
function wifi() {
|
|
74
|
+
let n = null;
|
|
75
|
+
try {
|
|
76
|
+
const m = require('@react-native-community/netinfo');
|
|
77
|
+
n = m?.default ?? m;
|
|
78
|
+
}
|
|
79
|
+
catch { }
|
|
80
|
+
if (n?.fetch)
|
|
81
|
+
return n.fetch().then((s) => s?.type === 'wifi' || s?.type === 'ethernet');
|
|
82
|
+
let x = null;
|
|
83
|
+
try {
|
|
84
|
+
x = require('expo-network');
|
|
85
|
+
}
|
|
86
|
+
catch { }
|
|
87
|
+
if (x?.getNetworkStateAsync)
|
|
88
|
+
return x.getNetworkStateAsync().then((s) => s?.type === 'WIFI' || s?.type === 'ETHERNET');
|
|
89
|
+
return Promise.resolve(false);
|
|
61
90
|
}
|
|
62
91
|
// Fiber helpers
|
|
63
92
|
const nameOf = (t) => {
|
|
@@ -67,35 +96,45 @@ const nameOf = (t) => {
|
|
|
67
96
|
return o.displayName || o.name || o.render?.displayName || o.render?.name || (o.type ? nameOf(o.type) : null);
|
|
68
97
|
};
|
|
69
98
|
const GENERIC = /^(View|Text|Image|Animated.*|AnimatedComponent.*|Pressable|Touchable.*|TouchableOpacity|Fragment|Memo|ForwardRef|Unknown|ScrollView|SafeAreaView|.{0,2}|.*Provider|.*Context|Screen.*|Navigation.*|Stack.*|Route.*|ShotikMask|ShotikAnalyticsRoot)$/;
|
|
70
|
-
const
|
|
99
|
+
const PRESS_HOST = /^(RNGestureHandlerButton|RCTSwitch|Switch|AndroidSwitch)$/;
|
|
71
100
|
const INPUT = /TextInput/;
|
|
72
101
|
const handles = (p) => !!(p.onPress || p.onPressIn || p.onLongPress || p.onClick || p.onResponderRelease || p.onValueChange || p.accessibilityRole === 'button' || p.role === 'button' || p.accessibilityRole === 'link');
|
|
102
|
+
// A press handler is a fiber with real press props or a host component known to be pressable, never a component name:
|
|
103
|
+
// providers such as GestureHandlerRootView or React 19's LinkingContext sit above every tap. Scroll hosts carry responder
|
|
104
|
+
// props for scrolling, not pressing. A GestureDetector (`gesture` prop) counts.
|
|
105
|
+
const isHandler = (f, p) => (typeof f.type === 'string' ? PRESS_HOST.test(f.type) || (!/Scroll/.test(f.type) && handles(p)) : handles(p) || !!p.gesture);
|
|
106
|
+
const isText = (f) => typeof f.type === 'string' && /Text$/.test(f.type) && !INPUT.test(f.type);
|
|
73
107
|
function textUnder(f, max = 40) {
|
|
74
108
|
const out = [];
|
|
75
109
|
let n = 0;
|
|
76
110
|
const walk = (x) => {
|
|
77
111
|
while (x && n++ < 60 && out.join(' ').length < max) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
112
|
+
const p = x.memoizedProps;
|
|
113
|
+
if (x.tag === 6 && typeof p === 'string')
|
|
114
|
+
out.push(p);
|
|
115
|
+
else if (x.type === 'RCTText' && typeof p === 'object' && p && typeof p.children === 'string')
|
|
116
|
+
out.push(p.children);
|
|
117
|
+
else
|
|
118
|
+
walk(x.child);
|
|
83
119
|
x = x.sibling;
|
|
84
120
|
}
|
|
85
121
|
};
|
|
86
122
|
walk(f?.child ?? null);
|
|
87
123
|
return out.join(' ').replace(/\s+/g, ' ').trim().slice(0, max);
|
|
88
124
|
}
|
|
89
|
-
const
|
|
125
|
+
const fnv = (s) => {
|
|
90
126
|
let h = 0x811c9dc5;
|
|
91
127
|
for (let i = 0; i < s.length; i++)
|
|
92
128
|
h = Math.imul(h ^ s.charCodeAt(i), 0x01000193);
|
|
93
|
-
return
|
|
129
|
+
return h >>> 0;
|
|
94
130
|
};
|
|
95
|
-
const
|
|
131
|
+
const hash = (s) => fnv(s).toString(36);
|
|
96
132
|
// id: testID, else nativeID, else a hash of the fiber index path from the pressable up (list rows collapse to one id).
|
|
97
|
-
// It ignores labels and component names, so it survives locale switches and minified release builds.
|
|
98
|
-
|
|
133
|
+
// It ignores labels and component names, so it survives locale switches and minified release builds. A tap nothing
|
|
134
|
+
// handles takes its ids only from the touched element (up to the next host view) and a label only when it hit text.
|
|
135
|
+
// labels (PROTOCOL.md §7): 'a11y' the accessibility label or a testID-derived name, 'visible' also the visible text, 'none'.
|
|
136
|
+
// masked: inside <ShotikMask> or a secure text input (no label, no tap position); secure: a secureTextEntry input.
|
|
137
|
+
export function describeTarget(target, labels = labelMode) {
|
|
99
138
|
let f = target;
|
|
100
139
|
let handler = null;
|
|
101
140
|
let tid;
|
|
@@ -103,19 +142,25 @@ export function describeTarget(target) {
|
|
|
103
142
|
let nid;
|
|
104
143
|
let path = [];
|
|
105
144
|
let masked = false;
|
|
145
|
+
let secure = false;
|
|
106
146
|
let input = false;
|
|
107
147
|
let scroll = null;
|
|
148
|
+
let own = null;
|
|
108
149
|
const names = [];
|
|
109
150
|
for (let d = 0; f && d < 80; d++, f = f.return) {
|
|
110
151
|
const p = (typeof f.memoizedProps === 'object' && f.memoizedProps) || {};
|
|
111
152
|
const name = nameOf(f.type);
|
|
153
|
+
if (!handler && !own && d > 0 && typeof f.type === 'string')
|
|
154
|
+
own = { tid, nid, a11y };
|
|
112
155
|
if (f.type === ShotikMask || p.shotikMask)
|
|
113
156
|
masked = true;
|
|
114
157
|
if ((name && INPUT.test(name)) || (typeof f.type === 'string' && /TextInput/.test(f.type)))
|
|
115
158
|
input = true;
|
|
159
|
+
if (p.secureTextEntry === true)
|
|
160
|
+
secure = true;
|
|
116
161
|
if (!scroll && f.stateNode?._scrollMetrics)
|
|
117
162
|
scroll = f;
|
|
118
|
-
if (!handler && (
|
|
163
|
+
if (!handler && isHandler(f, p))
|
|
119
164
|
(handler = f), (path = []);
|
|
120
165
|
path.push('cellKey' in p && 'item' in p ? '*' : f.index);
|
|
121
166
|
if (!handler || handler === f) {
|
|
@@ -129,10 +174,89 @@ export function describeTarget(target) {
|
|
|
129
174
|
if (handler && name && names.length < 2 && !GENERIC.test(name))
|
|
130
175
|
names.push(name);
|
|
131
176
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
177
|
+
if (!handler && own)
|
|
178
|
+
({ tid, nid, a11y } = own);
|
|
179
|
+
const hide = masked || secure || input || labels === 'none';
|
|
180
|
+
const raw = hide ? '' : a11y || (labels === 'visible' ? (handler ? textUnder(handler) : target && isText(target) ? textUnder(target) : '') : tid ? idWords(tid) : '');
|
|
181
|
+
const label = raw ? redact(raw).replace(/\d/g, '#').slice(0, 40) : undefined;
|
|
182
|
+
const el = input ? 'TextInput' : names.reverse().join('>') || (handler ? nameOf(handler.type) || 'Pressable' : 'View');
|
|
183
|
+
return { el: el + (tid ? '#' + tid : ''), id: tid ?? nid ?? 'h' + hash(path.join('.')), label, dead: !handler && !input, input, masked: masked || secure, secure, scroll };
|
|
184
|
+
}
|
|
185
|
+
// UI-state fingerprint (PROTOCOL.md §4.1 state): FNV-1a 32 of the sorted set of interactive elements visible in the window
|
|
186
|
+
// (testID, nativeID, else "h" + hash of the index path from the nearest navigator screen) plus "c" + key per visible
|
|
187
|
+
// list cell. Hidden subtrees are skipped: inactive react-native-screens, a11y-hidden stack cards, display: none, hidden
|
|
188
|
+
// Offscreen/Activity. Layout comes from the DOM API or nativeFabricUIManager; without it (old architecture) every mounted
|
|
189
|
+
// element counts. Pure pointer walking, no allocation per fiber; null past MAX_FIBERS.
|
|
190
|
+
const MAX_FIBERS = 30000;
|
|
191
|
+
const none = (s) => !!s && typeof s === 'object' && (Array.isArray(s) ? s.length < 6 && s.some(none) : s.display === 'none');
|
|
192
|
+
const hidden = (f, p) => (f.tag === 22 && f.memoizedState != null) || p.activityState === 0 || p.accessibilityElementsHidden === true || p['aria-hidden'] === true || p.importantForAccessibility === 'no-hide-descendants' || (typeof f.type === 'string' && none(p.style));
|
|
193
|
+
function rectOf(f) {
|
|
194
|
+
const s = f.stateNode;
|
|
195
|
+
const el = s?.canonical?.publicInstance ?? s;
|
|
196
|
+
if (el?.getBoundingClientRect)
|
|
197
|
+
return el.getBoundingClientRect();
|
|
198
|
+
const ui = globalThis.nativeFabricUIManager;
|
|
199
|
+
const r = s?.node ? ui?.getBoundingClientRect?.(s.node, true) : undefined;
|
|
200
|
+
return r ? { x: r[0], y: r[1], width: r[2], height: r[3] } : null;
|
|
201
|
+
}
|
|
202
|
+
const shown = (f, W, H) => {
|
|
203
|
+
let h = f;
|
|
204
|
+
for (let i = 0; h && typeof h.type !== 'string' && i < 8; i++)
|
|
205
|
+
h = h.child;
|
|
206
|
+
const r = h && typeof h.type === 'string' ? rectOf(h) : null;
|
|
207
|
+
return !r || (r.width > 0 && r.height > 0 && r.x < W && r.x + r.width > 0 && r.y < H && r.y + r.height > 0);
|
|
208
|
+
};
|
|
209
|
+
export function uiFingerprint(root, W, H) {
|
|
210
|
+
if (!root)
|
|
211
|
+
return null;
|
|
212
|
+
const ids = [];
|
|
213
|
+
const idx = [];
|
|
214
|
+
const bnd = [-1];
|
|
215
|
+
let f = root.child;
|
|
216
|
+
let d = 0;
|
|
217
|
+
let seen = 0;
|
|
218
|
+
while (f) {
|
|
219
|
+
if (++seen > MAX_FIBERS)
|
|
220
|
+
return null;
|
|
221
|
+
idx[d] = f.index;
|
|
222
|
+
const p = (typeof f.memoizedProps === 'object' && f.memoizedProps) || {};
|
|
223
|
+
let into = !hidden(f, p);
|
|
224
|
+
if (into) {
|
|
225
|
+
if (p.route && p.navigation)
|
|
226
|
+
bnd.push(d);
|
|
227
|
+
if ('cellKey' in p && 'item' in p) {
|
|
228
|
+
if (shown(f, W, H))
|
|
229
|
+
ids.push('c' + String(p.cellKey));
|
|
230
|
+
else
|
|
231
|
+
into = false;
|
|
232
|
+
}
|
|
233
|
+
else if (typeof f.type === 'string' && isHandler(f, p) && shown(f, W, H))
|
|
234
|
+
ids.push(typeof p.testID === 'string' ? p.testID : typeof p.nativeID === 'string' ? p.nativeID : 'h' + hash(idx.slice(bnd[bnd.length - 1] + 1, d + 1).join('.')));
|
|
235
|
+
}
|
|
236
|
+
if (into && f.child) {
|
|
237
|
+
f = f.child;
|
|
238
|
+
d++;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
while (f) {
|
|
242
|
+
if (bnd[bnd.length - 1] === d)
|
|
243
|
+
bnd.pop();
|
|
244
|
+
if (f.sibling) {
|
|
245
|
+
f = f.sibling;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
f = f.return;
|
|
249
|
+
if (--d < 0)
|
|
250
|
+
f = null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
ids.sort();
|
|
254
|
+
let s = '';
|
|
255
|
+
let n = 0;
|
|
256
|
+
for (let i = 0; i < ids.length; i++)
|
|
257
|
+
if (ids[i] !== ids[i - 1])
|
|
258
|
+
(s += ids[i] + '\n'), n++;
|
|
259
|
+
return { h: fnv(s).toString(16).padStart(8, '0'), n };
|
|
136
260
|
}
|
|
137
261
|
function wireframe(root) {
|
|
138
262
|
if (!root?.getBoundingClientRect || !root.children)
|
|
@@ -150,7 +274,7 @@ function wireframe(root) {
|
|
|
150
274
|
const st = (StyleSheet.flatten(p.style) ?? {});
|
|
151
275
|
const tag = el.tagName ?? '';
|
|
152
276
|
const box = (k, fill, rad) => [k, Math.round(r.x), Math.round(r.y), Math.round(r.width), Math.round(r.height), typeof fill === 'string' ? fill : undefined, typeof rad === 'number' ? rad : undefined];
|
|
153
|
-
if (p.nativeID === 'shotik-mask')
|
|
277
|
+
if (p.nativeID === 'shotik-mask' || p.shotikMask)
|
|
154
278
|
return void out.push(box('m'));
|
|
155
279
|
if (/Text$/.test(tag) && !/Input/.test(tag))
|
|
156
280
|
return void out.push(box('t', st.color));
|
|
@@ -158,7 +282,7 @@ function wireframe(root) {
|
|
|
158
282
|
return void out.push(box('n', st.backgroundColor, st.borderRadius));
|
|
159
283
|
if (/Image/.test(tag))
|
|
160
284
|
return void out.push(box('i', undefined, st.borderRadius));
|
|
161
|
-
const kind =
|
|
285
|
+
const kind = fib && isHandler(fib, p) ? 'b' : /Scroll/.test(tag) ? 's' : 'v';
|
|
162
286
|
if (kind !== 'v' || st.backgroundColor || st.borderWidth)
|
|
163
287
|
out.push(box(kind, st.backgroundColor ?? st.borderColor, st.borderRadius));
|
|
164
288
|
const kids = el.children;
|
|
@@ -169,52 +293,374 @@ function wireframe(root) {
|
|
|
169
293
|
visit(root);
|
|
170
294
|
return out;
|
|
171
295
|
}
|
|
296
|
+
// Replay walk (PROTOCOL.md 4.7): the host tree from the root, nodes intersecting the window grown by half a window on each
|
|
297
|
+
// side, keyed by base36 FNV-1a of kind + host index path (a list cell's view by its item index). Nodes inside a scroll
|
|
298
|
+
// view are in its content space (`in` = its key), so scrolling moves one offset instead of every node. Masked subtrees,
|
|
299
|
+
// secure inputs, inputs and web views are one box with nothing inside walked. Kinds come from the host fiber's type.
|
|
300
|
+
const MAX_REPLAY = 600;
|
|
301
|
+
const EMPTY = {};
|
|
302
|
+
const props = (f) => (f && typeof f.memoizedProps === 'object' && f.memoizedProps) || EMPTY;
|
|
303
|
+
const styleOf = (s) => (!s ? EMPTY : typeof s === 'object' && !Array.isArray(s) ? s : (StyleSheet.flatten(s) ?? EMPTY));
|
|
304
|
+
const cellIndex = (f) => {
|
|
305
|
+
for (let x = f?.return, i = 0; x && typeof x.type !== 'string' && i < 4; x = x.return, i++) {
|
|
306
|
+
const p = props(x);
|
|
307
|
+
if ('cellKey' in p && typeof p.index === 'number')
|
|
308
|
+
return p.index;
|
|
309
|
+
}
|
|
310
|
+
return -1;
|
|
311
|
+
};
|
|
312
|
+
export function replayWalk(root, W, H) {
|
|
313
|
+
if (!root?.getBoundingClientRect || !root.children)
|
|
314
|
+
return null;
|
|
315
|
+
const nodes = [];
|
|
316
|
+
const scroll = [];
|
|
317
|
+
const x0 = -W / 2;
|
|
318
|
+
const x1 = W * 1.5;
|
|
319
|
+
const y0 = -H / 2;
|
|
320
|
+
const y1 = H * 1.5;
|
|
321
|
+
const visit = (el, path, ox, oy, inKey) => {
|
|
322
|
+
if (nodes.length >= MAX_REPLAY)
|
|
323
|
+
return;
|
|
324
|
+
const r = el.getBoundingClientRect?.();
|
|
325
|
+
if (!r || r.width < 2 || r.height < 2 || r.y > y1 || r.y + r.height < y0 || r.x > x1 || r.x + r.width < x0)
|
|
326
|
+
return;
|
|
327
|
+
const fib = el.__internalInstanceHandle;
|
|
328
|
+
const p = props(fib);
|
|
329
|
+
if (fib && hidden(fib, p))
|
|
330
|
+
return;
|
|
331
|
+
const tag = (typeof fib?.type === 'string' ? fib.type : '') || el.tagName || '';
|
|
332
|
+
const st = styleOf(p.style);
|
|
333
|
+
const push = (k, fill, rad) => {
|
|
334
|
+
const key = fnv(k + path).toString(36);
|
|
335
|
+
const n = [key, k, Math.round(r.x - ox), Math.round(r.y - oy), Math.round(r.width), Math.round(r.height)];
|
|
336
|
+
const f = typeof fill === 'string' ? fill : null;
|
|
337
|
+
const rd = typeof rad === 'number' ? rad : null;
|
|
338
|
+
if (inKey)
|
|
339
|
+
n.push(f, rd, inKey);
|
|
340
|
+
else if (rd !== null)
|
|
341
|
+
n.push(f, rd);
|
|
342
|
+
else if (f !== null)
|
|
343
|
+
n.push(f);
|
|
344
|
+
nodes.push(n);
|
|
345
|
+
return key;
|
|
346
|
+
};
|
|
347
|
+
if (p.nativeID === 'shotik-mask' || p.shotikMask || p.secureTextEntry === true)
|
|
348
|
+
return void push('m');
|
|
349
|
+
if (/WebView/.test(tag))
|
|
350
|
+
return void push('w');
|
|
351
|
+
if (/Input/.test(tag))
|
|
352
|
+
return void push('n', st.backgroundColor, st.borderRadius);
|
|
353
|
+
if (/Text$|Paragraph/.test(tag))
|
|
354
|
+
return void push('t', st.color);
|
|
355
|
+
if (/Image/.test(tag))
|
|
356
|
+
return void push('i', undefined, st.borderRadius);
|
|
357
|
+
const sv = /ScrollView$/.test(tag);
|
|
358
|
+
const kind = fib && isHandler(fib, p) ? 'b' : sv ? 's' : 'v';
|
|
359
|
+
const key = kind !== 'v' || st.backgroundColor || st.borderWidth ? push(kind, st.backgroundColor ?? st.borderColor, st.borderRadius) : null;
|
|
360
|
+
const kids = el.children;
|
|
361
|
+
if (!kids)
|
|
362
|
+
return;
|
|
363
|
+
let cx = ox;
|
|
364
|
+
let cy = oy;
|
|
365
|
+
let inner = inKey;
|
|
366
|
+
const cr = sv && key && kids.length ? kids[0].getBoundingClientRect?.() : null;
|
|
367
|
+
if (cr && key) {
|
|
368
|
+
cx = cr.x;
|
|
369
|
+
cy = cr.y;
|
|
370
|
+
inner = key;
|
|
371
|
+
const s = { node: key, y: Math.round(r.y - cr.y) };
|
|
372
|
+
const sx = Math.round(r.x - cr.x);
|
|
373
|
+
if (sx)
|
|
374
|
+
s.x = sx;
|
|
375
|
+
scroll.push(s);
|
|
376
|
+
}
|
|
377
|
+
for (let i = 0; i < kids.length; i++) {
|
|
378
|
+
const ci = cellIndex(kids[i].__internalInstanceHandle);
|
|
379
|
+
visit(kids[i], path + '.' + (ci >= 0 ? 'r' + ci : i), cx, cy, inner);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
visit(root, '0', 0, 0, null);
|
|
383
|
+
return { nodes, scroll };
|
|
384
|
+
}
|
|
385
|
+
const isHz = (f, p) => p.horizontal === true || /Horizontal/.test(String(f.type));
|
|
386
|
+
function scrollHost(target, hz) {
|
|
387
|
+
for (let f = target, d = 0; f && d < 80; d++, f = f.return)
|
|
388
|
+
if (typeof f.type === 'string' && /ScrollView$/.test(f.type) && isHz(f, props(f)) === hz)
|
|
389
|
+
return f;
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
function readScroll(f, hz) {
|
|
393
|
+
let c = f.child;
|
|
394
|
+
while (c && typeof c.type !== 'string')
|
|
395
|
+
c = c.child;
|
|
396
|
+
const sr = rectOf(f);
|
|
397
|
+
const cr = c ? rectOf(c) : null;
|
|
398
|
+
if (sr && cr)
|
|
399
|
+
return hz ? { off: Math.round(sr.x - cr.x), len: Math.round(cr.width), vis: Math.round(sr.width) } : { off: Math.round(sr.y - cr.y), len: Math.round(cr.height), vis: Math.round(sr.height) };
|
|
400
|
+
for (let a = f.return, i = 0; a && i < 6; a = a.return, i++) {
|
|
401
|
+
const m = a.stateNode?._scrollMetrics;
|
|
402
|
+
if (m)
|
|
403
|
+
return { off: Math.round(m.offset), len: Math.round(m.contentLength), vis: Math.round(m.visibleLength) };
|
|
404
|
+
}
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
function containerOf(f) {
|
|
408
|
+
let tid;
|
|
409
|
+
let nid;
|
|
410
|
+
const path = [];
|
|
411
|
+
const names = [];
|
|
412
|
+
for (let x = f, d = 0; x && d < 80; d++, x = x.return) {
|
|
413
|
+
const p = props(x);
|
|
414
|
+
if (d < 6 && !tid && typeof p.testID === 'string')
|
|
415
|
+
tid = p.testID;
|
|
416
|
+
if (d < 6 && !nid && typeof p.nativeID === 'string')
|
|
417
|
+
nid = p.nativeID;
|
|
418
|
+
path.push('cellKey' in p && 'item' in p ? '*' : x.index);
|
|
419
|
+
const name = nameOf(x.type);
|
|
420
|
+
if (name && names.length < 2 && !GENERIC.test(name) && !/^VirtualizedList/.test(name))
|
|
421
|
+
names.push(name);
|
|
422
|
+
}
|
|
423
|
+
return { el: (names.reverse().join('>') || 'ScrollView') + (tid ? '#' + tid : ''), id: tid ?? nid ?? 'h' + hash(path.join('.')) };
|
|
424
|
+
}
|
|
425
|
+
const fiberOf = (e) => (e._targetInst ?? e.target?.__internalInstanceHandle ?? null);
|
|
426
|
+
const pnow = () => globalThis.performance?.now() ?? Date.now();
|
|
427
|
+
// Two pointers: the first reading is the pinch's start span and centroid, later ones its current span and centroid.
|
|
428
|
+
function pinchAt(s, ts) {
|
|
429
|
+
if (!ts || ts.length < 2)
|
|
430
|
+
return;
|
|
431
|
+
const span = Math.hypot(ts[1].pageX - ts[0].pageX, ts[1].pageY - ts[0].pageY);
|
|
432
|
+
const cx = (ts[0].pageX + ts[1].pageX) / 2;
|
|
433
|
+
const cy = (ts[0].pageY + ts[1].pageY) / 2;
|
|
434
|
+
if (s.span0 === undefined)
|
|
435
|
+
(s.span0 = span), (s.cx0 = cx), (s.cy0 = cy);
|
|
436
|
+
s.span = span;
|
|
437
|
+
s.cx = cx;
|
|
438
|
+
s.cy = cy;
|
|
439
|
+
}
|
|
440
|
+
// The class instance's fiber may be the stale alternate; the current one is the side whose root is the FiberRoot's current.
|
|
441
|
+
function currentFiber(f) {
|
|
442
|
+
if (!f)
|
|
443
|
+
return null;
|
|
444
|
+
let top = f;
|
|
445
|
+
while (top.return)
|
|
446
|
+
top = top.return;
|
|
447
|
+
return top.stateNode?.current === top || !f.alternate ? f : f.alternate;
|
|
448
|
+
}
|
|
172
449
|
export class ShotikAnalyticsRoot extends Component {
|
|
173
450
|
constructor() {
|
|
174
451
|
super(...arguments);
|
|
175
452
|
this.root = null;
|
|
176
453
|
this.start = null;
|
|
454
|
+
this.capTimer = null;
|
|
455
|
+
this.capAt = 0;
|
|
456
|
+
this.capAgain = false;
|
|
457
|
+
this.lastCap = 0;
|
|
458
|
+
this.capGap = 1000;
|
|
459
|
+
this.settling = new Map();
|
|
460
|
+
this.lastOff = new Map();
|
|
177
461
|
this.snapTimer = null;
|
|
178
|
-
this.
|
|
462
|
+
this.walkTimer = null;
|
|
463
|
+
this.firstKick = 0;
|
|
464
|
+
this.lastWalk = 0;
|
|
465
|
+
this.gap = 1000;
|
|
466
|
+
this.loud = false;
|
|
467
|
+
this.focus = null;
|
|
468
|
+
this.unsub = [];
|
|
469
|
+
this.walk = () => {
|
|
470
|
+
this.walkTimer = null;
|
|
471
|
+
if (!client)
|
|
472
|
+
return;
|
|
473
|
+
if (this.start)
|
|
474
|
+
return this.kick(300, !this.loud);
|
|
475
|
+
const loud = this.loud;
|
|
476
|
+
this.loud = false;
|
|
477
|
+
const { width, height } = Dimensions.get('window');
|
|
478
|
+
const t0 = pnow();
|
|
479
|
+
const fp = opt(() => uiFingerprint(currentFiber(this._reactInternals), width, height));
|
|
480
|
+
const ms = pnow() - t0;
|
|
481
|
+
this.lastWalk = Date.now();
|
|
482
|
+
this.gap = Math.min(30000, Math.max(1000, ms * 1000));
|
|
483
|
+
if (fp)
|
|
484
|
+
client.measure('fp', ms);
|
|
485
|
+
if (client.uiState(fp?.h ?? null, fp?.n ?? 0, !loud))
|
|
486
|
+
this.capture(0);
|
|
487
|
+
};
|
|
488
|
+
this.snap = () => {
|
|
489
|
+
this.capTimer = null;
|
|
490
|
+
if (!client || !this.root)
|
|
491
|
+
return;
|
|
492
|
+
if (this.start)
|
|
493
|
+
return this.capture(300, false, this.capAgain);
|
|
494
|
+
const again = this.capAgain;
|
|
495
|
+
this.capAgain = false;
|
|
496
|
+
const { width, height } = Dimensions.get('window');
|
|
497
|
+
const t0 = pnow();
|
|
498
|
+
const w = opt(() => replayWalk(this.root, width, height));
|
|
499
|
+
const made = !!w && client.replayFrame({ screen: client.currentScreen ?? '', w: Math.round(width), h: Math.round(height), ...w });
|
|
500
|
+
const ms = pnow() - t0;
|
|
501
|
+
this.lastCap = Date.now();
|
|
502
|
+
if (!w)
|
|
503
|
+
return;
|
|
504
|
+
client.measure('walk', ms);
|
|
505
|
+
this.capGap = Math.max(1000, ms * 500);
|
|
506
|
+
if (made && again)
|
|
507
|
+
this.capture(700);
|
|
508
|
+
};
|
|
509
|
+
this.onFocus = (e) => {
|
|
510
|
+
const f = client && fiberOf(e);
|
|
511
|
+
const d = f && opt(() => describeTarget(f));
|
|
512
|
+
if (!client || !f || !d?.input)
|
|
513
|
+
return;
|
|
514
|
+
this.endField();
|
|
515
|
+
const p = (typeof f.memoizedProps === 'object' && f.memoizedProps) || {};
|
|
516
|
+
const v = [p.value, p.text, p.defaultValue].find((x) => typeof x === 'string');
|
|
517
|
+
this.focus = { target: e.target, id: d.id, el: d.el, secure: d.secure, ts: Date.now(), screen: client.currentScreen, edits: 0, filled: !!v };
|
|
518
|
+
};
|
|
519
|
+
this.onChange = (e) => {
|
|
520
|
+
const f = this.focus;
|
|
521
|
+
if (!f || e.target !== f.target)
|
|
522
|
+
return;
|
|
523
|
+
f.edits++;
|
|
524
|
+
const t = e.nativeEvent?.text;
|
|
525
|
+
if (typeof t === 'string')
|
|
526
|
+
f.filled = t.length > 0;
|
|
527
|
+
};
|
|
528
|
+
this.onBlur = (e) => {
|
|
529
|
+
if (this.focus && e.target === this.focus.target)
|
|
530
|
+
this.endField();
|
|
531
|
+
};
|
|
532
|
+
// Touches (PROTOCOL.md 4.1 touch classes): first pointer down to last pointer up. Moves only track the farthest distance
|
|
533
|
+
// and, with two pointers, their span and centroid; everything else happens at the end.
|
|
179
534
|
this.onStart = (e) => {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
535
|
+
const ne = e.nativeEvent;
|
|
536
|
+
const s = (this.start ?? (this.start = { x: ne.pageX, y: ne.pageY, t: Date.now(), max: 0, n: 0, ids: new Set() }));
|
|
537
|
+
for (const c of ne.changedTouches ?? [ne])
|
|
538
|
+
s.ids.add(c.identifier);
|
|
539
|
+
s.n = Math.max(s.n, s.ids.size, ne.touches?.length ?? 1);
|
|
540
|
+
if (s.n > 1)
|
|
541
|
+
pinchAt(s, ne.touches);
|
|
184
542
|
};
|
|
185
|
-
this.
|
|
543
|
+
this.onMove = (e) => {
|
|
186
544
|
const s = this.start;
|
|
187
|
-
|
|
188
|
-
if (!client || !s)
|
|
545
|
+
if (!s)
|
|
189
546
|
return;
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
547
|
+
const ne = e.nativeEvent;
|
|
548
|
+
if (s.n > 1 || (ne.touches?.length ?? 1) > 1)
|
|
549
|
+
return void ((s.n = Math.max(s.n, ne.touches?.length ?? 1)), pinchAt(s, ne.touches));
|
|
550
|
+
const d = Math.hypot(ne.pageX - s.x, ne.pageY - s.y);
|
|
551
|
+
if (d > s.max)
|
|
552
|
+
s.max = d;
|
|
553
|
+
};
|
|
554
|
+
this.onCancel = (e) => this.onEnd(e, true);
|
|
555
|
+
// A native scroll view or gesture that takes the touch over cancels it after a few points (touch slop), so a cancel that
|
|
556
|
+
// moved > 3 pt can only be a scroll, read once it settles; a still one counts only as a long press.
|
|
557
|
+
this.onEnd = (e, cancel = false) => {
|
|
558
|
+
const s = this.start;
|
|
559
|
+
if (!s)
|
|
197
560
|
return;
|
|
198
|
-
|
|
199
|
-
|
|
561
|
+
const ne = e.nativeEvent;
|
|
562
|
+
for (const c of ne.changedTouches ?? [ne])
|
|
563
|
+
s.ids.delete(c.identifier);
|
|
564
|
+
if (s.ids.size && ne.touches?.length !== 0)
|
|
200
565
|
return;
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
566
|
+
this.start = null;
|
|
567
|
+
if (!client)
|
|
568
|
+
return;
|
|
569
|
+
const t0 = pnow();
|
|
570
|
+
const now = Date.now();
|
|
571
|
+
const { pageX: x, pageY: y } = ne;
|
|
572
|
+
const dx = x - s.x;
|
|
573
|
+
const dy = y - s.y;
|
|
574
|
+
const moved = Math.max(s.max, Math.hypot(dx, dy));
|
|
575
|
+
const ms = now - s.t;
|
|
576
|
+
const target = fiberOf(e);
|
|
577
|
+
const still = moved <= (cancel ? 3 : 12);
|
|
578
|
+
if (s.n > 1 || (still && ms > 500)) {
|
|
579
|
+
if (cancel && s.n > 1)
|
|
580
|
+
return;
|
|
581
|
+
const d = describeTarget(target);
|
|
582
|
+
const sc = s.span0 && s.span ? s.span / s.span0 : 1;
|
|
583
|
+
client.gesture(s.n > 1 ? { k: 'pinch', dir: sc > 1 ? 'out' : 'in', dx: (s.cx ?? 0) - (s.cx0 ?? 0), dy: (s.cy ?? 0) - (s.cy0 ?? 0), ms, el: d.el, id: d.id, sc, ts: s.t } : { k: 'long', dx, dy, ms, el: d.el, id: d.id, ts: s.t });
|
|
584
|
+
this.kick(400);
|
|
585
|
+
this.capture(300, false, true);
|
|
586
|
+
}
|
|
587
|
+
else if (!still) {
|
|
588
|
+
const hz = Math.abs(dx) > Math.abs(dy);
|
|
589
|
+
if (!cancel)
|
|
590
|
+
client.scroll(null, dy);
|
|
591
|
+
this.kick(hz ? 600 : 800, !hz);
|
|
592
|
+
const host = scrollHost(target, hz);
|
|
593
|
+
if (host)
|
|
594
|
+
this.settle(host, hz, now, client.currentScreen);
|
|
595
|
+
else if (!cancel) {
|
|
596
|
+
const d = describeTarget(target);
|
|
597
|
+
client.gesture({ k: ms <= 300 && moved / Math.max(1, ms) >= 0.3 ? 'swipe' : 'pan', dir: hz ? (dx > 0 ? 'right' : 'left') : dy > 0 ? 'down' : 'up', dx, dy, ms, el: d.el, id: d.id, ts: s.t });
|
|
598
|
+
this.capture(300, false, true);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (!cancel) {
|
|
602
|
+
const d = describeTarget(target);
|
|
603
|
+
const { width, height } = Dimensions.get('window');
|
|
604
|
+
client.tap({ el: d.el, id: d.id, label: d.label, x, y, nx: Math.round((x / width) * 1000), ny: Math.round((y / height) * 1000), dead: d.dead, input: d.input, masked: d.masked, ts: now });
|
|
605
|
+
this.kick(400);
|
|
606
|
+
this.capture(300, false, true);
|
|
607
|
+
}
|
|
608
|
+
client.measure('tap', pnow() - t0);
|
|
209
609
|
};
|
|
210
610
|
}
|
|
211
611
|
componentDidMount() {
|
|
212
|
-
this.unsub
|
|
612
|
+
this.unsub.push(onScreen((s) => (this.endField(), this.queueSnapshot(s), this.kick(700), this.capture(600, true))), onAway(() => this.endField()));
|
|
613
|
+
this.kick(700);
|
|
213
614
|
}
|
|
214
615
|
componentWillUnmount() {
|
|
215
|
-
this.unsub
|
|
616
|
+
this.unsub.splice(0).forEach((f) => f());
|
|
617
|
+
this.endField();
|
|
216
618
|
if (this.snapTimer)
|
|
217
619
|
clearTimeout(this.snapTimer);
|
|
620
|
+
if (this.walkTimer)
|
|
621
|
+
clearTimeout(this.walkTimer);
|
|
622
|
+
if (this.capTimer)
|
|
623
|
+
clearTimeout(this.capTimer);
|
|
624
|
+
this.settling.forEach((t) => clearTimeout(t));
|
|
625
|
+
this.settling.clear();
|
|
626
|
+
}
|
|
627
|
+
// Fingerprint walks: ms after the interaction, debounced up to 1 s after the first kick, at most one per gap (1 s, or
|
|
628
|
+
// 1000 x the last walk's ms), postponed while a finger is down. quiet: only vertical scrolls since the last walk.
|
|
629
|
+
kick(ms, quiet = false) {
|
|
630
|
+
if (!client)
|
|
631
|
+
return;
|
|
632
|
+
const now = Date.now();
|
|
633
|
+
if (!quiet)
|
|
634
|
+
this.loud = true;
|
|
635
|
+
if (this.walkTimer)
|
|
636
|
+
clearTimeout(this.walkTimer);
|
|
637
|
+
else
|
|
638
|
+
this.firstKick = now;
|
|
639
|
+
const at = Math.max(Math.min(now + ms, this.firstKick + 1000), this.lastWalk + this.gap);
|
|
640
|
+
this.walkTimer = setTimeout(this.walk, at - now);
|
|
641
|
+
}
|
|
642
|
+
// Replay captures (PROTOCOL.md 4.7): ms after the trigger, at most one per capGap (1 s, or 500 x the last capture's ms),
|
|
643
|
+
// postponed while a finger is down. A screen change (key) reschedules to its own time; again: after a capture that
|
|
644
|
+
// found changes, look once more 1 s after the interaction.
|
|
645
|
+
capture(ms, key = false, again = false) {
|
|
646
|
+
if (!client?.replayOn)
|
|
647
|
+
return;
|
|
648
|
+
const now = Date.now();
|
|
649
|
+
const at = Math.max(now + ms, this.lastCap + this.capGap);
|
|
650
|
+
if (this.capTimer) {
|
|
651
|
+
if (!key && this.capAt <= at)
|
|
652
|
+
return void (this.capAgain || (this.capAgain = again));
|
|
653
|
+
clearTimeout(this.capTimer);
|
|
654
|
+
}
|
|
655
|
+
this.capAt = at;
|
|
656
|
+
this.capAgain = again;
|
|
657
|
+
this.capTimer = setTimeout(this.snap, at - now);
|
|
658
|
+
}
|
|
659
|
+
endField() {
|
|
660
|
+
const f = this.focus;
|
|
661
|
+
this.focus = null;
|
|
662
|
+
if (f && client)
|
|
663
|
+
client.field({ id: f.id, el: f.el, ts: f.ts, ms: Date.now() - f.ts, edits: f.secure ? undefined : f.edits, filled: f.filled, screen: f.screen });
|
|
218
664
|
}
|
|
219
665
|
queueSnapshot(screen) {
|
|
220
666
|
if (!client?.wantsSnapshot(screen))
|
|
@@ -234,13 +680,49 @@ export class ShotikAnalyticsRoot extends Component {
|
|
|
234
680
|
});
|
|
235
681
|
}, 900);
|
|
236
682
|
}
|
|
683
|
+
// A scrolled container: read 600 ms after the touch, then every 300 ms until two reads agree (at most 3 s). A new touch on
|
|
684
|
+
// the same container restarts it.
|
|
685
|
+
settle(f, hz, ts, screen, prev = null, n = 0) {
|
|
686
|
+
const k = f.stateNode ?? f;
|
|
687
|
+
const old = this.settling.get(k);
|
|
688
|
+
if (old)
|
|
689
|
+
clearTimeout(old);
|
|
690
|
+
this.settling.set(k, setTimeout(() => {
|
|
691
|
+
this.settling.delete(k);
|
|
692
|
+
if (!client || client.currentScreen !== screen)
|
|
693
|
+
return;
|
|
694
|
+
const r = opt(() => readScroll(f, hz));
|
|
695
|
+
if (!r)
|
|
696
|
+
return;
|
|
697
|
+
if (!prev || (Math.abs(r.off - prev.off) >= 1 && n < 8))
|
|
698
|
+
return this.settle(f, hz, ts, screen, r, n + 1);
|
|
699
|
+
if (!hz && r.len > 0)
|
|
700
|
+
client.scroll(Math.min(1, (r.off + r.vis) / r.len), 0);
|
|
701
|
+
const c = containerOf(f);
|
|
702
|
+
const key = (screen ?? '') + '|' + c.id;
|
|
703
|
+
if (Math.abs(r.off - (this.lastOff.get(key) ?? 0)) < 1)
|
|
704
|
+
return;
|
|
705
|
+
if (this.lastOff.size > 200)
|
|
706
|
+
this.lastOff.clear();
|
|
707
|
+
this.lastOff.set(key, r.off);
|
|
708
|
+
const p = { ...props(f.return), ...props(f) };
|
|
709
|
+
const snap = typeof p.snapToInterval === 'number' && p.snapToInterval > 0 ? p.snapToInterval : p.pagingEnabled === true ? r.vis : 0;
|
|
710
|
+
client.scrolled({ ...c, ...r, hz, pg: snap ? Math.round(r.off / snap) : undefined, ts, screen });
|
|
711
|
+
this.capture(0);
|
|
712
|
+
}, prev ? 300 : 600));
|
|
713
|
+
}
|
|
237
714
|
render() {
|
|
238
715
|
return createElement(View, {
|
|
239
716
|
style: { flex: 1 },
|
|
240
717
|
collapsable: false,
|
|
241
718
|
ref: (r) => void (this.root = r),
|
|
242
719
|
onTouchStartCapture: this.onStart,
|
|
243
|
-
|
|
720
|
+
onTouchMoveCapture: this.onMove,
|
|
721
|
+
onTouchEndCapture: this.onEnd,
|
|
722
|
+
onTouchCancelCapture: this.onCancel,
|
|
723
|
+
onFocusCapture: this.onFocus,
|
|
724
|
+
onBlurCapture: this.onBlur,
|
|
725
|
+
onChangeCapture: this.onChange
|
|
244
726
|
}, this.props.children);
|
|
245
727
|
}
|
|
246
728
|
}
|
|
@@ -252,7 +734,13 @@ export function ShotikMask({ children, style }) {
|
|
|
252
734
|
const screenSubs = new Set();
|
|
253
735
|
function onScreen(fn) {
|
|
254
736
|
screenSubs.add(fn);
|
|
255
|
-
return () => screenSubs.delete(fn);
|
|
737
|
+
return () => void screenSubs.delete(fn);
|
|
738
|
+
}
|
|
739
|
+
// Background listeners, run before the client closes the visit (an open field span ends first).
|
|
740
|
+
const awaySubs = new Set();
|
|
741
|
+
function onAway(fn) {
|
|
742
|
+
awaySubs.add(fn);
|
|
743
|
+
return () => void awaySubs.delete(fn);
|
|
256
744
|
}
|
|
257
745
|
function screen(name) {
|
|
258
746
|
if (!client || name === client.currentScreen)
|
|
@@ -301,6 +789,18 @@ export function useShotikExpoRouter() {
|
|
|
301
789
|
export function useShotikScreen(name) {
|
|
302
790
|
useEffect(() => screen(name), [name]);
|
|
303
791
|
}
|
|
792
|
+
const base = (s) => s.split(':')[0];
|
|
793
|
+
const ms = (n, s) => (typeof n === 'number' && n > 0 ? n : s ? Date.parse(s) || 0 : 0);
|
|
794
|
+
const entitlementsOf = (info) => Object.values(info?.entitlements?.active ?? {})
|
|
795
|
+
.filter((e) => e && e.isActive !== false && e.productIdentifier)
|
|
796
|
+
.map((e) => ({ product: base(e.productIdentifier), at: ms(e.latestPurchaseDateMillis, e.latestPurchaseDate), first: ms(e.originalPurchaseDateMillis, e.originalPurchaseDate) || undefined, trial: e.periodType === 'TRIAL' }));
|
|
797
|
+
const startedTrial = (r, product) => {
|
|
798
|
+
const res = r;
|
|
799
|
+
const pid = base(res?.productIdentifier ?? product);
|
|
800
|
+
return entitlementsOf(res?.customerInfo).some((e) => e.trial && e.product === pid);
|
|
801
|
+
};
|
|
802
|
+
const buying = new Set();
|
|
803
|
+
let restoring = 0;
|
|
304
804
|
function trackRevenueCat(P) {
|
|
305
805
|
for (const m of ['purchasePackage', 'purchaseStoreProduct', 'purchaseProduct', 'purchaseSubscriptionOption']) {
|
|
306
806
|
const orig = P[m];
|
|
@@ -309,21 +809,55 @@ function trackRevenueCat(P) {
|
|
|
309
809
|
const w = async (...a) => {
|
|
310
810
|
const it = a[0];
|
|
311
811
|
const prod = (typeof it === 'object' && (it.product ?? it)) || {};
|
|
312
|
-
const info = { product: String(prod.identifier ?? it ?? 'unknown'), price: prod.price, currency: prod.currencyCode, store: 'revenuecat'
|
|
812
|
+
const info = { product: String(prod.identifier ?? it ?? 'unknown'), price: prod.price, currency: prod.currencyCode, store: 'revenuecat' };
|
|
813
|
+
const pid = base(info.product);
|
|
313
814
|
client?.purchase({ ...info, status: 'start' });
|
|
815
|
+
buying.add(pid);
|
|
314
816
|
try {
|
|
315
817
|
const r = await orig.apply(P, a);
|
|
316
|
-
|
|
818
|
+
const mine = opt(() => entitlementsOf(r?.customerInfo).filter((e) => e.product === pid)) ?? [];
|
|
819
|
+
client?.purchase({ ...info, status: 'done', trial: opt(() => startedTrial(r, info.product)) ?? false, at: mine[0]?.at || undefined });
|
|
820
|
+
client?.entitled(mine, true);
|
|
317
821
|
return r;
|
|
318
822
|
}
|
|
319
823
|
catch (e) {
|
|
320
824
|
client?.purchase({ ...info, status: e?.userCancelled ? 'cancel' : 'fail' });
|
|
321
825
|
throw e;
|
|
322
826
|
}
|
|
827
|
+
finally {
|
|
828
|
+
buying.delete(pid);
|
|
829
|
+
}
|
|
323
830
|
};
|
|
324
831
|
w.__shotik = 1;
|
|
325
832
|
P[m] = w;
|
|
326
833
|
}
|
|
834
|
+
const restore = P.restorePurchases;
|
|
835
|
+
if (typeof restore === 'function' && !restore.__shotik) {
|
|
836
|
+
const w = async (...a) => {
|
|
837
|
+
restoring++;
|
|
838
|
+
try {
|
|
839
|
+
const r = await restore.apply(P, a);
|
|
840
|
+
client?.entitled(opt(() => entitlementsOf(r)) ?? [], true);
|
|
841
|
+
return r;
|
|
842
|
+
}
|
|
843
|
+
finally {
|
|
844
|
+
restoring--;
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
w.__shotik = 1;
|
|
848
|
+
P.restorePurchases = w;
|
|
849
|
+
}
|
|
850
|
+
const add = P.addCustomerInfoUpdateListener;
|
|
851
|
+
if (typeof add !== 'function' || P.__shotikListener)
|
|
852
|
+
return;
|
|
853
|
+
const listener = (i) => client?.entitled((opt(() => entitlementsOf(i)) ?? []).filter((e) => !buying.has(e.product)), restoring > 0);
|
|
854
|
+
P.__shotikListener = listener;
|
|
855
|
+
add.call(P, listener);
|
|
856
|
+
off.push(() => {
|
|
857
|
+
;
|
|
858
|
+
P.removeCustomerInfoUpdateListener?.call(P, listener);
|
|
859
|
+
delete P.__shotikListener;
|
|
860
|
+
});
|
|
327
861
|
}
|
|
328
862
|
function hookErrors() {
|
|
329
863
|
const EU = globalThis.ErrorUtils;
|
|
@@ -338,23 +872,89 @@ function hookErrors() {
|
|
|
338
872
|
if (!globalThis.__DEV__)
|
|
339
873
|
H?.enablePromiseRejectionTracker?.({ allRejections: true, onUnhandled: (_, r) => client?.error(r) });
|
|
340
874
|
}
|
|
875
|
+
// Only 'background' closes the visit. iOS 'inactive' (the App Store purchase sheet, Control Center, the app switcher
|
|
876
|
+
// peek) keeps the screen open, so a paywall visit isn't split by its own purchase sheet.
|
|
341
877
|
function hookAppState() {
|
|
342
|
-
let
|
|
878
|
+
let away = false;
|
|
343
879
|
const sub = AppState.addEventListener('change', (next) => {
|
|
344
|
-
if (next ===
|
|
345
|
-
|
|
346
|
-
if (next === 'active') {
|
|
347
|
-
client?.foreground();
|
|
348
|
-
stalls(true);
|
|
349
|
-
}
|
|
350
|
-
else if (state === 'active') {
|
|
880
|
+
if (next === 'background' && !away) {
|
|
881
|
+
away = true;
|
|
351
882
|
stalls(false);
|
|
883
|
+
for (const fn of awaySubs)
|
|
884
|
+
fn();
|
|
352
885
|
client?.background();
|
|
353
886
|
}
|
|
354
|
-
|
|
887
|
+
else if (next === 'active' && away) {
|
|
888
|
+
away = false;
|
|
889
|
+
client?.foreground();
|
|
890
|
+
stalls(true);
|
|
891
|
+
}
|
|
355
892
|
});
|
|
356
893
|
off.push(() => sub.remove());
|
|
357
894
|
}
|
|
895
|
+
// Network: fetch and XMLHttpRequest are wrapped once. Every request start counts as a response to the newest tap;
|
|
896
|
+
// only failures and slow calls become events (core.net). Requests to the Shotik server are skipped. React Native's fetch
|
|
897
|
+
// runs on XMLHttpRequest, so an XHR opened inside a wrapped fetch call is left to the fetch wrapper.
|
|
898
|
+
let inFetch = false;
|
|
899
|
+
function hookNet(base) {
|
|
900
|
+
const g = globalThis;
|
|
901
|
+
const F = g.fetch;
|
|
902
|
+
if (F && !F.__shotik) {
|
|
903
|
+
const w = ((input, init) => {
|
|
904
|
+
const url = typeof input === 'string' ? input : (input.url ?? String(input));
|
|
905
|
+
if (!client || url.startsWith(base))
|
|
906
|
+
return F.call(globalThis, input, init);
|
|
907
|
+
const m = init?.method ?? input.method ?? 'GET';
|
|
908
|
+
const t0 = Date.now();
|
|
909
|
+
client.netStart();
|
|
910
|
+
inFetch = true;
|
|
911
|
+
let p;
|
|
912
|
+
try {
|
|
913
|
+
p = F.call(globalThis, input, init);
|
|
914
|
+
}
|
|
915
|
+
finally {
|
|
916
|
+
inFetch = false;
|
|
917
|
+
}
|
|
918
|
+
return p.then((r) => (client?.net(m, url, r.status, Date.now() - t0), r), (e) => {
|
|
919
|
+
if (e?.name !== 'AbortError')
|
|
920
|
+
client?.net(m, url, 0, Date.now() - t0);
|
|
921
|
+
throw e;
|
|
922
|
+
});
|
|
923
|
+
});
|
|
924
|
+
w.__shotik = 1;
|
|
925
|
+
g.fetch = w;
|
|
926
|
+
off.push(() => void (g.fetch === w && (g.fetch = F)));
|
|
927
|
+
}
|
|
928
|
+
const X = g.XMLHttpRequest?.prototype;
|
|
929
|
+
if (X && !X.open.__shotik) {
|
|
930
|
+
const { open, send } = X;
|
|
931
|
+
const o = function (m, url, ...rest) {
|
|
932
|
+
const u = String(url);
|
|
933
|
+
this.__shotik = inFetch || !client || u.startsWith(base) ? null : { m, url: u };
|
|
934
|
+
return open.call(this, m, url, ...rest);
|
|
935
|
+
};
|
|
936
|
+
const s = function (body) {
|
|
937
|
+
const r = this.__shotik;
|
|
938
|
+
if (r && client) {
|
|
939
|
+
const t0 = Date.now();
|
|
940
|
+
let aborted = false;
|
|
941
|
+
client.netStart();
|
|
942
|
+
this.addEventListener('abort', () => (aborted = true));
|
|
943
|
+
this.addEventListener('loadend', () => aborted || client?.net(r.m, r.url, this.status, Date.now() - t0));
|
|
944
|
+
}
|
|
945
|
+
return send.call(this, body);
|
|
946
|
+
};
|
|
947
|
+
Object.assign(o, { __shotik: 1 });
|
|
948
|
+
X.open = o;
|
|
949
|
+
X.send = s;
|
|
950
|
+
off.push(() => {
|
|
951
|
+
if (X.open === o)
|
|
952
|
+
X.open = open;
|
|
953
|
+
if (X.send === s)
|
|
954
|
+
X.send = send;
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
}
|
|
358
958
|
let stallTimer = null;
|
|
359
959
|
function stalls(on) {
|
|
360
960
|
if (stallTimer)
|
|
@@ -375,9 +975,12 @@ export const ShotikAnalytics = {
|
|
|
375
975
|
init(cfg) {
|
|
376
976
|
if (client)
|
|
377
977
|
return client;
|
|
378
|
-
|
|
978
|
+
labelMode = cfg.labels ?? 'a11y';
|
|
979
|
+
client = new Analytics({ ...cfg, storage: cfg.storage ?? storage(), device, wifi: cfg.wifi ?? wifi });
|
|
980
|
+
registerAnalytics(client);
|
|
379
981
|
hookErrors();
|
|
380
982
|
hookAppState();
|
|
983
|
+
hookNet(cfg.serverUrl);
|
|
381
984
|
// Dev builds only: <scheme>://…?shotik-analytics=new-user starts a fresh install identity (test runs on one device).
|
|
382
985
|
if (globalThis.__DEV__) {
|
|
383
986
|
const sub = Linking.addEventListener('url', ({ url }) => {
|
|
@@ -401,7 +1004,9 @@ export const ShotikAnalytics = {
|
|
|
401
1004
|
optOut: () => client?.optOut(),
|
|
402
1005
|
optIn: () => client?.optIn(),
|
|
403
1006
|
reset: () => client?.reset(),
|
|
1007
|
+
forget: () => client?.forget() ?? Promise.resolve(false),
|
|
404
1008
|
flush: () => client?.flush(),
|
|
1009
|
+
sendTestEvent: () => client?.sendTestEvent() ?? Promise.resolve(),
|
|
405
1010
|
trackNavigation,
|
|
406
1011
|
trackRevenueCat,
|
|
407
1012
|
stop() {
|
|
@@ -409,5 +1014,7 @@ export const ShotikAnalytics = {
|
|
|
409
1014
|
off.splice(0).forEach((f) => f());
|
|
410
1015
|
client?.stop();
|
|
411
1016
|
client = null;
|
|
1017
|
+
registerAnalytics(null);
|
|
1018
|
+
labelMode = 'a11y';
|
|
412
1019
|
}
|
|
413
1020
|
};
|