()
| 54 | * Attempts to retrieve the distinct id of the current user. |
| 55 | */ |
| 56 | export 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 | |
| 72 | export const createPostHogClient = async () => { |
| 73 | const posthog = new PostHog(env.POSTHOG_PAPIK, { |
no test coverage detected