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, }, } }