Skip to content

Commit

Permalink
feat: add posthog analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Aug 7, 2024
1 parent 9483af3 commit c7401ca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"next": "^14.2.4",
"next-themes": "^0.3.0",
"postgres": "^3.4.4",
"posthog-js": "^1.154.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.51.5",
Expand Down
38 changes: 38 additions & 0 deletions src/app/_analytics/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use client";
import { useAuth, useUser } from "@clerk/nextjs";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { useEffect } from "react";

if (typeof window !== "undefined") {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: "/ingest",
ui_host: "https://app.posthog.com",
});
}

export function CSPostHogProvider({ children }: { children: React.ReactNode }) {
return (
<PostHogProvider client={posthog}>
<PostHogAuthWrapper>{children}</PostHogAuthWrapper>
</PostHogProvider>
);
}

function PostHogAuthWrapper({ children }: { children: React.ReactNode }) {
const auth = useAuth();
const userInfo = useUser();

useEffect(() => {
if (userInfo.user) {
posthog.identify(userInfo.user.id, {
email: userInfo.user.emailAddresses[0]?.emailAddress,
name: userInfo.user.fullName,
});
} else if (!auth.isSignedIn) {
posthog.reset();
}
}, [auth, userInfo]);

return children;
}
43 changes: 23 additions & 20 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ClerkProvider } from "@clerk/nextjs";
import { ThemeProvider } from "@/components/theme-provider";
import { cn } from "@/lib/utils";
import { dark } from "@clerk/themes";
import { CSPostHogProvider } from "@/app/_analytics/provider";

export const metadata = {
title: "Homelab Connector",
Expand All @@ -26,27 +27,29 @@ export default function RootLayout({
baseTheme: dark,
}}
>
<html
suppressHydrationWarning
lang="en"
className={cn(
"min-h-screen bg-background font-sans antialiased",
GeistSans.variable,
)}
>
<body className="h-screen">
<ThemeProvider attribute="class" defaultTheme="dark">
<div className="flex h-full flex-col gap-12 md:gap-0">
<TopNav />
<div className="flex h-full flex-col items-center p-4 px-6">
{children}
<CSPostHogProvider>
<html
suppressHydrationWarning
lang="en"
className={cn(
"min-h-screen bg-background font-sans antialiased",
GeistSans.variable,
)}
>
<body className="h-screen">
<ThemeProvider attribute="class" defaultTheme="dark">
<div className="flex h-full flex-col gap-12 md:gap-0">
<TopNav />
<div className="flex h-full flex-col items-center p-4 px-6">
{children}
</div>
</div>
</div>
{modal}
<div id="modal-root" />
</ThemeProvider>
</body>
</html>
{modal}
<div id="modal-root" />
</ThemeProvider>
</body>
</html>
</CSPostHogProvider>
</ClerkProvider>
);
}

0 comments on commit c7401ca

Please sign in to comment.