MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / tryGetPostHogDistinctId

Function tryGetPostHogDistinctId

packages/web/src/lib/posthog.ts:56–70  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

54 * Attempts to retrieve the distinct id of the current user.
55 */
56export const tryGetPostHogDistinctId = async () => {
57 // First, attempt to retrieve the distinct id from the PostHog cookie
58 // (set by the client-side PostHog SDK). This preserves identity
59 // continuity between client-side and server-side events.
60 const cookieStore = await cookies();
61 const cookie = getPostHogCookie(cookieStore);
62 if (cookie) {
63 return cookie.distinct_id;
64 }
65
66 // Fall back to the authenticated user's ID. This covers all auth
67 // methods: session cookies, OAuth Bearer tokens, and API keys.
68 const authResult = await getAuthenticatedUser();
69 return authResult?.user.id;
70}
71
72export const createPostHogClient = async () => {
73 const posthog = new PostHog(env.POSTHOG_PAPIK, {

Callers 2

captureEventFunction · 0.85

Calls 2

getAuthenticatedUserFunction · 0.90
getPostHogCookieFunction · 0.85

Tested by

no test coverage detected