Skip to content

Commit 42d8308

Browse files
committed
User Identification
1 parent 0a3b269 commit 42d8308

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/app/_providers/posthog-provider.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"use client";
22

3-
import { useEffect } from "react";
43
import posthog from "posthog-js";
54
import { PostHogProvider as PHProvider } from "posthog-js/react";
5+
import { useEffect } from "react";
6+
7+
import UserIdentification from "./user-identification";
68

79
import { env } from "~/env";
810

@@ -16,5 +18,10 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
1618
});
1719
}, []);
1820

19-
return <PHProvider client={posthog}>{children}</PHProvider>;
21+
return (
22+
<PHProvider client={posthog}>
23+
<UserIdentification />
24+
{children}
25+
</PHProvider>
26+
);
2027
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useUser } from "@clerk/nextjs";
2+
import { usePostHog } from "posthog-js/react";
3+
import { useEffect } from "react";
4+
5+
export default function UserIdentification() {
6+
const posthog = usePostHog();
7+
const { user } = useUser();
8+
9+
useEffect(() => {
10+
if (user) {
11+
posthog.identify(user.id, {
12+
email: user.emailAddresses[0]?.emailAddress,
13+
});
14+
} else {
15+
posthog.reset();
16+
}
17+
}, [posthog, user]);
18+
19+
return null; // Do not render anything
20+
}

0 commit comments

Comments
 (0)