PostHog / posthog-js

Send usage data from your web app or site to PostHog, with autocapture.
https://posthog.com/docs/libraries/js
Other
236 stars 104 forks source link

Type 'SentryIntegration' is not assignable to type 'Integration' #1205

Open mozeryansky opened 1 month ago

mozeryansky commented 1 month ago

Following the instructions from here: https://posthog.com/docs/libraries/sentry#installation

The SentryIntegration class is not type compatible. Force casting throws causing an error from sentry for the same reason.

Type 'SentryIntegration' is not assignable to type 'Integration'.
  Types of property 'setupOnce' are incompatible.
    Type '(addGlobalEventProcessor: (callback: any) => void, getCurrentHub: () => any) => void' is not assignable to type '() => void'.
      Target signature provides too few arguments. Expected 2 or more, but got 0.ts(2322)

Turns out Sentry recently changed the signature: https://github.com/getsentry/sentry-javascript/pull/11238

mozeryansky commented 1 month ago

I am able to get it to work by doing this after Sentry.init:

const postHogIntegration = 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)
)

postHogIntegration.setupOnce(Sentry.addEventProcessor, () => {})

Essentially passing the handler inside setupOnce to the addEventProcessor.

However, another issue is that the urls being generated are incorrect. The config token here is the API key, but Sentry uses a the project ID (i.e. 80397).

const personUrl = _posthog.requestRouter.endpointFor(
    'ui',
    `/project/${_posthog.config.token}/person/${_posthog.get_distinct_id()}` // <--- wrong url
)