Skip to main content
@traffical/react provides a context provider and hooks for resolving parameters and tracking events in React apps. It wraps @traffical/js-client, so everything the browser client supports works here too.

Installation

Setup

Wrap your app with TrafficalProvider:
Use an SDK key (traffical_sk_..., scopes sdk:read+sdk:write) in browser code — it is browser-safe.

The useTraffical hook

Use useTraffical in any component to resolve parameters and track events:

Hook options

Hook return value

Tracking modes

"decision" is useful when the variant is below the fold — you want to record the decision but only count exposure once the user actually sees the change.

Setting context

Pass user context through the provider; it flows to all hooks:
Context is supplied via contextFn inside config — a function called on each resolution. When the values it returns change (e.g. after login), hooks re-resolve with the new values. To customize the unit key, pass unitKeyFn inside config as well.

Anonymous users and identify

The React SDK inherits the browser client’s stable-ID handling. Before the user logs in, the SDK uses an auto-generated stable ID. After login:

SSR (Next.js, RSC)

For Next.js (App Router or Pages), fetch the bundle on the server and pass it to TrafficalProvider as localConfig. There is no @traffical/react/server entry point — fetch the config bundle directly from the SDK config endpoint (the same one the client SDK calls):
The same traffical_sk_... SDK key is browser-safe, so there is no separate server secret for the SDK path. See the SSR patterns page for the full setup, including the Pages Router.

Standalone tracking

If you only need to track an event, use useTrafficalTrack:

Direct client access

If you need to do something the hooks don’t expose, get the underlying client. useTrafficalClient() returns { client, ready, error } — the client can be null until the provider has initialized, so guard it:

Provider options

TrafficalProvider takes exactly two props: config and children. Everything else is a field of config (TrafficalProviderConfig): These are the same options as the browser SDK; pass them all inside config.

Next steps

SSR patterns

No FOOC, hydration with the same bundle.

Canonical experiments

Patterns for web UI and SSR tests.