@livx.cc/native-kit 0.16.1 → 0.17.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/README.md CHANGED
@@ -13,6 +13,17 @@ await kit.haptics.impact('medium'); // native vibration in the shell, no-op/
13
13
  const photo = await kit.photos.pick(); // native picker on device, <input type=file> on web
14
14
  ```
15
15
 
16
+ ### Analytics context
17
+
18
+ `kit.context()` returns one flat, vendor-neutral property bag (client taxonomy, app version/build, install source, device, push permission, …) — spread it straight into your analytics super-properties. Native-only fields degrade gracefully on web. First-party & non-tracking: the install id is iOS IDFV / an Android UUID, never IDFA/GAID.
19
+
20
+ ```ts
21
+ import { kit } from '@livx.cc/native-kit';
22
+ mixpanel.register(await kit.context());
23
+ // → { client: 'native-ios', platform: 'ios', app_version: '1.0.3',
24
+ // install_source: 'testflight', device_model: 'iPhone15,3', push_permission: 'granted', … }
25
+ ```
26
+
16
27
  The kit detects whether it's running inside the appwrap shell and routes each call to the native bridge or a web fallback, so a single web build works everywhere. Capabilities advertise their real backing (`native` / `web` / `none`) so your UI can be honest about what's available.
17
28
 
18
29
  Covers haptics, share, storage, notifications, OAuth, billing (IAP), health, geo, camera, photos, contacts, calendar, motion, reviews, and more.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livx.cc/native-kit",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "description": "Isomorphic native-capabilities kit for PWAs — same API in browser and in an appwrap native shell. Zero dependencies.",
5
5
  "license": "MIT",
6
6
  "author": "Elya Livshitz",
@@ -33,6 +33,38 @@ export class NativeKitOptions {
33
33
  handshakeTimeoutMs = 3000;
34
34
  }
35
35
 
36
+ /**
37
+ * Flat, vendor-neutral analytics bag — spread straight into your analytics provider's
38
+ * super-properties (e.g. `mixpanel.register(await kit.context())`). Native-only fields
39
+ * degrade gracefully (omitted/undefined) on web and on older shells. snake_case keys
40
+ * match common analytics conventions.
41
+ */
42
+ export interface KitContext {
43
+ /** Coarse runtime taxonomy: 'native-ios' | 'native-android' | 'web'. */
44
+ client: string;
45
+ is_native: boolean;
46
+ platform: Platform;
47
+ app_id?: string;
48
+ app_name?: string;
49
+ app_version?: string;
50
+ app_build?: string;
51
+ /** App Store / TestFlight / sideload / simulator / web. */
52
+ install_source?: string;
53
+ /** Stable per-install id (iOS IDFV / Android UUID) — first-party, non-tracking. */
54
+ install_id?: string;
55
+ first_install_at?: number;
56
+ last_update_at?: number;
57
+ is_emulator?: boolean;
58
+ device_model?: string;
59
+ device_os?: string;
60
+ device_os_version?: string;
61
+ device_manufacturer?: string;
62
+ device_language?: string;
63
+ device_region?: string;
64
+ push_permission?: string;
65
+ network_type?: string;
66
+ }
67
+
36
68
  export class NativeKit {
37
69
  public readonly haptics = new HapticsModule(this);
38
70
  public readonly share = new ShareModule(this);
@@ -64,6 +96,7 @@ export class NativeKit {
64
96
  public options: NativeKitOptions;
65
97
  private adapter: NativeKitAdapter | null = null;
66
98
  private readyPromise: Promise<Handshake> | null = null;
99
+ private contextPromise: Promise<KitContext> | null = null;
67
100
 
68
101
  constructor(options?: Partial<NativeKitOptions>) {
69
102
  this.options = { ...new NativeKitOptions(), ...options };
@@ -94,6 +127,57 @@ export class NativeKit {
94
127
  return this.readyPromise;
95
128
  }
96
129
 
130
+ /**
131
+ * One flat analytics bag (handshake + device + install env + push perm + network),
132
+ * ready to spread into super-properties. Resilient: each native probe degrades to
133
+ * omitted on failure, so this never rejects once {@link ready} resolves. Cached.
134
+ */
135
+ context(): Promise<KitContext> {
136
+ if (!this.contextPromise) {
137
+ this.contextPromise = (async () => {
138
+ await this.ready();
139
+ const hs = this.handshakeInfo!;
140
+ const client = this.is.native ? `native-${hs.platform}` : 'web';
141
+ const ctx: KitContext = {
142
+ client,
143
+ is_native: this.is.native,
144
+ platform: hs.platform,
145
+ app_id: hs.app?.id,
146
+ app_name: hs.app?.name,
147
+ app_version: hs.app?.version,
148
+ app_build: hs.app?.build,
149
+ };
150
+ // Probe in parallel; a failing/absent capability simply leaves its fields unset.
151
+ const safe = <T>(p: Promise<T>): Promise<T | null> => p.catch(() => null);
152
+ const [env, device, push, net] = await Promise.all([
153
+ safe(this.app.environment()),
154
+ safe(this.device.info()),
155
+ this.push.capability === 'native' ? safe(this.push.permissionStatus()) : Promise.resolve(null),
156
+ safe(this.network.status()),
157
+ ]);
158
+ if (env) {
159
+ ctx.install_source = env.source;
160
+ ctx.install_id = env.installId;
161
+ ctx.first_install_at = env.firstInstallAt;
162
+ ctx.last_update_at = env.lastUpdateAt;
163
+ ctx.is_emulator = env.isEmulator;
164
+ }
165
+ if (device) {
166
+ ctx.device_model = device.model;
167
+ ctx.device_os = device.os;
168
+ ctx.device_os_version = device.osVersion;
169
+ ctx.device_manufacturer = device.manufacturer;
170
+ ctx.device_language = device.language;
171
+ ctx.device_region = device.region;
172
+ }
173
+ if (push) ctx.push_permission = push;
174
+ if (net) ctx.network_type = net.type;
175
+ return ctx;
176
+ })();
177
+ }
178
+ return this.contextPromise;
179
+ }
180
+
97
181
  get is() {
98
182
  return { native: this.adapter?.kind === 'appwrap', web: this.adapter?.kind === 'web' };
99
183
  }
@@ -322,6 +322,17 @@ export class WebAdapter implements NativeKitAdapter {
322
322
  case 'app.openSettings':
323
323
  throw new KitError('UNSUPPORTED', 'No app settings page on web');
324
324
 
325
+ case 'app.environment':
326
+ // On web there's no install — report the honest 'web' source. is_emulator is native-only.
327
+ return { source: 'web', isEmulator: false } as T;
328
+
329
+ case 'push.permissionStatus': {
330
+ // Best-effort via the Notification API (web push ≠ native push, but the perm signal is useful).
331
+ if (!('Notification' in window)) return 'notDetermined' as T;
332
+ const perm = Notification.permission; // 'granted' | 'denied' | 'default'
333
+ return (perm === 'default' ? 'notDetermined' : perm) as T;
334
+ }
335
+
325
336
  case 'billing.products':
326
337
  case 'billing.purchase':
327
338
  case 'billing.restore':
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { NativeKit, NativeKitOptions, kit } from './core/NativeKit';
2
+ export type { KitContext } from './core/NativeKit';
3
+ export type { AppEnvironment, InstallSource } from './modules/app';
2
4
  export { AppwrapAdapter } from './core/appwrap-adapter';
3
5
  export { WebAdapter } from './core/web-adapter';
4
6
  export { KitError } from './core/types';
@@ -1,5 +1,28 @@
1
1
  import type { NativeKit } from '../core/NativeKit';
2
2
 
3
+ /** Where this build was installed from. Drives beta-vs-prod analytics cohorts.
4
+ * - `appstore` / `playstore` — public store install
5
+ * - `testflight` — iOS beta
6
+ * - `sideload` — direct install (dev build, ad-hoc, APK), or unknown installer
7
+ * - `simulator` — running on a simulator/emulator
8
+ * - `web` — not running inside the native shell */
9
+ export type InstallSource = 'appstore' | 'playstore' | 'testflight' | 'sideload' | 'simulator' | 'web';
10
+
11
+ /** Install-level environment facts the page can't derive itself. First-party,
12
+ * non-tracking (no IDFA/GAID): `installId` is iOS IDFV / an Android UUID kept in
13
+ * app storage — app-scoped and reset on uninstall. */
14
+ export interface AppEnvironment {
15
+ source: InstallSource;
16
+ /** Stable per-install id (iOS identifierForVendor / Android random UUID). */
17
+ installId?: string;
18
+ /** First install epoch-ms (Android only; omitted on iOS — no clean API). */
19
+ firstInstallAt?: number;
20
+ /** Last update epoch-ms (Android only). */
21
+ lastUpdateAt?: number;
22
+ /** Running on a simulator/emulator. */
23
+ isEmulator: boolean;
24
+ }
25
+
3
26
  /** System-level app actions: hand a URL to the OS, or open this app's settings page. */
4
27
  export class AppModule {
5
28
  constructor(private kit: NativeKit) {}
@@ -8,6 +31,11 @@ export class AppModule {
8
31
  return this.kit.capability('app');
9
32
  }
10
33
 
34
+ /** Install-level environment facts for analytics/identification (source, install id, timestamps). */
35
+ environment(): Promise<AppEnvironment> {
36
+ return this.kit.invoke('app.environment');
37
+ }
38
+
11
39
  /** Open a URL in the OS default handler (external browser, mail:, tel:, maps:, …). */
12
40
  openUrl(url: string): Promise<void> {
13
41
  return this.kit.invoke('app.openUrl', { url });
@@ -49,6 +49,12 @@ export class PushModule {
49
49
  return this.kit.invoke('push.requestPermission', undefined, { timeoutMs: 60_000 });
50
50
  }
51
51
 
52
+ /** Current authorization WITHOUT prompting — for opt-in funnel analytics.
53
+ * 'notDetermined' means the user hasn't been asked yet. */
54
+ permissionStatus(): Promise<'granted' | 'denied' | 'notDetermined'> {
55
+ return this.kit.invoke('push.permissionStatus');
56
+ }
57
+
52
58
  /** Register with APNs/FCM and resolve the device token. Call after permission is granted. */
53
59
  register(): Promise<PushToken> {
54
60
  return this.kit.invoke('push.register', undefined, { timeoutMs: 30_000 });