Skip to content

Commit 3749851

Browse files
committed
posthog
1 parent 4abf4ea commit 3749851

File tree

6 files changed

+133
-28
lines changed

6 files changed

+133
-28
lines changed

apps/web/lib/posthog.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import posthog from "posthog-js";
2+
import { IS_DEV } from "./is-dev";
3+
4+
type EventId = "feedback";
5+
6+
export function posthogCapture(
7+
eventId: EventId,
8+
payload: Record<string, any> = {}
9+
) {
10+
posthog.capture(eventId, {
11+
...payload,
12+
is_dev: IS_DEV,
13+
});
14+
}
15+
16+
export function posthogCaptureException(
17+
error: Error,
18+
payload: Record<string, any> = {}
19+
) {
20+
posthog.captureException(error, {
21+
...payload,
22+
is_dev: IS_DEV,
23+
});
24+
}
25+
26+
export function posthogIdentify({ email }: { email: string }) {
27+
posthog.identify(email, {
28+
is_dev: IS_DEV,
29+
});
30+
}

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"next-plausible": "^3.12.4",
101101
"nuqs": "^2.4.3",
102102
"openai": "^4.12.4",
103+
"posthog-js": "^1.284.0",
103104
"raw-body": "^2.5.2",
104105
"react": "18.2.0",
105106
"react-confetti": "^6.1.0",

apps/web/src/components/Feedback.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { Button } from "./ui/button";
1010
import { toast } from "sonner";
1111
import { createSupabaseBrowserClient } from "@/lib/supabase";
1212
import { FeedbackForm } from "./Feedback/feedback-form";
13+
import posthog from "posthog-js";
14+
import { posthogCapture } from "lib/posthog";
1315

1416
type Props = {};
1517

@@ -37,6 +39,12 @@ const Feedback = (props: Props) => {
3739
throw user.error;
3840
}
3941

42+
posthogCapture("feedback", {
43+
feedback,
44+
type,
45+
user_email: user.data.user.email,
46+
});
47+
4048
const { data, error } = await sb
4149
.from("feedback")
4250
.insert({ feedback, type, user_email: user.data.user.email });

apps/web/src/layouts/AppLayout.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useSubscriptionQuery } from "@/queries/subscription";
2323
import { ZenblogToolbar } from "@/components/dev/zenblog-toolbar";
2424
import { IS_DEV } from "@/lib/constants";
2525
import { OnboardingDropdown } from "@/components/onboarding";
26+
import { posthogIdentify } from "lib/posthog";
2627

2728
type Props = {
2829
children?: React.ReactNode;
@@ -53,9 +54,14 @@ export default function AppLayout({
5354
// eslint-disable-next-line react-hooks/exhaustive-deps
5455
}, [user, loading]);
5556

56-
const selectedBlog = blogs?.find((blog) => blog.id === router.query.blogId);
57+
useEffect(() => {
58+
if (user && user.email) {
59+
console.log("Identifying user", user.email);
60+
posthogIdentify({ email: user.email });
61+
}
62+
}, [user]);
5763

58-
const isDev = IS_DEV;
64+
const selectedBlog = blogs?.find((blog) => blog.id === router.query.blogId);
5965

6066
const BlogNavItems = [
6167
{
@@ -211,7 +217,7 @@ export default function AppLayout({
211217
</nav>
212218
{selectedBlog && (
213219
<div className="border-b bg-white shadow-sm">
214-
<div className="mx-auto flex max-w-5xl items-center justify-between overflow-x-auto">
220+
<div className="mx-auto flex max-w-5xl items-center justify-between">
215221
<div className="flex max-w-5xl items-center px-2">
216222
{BlogNavItems.map((item) => (
217223
<NavItem

apps/web/src/pages/_app.tsx

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
QueryClientProvider,
1010
} from "@tanstack/react-query";
1111
import { Toaster } from "sonner";
12-
import { useState } from "react";
12+
import { useEffect, useState } from "react";
1313
import { UserProvider } from "@/utils/supabase/browser";
14+
import { PostHogProvider } from "posthog-js/react";
15+
import posthog from "posthog-js";
1416

1517
// Fonts
1618
const inter = Inter({
@@ -33,29 +35,44 @@ function MyApp({ Component, pageProps }: AppProps) {
3335
})
3436
);
3537

38+
useEffect(() => {
39+
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY as string, {
40+
api_host: "https://eu.i.posthog.com",
41+
person_profiles: "always",
42+
defaults: "2025-05-24",
43+
// Enable debug mode in development
44+
loaded: (posthog) => {
45+
if (process.env.NODE_ENV === "development") posthog.debug();
46+
},
47+
});
48+
console.log("PostHog initialized", posthog);
49+
}, []);
50+
3651
return (
3752
<div className={`${inter.variable}`}>
38-
<UserProvider>
39-
<PlausibleProvider domain="zenblog.com">
40-
<QueryClientProvider client={queryClient}>
41-
<HydrationBoundary state={pageProps.dehydratedState}>
42-
<Component key={pathname} {...pageProps} />
43-
<Toaster
44-
position="bottom-center"
45-
toastOptions={{
46-
style: {
47-
borderRadius: "12px",
48-
backgroundColor: "#333333",
49-
color: "white",
50-
padding: "10px 12px",
51-
border: "none",
52-
},
53-
}}
54-
/>
55-
</HydrationBoundary>
56-
</QueryClientProvider>
57-
</PlausibleProvider>
58-
</UserProvider>
53+
<PostHogProvider client={posthog}>
54+
<UserProvider>
55+
<PlausibleProvider domain="zenblog.com">
56+
<QueryClientProvider client={queryClient}>
57+
<HydrationBoundary state={pageProps.dehydratedState}>
58+
<Component key={pathname} {...pageProps} />
59+
<Toaster
60+
position="bottom-center"
61+
toastOptions={{
62+
style: {
63+
borderRadius: "12px",
64+
backgroundColor: "#333333",
65+
color: "white",
66+
padding: "10px 12px",
67+
border: "none",
68+
},
69+
}}
70+
/>
71+
</HydrationBoundary>
72+
</QueryClientProvider>
73+
</PlausibleProvider>
74+
</UserProvider>
75+
</PostHogProvider>
5976
</div>
6077
);
6178
}

package-lock.json

Lines changed: 46 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)