Skip to main content

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.
1

Set environment variables

Add the keys to your .env.local file:
.env.local
See the User (StatsigUser) doc for more info on the user property. From here, you’re ready to start checking gates & experiments and sending events in any sub-file of 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 (think return false;) by default.

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:

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.

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.

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:

Session Replay

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.

Advanced Setup

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.
loggingEnabled
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.
disableLogging
boolean
deprecated
Use loggingEnabled: 'disabled' instead.
disableStableID
boolean
default:"false"
Skip generating a device-level Stable ID.
disableEvaluationMemoization
boolean
default:"false"
Recompute every evaluation instead of using the memoized result.
initialSessionID
string
Override the generated session ID.
enableCookies
boolean
default:"false"
Persist Stable ID in cookies for cross-domain tracking.
disableStorage
boolean
Prevent any local storage writes (disables caching).
networkConfig
NetworkConfig
Override network endpoints per request type.
environment
StatsigEnvironment
Set environment-wide defaults (for example { tier: 'staging' }).
logLevel
LogLevel
default:"Warn"
Console verbosity.
loggingBufferMaxSize
number
default:"50"
Max events per log batch.
loggingIntervalMs
number
default:"10_000"
Interval between automatic flushes.
overrideAdapter
OverrideAdapter
Modify evaluations before returning them.
includeCurrentPageUrlWithEvents
boolean
default:"true"
Attach the current page URL to logged events.
disableStatsigEncoding
boolean
default:"false"
Send requests without Statsig-specific encoding.
logEventCompressionMode
LogEventCompressionMode
default:"Enabled"
Control compression for batched events.
disableCompression
boolean
deprecated
Use logEventCompressionMode instead.
dataAdapter
EvaluationsDataAdapter
Provide a custom data adapter to control caching/fetching.
customUserCacheKeyFunc
CustomCacheKeyGenerator
Override cache key generation for stored evaluations.
api
string
default:"https://api.statsig.com"
Base URL for all requests (append /v1).
logEventUrl
string
default:"https://prodregistryv2.org/v1/rgstr"
Endpoint for event uploads.
logEventFallbackUrls
string[]
Fallback endpoints for event uploads.
networkTimeoutMs
number
default:"10000"
Request timeout in milliseconds.
preventAllNetworkTraffic
boolean
Disable all outbound requests; combine with loggingEnabled: 'disabled' to silence log warnings.
networkOverrideFunc
function
Provide custom transport (e.g., Axios).
initializeUrl
string
default:"https://featureassets.org/v1/initialize"
Endpoint for initialization requests.

Additional Resources