Source code: statsig-io/js-client-monorepo
Set Up the SDK
This page explains 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
1
Set environment variables
Add the keys to your .env.local file:
.env.local
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
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
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
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
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
Session Replay
- App Router
Web Analytics / Auto Capture
By including the@statsig/web-analytics package in your project, you can automatically capture common web events like clicks and page views.
For more information on filtering events, enabling console log capture, and other configuration options available in web analytics, see the Web Analytics Configuration documentation.
- App Router
Advanced Setup
Client Bootstrapping (Recommended)
- App Router
app/api/statsig-bootstrap/route.ts
Proxying Network Traffic (Optional)
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
This page explains 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
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
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.