fix(telemetry): pass PostHog distinct_id in identify request (#41322)

This commit is contained in:
Sean Oliver authored and GitHub committed 2025-12-15 08:53:06 -08:00
1 parent 641a409772
commit 71936a69da
3 files changed
+28 -5

No files matched your search

+2
View File
@@ -9218,6 +9218,8 @@ export interface components {
reset_project?: boolean
}
TelemetryIdentifyBodyV2: {
/** PostHog JS SDK's distinct_id - used for aliasing anonymous → authenticated users */
anonymous_id?: string
organization_slug?: string
project_ref?: string
user_id: string
+23 -3
View File
@@ -11,6 +11,9 @@ interface PostHogClientConfig {
}
class PostHogClient {
/** True after posthog.init() is called (prevents double-init) */
private initStarted = false
/** True after the `loaded` callback fires, meaning PostHog has fully bootstrapped */
private initialized = false
private pendingGroups: Record<string, string> = {}
private pendingIdentification: { userId: string; properties?: Record<string, any> } | null = null
@@ -27,7 +30,7 @@ class PostHogClient {
}
init(hasConsent: boolean = true) {
if (this.initialized || typeof window === 'undefined' || !hasConsent) return
if (this.initStarted || typeof window === 'undefined' || !hasConsent) return
if (!this.config.apiKey) {
console.warn('PostHog API key not found. Skipping initialization.')
@@ -72,11 +75,13 @@ class PostHogClient {
}
})
this.pendingEvents = []
this.initialized = true
},
}
this.initStarted = true
posthog.init(this.config.apiKey, config)
this.initialized = true
}
capturePageView(properties: Record<string, any>, hasConsent: boolean = true) {
@@ -148,7 +153,7 @@ class PostHogClient {
this.pendingGroups = {}
this.pendingEvents = []
if (!this.initialized) return
if (!this.initStarted) return
try {
posthog.reset()
@@ -156,6 +161,21 @@ class PostHogClient {
console.error('PostHog reset failed:', error)
}
}
/**
* Returns PostHog's distinct_id, which holds first-touch attribution data.
* Returns undefined until PostHog's `loaded` callback fires.
*/
getDistinctId(): string | undefined {
if (!this.initialized) return undefined
try {
return posthog.get_distinct_id()
} catch (error) {
console.error('PostHog getDistinctId failed:', error)
return undefined
}
}
}
export const posthogClient = new PostHogClient()
+3 -2
View File
@@ -320,12 +320,13 @@ export function useTelemetryIdentify(API_URL: string) {
useEffect(() => {
if (user?.id) {
// Send to backend
const anonymousId = posthogClient.getDistinctId()
sendTelemetryIdentify(API_URL, {
user_id: user.id,
...(anonymousId && { anonymous_id: anonymousId }),
})
// Also identify in PostHog client-side
posthogClient.identify(user.id)
}
}, [API_URL, user?.id])