fix(packages/sui-segment-wrapper): improve session id assignment#1972
Merged
fix(packages/sui-segment-wrapper): improve session id assignment#1972
Conversation
… fix session ID inconsistencies
tomasmax
approved these changes
Mar 16, 2026
stivaliserna
approved these changes
Mar 16, 2026
andresz1
approved these changes
Mar 16, 2026
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.
Summary
Fix GA4 session ID inconsistencies that caused "unassigned" sessions when matching events from Segment (cloud) and client-side "sui" events.
Root cause:
gtag.get('session_id')returns incorrect/temporary IDs during the first hits due to a race condition between the API call and the physical cookie write.Solution: Prioritize reading session ID directly from the GA4 cookie (
segment_ga_*) as the source of truth, with API fallback.Changes
getGA4SessionIdFromCookie()helper inutils/cookies.jsto read session ID from cookie using regex patterngetGoogleSessionId()to prioritize cookie value over gtag APIcheckNewSession()helper to deduplicate session detection logicgetGA4Data()function to expose complete GA4 context (clientId, sessionId, isNewSession)getGA4Data,getGoogleClientId,getGoogleSessionIdgoogleAnalyticsCookiePrefixconfig (defaults to'segment')window.__GA4_DATAfor debuggingBackwards Compatibility
✅ 100% backwards compatible
Benefits