Getting started
Install
npm install @nochurn/sdk
Initialize
Call NoChurn.init() once, as early as possible in your app.
import { NoChurn } from '@nochurn/sdk';
const tracker = NoChurn.init({
apiKey: 'ck_...',
userId: 'user_abc123',
consent: true,
});
tracker.track('feature_used', { feature: 'analytics_dashboard' });
You get the API key from the NoChurn dashboard. It looks like ck_<8 hex>_<random>.
:::info There is no endpoint to configure
The SDK sends to https://api.nochurn.dev on its own. If you are self-hosting the backend, pass endpoint with the API root — the SDK appends /v1/events itself:
// ✅ correct
NoChurn.init({ apiKey, consent: true, endpoint: 'https://api.your-domain.com' });
// ❌ wrong — it becomes /v1/events/v1/events
NoChurn.init({ apiKey, consent: true, endpoint: 'https://api.your-domain.com/v1/events' });
:::
Consent is required
consent: true is not optional. Without it the SDK is a no-op: no listeners are registered, no events are collected, nothing is sent. That is the GDPR/LGPD default, and it is deliberate.
The API key format
Keys are ck_<8 hex>_<random>. Legacy keys (live_..., sk_...) are rejected with 401 — the SDK logs a warning in the console when it spots one so you do not debug a silent retry loop.
Sending events
The SDK captures activity on its own — clicks, scrolls, page views, time spent on billing pages, and JavaScript errors. You only need track() for your own domain events.
tracker.track('feature_used', { feature: 'analytics_dashboard' });
tracker.track('subscription_upgraded', { plan: 'pro' });
Identifying a user
If you know who the user is when they sign up, pass userId to init(). If you only learn it later, call identify():
tracker.identify('user_abc123', { plan: 'pro', country: 'BR' });
The events collected before the signup follow the user — an anonymous visitor that identifies itself does not start from zero.
Server-side rendering
The SDK is safe in Node. Without window, document or localStorage (SSR, nuxt generate, a worker) it registers no DOM listeners and does not use the local queue — but it still accepts track() and ships the events.
Testing on localhost
The backend rejects a domain that is not on the tenant's allowlist. To test a production key from localhost, pass the domain explicitly:
NoChurn.init({
apiKey: 'ck_...',
consent: true,
tenantDomain: 'app.your-company.com',
});
In production, leave it out — the SDK detects location.hostname on its own.
See a score
- Install the SDK and send events.
- Open the dashboard and pick your tenant.
- The score appears after the cron runs — every 30 minutes — or immediately if you request it through the API.
A score needs a history. A user with no events at all is a new user, not a churn — the scoring model needs at least one prior event to tell silence from absence.
The tracker API
| Method | What it does |
|---|---|
track(type, properties?) | Track a custom event |
identify(userId, traits?) | Attach a user id and traits to the events collected so far |
flush() | Force a flush of the current batch |
pause() / resume() | Stop / restart collecting (events stay queued) |
notifyRouteChange(path) | Tell the tracker the SPA route changed — triggers the billing-page signal and a page view |
deleteAllData() | Erase every local event and request backend deletion |
getState() | Inspect session id, user id, queue size, last flush |
disable(signal) | Disable a built-in signal |
addSignal(name, evaluator) | Register a custom signal evaluator |
disable() and addSignal() are accepted by the SDK but the scoring runs on the backend, which does not read them yet. They currently have no effect on the score.