For Next.js, we recommend following the Next.js integration guide instead.
- 1
Install PostHog web SDK
Required- Install
posthog-js
using your package manager:
npm install --save posthog-js- Add your environment variables to your
.env.local
file 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.localVITE_PUBLIC_POSTHOG_KEY=<ph_project_api_key>VITE_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com- Integrate PostHog at the root of your app (such as
main.jsx
if you are using Vite).
React// src/main.jsximport { StrictMode } from 'react'import { createRoot } from 'react-dom/client'import './index.css'import App from './App.jsx'import { PostHogProvider } from 'posthog-js/react'const options = {api_host: import.meta.env.VITE_PUBLIC_POSTHOG_HOST,defaults: '2025-05-24',}createRoot(document.getElementById('root')).render(<StrictMode><PostHogProvider apiKey={import.meta.env.VITE_PUBLIC_POSTHOG_KEY} options={options}><App /></PostHogProvider></StrictMode>,)Using React Router v7?
You need to set
posthog-js
andposthog-js/react
as external packages in yourvite.config.ts
file to avoid SSR errors.vite.config.ts// ... importsexport default defineConfig({plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],ssr: {noExternal: ['posthog-js', 'posthog-js/react']}});See our Remix docs for more details.
- Install
Verify PostHog is initialized
CheckpointConfirm you can capture events with PostHogBefore proceeding, confirm that you can capture events with PostHog using
posthog.capture('test_event')
.TSXimport { usePostHog } from 'posthog-js/react'const posthog = usePostHog()posthog?.capture('test_event')- 2
Set up exception autocapture
RequiredNote: A minimum SDK version of v1.207.8 is required, but we recommend keeping up to date with the latest version to ensure you have all of error tracking's features.
You can enable exception autocapture for the JavaScript Web SDK in the Error tracking section of your project settings.
When enabled, this automatically captures
$exception
events when errors are thrown by wrapping thewindow.onerror
andwindow.onunhandledrejection
listeners. - 3
Set up error boundaries
OptionalYou can add a custom error boundary to capture rendering errors thrown by components:
JavaScriptimport * as React from 'react';class CustomErrorBoundary extends React.Component {constructor(props) {super(props);this.state = { hasError: false };}static getDerivedStateFromError(error) {// Update state so the next render will show the fallback UI.return { hasError: true };}componentDidCatch(error) {posthog.captureException(error)}render() {if (this.state.hasError) {// You can render any custom fallback UIreturn this.props.fallback;}return this.props.children;}} - 4
Manually capture exceptions
OptionalIt is also possible to manually capture exceptions using the
captureException
method:JavaScriptposthog.captureException(error, additionalProperties) - 6