React experiments installation
Install the PostHog JavaScript library using your package manager: Add your PostHog API key and host to your environment variables. For Vite-based React apps, use the Wrap your app with the The Use the You can also import 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 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
RequiredVITE_PUBLIC_ prefix:Initialize PostHog
RequiredPostHogProvider component at the root of your application (such as main.tsx if you're using Vite):defaults option automatically configures PostHog with recommended settings for new projects. See SDK defaults for details.Accessing PostHog in your code
RecommendedusePostHog hook to access the PostHog instance in any component wrapped by PostHogProvider:posthog directly for non-React code or utility functions:Implement your experiment
RequireduseFeatureFlagVariantKey hook or the PostHogFeature component: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