Source code: statsig-io/js-client-monorepo
Set Up the SDK
1
Install the SDK
If you need a starter project, follow the official React quickstart. Looking for Next.js instead? See the Next.js SDK docs.
AI-powered Setup
Setup Statsig in 90 seconds by copying this AI prompt into your IDE:Install Packages
- npm
- yarn
2
Initialize the SDK
Next, initialize the SDK with a client SDK key from the “API Keys” tab on the Statsig console. These keys are safe to embed in a client application.Along with the key, pass in a User Object with the attributes you’d like to target later on in a gate or experiment.Wrap Your App With
Provide your client SDK key and initial user when you render the provider.
Wrap Your App With StatsigProvider
Provide your client SDK key and initial user when you render the provider.Typical Project Structure
Most projects render a root component inside the provider.Need to balance startup speed with freshness? Review Initialization Strategies for bootstrap and async options.
Use the SDK
UseuseStatsigClient inside components to retrieve the client when you need to evaluate something.
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.
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.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: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.Manage Users
Updating User Properties
Switch identities when a user logs in or when you collect richer attributes.Loading State
Wait for the latest values during initialization with either the provider or the async hook.- StatsigProvider
- useClientAsyncInit
React Hooks
Feature Gate Hooks
- Recommended:
useStatsigClient().checkGatelogs when invoked. useGateValuereturns the boolean value and logs immediately.useFeatureGatereturns the full gate object with details.
Dynamic Config Hooks
- Recommended:
useStatsigClient().getDynamicConfigdefers exposure until called. useDynamicConfiglogs on render.
Experiment Hooks
- Recommended:
useStatsigClient().getExperimentto control exposures. useExperimentlogs on render.
Layer Hooks
Layers only log exposures when you call.get().
Parameter Store Hooks
Log Events From Hooks
StatsigUser Hook
Direct Access to the Client
Client Initialization Hooks
useClientAsyncInit— fetches the latest values before rendering.useClientBootstrapInit— bootstrap from server-provided values.
You can also initialize your own client instance manually. See Initialization Strategies for alternatives.
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.
Testing
Mock Statsig hooks in Jest to isolate component logic.Session Replay
Install@statsig/session-replay and register the plugin to record user sessions.
Web Analytics / Auto Capture
Capture clicks and page views automatically with@statsig/web-analytics.