:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useValues } from 'kea'
import { CodeSnippet, Language } from 'lib/components/CodeSnippet'
import { apiHostOrigin } from 'lib/utils/apiHost'
import SetupWizardBanner from 'scenes/onboarding/shared/SetupWizardBanner'
import { teamLogic } from 'scenes/teamLogic'
import { SDK_DEFAULTS_DATE } from '~/loadPostHogJS'
import { ReactInstallSnippet } from './js-web'
function ReactEnvVarsSnippet(): JSX.Element {
const { currentTeam } = useValues(teamLogic)
return (
<CodeSnippet language={Language.Bash}>
{[`VITE_POSTHOG_PROJECT_TOKEN=${currentTeam?.api_token}`, `VITE_POSTHOG_HOST=${apiHostOrigin()}`].join(
'\n'
)}
</CodeSnippet>
)
}
function ReactSetupSnippet(): JSX.Element {
return (
<CodeSnippet language={Language.JavaScript}>
{`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: '${SDK_DEFAULTS_DATE}',
} as const
createRoot(document.getElementById('root')).render(
<StrictMode>
<PostHogProvider apiKey={import.meta.env.VITE_POSTHOG_PROJECT_TOKEN} options={options}>
<App />
</PostHogProvider>
</StrictMode>
)`}
</CodeSnippet>
)
}
export function SDKInstallReactInstructions({ hideWizard }: { hideWizard?: boolean }): JSX.Element {
return (
<>
<SetupWizardBanner integrationName="React" hide={hideWizard} />
<h3>Install the package</h3>
<ReactInstallSnippet />
<h3>Add environment variables</h3>
<ReactEnvVarsSnippet />
<h3>Initialize</h3>
<p>
Integrate PostHog at the root of your app (such as <code>main.tsx</code> if you're using Vite).
</p>
<ReactSetupSnippet />
</>
)
}