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-server

Railyard Insights from your server.

import { createInsightsServer } from '@railyard/insights-server';

const insights = createInsightsServer({
  site: process.env.RAILYARD_SITE_KEY,          // to send (public)
  collector: process.env.RAILYARD_COLLECTOR_URL,
  token: process.env.RAILYARD_TOKEN,            // to read, export and delete (secret)
  app: 'shop',
});

await insights.identify(user.id, { plan: 'pro' });
await insights.track(user.id, 'invoice_paid', { amount: 49 });

const cohort = await insights.cohorts.save('Active pro', {
  performed: { event: 'created_project', min_times: 2, within_days: 30 },
  trait: { key: 'plan', equals: 'pro' },
});
const { members, people } = await insights.cohorts.members(cohort.cohort_id);
const byPlan = await insights.retention({ property: 'trait.plan', period: 'week' });
const topPages = await insights.breakdown({ dimension: 'path', days: 7 });
const csv = await insights.exportEvents({ format: 'csv' });   // up to 31 days
await insights.deleteUser(user.id);                          // done at once, or within 24 hours
Call What it gives
identify(userId, traits) Names a person; traits merge into their profile
track(userId, name, data) A custom event by that person
profiles({ user }) People with first and last seen, traits and sessions; with user, their recent events
cohorts.save / list / members / remove Saved cohorts by what people did, did not do, a trait, or when they were first seen; counts are live
retention({ property, period }) For each value of country, language, screen or trait.<key>: people in period 0 and how many came back in each later period
breakdown({ dimension }) Events and visitors by path, referrer, country, language, screen, event, hostname, data.<key> or trait.<key>
exportEvents / exportProfiles JSON rows or CSV text; spreadsheet formulas in cells are defused
deleteUser(userId) Removes every event, session and trait of that person

token is the org token from railyard login. Keep it in your secret store; this package never prints it. No dependencies.