Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions apps/website/app/PostHogPageView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect, Suspense } from "react";
import { usePostHog } from "posthog-js/react";

function PostHogPageView() {
const pathname = usePathname();
const searchParams = useSearchParams();
const posthog = usePostHog();

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;
}

export default function SuspendedPostHogPageView() {
return (
<Suspense fallback={null}>
<PostHogPageView />
</Suspense>
);
}
5 changes: 4 additions & 1 deletion apps/website/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import "./globals.css";
import { PostHogProvider } from "./providers";

export const metadata: Metadata = {
title: "Discourse Graphs | A Tool for Collaborative Knowledge Synthesis",
Expand All @@ -24,7 +25,9 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<body className={`antialiased`}>{children}</body>
<body className={`antialiased`}>
<PostHogProvider>{children}</PostHogProvider>
</body>
</html>
);
}
29 changes: 29 additions & 0 deletions apps/website/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use client";

import posthog from "posthog-js";
import { PostHogProvider as PHProvider } from "posthog-js/react";
import { useEffect } from "react";
import PostHogPageView from "./PostHogPageView";

if (
!process.env.NEXT_PUBLIC_POSTHOG_KEY ||
!process.env.NEXT_PUBLIC_POSTHOG_HOST
) {
throw new Error("PostHog environment variables are not set");
}

export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST!,
capture_pageview: false,
});
}, []);

return (
<PHProvider client={posthog}>
<PostHogPageView />
{children}
</PHProvider>
);
}
57 changes: 48 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"prettier-plugin-tailwindcss"
],
"tailwindConfig": "./packages/tailwind-config/tailwind.config.ts"
},
"dependencies": {
"posthog-js": "^1.203.1"
}
}