React error tracking installation
For Next.js, we recommend following the Next.js integration guide instead.
- 1
Install PostHog web SDK
Required- Install
posthog-jsusing your package manager:
- Add your environment variables to your
.env.localfile 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.local- Integrate PostHog at the root of your app (such as
main.jsxif you are using Vite).
ReactUsing React Router v7?
You need to set
posthog-jsandposthog-js/reactas external packages in yourvite.config.tsfile to avoid SSR errors.vite.config.tsSee 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').TSX- 2
Set up exception autocapture
RecommendedNote: 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
$exceptionevents when errors are thrown by wrapping thewindow.onerrorandwindow.onunhandledrejectionlisteners. - 3
Set up error boundaries
OptionalYou can use the
PostHogErrorBoundarycomponent to capture rendering errors thrown by components:JavaScript - 4
Manually capture exceptions
OptionalIt is also possible to manually capture exceptions using the
captureExceptionmethod:JavaScript - 6

