Sentry

Last updated:

|Edit this page

Why does this exist?

Our Sentry integration is a two-way integration which works on Javascript & Python. Once installed, it will:

  • Add a direct link in Sentry to the profile of the person affected in PostHog.
  • Send an $exception event to PostHog with a direct link to Sentry.

This way, debugging issues becomes a lot easier, and you can also correlate error data with your product metrics.

If you're looking for the PostHog + Sentry integration for Python, please check out the Python docs.

Installation

Make sure you're using both PostHog and Sentry as JS modules. You'll need to replace 'your organization' and project-id with the organization and project-id from Sentry.

  • 'your organization': will be in the URL when you go to your Sentry instance, like so: https://sentry.io/organizations/your-organization/projects/
  • project-id: will be the last few digits in your Sentry DSN, such as https://adf90sdc09asfd3@9ads0fue.ingest.sentry.io/project-id
  • prefix: Optional: Url of a self-hosted sentry instance (default: https://sentry.io/organizations/)
  • severityAllowList: Optional: by default this is ['error'], you can provide more Sentry severity levels (e.g. ['error', 'info']) or '*' to capture any severity. Only available from posthog-js version 1.118.0 forward
Web
import posthog from 'posthog-js'
import * as Sentry from '@sentry/browser'
posthog.init('<ph_project_api_key>')
Sentry.init({
dsn: '<your Sentry DSN>',
integrations: [posthog.sentryIntegration({
organization: 'your organization',
projectId: 'project-id',
severityAllowList: ['error', 'info'] // optional: here is set to handle captureMessage (info) and captureException (error)
}
)],
})

Usage

In PostHog, you'll now have $exception events, which have a "Sentry URL" link to take you to the exception:

Sentry exception event in PostHog

From Sentry you will now be able to go directly to the affected person in PostHog and watch the session recording for when the exception happened, see what else the user has done, and find their details. Don't forget to click the little icon to the side of the URL, not the URL itself.

PostHog user profile URL in Sentry

Sentry SDK v6 and below

If you are using a lower version of the Sentry SDK you may need to use the "class based" integration as below

Web
Sentry.init({
dsn: '<your Sentry DSN>',
integrations: [new posthog.SentryIntegration(
posthog,
'your organization',
project-id,
undefined, // optional: but necessary if you want to set a severity allowlist
['error', 'info'] // optional: here is set to handle captureMessage (info) and captureException (error)
)],
})

Questions?

  • Deniz
    18 days ago

    Integration not working, and error autocapture setting uclear

    Hi, the integration and the posthog person and recording URL's were working, but for a while now they are instead starting with my base domain instead of posthog in the format (see screenshot attached).

    Secondly, this post says the error autocapture should be turned off in settings - but the linked settings page does not have a specific error autocapture setting: Instead there is just generic autocapture, deadclick autocapture, web vitals, heatmaps etc.

    Could you please clarify this part? Thanks!

    Screenshot-20250224094411-641x234.png

    • David
      11 days agoSolution

      Hi,

      Thanks for the context. So I think your api_host is correct but you will need to explicitly set the ui_host as mentioned above.

      https://github.com/PostHog/posthog-js/blob/a9cc96ea396b6124bba754e7d21f1019c29417e3/src/types.ts#L265-L271

      Your config should look something like this:

      posthog.init(key, {
      api_host,
      ui_host: 'https://us.posthog.com',
      persistence: 'localStorage+cookie',
      person_profiles: 'identified_only',
      opt_out_capturing_by_default: true,
      opt_out_persistence_by_default: true,
      capture_pageview: false,
      capture_pageleave: false
      })
  • Nilson
    9 months ago
    • Paul(he/him)
      9 months agoSolution

      ah, you need to change your posthog config.

      it's pointing at app.posthog.com you might be manually specifying that somewhere. you can omit it or need to set us.posthog.com

      the combination of redirect from app to us and redirect from project/token to project/id is being confused somewhere along the way 😅

  • Abhishek
    9 months ago

    Need a clear documentation/tutorial for Next.js to integrate Sentry and Posthog together

    Dear Posthog Team, We are looking for a integration guide to get posthog integrated with Sentry errors across client and server on Next.js 14 (app router). Could you please help us?

  • lucas
    9 months ago

    Viewing insight sessions via shortcut to sentry

    Does this allow you to create a segment (insight, cohort, etc) then take that search over to Sentry?

    • lucas
      Author9 months ago

      Bummer. Ok

  • Hugo
    10 months ago

    Sentry v8 Class-based Integrations deprecated

    I am using Sentry v8 SDK on my app, and I'm trying to integrate Posthog to Sentry. However, in Sentry v8, class-based integrations has been dropped, while it seems that Posthog currently only provides class-based Sentry integration (SentryIntegration).

    Any timeline or estimate when the new function-based integration will be implemented in Posthog?

    • Ben
      9 months ago

      I updated the docs as well to mention the new method.

      We couldn’t just replace it as that would break for anyone using the old ask version.

      Either way thanks for pushing for the solution!

  • Zachary
    10 months ago

    Proxy events messing up url added to sentry

    We proxy our events through vercel to avoid getting filtered, but that makes the link given to sentry wrong.

  • Moe
    a year ago

    React Native?

    I use posthog for react native. Can sentry be integrated with the RN SDK?

    • Maksim
      a year ago

      Upvote for the RN SDK integration

  • John
    a year ago

    How to use with NextJS?

    I followed the NextJS docs so I'm calling posthog.init from a providers file: https://posthog.com/docs/libraries/next-js#app-router

    When I installed Sentry I did so with "npx @sentry/wizard@latest -i nextjs" (https://docs.sentry.io/platforms/javascript/guides/nextjs/#install) which has created 3 files which call Sentry.init:

    • sentry.client.config.ts
    • sentry.edge.config.ts
    • sentry.server.config.ts

    How can I use both together? I dont know if posthog will be initialised by the time Sentry.init calls it, could this be an issue?

    • Paul(he/him)
      a year agoSolution

      Hey John,

      You can run Sentry and PostHog alongside each other without any clashes (we use Sentry too)

      If you want to use the PostHog Sentry integration you'll need to edit the calls to Sentry init to pass in the posthog integration. The integration takes a posthog instance, so you'd need to arrange your code so that you can pass that in. Your best bet is to try it and then see what happens :)

  • Omar
    a year ago

    Sentry organization slug

    FYI - Sentry shows my org's URL as https://organization-name.sentry.io, not the URL you displayed, so the default instructions aren't that clear. I think it's much clearer to direct people to go to their organization settings to see the organization slug. Not a question, just stating that the instructions could be easier to follow. Also wonder if there would be a way to use the Sentry project slug instead of project ID, but shrug

  • Maren
    2 years ago

    Optional properties 'your organization' and project-id

    The 'organization' & 'projectId' are optional, what benefit does passing these provide? or what functionality is missing when they are not provided?

    • Marcus
      2 years agoSolution

      Adding them will add the sentry URL property to the exception events in PostHog. Screenshot 2023-09-05 at 16.38.19@2x.png

Was this page useful?