Next.js experiments installation

  1. Install the package

    Required

    Install the PostHog JavaScript library using your package manager:

    npm install posthog-js
  2. Add environment variables

    Required

    Add your PostHog API key and host to your .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.

    .env.local
    NEXT_PUBLIC_POSTHOG_KEY=<ph_project_api_key>
    NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
  3. Initialize PostHog

    Required

    Choose the integration method based on your Next.js version and router type.

    If you're using Next.js 15.3+, you can use instrumentation-client.ts for a lightweight, fast integration:

    instrumentation-client.ts
    import posthog from 'posthog-js'
    posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
    api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
    defaults: '2025-11-30'
    })
    Defaults option

    The defaults option automatically configures PostHog with recommended settings for new projects. See SDK defaults for details.

  4. Implement your experiment

    Required

    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 useFeatureFlagVariantKey hook or the PostHogFeature component:

    // You can either use the useFeatureFlagVariantKey hook,
    // or you can use the feature flags component - https://posthog.com/docs/libraries/react#feature-flags-react-component
    // Method one: using the useFeatureFlagVariantKey hook
    import { useFeatureFlagVariantKey } from 'posthog-js/react'
    function App() {
    const variant = useFeatureFlagVariantKey('your-experiment-feature-flag')
    if (variant === 'test') {
    // do something
    }
    }
    // Method two: using the feature flags component
    import { PostHogFeature } from 'posthog-js/react'
    function App() {
    return (
    <PostHogFeature flag='your-experiment-feature-flag' match='test'>
    <div>
    {/* the component to show */}
    </div>
    </PostHogFeature>
    )
    }
    // You can also test your code by overriding the feature flag:
    posthog.featureFlags.overrideFeatureFlags({ flags: {'your-experiment-feature-flag': 'test'} })
  5. Run your experiment

    Required

    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.

  6. Next steps

    Recommended

    ResourceDescription
    Creating an experimentHow to create an experiment in PostHog
    Adding experiment codeHow to implement experiments for all platforms
    Statistical significanceUnderstanding when results are meaningful
    Experiment insightsHow to analyze your experiment data
    More tutorialsOther real-world examples and use cases

Community questions

Was this page useful?

Questions about this page? or post a community question.