React Native SDK
@cohorly/react-native is a standalone React Native client. It has no hard dependency on react-native or any storage library - it works out of the box with an in-memory store and upgrades to persistent storage when you inject one.
Install
bashpnpm add @cohorly/react-native# recommended, for persistence across app restartspnpm add @react-native-async-storage/async-storage
Initialize
Call init() once near app launch. Inject AsyncStorage so the distinct id,
queued events, and super properties survive restarts.
tsimport AsyncStorage from "@react-native-async-storage/async-storage";import { init, track, identify, people } from "@cohorly/react-native";init({token: "YOUR_PROJECT_TOKEN",storage: AsyncStorage, // omit to fall back to in-memory storage});track("Screen Viewed", { screen: "Home" });identify("user_123");people.set({ plan: "pro" });
API
| Member | Signature | Notes |
|---|---|---|
init | (options) => CohorlyClient | All optional: token, apiHost (defaults to the hosted API), storage, flushInterval, flushAt, fetch, disabled, loadFeatureFlags, sendExposureEvents, flagPersistence, flagCacheTtlMs, and the error tracking options captureExceptions, release, dist, beforeSendException, maxExceptionQueueSize, sanitizeUrl (see Automatic capture), and debug (logs SDK warnings with console.warn). |
track | (event, properties?) => void | Queue an event. |
identify | (distinctId) => void | Switches identity synchronously, then posts /alias (best effort) when the previous id was anonymous. |
reset | () => void | Clears identity, super properties, and un-flushed events. |
register / unregister | (props) / (key) | Super properties merged into every event. |
people.* | set / setOnce / increment / unset / deleteUser | Queued and sent to /engage. |
flush | () => Promise<void> | Manual flush. |
getDistinctId | () => string | Current distinct id. |
getFeatureFlag / isFeatureEnabled | (key, fallback?) | Flag reads; optional fallback for unknown/unloaded flags (never an exposure). |
getFeatureFlagDetails | (key, fallback?) => FlagDetails | Full result with source ("network" / "persistence" / "fallback") plus ttlMs - and persistedAt when the cache carries stored_at - when served from the cache. |
getFeatureFlagPayload | (key) => unknown | null | Variant payload. |
reloadFeatureFlags / onFeatureFlags | () => Promise<void> / (cb) => unsub | Re-evaluate; subscribe to updates. |
captureException | (error, options?) => void | Queue a $exception event. Never throws, no-op before init(). |
addBreadcrumb | (crumb) => void | Record a step sent with the next exceptions. |
trackNavigation | (navigationRef) => unsubscribe | Navigation breadcrumbs from a React Navigation or expo-router ref. |
CohorlyErrorBoundary | component | Catches render errors, captures them, renders fallback. |
optOutTracking / optInTracking / hasOptedOutTracking | () => void / () => void / () => boolean | Device opt-out, persisted. Opting out drops everything queued and clears breadcrumbs; while opted out nothing is sent (events, people, alias, $exception) and no flag request is made. No-ops (and false) before init(). |
isExceptionCaptureEnabled / onExceptionCaptureChange | () => boolean / (listener) => unsub | Whether an exception would be recorded now: false until the stored state is read, when opted out, or with captureExceptions: false. The listener fires when the value changes; native crash reporters take enabled from it. |
Flags are persisted per identity (cohorly:flags, scoped by distinct_id) and
bounded by flagCacheTtlMs (default 86400000 ms, 24 h). flagPersistence
selects the policy - "persistenceUntilNetworkSuccess" (default, still
serves an expired cache up to 7 days stale), "networkFirst" (strictly
within the TTL), or "networkOnly" (never touches the cache). A legacy
cache without stored_at is served indefinitely until a successful reload
rewrites it. See
Feature flags.
Batching
Events are queued and flushed automatically every 5 seconds or once 20
events have accumulated - both configurable via flushInterval and
flushAt. Queued events are persisted to storage, so nothing is lost if
the app closes before a flush. You can also construct
new CohorlyClient(options) directly for multiple instances or dependency
injection.
Custom metrics
metrics.count, gauge and distribution have the same signature and send
the same $metric events as @cohorly/core (see
Core SDK): one event per metric name and tag
combination per 60 second window, never one per sample.
tsimport { init, metrics } from "@cohorly/react-native";const client = init({ token: "<project token>", storage: AsyncStorage });metrics.count("checkout_started", 1, { plan: "pro" });metrics.gauge("queue_length", queue.length);metrics.distribution("js_cost", elapsedMs, { tags: { service: "cart" }, unit: "ms" });// Same thing on the client:client.metrics.distribution("js_cost", elapsedMs, { service: "cart" });
The module helpers do nothing before init() and never throw. The metrics
option takes flagKey, windowMs and maxSeries.
- Kill switch. Deactivate the feature flag
cohorly_metrics(or set its rollout to 0%) and the client stops recording and drops the window in progress. A missing flag means on. - Background. The window in progress is queued when the app goes to the
background or becomes inactive, so a killed app loses at most the samples of
the last foreground stretch.
identify()also queues it first, so the samples keep the identity they were taken under;reset()andoptOutTracking()discard it. - Flags not loaded yet. While the first flag evaluation is running and no
flags are loaded,
$metricevents stay in the queue and are not sent. A disabledcohorly_metricsthen drops them; otherwise they are sent. An evaluation that fails, orloadFeatureFlags: false, holds nothing back.
Session replay settings
With sessionReplay: { remoteConfig: true }, the rates and the minimum
duration saved in Settings > Session replay apply without a new release:
tsinit({ token: "YOUR_PROJECT_TOKEN", storage: AsyncStorage, sessionReplay: { sampleRate: 0, remoteConfig: true } });
remoteConfigdefaults tofalse; then the SDK never asks the server.- Each field the project saved wins over your code's value, a field it never saved keeps yours, and with no network and no cached copy your values apply.
- The settings are kept for 1 hour per token and apply to new sessions only.
A new session gets its
$session_idat once and waits at most 3 seconds for them before it is sampled;readyandtrack()never wait. - With a minimum duration, a recorded session sends nothing until it is that long, and a shorter one is never sent, unless it holds more than 2.5 MB first, which is then sent anyway. Sessions recorded on error are always sent.
- Triggers, masking and the capture mode always come from your code.
Pausing session replay
pauseSessionReplay() stops recording the screen and resumeSessionReplay()
records again, while events keep flowing:
tsimport { pauseSessionReplay, resumeSessionReplay } from "@cohorly/react-native";pauseSessionReplay();resumeSessionReplay();
- Events, errors and breadcrumbs continue during the pause, in the same session. Resuming records again in that session from a full snapshot; the player shows the gap as an inactive stretch.
- The pause lives in memory only: a new
init()starts without it, so callpauseSessionReplay()again when the app restores that state. - The session still rotates after more than 30 minutes in background (24 hours at most); resuming then records into the new session.
- A session recorded on error sends no replay for an error during the pause, and an error after resuming sends only what was recorded after it.
- Without replay, or without a session drawn for recording, both calls do nothing.
Error tracking
Errors are ordinary events named $exception, so they can be broken down,
filtered and put into funnels like any other event. Capture one yourself:
tsimport { captureException } from "@cohorly/react-native";try {await submitOrder(cart);} catch (error) {captureException(error, {level: "error", // "fatal" | "error" (default) | "warning" | "info"title: "Checkout submit failed", // display title, also prefixes the fingerprinttags: { step: "payment" },extra: { cartSize: cart.items.length },});}
The event carries the error and its cause chain with the Hermes (or
JavaScriptCore) stack parsed into frames, the current breadcrumbs, the
release, and the same default and super properties as track() ($os,
$model, $app_version_string, $app_build_number, $release,
$release_dist, ...). Every event, not only exceptions, carries $release
and $release_dist (the same values as $exception_release /
$exception_dist; no dist on the embedded bundle), which is what Errors >
Releases groups crash-free users by. tags and extra
never overwrite a reserved key ($exception_*, error_title, distinct_id,
$device_id, $user_id, ...). A thrown value that is not an Error is sent
as a synthetic exception. The identity is the one in effect when the error is
captured: an identify() afterwards does not change it.
Automatic capture
tsinit({token: "YOUR_PROJECT_TOKEN",storage: AsyncStorage,captureExceptions: true,});
With captureExceptions: true the SDK installs:
- A global JS error handler (
ErrorUtils.setGlobalHandler), chained to the one already there. A fatal error is sent withlevel: "fatal"(it counts against crash-free users), a non-fatal one withlevel: "error", both withmechanism: { type: "onerror", handled: false }. The fatal exception is written to AsyncStorage before the previous handler runs (which usually closes the app); the SDK waits for that write at most 1.5 seconds. It is sent right away when the network allows, and otherwise on the next launch, still with the release it crashed in. - An unhandled promise rejection tracker on Hermes
(
HermesInternal.enablePromiseRejectionTracker), sent withlevel: "error"andmechanism.type: "onunhandledrejection". In__DEV__the usual "Possible Unhandled Promise Rejection" warning is kept. Without Hermes nothing is installed. A rejection with a value that is not anErroris reported asUnhandledRejection, as on the web. - Automatic breadcrumbs:
| Category | What | Never |
|---|---|---|
app.lifecycle | The new AppState ("background", "active") | |
fetch | "POST https://api.example.com/v1/cart 500" for failed (status 400+) or rejected requests | query string, fragment, headers, bodies, the SDK's own requests |
Paths of failed requests can carry ids or names (/customers/maria-souza).
Set sanitizeUrl in init() to rewrite the URL shown in the fetch
breadcrumb, in both its message and data.url. It receives the URL after the
query and fragment are dropped:
tsinit({token: "YOUR_PROJECT_TOKEN",storage: AsyncStorage,captureExceptions: true,sanitizeUrl: (url) => url.replace(/\/customers\/[^/]+/, "/customers/:id"),});
A hook that throws or returns something that is not a string records an empty
URL: the original is never the fallback (with debug: true a warning says so).
The SDK's own requests are recognised before the hook runs.
One exception: the file names of stack trace frames do not go through the hook. They stay as the JavaScript runtime reports them (the app bundle, or the Metro URL in development) because symbolication matches on them. The SDK sends no other URL on its own.
Without captureExceptions none of this is touched:
ErrorUtils, the rejection tracker, fetch and AppState stay as they are,
and captureException() still works. captureExceptions: false goes further:
nothing global is installed and no $exception is sent at all, manual
captureException() included - the switch for a user who turned error reports
off. optOutTracking() stops exceptions along with everything else. stop() on the client removes the
handlers and ends its exception capture, and calling init() again stops the
previous client the same way, so two clients never share the persisted
exception queue. Use one init() per app.
The same error is sent at most 10 times per minute (same type, message and
crashing frame), whether captured automatically or with captureException();
a render loop cannot flood the queue.
Navigation breadcrumbs
Pass your navigation container ref to trackNavigation(). It records
navigation breadcrumbs such as "Home -> Cart" with route names only -
params are never read, so ids and e-mails in them never leave the device.
tsximport { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";import { trackNavigation } from "@cohorly/react-native";export function App() {const navigationRef = useNavigationContainerRef();useEffect(() => trackNavigation(navigationRef), [navigationRef]);return <NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>;}
With expo-router, use useNavigationContainerRef() from expo-router in the
root layout the same way.
Screen views
Off by default. With trackScreenViews: true in init, trackNavigation() also
sends the event $screen with the property $screen_name: the route name of
the initial route and of every route you navigate to. It needs
trackNavigation(); the option alone sends nothing. Route params are never read.
tsximport { NavigationContainer, useNavigationContainerRef } from "@react-navigation/native";import { init, trackNavigation } from "@cohorly/react-native";init({ token: "YOUR_PROJECT_TOKEN", storage: AsyncStorage, trackScreenViews: true });export function App() {const navigationRef = useNavigationContainerRef();useEffect(() => trackNavigation(navigationRef), [navigationRef]);return <NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>;}
With Expo Router, call useNavigationContainerRef() from expo-router in the
root layout the same way. $screen_name is the route name, which for Expo Router
is the file route name relative to its nearest layout: app/profile/[id].tsx
is profile/[id], or [id] when app/profile/_layout.tsx exists. Two nested layouts can
therefore report the same name (index in two tabs): give those files
distinct names when you need to tell them apart.
A route that has not changed (setParams, or any other state event on the
same screen) is not sent again, and returning from the background sends
nothing. Each $screen is an ordinary event and counts against your event
quota. Session replay never turns this on.
Breadcrumbs and scrubbing
Add your own breadcrumbs with addBreadcrumb(); the last 50 travel with every
exception. They are kept in memory only, and reset() clears them (queued
exceptions are kept and still sent).
tsaddBreadcrumb({ category: "checkout", message: "Applied coupon", level: "info" });
beforeSendException sees every exception right before it is queued. Return
the event, changed if needed, or null to drop it. If the hook throws, the
exception is dropped - it is never sent unscrubbed.
tsimport { init, type ExceptionListEntry } from "@cohorly/react-native";init({token: "YOUR_PROJECT_TOKEN",captureExceptions: true,beforeSendException(event) {const exceptions = event.properties.$exception_list as ExceptionListEntry[];for (const exception of exceptions) {exception.value = exception.value.replace(/[\w.+-]+@[\w-]+\.[\w.]+/g, "[email]");}return event;},});
Error boundary
CohorlyErrorBoundary catches render errors below it, captures them
(mechanism.type: "react.errorboundary", level "error", the component stack
as a react.component_stack breadcrumb) and renders fallback. It does not
need @cohorly/react; without init() it still renders the fallback.
tsximport { CohorlyErrorBoundary } from "@cohorly/react-native";<CohorlyErrorBoundaryfallback={({ reset }) => <Button title="Try again" onPress={reset} />}onError={(error, componentStack) => console.log(componentStack)}><CartScreen /></CohorlyErrorBoundary>
Delivery
Exceptions have their own persisted queue (maxExceptionQueueSize, 50 by
default), so offline events can never push one out. Every flush sends
exceptions first, fatal ones before the rest, then events; when the
exceptions cannot be delivered the events wait for the next flush, unless the
exception request stalls for more than 10 seconds. With
disabled: true nothing is sent and exceptions stay persisted.
Release, dist and OTA updates
Cohorly finds the source map for a crash by the exception's release and dist,
so they must equal what you upload with
cohorly sourcemaps upload:
releasedefaults to<bundleId>@<version>+<build>, e.g.com.example.shop@3.8.1+415. The bundle id comes fromexpo-application(applicationId) orreact-native-device-info(getBundleId()); without either, the lowercase platform is the prefix (android@3.8.1+415). With no build number there is no+part; with no app version there is no release. Areleasepassed toinit()is used exactly as given.distdefaults to the runningexpo-updatesupdate id when the bundle is a downloaded OTA update, and is absent for the bundle embedded in the binary. Adistpassed toinit()wins. Every exception also carriesexpo_update_id,expo_channel,expo_runtime_versionandexpo_is_embedded_launch(only the onesexpo-updatesreports).
bash# binary build: the embedded bundle has no distcohorly sourcemaps upload --release com.example.shop@3.8.1+415 build# OTA update: same release, the update id as distcohorly sourcemaps upload --release com.example.shop@3.8.1+415 --dist "$UPDATE_ID" dist
Frames from the app's bundle are reported under the canonical bundle name -
index.android.bundle on Android, main.jsbundle on iOS - whatever the file
is called on the device (an OTA bundle has a hashed name), with the device
path kept in abs_path. Upload the map under that name (--bundle-file for
a Hermes map pair). In development, frames keep the Metro URL.
Native crashes
A native crash (Swift/Objective-C or Java/Kotlin code that never reaches the JS
handler) is recorded on disk by native code and sent by native code on the next
launch, before JS starts, as an $exception event with level fatal. On iOS
the reporter is KSCrash (signals, Mach exceptions, NSExceptions, C++
exceptions), and MetricKit crash, hang and CPU diagnostics (levels error and
warning) come with it by default; on Android it is
the Android SDK's crash reporter, with an optional ANR
watchdog. Add the config plugin and turn it on in init():
json"plugins": [["@cohorly/react-native", { "projectToken": "YOUR_PROJECT_TOKEN", "apiHost": "https://YOUR_COHORLY_API" }]]
tsinit({ token: "YOUR_PROJECT_TOKEN", storage: AsyncStorage, nativeCrashReporting: true });
- The plugin writes the project token and API host where the native startup
code reads them (Info.plist on iOS,
<application>meta-data on Android), so a crash before the JS bundle loads is still sent. Both props are required. On iOS it also callsCohorlyNativeCrashes.start()first thing in the AppDelegate'sapplication(_:didFinishLaunchingWithOptions:)and forwardsapplication(_:handleEventsForBackgroundURLSession:completionHandler:)toCohorlyNativeCrashes.handleEventsForBackgroundURLSession(_:completionHandler:).androidAnrWatchdog: trueturns on the Android ANR watchdog. Without Expo, make the same changes by hand, AppDelegate calls included. Native modules need a rebuild, not an OTA update. - The switch applies to the next launch: JS writes
nativeCrashReporting, the release, dist and identity on every launch and onidentify()/reset(), and native code reads them before JS exists. It is on only whileisExceptionCaptureEnabled()is true, socaptureExceptions: falseoroptOutTracking()turn it off. Defaultfalse. - Never run two native crash handlers: in apps that also use Sentry, set
enableNativeCrashHandling: falseinSentry.initfirst, or use the MetricKit-only mode below. - Upload dSYMs (iOS) and R8 mappings (Android) for every build, with the same release the SDK sends, so native frames are symbolicated.
MetricKit without KSCrash (iOS)
metricKitDiagnostics turns MetricKit on or off on its own. It defaults to the
value of nativeCrashReporting, so an app that only sets
nativeCrashReporting: true keeps both, and an app that sets neither has both
off.
nativeCrashReporting | metricKitDiagnostics | Next launch on iOS |
|---|---|---|
true | omitted or true | KSCrash and MetricKit |
true | false | KSCrash only |
false or omitted | true | MetricKit only |
false or omitted | omitted or false | nothing |
The MetricKit-only mode installs no signal handler, so it can run alongside
another native crash reporter, such as Sentry with
enableNativeCrashHandling: true. It still sends the crashes KSCrash recorded
on earlier launches, so moving a device out of KSCrash loses nothing. A typical
cohort split keeps MetricKit for everyone and KSCrash for part of the users,
with Sentry handling native crashes for the rest:
tsinit({token: "YOUR_PROJECT_TOKEN",storage: AsyncStorage,nativeCrashReporting: inCohort,metricKitDiagnostics: true,});
On Android metricKitDiagnostics is ignored: nativeCrashReporting alone
decides.
Changing it after init
Feature flags usually arrive after init(). setNativeCrashReporting() changes
either option later; an omitted key keeps its current value. Like the options,
it takes effect on the next launch: it rewrites the config native code reads
at startup, but never installs or removes a handler in the running process.
The value is persisted: on the next launch it is the starting value of every
key init() leaves out, so a launch that ends before its flags arrive keeps
the previous launch's choice instead of writing the default. Leave the option
out of init() for a key you drive with the setter (an explicit init() value
wins over the persisted one until the setter runs again), and call it on every
launch once the flags are known:
tsimport { init, onFeatureFlags, isFeatureEnabled, setNativeCrashReporting } from "@cohorly/react-native";init({ token: "YOUR_PROJECT_TOKEN", storage: AsyncStorage, metricKitDiagnostics: true });onFeatureFlags(() => {setNativeCrashReporting({ nativeCrashReporting: isFeatureEnabled("ks_crash") });});// Kill switch: no KSCrash from the next launch, MetricKit kept.setNativeCrashReporting({ nativeCrashReporting: false });
Called before the client is ready, it only records the values and the first
write already uses them. disabled, optOutTracking() and
captureExceptions: false keep both off whatever it says.
Not covered yet: JS errors thrown before init() runs.