Fathom Analytics is a privacy-focused alternative to traditional analytics tools. This guide will show you how to integrate Fathom with your Next.js application.
First, install the Fathom client package:
Create a new file called app/fathom.tsx in your project:
This component does the following:
- Loads the Fathom script when the component mounts
- Tracks pageviews when the route changes
Note: Ensure you add your Fathom ID to your .env file.
Add the Fathom component to your root app/layout.tsx file:
- The Fathom component is a client component (e.g.
"use client") - We use
usePathname()anduseSearchParams()to track route changes - The Fathom script is loaded with
auto: falseto prevent automatic pageview tracking
To track custom events, you can use the trackEvent function from fathom-client:
Replace 'GOAL_ID' with the actual goal ID from your Fathom dashboard.
By following this guide, you'll have Fathom Analytics integrated into your Next.js application, providing privacy-focused analytics for your site.