This commit is contained in:
Nicholas Koben Kao 2024-01-19 19:38:35 -08:00
parent b2690ab2c5
commit a2d0e93172
6 changed files with 78 additions and 15 deletions

View File

@ -16,4 +16,7 @@ SPACES_CDN_FORCE_PATH_STYLE="true"
MODAL_BUILDER_URL=
JWT_SECRET="openssl rand -hex 32"
PLAUSIBLE_DOMAIN=
PLAUSIBLE_DOMAIN=
NEXT_PUBLIC_POSTHOG_KEY="your-api-key"
NEXT_PUBLIC_POSTHOG_HOST="your-ph-address"

Binary file not shown.

View File

@ -76,6 +76,8 @@
"next-themes": "^0.2.1",
"next-usequerystate": "^1.13.2",
"pg": "^8.11.3",
"posthog-js": "^1.100.0",
"posthog-node": "^3.5.0",
"react": "^18",
"react-day-picker": "^8.9.1",
"react-dom": "^18",

View File

@ -0,0 +1,29 @@
// app/PostHogPageView.tsx
'use client'
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import { usePostHog } from 'posthog-js/react';
export default function PostHogPageView() {
const pathname = usePathname();
const searchParams = useSearchParams();
const posthog = usePostHog();
// Track pageviews
useEffect(() => {
if (pathname && posthog) {
let url = window.origin + pathname
if (searchParams.toString()) {
url = url + `?${searchParams.toString()}`
}
posthog.capture(
'$pageview',
{
'$current_url': url,
}
)
}
}, [pathname, searchParams, posthog])
return null
}

View File

@ -7,6 +7,13 @@ import meta from "next-gen/config";
import PlausibleProvider from "next-plausible";
import { Inter } from "next/font/google";
import { Toaster } from "sonner";
import { PHProvider } from "./providers";
import dynamic from "next/dynamic";
const PostHogPageView = dynamic(() => import("./PostHogPageView"), {
ssr: false,
});
const inter = Inter({ subsets: ["latin"] });
@ -39,20 +46,23 @@ export default function RootLayout({
<PlausibleProvider domain={process.env.PLAUSIBLE_DOMAIN} />
</head>
)}
<body className={inter.className}>
<main className="w-full flex min-h-[100dvh] flex-col items-center justify-start">
<div className="z-[-1] fixed h-full w-full bg-white">
<div className="absolute h-full w-full bg-[radial-gradient(#e5e7eb_1px,transparent_1px)] [background-size:16px_16px] [mask-image:radial-gradient(ellipse_50%_50%_at_50%_50%,#000_70%,transparent_100%)]" />
</div>
<div className="sticky w-full h-18 flex items-center justify-between gap-4 p-4 border-b border-gray-200">
<Navbar />
</div>
<div className="md:px-10 px-6 w-full h-[calc(100dvh-73px)]">
{children}
</div>
<Toaster richColors />
</main>
</body>
<PHProvider>
<body className={inter.className}>
<PostHogPageView />
<main className="w-full flex min-h-[100dvh] flex-col items-center justify-start">
<div className="z-[-1] fixed h-full w-full bg-white">
<div className="absolute h-full w-full bg-[radial-gradient(#e5e7eb_1px,transparent_1px)] [background-size:16px_16px] [mask-image:radial-gradient(ellipse_50%_50%_at_50%_50%,#000_70%,transparent_100%)]" />
</div>
<div className="sticky w-full h-18 flex items-center justify-between gap-4 p-4 border-b border-gray-200">
<Navbar />
</div>
<div className="md:px-10 px-6 w-full h-[calc(100dvh-73px)]">
{children}
</div>
<Toaster richColors />
</main>
</body>
</PHProvider>
</TooltipProvider>
</ClerkProvider>
</html>

View File

@ -0,0 +1,19 @@
// app/providers.tsx
'use client'
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'
if (typeof window !== 'undefined') {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: false // Disable automatic pageview capture, as we capture manually
})
}
export function PHProvider({
children,
}: {
children: React.ReactNode
}) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>
}