diff --git a/bun.lockb b/bun.lockb index 068b576..70b9ad3 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 8fa000d..e16febc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/_analytics/provider.tsx b/src/app/_analytics/provider.tsx new file mode 100644 index 0000000..13dcf18 --- /dev/null +++ b/src/app/_analytics/provider.tsx @@ -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 ( + + {children} + + ); +} + +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; +} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index f9918c6..d17459a 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -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", @@ -26,27 +27,29 @@ export default function RootLayout({ baseTheme: dark, }} > - - - -
- -
- {children} + + + + +
+ +
+ {children} +
-
- {modal} -