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.Controls logging behavior.
browser-only(default): log events from browser environments.disabled: never send events.always: log in every environment, including non-browser contexts.
Use
loggingEnabled: 'disabled' instead.Skip generating a device-level Stable ID.
Recompute every evaluation instead of using the memoized result.
Override the generated session ID.
Persist Stable ID in cookies for cross-domain tracking.
Prevent any local storage writes (disables caching).
Override network endpoints per request type.
Set environment-wide defaults (for example
{ tier: 'staging' }).Console verbosity.
Max events per log batch.
Interval between automatic flushes.
Modify evaluations before returning them.
Attach the current page URL to logged events.
Send requests without Statsig-specific encoding.
Control compression for batched events.
Use
logEventCompressionMode instead.Provide a custom data adapter to control caching/fetching.
Override cache key generation for stored evaluations.
Network Config Options
Network Config Options
Base URL for all requests (append
/v1).Endpoint for event uploads.
Fallback endpoints for event uploads.
Request timeout in milliseconds.
Disable all outbound requests; combine with
loggingEnabled: 'disabled' to silence log warnings.Provide custom transport (e.g., Axios).
Endpoint for initialization requests.