Get started with the Statsig SDK
This quickstart will guide you through quickly installing the Statsig SDK in a client app. If you're looking for a more detailed guide, check out the SDK Overview or read about choosing between client or server SDKs.
JS snippet
React
Python
Node
+24 more SDKs+24 SDKs
1. Install Statsig packages
npm install @statsig/react-bindings
2. Wrap child components
Next, update your app's default function so that the StatsigProvider wraps around all child components.
import { StatsigProvider } from "@statsig/react-bindings";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<StatsigProvider
sdkKey={"client-MY-STATSIG-CLIENT-KEY"} //
user={{ userID: "quickstart-user" }}
loadingComponent={<div>Loading...</div>}>
{children}
</StatsigProvider>
);
}
3. Add client key
Create a client API key in the Statsig console Settings. Copy and paste it to replace <REPLACE_WITH_YOUR_CLIENT_KEY>
in the code snippet from the previous step.
4. Basic Usage
- Check a Gate
- Get an Experiment Value
- Log an Event
First, create a gate on the Feature Gates page in console, then check it in-code:
const { client } = useStatsigClient();
return (
<div>Gate is {client.checkGate('check_user') ? 'passing' : 'failing'}.</div>
);
First, create an Experiment on the Experiments page in console
const { client } = useStatsigClient();
const experiment = client.getExperiment('my_experiment_name');
return (
<div>Headline Parameter: {experiment.get('my_experiment_parameter_name', 'fallback_value')}.</div>
);
You can use Events to power metrics in your experiment or gates. Events don't need to be set up in console first, just add to your code:
const { client } = useStatsigClient();
return <button onClick={() => client.logEvent("button_click")}>Click Me</button>
5. Next steps
Congratulations! You've successfully set up the Statsig SDK in React. Continue on to the tutorials, or jump in to the full Next.js or React SDK libraries.
The easiest way to get up and running is by adding the Statsig JavaScript snippet to your website.
1. Paste the code snippet
In the <head>
section of your website, paste the following code snippet:
<script src="https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js?apikey=<REPLACE_WITH_YOUR_CLIENT_KEY>"></script>
2. Add client key
Create a client API key in the Statsig console Settings. Copy and paste it to replace <REPLACE_WITH_YOUR_CLIENT_KEY>
in the code snippet from the previous step.
3. Basic usage
- Check a Gate
- Get an Experiment Value
- Log an Event
First, create a gate on the Feature Gates page in console, then check it in-code:
window.Statsig.instance().checkGate("my_feature_gate_name");
You'll want to wait for the SDK to initialize before checking a gate to ensure it has fresh values, one way to accomplish this is waiting for the "values_updated" event.
First, create an Experiment on the Experiments page in console
window.Statsig.instance().getExperiment("my_experiment_name").get('my_experiment_parameter_name');
You'll want to wait for the SDK to initialize before getting an experiment to ensure it has fresh values, one way to accomplish this is waiting for the "values_updated" event.
You can use Events to power metrics in your experiment or gates. Events don't need to be set up in console first, just add to your code:
window.Statsig.instance().logEvent("my_checkout_event_name", "event_value_item_1234", {"event_metadata": "my_metadata"})
4. Next steps
Congratulations! You've set up the Statsig JavaScript snippet. You can now start:
- Start recording events
- Watch session replays
- Run experiments
- Use feature flags
See the Javascript SDK reference for more info.
1. Install Statsig packages
pip install statsig-python-core
2. Initialize the Statsig SDK
from statsig_python_core import Statsig, StatsigOptions
options = StatsigOptions()
options.environment = "development"
statsig = Statsig("<REPLACE_WITH_YOUR_SERVER_SECRET_KEY>", options)
statsig.initialize().wait()
statsig.shutdown().wait()
3. Add server secret key
Create a server secret key in the Statsig console Settings. Copy and paste it to replace <REPLACE_WITH_YOUR_SERVER_SECRET_KEY>
in the code snippet from the previous step.