Setup Statsig in 90 seconds using one of these two options:
Wizard
Run this wizard command in your terminal
Copy
npx @statsig/wizard@latest
(Or) AI Prompt
Copy this pre-built prompt in your IDE
Copy
# Statsig SDK Integration for Next.jsYou are a frontend engineer integrating the Statsig SDK into a **Next.js application**. Follow all steps below one by one:---## Full Integration Instructions1. **Detect the package manager** by checking for: - `package-lock.json` → use `npm` - `yarn.lock` → use `yarn` - `pnpm-lock.yaml` → use `pnpm`2. **Detect the Next.js router type** by checking for: - `app/` directory → **App Router** - `pages/` directory → **Pages Router**3. **Install the Statsig package** using the correct package manager:# For npmnpm install @statsig/react-bindings @statsig/session-replay @statsig/web-analytics# For yarnyarn add @statsig/react-bindings @statsig/session-replay @statsig/web-analytics# For pnpmpnpm add @statsig/react-bindings @statsig/session-replay @statsig/web-analytics4. Add your Statsig client key to .env.local:NEXT_PUBLIC_STATSIG_CLIENT_KEY=ask the user for their CLIENT KEY and use that input5. Integrate Statsig into the app (auto-detect router type):### If the project uses the App Router (has an app/ directory):// Create app/my-statsig.tsx"use client";import React from "react";import { LogLevel, StatsigProvider } from "@statsig/react-bindings";export default function MyStatsig({ children }: { children: React.ReactNode }) { const id = typeof userID !== "undefined" ? userID : "a-user"; const user = { userID: id, // Optional additional fields: // email: 'user@example.com', // customIDs: { internalID: 'internal-123' }, // custom: { plan: 'premium' } }; return ( <StatsigProvider sdkKey={process.env.NEXT_PUBLIC_STATSIG_CLIENT_KEY!} user={user} options={{ logLevel: LogLevel.Debug }} > {children} </StatsigProvider> );}// Update app/layout.tsx to wrap children with MyStatsigimport MyStatsig from "./my-statsig";export default function RootLayout({ children,}: Readonly<{ children: React.ReactNode }>) { return ( <html lang="en"> <body> <MyStatsig> {children} {/* Preserve all existing layout content */} </MyStatsig> </body> </html> );}### If the project uses the Pages Router (has a pages/ directory):// Update pages/_app.tsximport type { AppProps } from "next/app";import { LogLevel, StatsigProvider } from "@statsig/react-bindings";export default function App({ Component, pageProps }: AppProps) { const id = typeof userID !== "undefined" ? userID : "a-user"; const user = { userID: id, // Optional additional fields: // email: 'user@example.com', // customIDs: { internalID: 'internal-123' }, // custom: { plan: 'premium' } }; return ( <StatsigProvider sdkKey={process.env.NEXT_PUBLIC_STATSIG_CLIENT_KEY!} user={user} options={{ logLevel: LogLevel.Debug }} > <Component {...pageProps} /> {/* Preserve all existing pages */} </StatsigProvider> );}### Final Notes- The system must **detect the router type** and **apply the correct integration automatically**.- **Do not remove or change any existing JSX or layout structure** — only wrap the app with `StatsigProvider`.- **Preserve the file's language**: if it's TypeScript (`.tsx`), keep it as TypeScript; if it's JavaScript (`.jsx`), keep it as JavaScript.- After these steps, Statsig will be integrated across the entire app, with **feature gates, configs, and experiments** available everywhere.
2
Initialize the SDK
Statsig supports both Page Router & App Router, with some differences in integration patterns.
App Router
Page Router
First, add the keys to your .env.local file:
.env.local
Copy
# the NEXT_PUBLIC_ prefix is required for this to be available on the client sideNEXT_PUBLIC_STATSIG_CLIENT_KEY=client-<REPLACE_WITH_YOUR_CLIENT_KEY>STATSIG_SERVER_KEY=secret-<REPLACE_WITH_YOUR_SERVER_KEY>
For App Router, install the @statsig/next package:
Copy
npm i @statsig/next
The <StatsigBootstrapProvider> creates both a Statsig Client and Server instance under the hood, and “bootstraps” the client so it can render each page without a blocking network request. this will keep your app speedy and is recommended for most users. If you need more control over your setup, our Bootstrapping and React docs can provide more guidance.Add this component around the content in your root layout.tsx file:
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!
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.
App Router
Page Router
Copy
'use client';import { useExperiment, useLayer} from "@statsig/react-bindings";export default function Home() { const layer = useLayer("my_experiment_layer"); // or const experiment = useExperiment("my_experiment"); return ( <div> Title: {layer.get('title', 'Fallback Title')} {/* or */} Title: {experiment.get('title', 'Fallback Title')} </div> );}
In an App Router app, you need to use the use client directive to ensure your logic runs on the frontend.
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.
App Router
Page Router
Copy
'use client';import { useParameterStore} from "@statsig/react-bindings";export default function Home() { const store = useParameterStore("my_param_store"); return ( <div> Title: {store.get('title', 'Fallback Title')} </div> );}
In an App Router app, you need to use the use client directive to ensure your logic runs on the frontend.
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:
App Router
Page Router
Copy
'use client';import { useStatsigClient } from "@statsig/react-bindings";export default function Home() { const { client } = useStatsigClient(); return ( <div> <button onClick={() => client.logEvent("my_custom_event")}> Click Me </button> </div> );}
In an App Router app, you need to use the use client directive to ensure your logic runs on the frontend.
To share Stable ID with a backend Statsig SDK, send the value with requests and persist it server-side when missing. The server can bootstrap the client with the same Stable ID.
Copy
// Server: ensure Stable ID exists, then return initialize response for the clientconst values = Statsig.getClientInitializeResponse(user, YOUR_CLIENT_KEY, { hash: 'djb2',});// Client: apply the server-provided values and initialize synchronouslyconst { values, user: verifiedUser } = await fetch('/init-statsig-client', { method: 'POST', body: loadUserData(),}).then((res) => res.json());const myClient = new StatsigClient(YOUR_CLIENT_KEY, verifiedUser);myClient.dataAdapter.setData(values);myClient.initializeSync();
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.
Copy
// Create a single client and share it across multiple StatsigProvidersconst myStatsigClient = new StatsigClient(YOUR_SDK_KEY, user, options);await myStatsigClient.initializeAsync();<StatsigProvider client={myStatsigClient}> <YourComponent /></StatsigProvider><StatsigProvider client={myStatsigClient}> <AnotherComponent /></StatsigProvider>