Source code: statsig-io/js-client-monorepo
Setup the SDK
1
Install the SDK
Peer Dependencies
The@statsig/expo-bindings
package has peer dependencies which may also need to be installed if they are not already in your project.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.The setup for an Expo environment is very similar to a plain React environment.
The only difference is that you need to use the Expo specific
StatsigProviderExpo
.
This automatically switches out the storage layer used by the SDK, utilizing AsyncStorage instead of LocalStorage (which isn’t available in RN environments).Avoid iOS 18.4 on Simulator: Apple introduced a networking bug in iOS 18.4 that causes requests to fail when running in the Simulator. For more details, see this thread on Apple’s forums.
Use the SDK
You can get an instance of the StatsigClient to check gates, experiments, dynamic configs, layers, and log events.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.
You can evaluate a gate by getting the client with the useStatsigClient
hook, and then calling checkGate
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: You can get a DynamicConfig value by getting the client with theuseStatsigClient
hook, and then calling getConfig
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. You can access the experiment variant and parameters for the user by getting the client with theuseStatsigClient
hook, and then calling getExperiment
.
useStatsigClient
hook, and then calling getLayer
.
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: You can get the client with theuseStatsigClient
hook, and then call logEvent
Loading State
Dependent on your setup, you may want to wait for the latest values before checking a gate or experiment. If you are usingStatsigProviderExpo
, you can pass in a loadingComponent
prop to display a loading state while the SDK is initializing.
If you are using the useClientAsyncInitExpo
hook, you can check the isLoading
prop to determine if the SDK is still loading.
- StatsigProviderExpo
- useClientAsyncInitExpo
Advanced
Expo Without React
In some scenarios, you may need to use theStatsigClient
when you are not in the React component tree. Things like background tasks or handling notifications.
For these, you can use the Expo specific StatsigClientExpo
.
Synchronous Storage with MMKV
If you are utilizing MMKV in your project, and would prefer to use that instead of the default (AsyncStorage). You can provide you ownStorageProvider
via StatsigOptions
.
Something like:
Debugging
Network Issues
Some users have reported aERROR: A networking error occured during POST request
messages when first initializing Statsig. This issue is solved in releases of the SDK after 3.1.0, so upgrading your SDK should solve the issue. If it persists, reach out to us in Slack.