> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # React Experiments installation Canonical URL: https://posthog.com/docs/experiments/installation/react > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.i.posthog.com` instead of `https://us.i.posthog.com` (ingestion host). ## On this page - [Install with the wizard](#install-with-the-wizard) - [Install the package](#install-the-package) - [Add environment variables](#add-environment-variables) - [Initialize PostHog](#initialize-posthog) - [Accessing PostHog in your code](#accessing-posthog-in-your-code) - [Implement your experiment](#implement-your-experiment) - [Run your experiment](#run-your-experiment) - [Next steps](#next-steps) ## Install with the wizard Run this command in your project directory to add PostHog to your React project. The wizard detects your framework, installs the SDK, and sets up your first events. `npx @posthog/wizard` Or set it up manually by following the rest of this guide. 1. 1 ## Install the package Required Install [`posthog-js`](https://github.com/posthog/posthog-js) and `@posthog/react` using your package manager: ### npm ```bash npm install posthog-js@1 @posthog/react@1 ``` ### yarn ```bash yarn add posthog-js@1 @posthog/react@1 ``` ### pnpm ```bash pnpm add posthog-js@1 @posthog/react@1 ``` ### bun ```bash bun add posthog-js@1 @posthog/react@1 ``` 2. 2 ## Add environment variables Required Add your PostHog project token and host to your environment variables. For Vite-based React apps, use the `VITE_` prefix to expose them to the client: .env ```bash VITE_POSTHOG_PROJECT_TOKEN= VITE_POSTHOG_HOST=https://us.i.posthog.com ``` 3. 3 ## Initialize PostHog Required Wrap your app with the `PostHogProvider` component at the root of your application (such as `main.tsx` if you're using Vite): main.tsx ```jsx import { StrictMode } from 'react' import { createRoot } from 'react-dom/client' import './index.css' import App from './App.jsx' import { PostHogProvider } from '@posthog/react' const options = { api_host: import.meta.env.VITE_POSTHOG_HOST, defaults: '2026-08-30', } as const createRoot(document.getElementById('root')).render( ) ``` **defaults option** The `defaults` option automatically configures PostHog with recommended settings for new projects. See [SDK defaults](https://posthog.com/docs/libraries/js.md#sdk-defaults) for details. 4. 4 ## Accessing PostHog in your code Recommended Use the `usePostHog` hook to access the PostHog instance in any component wrapped by `PostHogProvider`: MyComponent.tsx ```jsx import { usePostHog } from '@posthog/react' function MyComponent() { const posthog = usePostHog() function handleClick() { posthog.capture('button_clicked', { button_name: 'signup' }) } return } ``` You can also import `posthog` directly for non-React code or utility functions: utils/analytics.ts ```jsx import posthog from 'posthog-js' export function trackPurchase(amount: number) { posthog.capture('purchase_completed', { amount }) } ``` 5. 5 ## 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. You can use the `useFeatureFlagVariantKey` hook or the `PostHogFeature` component: ```jsx // 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/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/react' function App() { return (
{/* the component to show */}
) } // You can also test your code by overriding the feature flag: posthog.featureFlags.overrideFeatureFlags({ flags: {'your-experiment-feature-flag': 'test'} }) ``` 6. 6 ## 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. 7. 7 ## Next steps Recommended Now that you're running experiments, continue with the resources below to learn what else Experiments enables within the PostHog platform. | Resource | Description | | --- | --- | | [Creating an experiment](https://posthog.com/docs/experiments/creating-an-experiment.md) | How to create an experiment in PostHog | | [Adding experiment code](https://posthog.com/docs/experiments/adding-experiment-code.md) | How to implement experiments for all platforms | | [Statistical significance](https://posthog.com/docs/experiments/statistics-bayesian.md) | Understanding when results are meaningful | | [Experiment insights](https://posthog.com/docs/experiments/analyzing-results.md) | How to analyze your experiment data | | [More tutorials](https://posthog.com/docs/experiments/tutorials.md) | Other real-world examples and use cases |