mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
* fix: resolve 50% session tracking discrepancy on root route - Fixes race condition causing ~50% discrepancy in session tracking between client and server on root route - Implements event queuing to capture telemetry events before PostHog initializes - Adds session ID correlation to enable unified tracking analysis between client and server events - Uses sendBeacon API for both pageview and pageleave events to ensure reliable capture even during navigation - Adds production safeguards with try/catch blocks and memory caps to prevent API errors from breaking tracking * refactor(posthog-client): reduce max pending events and improve event handling - Decreased the maximum number of queued events from 200 to 20 - Added the use of the sendBeacon API for pending events to survive page unload - Added comments to clarify the handling of pending properties and events during posthog initialization
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { isBrowser } from './helpers'
|
|
|
|
// Parse session_id from PostHog cookie since SDK doesn't expose session ID
|
|
// (needed to correlate client and server events)
|
|
function getPostHogSessionId(): string | null {
|
|
if (!isBrowser) return null
|
|
|
|
try {
|
|
// Parse PostHog cookie to extract session ID
|
|
const phCookies = document.cookie.split(';').find((cookie) => cookie.trim().startsWith('ph_'))
|
|
|
|
if (phCookies) {
|
|
const cookieValue = decodeURIComponent(phCookies.split('=')[1])
|
|
const phData = JSON.parse(cookieValue)
|
|
if (phData.$sesid && Array.isArray(phData.$sesid) && phData.$sesid[1]) {
|
|
return phData.$sesid[1]
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.warn('Could not extract PostHog session ID:', error)
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
export function getSharedTelemetryData(pathname?: string) {
|
|
const sessionId = getPostHogSessionId()
|
|
|
|
return {
|
|
page_url: isBrowser ? window.location.href : '',
|
|
page_title: isBrowser ? document?.title : '',
|
|
pathname: pathname ? pathname : isBrowser ? window.location.pathname : '',
|
|
session_id: sessionId,
|
|
ph: {
|
|
referrer: isBrowser ? document?.referrer : '',
|
|
language: navigator.language ?? 'en-US',
|
|
user_agent: navigator.userAgent,
|
|
search: isBrowser ? window.location.search : '',
|
|
viewport_height: isBrowser ? window.innerHeight : 0,
|
|
viewport_width: isBrowser ? window.innerWidth : 0,
|
|
},
|
|
}
|
|
}
|