fix(telemetry): add cookie fallback to getDistinctId for identity resolution#1203
Closed
everettbu wants to merge 2 commits into
Closed
fix(telemetry): add cookie fallback to getDistinctId for identity resolution#1203everettbu wants to merge 2 commits into
everettbu wants to merge 2 commits into
Conversation
…olution When PostHog SDK isn't initialized yet (e.g., immediately after OAuth redirect), getDistinctId() now falls back to parsing the distinct_id from the PostHog cookie instead of returning undefined. This fixes the identity resolution break that was causing ~43% of Google Ads signups to not be attributed correctly. The anonymous PostHog session (which captures UTM/gclid params on ad click) will now be properly aliased to the user ID on signup, even when the SDK hasn't fully loaded. Fixes GROWTH-616
Address CodeRabbit feedback:
- Use substring instead of split('=')[1] to handle '=' in cookie value
- Match exact cookie name ph_<apiKey>_posthog instead of any ph_ prefix
Author
|
Upstream PR was closed or merged. Code is synced via branch mirror. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mirror of supabase/supabase#42535
Original author: seanoliver
Summary
Fixes the PostHog identity resolution break that was causing ~43% of Google Ads signups to lose attribution.
getDistinctId()now falls back to parsing thedistinct_idfrom the PostHog cookieProblem
There's a ~10,500 user gap between Google Ads reported signups (24.3K) and our internal attribution data (13.8K) for Nov 30, 2025 – Feb 2, 2026. IP address matching analysis confirmed 69% of this gap is recoverable by fixing identity resolution.
The root cause: after OAuth redirect,
getDistinctId()returnedundefinedbecause PostHog SDK wasn't initialized yet, so the identify call didn't includeanonymous_id, and no alias was created.Changes
packages/common/posthog-client.tsgetDistinctId()to fall back to cookie parsing when SDK isn't initializedgetDistinctIdFromCookie()private method to parsedistinct_idfrom PostHog'sph_*cookieTesting
/sign-in?gclid=test123&utm_source=googledistinct_idfrom PostHog cookie:POST /platform/telemetry/identifyanonymous_idin request payload matches the pre-OAuthdistinct_idNote on reproducing the race condition locally
The bug is caused by a race condition where PostHog SDK isn't initialized by the time
useTelemetryIdentifyruns after OAuth. This is difficult to reliably reproduce locally because:You can try throttling to "Slow 3G" in DevTools Network tab, but even then PostHog often initializes in time. The race condition depends on multiple factors: user connection speed, PostHog CDN latency, OAuth provider response time, browser caching, etc.
This is a safe, defensive change - when PostHog IS initialized (the common case), behavior is unchanged. The cookie fallback only activates when the SDK isn't ready yet.
Validating in production
We'll know the fix is working after 1-2 weeks by monitoring:
Linear
Fixes GROWTH-616
Summary by CodeRabbit