railyard/packages/insights-react-native
caelum0x 6baaadf91a feat(insights): identify and people, saved cohorts, retention by property, breakdowns, CSV and JSON export, delete by user (PY3.2)
RYP-F1 in Wave P. Everything scoped by the org through website.team_id with
bound parameters only; tested at the SQL level on a throwaway cluster with
the upstream migrations plus the fork's 90 and 91.

- Identify: the edge collector (contract v1.1, additive) and the page tag
  accept `identify(userId, traits)` and a `user` on any event. Insights links
  the session (upstream session_link and session.distinct_id) and keeps one
  profile per (app, user) with first and last seen and merged traits
  (migration 91_railyard_people). Before this, an identify from the collector
  made Insights refuse the whole batch.
- Saved cohorts: performed (with min times), not performed, trait equals and
  first seen within; counts are live. On fixtures a cohort equals a
  hand-written SQL count (two definitions).
- Retention by property (country, language, screen, trait.<key>; day or week
  periods), breakdowns (path, referrer, country, language, screen, event,
  hostname, data.<key>, trait.<key>).
- Export: events (31 days) or profiles, JSON or CSV, 100,000 rows; formula
  cells defused.
- Delete by user id: one statement removes events, event data, session data,
  revenue, replay and heatmap rows, links, sessions and the profile in the
  org's apps only; recorded in railyard_erasure; a pending request is retried
  by POST /api/v1/erasure/sweep (hourly), so it is done within 24 h.
- Console /api/v1/insights/* for an org's ry_ token: the org comes from the
  token, app names resolve to the org's own app, the platform token never
  leaves; served only with RAILYARD_REPLAY_LIVE=1 (denylist rule
  railyard-slot-replay), else 503 "arriving".
- New packages: @railyard/insights-react-native (offline queue, identify kept
  across restarts, reset, consent) and @railyard/insights-server (server
  track and identify; profiles, cohorts, retention, breakdown, export,
  deleteUser through the console). No dependencies.

Env: RAILYARD_REPLAY_LIVE (console). Migration: insights
91_railyard_people (idempotent; scratch Postgres only).

Tests: insights fork 9/9 (depth SQL incl. isolation, cohort vs hand SQL,
erasure), edge 18/18, console 125/125 (tsc clean), RN 3/3, server 5/5
(incl. copy lint), CLI 18/18. The upstream vitest suite runs in the image
build on box 2 or CI (not on the Mac).

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

@railyard/insights-react-native

Railyard Insights for React Native and Expo apps: screens, events and the signed-in person, sent from a queue on the device, so an app that was offline sends everything once it is back, and each event only once.

import AsyncStorage from '@react-native-async-storage/async-storage';
import { createInsights } from '@railyard/insights-react-native';

const insights = createInsights({
  site: process.env.EXPO_PUBLIC_RAILYARD_SITE!,          // ry_site_..., public
  collector: process.env.EXPO_PUBLIC_RAILYARD_COLLECTOR!,
  storage: AsyncStorage,
  app: 'com.acme.shop',
});

insights.screen('/home');
await insights.identify(user.id, { plan: 'pro' });  // after sign-in
insights.track('created_project', { template: 'blank' });
await insights.reset();                              // after sign-out
  • identify takes your app's own user id (1 to 50 of A-Z a-z 0-9 @ . _ : + = -) and up to 20 traits. The id is kept on the device, so later events, even after a restart, belong to that person until reset.
  • Pass consent: () => boolean; while it returns false nothing is queued or sent.
  • No IP address is read or sent by this package; the collector never stores one.
  • No dependencies. Any storage with getItem and setItem works.

Profiles, cohorts, retention by property, breakdowns, export and delete by user id are read on your server with @railyard/insights-server.