Paste this prompt into your AI coding assistant to scaffold the integration.
Copy
You are a frontend engineer integrating the Statsig SDK into a React app. Follow these instructions carefully:1. Install the required Statsig packages: npm install @statsig/react-bindings @statsig/session-replay @statsig/web-analytics2. In the main component file (`App.jsx` or `App.tsx`): - Import `StatsigProvider` and `useClientAsyncInit` from `@statsig/react-bindings` - Import `StatsigAutoCapturePlugin` from `@statsig/web-analytics` and `StatsigSessionReplayPlugin` from `@statsig/session-replay` - Initialize the SDK using your client key: 'YOUR-CLIENT-API-KEY' - Use `userID` from an existing variable if it's already declared in the file; otherwise, default to `'a-user'` - Wrap the existing app content inside `<StatsigProvider>`, using `<div>Loading...</div>` as the `loadingComponent`3. DO NOT remove any existing JSX content from the component. Just wrap it.4. Here is what the final file structure should look like: import { StatsigProvider, useClientAsyncInit } from '@statsig/react-bindings'; import { StatsigAutoCapturePlugin } from '@statsig/web-analytics'; import { StatsigSessionReplayPlugin } from '@statsig/session-replay'; import YourApp from './YourApp'; function App() { const id = typeof userID !== 'undefined' ? userID : 'a-user'; const { client } = useClientAsyncInit( 'YOUR-CLIENT-API-KEY', { userID: id }, { plugins: [new StatsigAutoCapturePlugin(), new StatsigSessionReplayPlugin()] } ); return ( <StatsigProvider client={client} loadingComponent={<div>Loading...</div>}> <YourApp /> </StatsigProvider> ); }5. Ask the user to provide their CLIENT-API-KEY and insert it where prompted above.
Add @statsig/session-replay and @statsig/web-analytics if you plan to enable Session Replay or Auto Capture.
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.
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.
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:
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.
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 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.
Hooks that read gates, configs, experiments, or layers will log exposures on render. Use useStatsigClient to defer checks until you actually change the UI.
Keep experiment variants stable across rerenders or user transitions by plugging in persistent storage. The React integration mirrors the JavaScript workflow and you can adapt the Next.js sample to your setup.Read more in Client Persistent Assignment.