Skip to content

Commit

Permalink
add fathom
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Nov 20, 2023
1 parent 43e8099 commit 1071ac9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "next start"
},
"dependencies": {
"fathom-client": "3.6.0",
"iron-session": "workspace:*",
"next": "14.0.3",
"react": "^18",
Expand Down
37 changes: 37 additions & 0 deletions examples/next/src/app/fathom.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"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();

// Load the Fathom script on mount
useEffect(() => {
load("YKGUEAZB", {
auto: false,
});
}, []);

// Record a pageview when route changes
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>
);
}
6 changes: 5 additions & 1 deletion examples/next/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import Fathom from "./fathom";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -16,7 +17,10 @@ export default function RootLayout({
}) {
return (
<html lang="en">
<body className={`${inter.className} bg-gray-50`}>{children}</body>
<body className={`${inter.className} bg-gray-50`}>
<Fathom />
{children}
</body>
</html>
);
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 1071ac9

Please sign in to comment.