Source code: statsig-io/js-client-monorepo
Set Up the SDK
AI-powered Setup
Setup Statsig in 90 seconds by copying this AI prompt into your IDE:Prompt
Manual Setup
Statsig supports both Page Router & App Router, with some differences in integration patterns.- App Router
- Page Router
1
Set environment variables
Add the keys to your .env.local file:
.env.local
2
Install packages
For App Router, install the @statsig/next package:
3
Add the StatsigBootstrapProvider
The <StatsigBootstrapProvider> creates both a Statsig Client and Server instance under the hood, and “bootstraps” the client so it can render each page without a blocking network request. this will keep your app speedy and is recommended for most users. If you need more control over your setup, our Bootstrapping and React docs can provide more guidance.Add this component around the content in your root
layout.tsx file:app/layout.tsx
Use the SDK
Checking a Feature Flag/Gate
Now that your SDK is initialized, let’s check a Feature Gate. Feature Gates can be used to create logic branches in code that can be rolled out to different users from the Statsig Console. Gates are always CLOSED or OFF (thinkreturn false;) by default.
- App Router
- Page Router
In an App Router app, you need to use the
use client directive to ensure your logic runs on the frontend.Reading a Dynamic Config
Feature Gates can be very useful for simple on/off switches, with optional but advanced user targeting. However, if you want to be able send a different set of values (strings, numbers, and etc.) to your clients based on specific user attributes, e.g. country, Dynamic Configs can help you with that. The API is very similar to Feature Gates, but you get an entire json object you can configure on the server and you can fetch typed parameters from it. For example:- App Router
- Page Router
In an App Router app, you need to use the
use client directive to ensure your logic runs on the frontend.Getting a Layer/Experiment
Then we have Layers/Experiments, which you can use to run A/B/n experiments. We offer two APIs, but we recommend the use of layers to enable quicker iterations with parameter reuse.- App Router
- Page Router
In an App Router app, you need to use the
use client directive to ensure your logic runs on the frontend.Parameter Stores
Parameter Stores hold a set of parameters for your mobile app. These parameters can be remapped on-the-fly from a static value to a Statsig entity (Feature Gates, Experiments, and Layers), so you can decouple your code from the configuration in Statsig. Read more about Param Stores here.- App Router
- Page Router
In an App Router app, you need to use the
use client directive to ensure your logic runs on the frontend.Logging an Event
Now that you have a Feature Gate or an Experiment set up, you may want to track some custom events and see how your new features or different experiment groups affect these events. This is super easy with Statsig - simply call the Log Event API for the event, and you can additionally provide some value and/or an object of metadata to be logged together with the event:- App Router
- Page Router
In an App Router app, you need to use the
use client directive to ensure your logic runs on the frontend.Session Replay
- App Router
- Page Router
Web Analytics / Auto Capture
- App Router
- Page Router
Stable ID
Stable ID provides a consistent device identifier. It lets you run logged-out experiments and target gates at the device level.How Stable ID Works
- On first initialization the SDK generates a Stable ID and stores it in
localStorageunderstatsig.stable_id.<SDK_KEY_HASH>. - Subsequent sessions reuse the stored value. Each client SDK key has its own Stable ID entry.
- Local storage is scoped per domain, so cross-domain usage requires sharing the value manually (see below).
Reading the Stable ID
- JavaScript
- React
Overriding the Stable ID
Provide a custom Stable ID throughStatsigUser.customIDs.stableID if you already manage a durable device identifier.
- JavaScript
- React
When you override the Stable ID it is persisted to local storage, so subsequent sessions reuse your custom value.
Sharing Stable ID Across Subdomains
Add this helper script before initializing the SDK and then copy the stored value onto your user object.Aligning Stable ID Between Client and Server
To share Stable ID with a backend Statsig SDK, send the value with requests and persist it server-side when missing. The server can bootstrap the client with the same Stable ID.Advanced Setup
Client Bootstrapping (Recommended)
- App Router
- Page Router
app/api/statsig-bootstrap/route.ts
app/layout.tsx
Proxying Network Traffic (Optional)
- App Router
- Page Router
app/statsig-proxy/initialize/route.ts
app/statsig-proxy/log_event/route.ts
Statsig Site Generation (SSG)
Vercel’s Static Site Generation renders HTML at build time. Because static HTML can’t be responsive to per-user values, experimenting on SSG content requires one of these patterns:- Use Vercel Edge Middleware with Statsig’s Edge Config Adapter for zero-latency redirects.
- Isolate Statsig usage to hydrated client components only.
Statsig Options
LoggingEnabledOption
default:"browser-only"
Controls logging behavior.
browser-only(default): log events from browser environments.disabled: never send events.always: log in every environment, including non-browser contexts.
boolean
deprecated
deprecated
Use
loggingEnabled: 'disabled' instead.boolean
default:"false"
Skip generating a device-level Stable ID.
boolean
default:"false"
Recompute every evaluation instead of using the memoized result.
string
Override the generated session ID.
boolean
default:"false"
Persist Stable ID in cookies for cross-domain tracking.
boolean
Prevent any local storage writes (disables caching).
NetworkConfig
Override network endpoints per request type.
StatsigEnvironment
Set environment-wide defaults (for example
{ tier: 'staging' }).LogLevel
default:"Warn"
Console verbosity.
number
default:"50"
Max events per log batch.
number
default:"10_000"
Interval between automatic flushes.
OverrideAdapter
Modify evaluations before returning them.
boolean
default:"true"
Attach the current page URL to logged events.
boolean
default:"false"
Send requests without Statsig-specific encoding.
LogEventCompressionMode
default:"Enabled"
Control compression for batched events.
boolean
deprecated
deprecated
Use
logEventCompressionMode instead.EvaluationsDataAdapter
Provide a custom data adapter to control caching/fetching.
CustomCacheKeyGenerator
Override cache key generation for stored evaluations.
Network Config Options
Network Config Options
string
default:"https://api.statsig.com"
Base URL for all requests (append
/v1).string
default:"https://prodregistryv2.org/v1/rgstr"
Endpoint for event uploads.
string[]
Fallback endpoints for event uploads.
number
default:"10000"
Request timeout in milliseconds.
boolean
Disable all outbound requests; combine with
loggingEnabled: 'disabled' to silence log warnings.function
Provide custom transport (e.g., Axios).
string
default:"https://featureassets.org/v1/initialize"
Endpoint for initialization requests.