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 publishable SDK key (traffical_pk_...) in client code — it is safe to ship in a client bundle.

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 treatment is below the fold — you want to record the decision but only record the exposure event 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):
Use a publishable SDK key (traffical_pk_...) in the client provider and a server-side SDK key (traffical_sk_...) for the server-side bundle fetch, kept in a private environment variable. 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.

Experimentation patterns

Patterns for web UI and SSR tests.