|
| 1 | +/** |
| 2 | + * Composable that syncs the Better Auth session with PostHog identity. |
| 3 | + * Call once in a root-level layout or app.vue to enable automatic |
| 4 | + * user identification and organization group analytics. |
| 5 | + * |
| 6 | + * Must be called in `<script setup>` context (not in a plugin). |
| 7 | + * |
| 8 | + * Identity and group calls are gated on analytics consent so that events are |
| 9 | + * never sent before the user has opted in. When a user grants consent during |
| 10 | + * their session (ConsentBanner), the watchers re-fire and identify them |
| 11 | + * immediately without requiring a page reload. |
| 12 | + */ |
| 13 | +export async function usePostHogIdentity() { |
| 14 | + const { $posthogIdentifyUser, $posthogSetOrganization, $posthogReset, $posthogResetGroups } = useNuxtApp() |
| 15 | + |
| 16 | + if (!$posthogIdentifyUser) return |
| 17 | + |
| 18 | + const { data: session } = await authClient.useSession(useFetch) |
| 19 | + const activeOrgState = authClient.useActiveOrganization() |
| 20 | + |
| 21 | + // Share the consent reactive state. useAnalyticsConsent also applies the |
| 22 | + // stored consent flag to PostHog on the client, so calling it here means |
| 23 | + // consent is active before the immediate watchers fire below. |
| 24 | + const { hasConsented } = useAnalyticsConsent() |
| 25 | + |
| 26 | + // Watch session AND consent so identify re-fires when a new user accepts |
| 27 | + // consent during their visit, and is skipped when PostHog is opted-out. |
| 28 | + watch( |
| 29 | + [() => session.value, hasConsented] as const, |
| 30 | + ([currentSession, consented], prev) => { |
| 31 | + const user = currentSession?.user |
| 32 | + const previousUser = (prev?.[0] as typeof session.value)?.user |
| 33 | + |
| 34 | + if (user?.id && consented) { |
| 35 | + // Only the user ID is forwarded — name and createdAt are intentionally |
| 36 | + // omitted so PostHog receives the minimal data needed for analytics. |
| 37 | + ;($posthogIdentifyUser as (userId: string) => void)(user.id) |
| 38 | + } |
| 39 | + else if (previousUser?.id && !user?.id) { |
| 40 | + // Always reset on log-out regardless of consent state so that |
| 41 | + // no user identity leaks into the next anonymous session. |
| 42 | + ($posthogReset as () => void)() |
| 43 | + } |
| 44 | + }, |
| 45 | + { immediate: true }, |
| 46 | + ) |
| 47 | + |
| 48 | + // Watch org AND consent for group analytics — same gating logic as above. |
| 49 | + watch( |
| 50 | + [() => activeOrgState.value?.data, hasConsented] as const, |
| 51 | + ([org, consented]) => { |
| 52 | + if (consented) { |
| 53 | + if (org?.id) { |
| 54 | + // Only org id and name are forwarded; slug is omitted to minimise data. |
| 55 | + ;($posthogSetOrganization as (org: { id: string, name?: string }) => void)({ |
| 56 | + id: org.id, |
| 57 | + name: org.name || undefined, |
| 58 | + }) |
| 59 | + } |
| 60 | + else { |
| 61 | + // Clear org group when user has no active organization to avoid |
| 62 | + // attributing events to the previously selected org. |
| 63 | + ($posthogResetGroups as (() => void) | undefined)?.() |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + { immediate: true }, |
| 68 | + ) |
| 69 | +} |
0 commit comments