Skip to content

Commit

Permalink
add fathom analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
coffee-cup committed May 13, 2021
1 parent 52df8cf commit b8c9ec8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"copy-to-clipboard": "^3.3.1",
"fathom-client": "^3.0.0",
"marked": "2.0.3",
"next": "10.2.0",
"next-seo": "^4.24.0",
Expand Down
31 changes: 31 additions & 0 deletions src/hooks/useFathom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Fathom from "fathom-client";
import { useRouter } from "next/router";
import { useEffect } from "react";

const fathomCode = process.env.NEXT_PUBLIC_FATHOM_CODE;
const siteURL = process.env.NEXT_PUBLIC_SITE_URL;

export const useFathom = () => {
const router = useRouter();

useEffect(() => {
if (!fathomCode || !siteURL) return;

// Initialize Fathom when the app loads
Fathom.load(fathomCode, {
url: "https://kiwi.railway.app/script.js",
includedDomains: [siteURL],
});

const onRouteChangeComplete = () => {
Fathom.trackPageview();
};
// Record a pageview when route changes
router.events.on("routeChangeComplete", onRouteChangeComplete);

// Unassign event listener
return () => {
router.events.off("routeChangeComplete", onRouteChangeComplete);
};
}, []);
};
3 changes: 3 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import React from "react";
import { SEO } from "../components/SEO";
import { GlobalStyles } from "../styles/GlobalStyles";
import { TwinGlobalStyles } from "../styles/TwinGlobalStyles";
import { useFathom } from "../hooks/useFathom";

const MyApp = ({ Component, pageProps }: AppProps) => {
useFathom();

return (
<>
<GlobalStyles />
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,11 @@ fastq@^1.6.0:
dependencies:
reusify "^1.0.4"

fathom-client@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/fathom-client/-/fathom-client-3.0.0.tgz#409c047cf1e2fea45b148e28d50fcd635e992893"
integrity sha512-d0oH2SHWCMIVLbbegB7nBIjSvbqbHrZBZxIOWSVAxlJL/roL0Ah9NNb6rTIcKMlA4gov9AjWQGEcZRzlnGc3XQ==

fd-slicer@~1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/fd-slicer/-/fd-slicer-1.1.0.tgz#25c7c89cb1f9077f8891bbe61d8f390eae256f1e"
Expand Down

0 comments on commit b8c9ec8

Please sign in to comment.