Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostHog JS SDK not compatible with Segment analytics-next package #1106

Closed
1 of 4 tasks
kalaynn opened this issue Mar 29, 2024 · 3 comments · Fixed by #1119
Closed
1 of 4 tasks

PostHog JS SDK not compatible with Segment analytics-next package #1106

kalaynn opened this issue Mar 29, 2024 · 3 comments · Fixed by #1119
Labels
bug Something isn't working

Comments

@kalaynn
Copy link

kalaynn commented Mar 29, 2024

Bug description

Please describe.
The PostHog JS SDK supports integrating with Segment by passing the Segment analytics object into the posthog.init() call, as detailed in this documentation: https://posthog.com/docs/libraries/segment. This works as expected when the Segment analytics object is created from the Segment snippet, but not when it is created from the analytics-next package (https://github.com/segmentio/analytics-next/tree/master/packages/browser#readme).

When trying to do so, I receive the following error:

Uncaught (in promise) TypeError: s2.segment.user().id is not a function

image

This appears to be because, with the analytics object from the analytics-next package, the user() function is async and returns a promise instead of the object that PostHog expects.

How to reproduce

  1. Set up Segment and Posthog per the documentation here: https://posthog.com/docs/libraries/segment
  2. Initialize the Segment analytics object using the analytics-next package instead of the Segment snippet
  3. Attempt a page load

Environment

  • PostHog Cloud US, project ID: 48533
  • PostHog Cloud EU, project ID: [please provide from https://eu.posthog.com/settings/project-details#variables]
  • PostHog Hobby self-hosted with docker compose, version/commit: [please provide]
  • PostHog self-hosted with Kubernetes (deprecated, see "Sunsetting Kubernetes support"), version/commit: [please provide]

Additional context

Thank you for your bug report – we love squashing them!

@kalaynn kalaynn added the bug Something isn't working label Mar 29, 2024
@timgl timgl transferred this issue from PostHog/posthog Apr 1, 2024
@benjackwhite
Copy link
Collaborator

Created this PR to import the package and try and have proper type checking - but even their types don't show it as being a promise 🙈 #1119

Could you please share your analytics-next version as well as a snippet of how you are initialising posthog-js with it so we can have a better chance of debugging.

@kamui
Copy link

kamui commented Apr 8, 2024

Here's how we're initializing analytics-next (1.66.0) and posthog (1.118.1):

import posthog from "posthog-js"
import { AnalyticsBrowser } from "@segment/analytics-next"

const analytics = AnalyticsBrowser.load({ writeKey: process.env.WRITE_KEY})
analytics.ready(function () {
 posthog.init(process.env.POSTHOG_API_KEY, {
   api_host: process.env.POSTHOG_API_HOST,
import posthog from "posthog-js"
import { AnalyticsBrowser } from "@segment/analytics-next"

const analytics = AnalyticsBrowser.load({ writeKey: process.env.WRITE_KEY})
analytics.ready(function () {
posthog.init(process.env.POSTHOG_API_KEY, {
  api_host: process.env.POSTHOG_API_HOST,
  segment: analytics,
  capture_pageview: false,
  loaded: function () {
    analytics.page()
  }
})
})
   capture_pageview: false,
   loaded: function () {
     analytics.page()
   }
 })
})

@benjackwhite
Copy link
Collaborator

Took a while but managed to reproduce and fix. The Segment types and docs are off which was annoying but oh well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants