forked from railwayapp/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFathom.ts
More file actions
26 lines (22 loc) · 740 Bytes
/
Copy pathuseFathom.ts
File metadata and controls
26 lines (22 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as Fathom from "fathom-client";
import { useRouter } from "next/router";
import { useEffect } from "react";
export const useFathom = (trackingCode: string, siteUrl: string) => {
const router = useRouter();
useEffect(() => {
// Initialize Fathom when the app loads
Fathom.load(trackingCode, {
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);
};
}, []);
};