Next.js experiments installation
Install the PostHog JavaScript library using your package manager: Add your PostHog API key and host to your Choose the integration method based on your Next.js version and router type. If you're using Next.js 15.3+, you can use For the App router, create a Then import the For the Pages router, integrate PostHog at the root of your app in The Experiments run on top of our feature flags. You can define which version of your code runs based on the return value of the feature flag: For client-side experiments in React components, you can use the For server-side experiments in API routes or server actions, use Once you've implemented the feature flag in your code, you'll enable it for a target audience by creating a new experiment in the PostHog dashboard.Install the package
RequiredAdd environment variables
Required.env.local file and to your hosting provider (e.g. Vercel, Netlify). These values need to start with NEXT_PUBLIC_ to be accessible on the client-side.Initialize PostHog
Requiredinstrumentation-client.ts for a lightweight, fast integration:providers.tsx file in your app folder. The posthog-js library needs to be initialized on the client-side using the 'use client' directive:PostHogProvider component in your app/layout.tsx and wrap your app with it:pages/_app.tsx:defaults option automatically configures PostHog with recommended settings for new projects. See SDK defaults for details.Implement your experiment
RequireduseFeatureFlagVariantKey hook or the PostHogFeature component:posthog-node:Run your experiment
RequiredNext steps
RecommendedResource Description Creating an experiment How to create an experiment in PostHog Adding experiment code How to implement experiments for all platforms Statistical significance Understanding when results are meaningful Experiment insights How to analyze your experiment data More tutorials Other real-world examples and use cases