React experiments installation
- 1
Install PostHog React SDK
RequiredFor Next.js, we recommend following the Next.js integration guide instead.
- Install
posthog-jsand@posthog/reactusing your package manager:
- Add your environment variables to your
.env.localfile and to your hosting provider (e.g. Vercel, Netlify, AWS). You can find your project API key and host in your project settings. IncludingVITE_PUBLIC_in their names ensures they are accessible in the frontend.
.env.local- Integrate PostHog at the root of your app (such as
main.jsxif you are using Vite).
ReactDon't directly import PostHogDo not directly import
posthogapart from installation as shown above. This will likely cause errors as the library might not be initialized yet. Initialization is handled automatically when you use thePostHogProviderandusePostHoghook.Using React Router v7?
You need to set
posthog-jsand@posthog/reactas external packages in yourvite.config.tsfile to avoid SSR errors.vite.config.tsSee our Remix docs for more details.
- Install
- 2
Capture conversion event
RequiredOnce PostHog is initialized, you should be able to capture events.
For this tutorial, let's capture a conversion event on a
<button id="cta">click.JSX - 3
Create an experiment
RequiredGo to the Experiments tab in the PostHog app and click on the New experiment button in the top right.


For this tutorial, let's create a new experiment using simplified test values:
- Name: "Test experiment"
- Description: "This is a test experiment"
- Feature flag key: "test-experiment-ff-key"
- Experiment type: "Feature flag"
- Variants: "control" and "test"
- Participant type: "Users"
Then click Save as draft.
- 4
Add primary metric and launch
RequiredScroll down to the Primary metrics section and click + Add primary metric.
Choose Single-use and select Type > Mean.
Then search for the event
cta clickedunder Metric and click Save.

By default, experiments are exposed to 100% of users. You can customize release conditions to expose the experiment to a subset of users.
For this tutorial, we'll ship the experiment to all users and click Launch in the top right.
- 5
Call feature flag
RequiredUse the React hooks to evaluate the experiment flag and render the
<button id="cta">text based on the assigned variant.JSXNow when a user triggers a
cta clickedevent, PostHog automatically assigns the user to a variant and records an experiment exposure.By default, users are split equally between variants. If you want to assign specific users to a specific variant, see more about distribution and release conditions.
- 6
Evaluate experiment results
RecommendedAs you capture more
cta clickedevents, more exposures will populate the primary metrics in your experiment.

With enough data, you can analyze the experiment and its variants by:
- Conversion rates
- Statistical significance
- Credible intervals
- Chance to win %
- Minimum detectable effect
- And more