diff --git a/frontend/src/oso.tsx b/frontend/src/oso.tsx index 842dcb01af3..5f4c8502d6a 100644 --- a/frontend/src/oso.tsx +++ b/frontend/src/oso.tsx @@ -2,6 +2,7 @@ import posthog from 'posthog-js' import { mount } from "@/oso-mount"; +import { Logger } from "@/utils/Logger"; declare global { @@ -11,14 +12,21 @@ declare global { } if (import.meta.env.VITE_POSTHOG_PUBLIC_API_KEY) { + // Read identity passed from the parent page via URL fragment to stitch events + // under the same distinct_id without emitting a spurious $identify event. + const fragment = new URLSearchParams(window.location.hash.slice(1)); + const distinctID = fragment.get("posthogDistinctId") ?? undefined; + const sessionID = fragment.get("posthogSessionId") ?? undefined; + posthog.init(import.meta.env.VITE_POSTHOG_PUBLIC_API_KEY, { + bootstrap: distinctID ? { distinctID, sessionID, isIdentifiedID: true } : undefined, session_recording: { // WARNING: Only enable this if you understand the security implications recordCrossOriginIframes: true, - } + }, }); } else { - console.warn("POSTHOG_PUBLIC_API_KEY not set, skipping posthog init"); + Logger.warn("POSTHOG_PUBLIC_API_KEY not set, skipping posthog init"); } // eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope @@ -28,7 +36,7 @@ if (el) { throw new Error("[marimo] mount config not found"); } mount(window.__MARIMO_MOUNT_CONFIG__, el).catch((e) => { - console.error("Failed to mount marimo app", e); + Logger.error("Failed to mount marimo app", e); }); } else { throw new Error("[marimo] root element not found");