React product analytics 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 Click around and view a couple pages to generate some events. PostHog automatically captures pageviews, clicks, and other interactions for you. If you'd like, you can also manually capture custom events: PostHog provides several hooks to make it easy to use feature flags in your React app. Use For multivariate flags, use The The The If your flag has a payload, you can pass a function to children whose first argument is the payload: Experiments run on top of our feature flags. Once you've implemented the flag in your code, you run an experiment 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:Send events
RecommendedUse feature flags
RequireduseFeatureFlagEnabled for boolean flags:Multivariate flags
useFeatureFlagVariantKey:Flag payloads
useFeatureFlagPayload hook does not send a $feature_flag_called event, which is required for experiments. Always use it with useFeatureFlagEnabled or useFeatureFlagVariantKey:PostHogFeature component simplifies code by handling feature flag related logic:match prop can be either true, or the variant key, to match on a specific variant. If you also want to show a default message, you can pass these in the fallback prop.Running experiments
OptionalNext steps
RecommendedResource Description Creating a feature flag How to create a feature flag in PostHog Adding feature flag code How to check flags in your code for all platforms Framework-specific guides Setup guides for React Native, Next.js, Flutter, and other frameworks How to do a phased rollout Gradually roll out features to minimize risk More tutorials Other real-world examples and use cases