diff --git a/apps/web/src/app/(private)/layout.tsx b/apps/web/src/app/(private)/layout.tsx index 057817fc8..1585f4cd1 100644 --- a/apps/web/src/app/(private)/layout.tsx +++ b/apps/web/src/app/(private)/layout.tsx @@ -11,7 +11,7 @@ import { getSession } from '$/services/auth/getSession' import { ROUTES } from '$/services/routes' import { redirect } from 'next/navigation' -import { CSPostHogProvider } from '../providers' +import { CSPostHogProvider, IdentifyUser } from '../providers' export default async function PrivateLayout({ children, @@ -25,13 +25,15 @@ export default async function PrivateLayout({ return ( - - - - {children} - - - + + + + + {children} + + + + ) } diff --git a/apps/web/src/app/providers.tsx b/apps/web/src/app/providers.tsx index 1058909d1..cbc647573 100644 --- a/apps/web/src/app/providers.tsx +++ b/apps/web/src/app/providers.tsx @@ -1,10 +1,11 @@ 'use client' -import { ReactNode } from 'react' +import { ReactNode, useEffect } from 'react' +import { User, Workspace } from '@latitude-data/core/browser' import { envClient } from '$/envClient' import posthog from 'posthog-js' -import { PostHogProvider } from 'posthog-js/react' +import { PostHogProvider, usePostHog } from 'posthog-js/react' if (typeof window !== 'undefined') { posthog.init(envClient.NEXT_PUBLIC_POSTHOG_KEY, { @@ -15,3 +16,26 @@ if (typeof window !== 'undefined') { export function CSPostHogProvider({ children }: { children: ReactNode }) { return {children} } + +export function IdentifyUser({ + user, + workspace, + children, +}: { + user: User + workspace: Workspace + children: ReactNode +}) { + const posthog = usePostHog() + + useEffect(() => { + if (user) { + posthog?.identify(user.id, { + email: user.email, + }) + posthog?.group('workspace', String(workspace.id)) + } + }, [posthog, user.id, user.email, workspace.id]) + + return children +}