caelum0x dbef356e6e feat(analytics-sdk): @railyard/analytics-js (web + Expo) and @railyard/analytics-node wrappers, tested with the unmodified SDKs (A1)
- analytics-js wraps posthog-js 1.435.1 and posthog-react-native 4.78.2:
  reads NEXT_PUBLIC_/EXPO_PUBLIC_/VITE_ANALYTICS_SITE and _HOST, waits for
  consent (no client, no storage, no request before it), masks all inputs,
  [data-private]/.ph-mask text, blocks [data-block], no canvas, headers or
  bodies; Expo entry keeps the queue on the device and sends $screen per
  route change (screenTracker).
- analytics-node wraps posthog-node 5.54.1.
- test/a1: posthog-node (gzip), posthog-js under jsdom, one Chromium run
  (cached 1243, full build in new headless mode), React Native offline for
  10 minutes plus a restart and a lost response: every event stored once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 03:58:53 +03:00

65 lines
2.2 KiB
Markdown

# @railyard/analytics-js
Event analytics for web and Expo apps on Railyard. After `scribase add analytics`
the site key is already in your environment, so this is all you add:
```ts
import { createAnalytics } from "@railyard/analytics-js";
export const analytics = createAnalytics();
// When your consent banner says yes:
analytics.setConsent("granted");
analytics.capture("checkout_started", { items: 3 });
analytics.identify("user_123", { email: "ada@example.com" });
analytics.group("company", "acme", { name: "Acme" });
```
In Expo, import from `@railyard/analytics-js/react-native` (the package picks
it automatically in React Native bundlers):
```tsx
import { useEffect } from "react";
import { usePathname } from "expo-router";
import { createNativeAnalytics, screenTracker } from "@railyard/analytics-js/react-native";
export const analytics = createNativeAnalytics();
const track = screenTracker(analytics);
export function useScreens() {
const path = usePathname();
useEffect(() => void track(path), [path]);
}
```
## Where the key and host come from
| Framework | Site key | Host (optional) |
|---|---|---|
| Next.js | `NEXT_PUBLIC_ANALYTICS_SITE` | `NEXT_PUBLIC_ANALYTICS_HOST` |
| Expo | `EXPO_PUBLIC_ANALYTICS_SITE` | `EXPO_PUBLIC_ANALYTICS_HOST` |
| Vite | `VITE_ANALYTICS_SITE` | `VITE_ANALYTICS_HOST` |
You can also pass `{ site, host }` directly.
## Privacy defaults
- Consent first: until `setConsent("granted")` nothing is loaded, stored on the
device or sent. Calls made before then are dropped, not queued. Pass
`{ requireConsent: false }` only where your users need no consent.
- `setConsent("denied")` stops sending and forgets the visitor on this device.
- Recordings, when your project turns them on, mask every input, mask text
inside `[data-private]` or `.ph-mask`, leave out anything inside
`[data-block]`, and never record canvas, request headers or bodies.
- No console output and no performance timings are captured.
- The service stores no IP address.
- On mobile the queue is kept on the device, so events made offline are sent
once the app is back online, and each event is stored once.
## Server side
Use `@railyard/analytics-node` for events from your server.
The underlying open-source SDKs and their licences are listed in `NOTICE`.