From 25799e446197c238989cc748e0bd7f350a1d6d67 Mon Sep 17 00:00:00 2001
From: Yuval Adam <_@yuv.al>
Date: Mon, 23 Sep 2024 16:20:18 +0200
Subject: Add fathom analytics client

---
 app/components/Fathom.tsx | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 app/components/Fathom.tsx

(limited to 'app/components/Fathom.tsx')

diff --git a/app/components/Fathom.tsx b/app/components/Fathom.tsx
new file mode 100644
index 0000000..4dbd21b
--- /dev/null
+++ b/app/components/Fathom.tsx
@@ -0,0 +1,35 @@
+'use client';
+
+import { load, trackPageview } from 'fathom-client';
+import { useEffect, Suspense } from 'react';
+import { usePathname, useSearchParams } from 'next/navigation';
+
+function TrackPageView() {
+  const pathname = usePathname();
+  const searchParams = useSearchParams();
+
+  useEffect(() => {
+    load('MBXPLVRM', {
+      auto: false
+    });
+  }, []);
+
+  useEffect(() => {
+    if (!pathname) return;
+
+    trackPageview({
+      url: pathname + searchParams?.toString(),
+      referrer: document.referrer
+    });
+  }, [pathname, searchParams]);
+
+  return null;
+}
+
+export default function Fathom() {
+  return (
+    <Suspense fallback={null}>
+      <TrackPageView />
+    </Suspense>
+  );
+}
\ No newline at end of file
-- 
cgit v1.3.1

