Skip to content

Commit f8f7049

Browse files
committed
feat: add PostHog analyze
1 parent 032ad91 commit f8f7049

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

app/(app)/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AppShell from "@/components/app-shell";
2+
import PostHogIdentifier from "@/providers/posthog-identifier";
23
import AuthorizedApolloWrapper from "@/providers/use-apollo.rsc";
34
import ProtectedRoute from "@/providers/use-protected-route";
45
import { unstable_ViewTransition as ViewTransition } from "react";
@@ -7,6 +8,7 @@ export default function Layout({ children }: { children: React.ReactNode }) {
78
return (
89
<ProtectedRoute>
910
<AuthorizedApolloWrapper>
11+
<PostHogIdentifier />
1012
<AppShell>
1113
<div className="mx-auto w-full max-w-7xl flex-1 p-3">
1214
<ViewTransition name="app-content">
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
5+
import posthog from "posthog-js";
6+
7+
export default function PostHogResetter() {
8+
useEffect(() => {
9+
posthog.reset();
10+
}, []);
11+
12+
return null;
13+
}

app/login/page.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import DoYouKnowSkeleton from "./_components/do-you-know/skeleton";
77
import GithubLink from "./_components/github-link";
88
import { LoginForm } from "./_components/login-form";
99
import { UpstreamStatus, UpstreamStatusPlaceholder } from "./_components/status";
10+
import PostHogResetter from "./_components/posthog-resetter";
1011

1112
export const metadata: Metadata = {
1213
title: "登入",
@@ -21,6 +22,9 @@ export default async function LoginPage() {
2122
lg:px-14 lg:py-8
2223
`}
2324
>
25+
{/* Reset the session on the login page */}
26+
<PostHogResetter />
27+
2428
<div className="flex max-w-sm flex-1 flex-col justify-center gap-6">
2529
<Link
2630
href="/"

providers/posthog-identifier.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use client";
2+
3+
import useUser from "@/hooks/use-user";
4+
import posthog from "posthog-js";
5+
import { useEffect } from "react";
6+
7+
/**
8+
* Identify the current user if they are logged in.
9+
* If they are not logged in, reset the posthog session.
10+
*/
11+
export default function PostHogIdentifier() {
12+
const { user } = useUser();
13+
14+
useEffect(() => {
15+
if (user) {
16+
posthog.identify(user.id, {
17+
name: user.name,
18+
email: user.email,
19+
});
20+
}
21+
}, [user]);
22+
23+
return null;
24+
}

0 commit comments

Comments
 (0)