3 min read
We will explore how to integrate analytic tools such as Google Analytics, Matomo or Fathom with the Next.js 13 app directory. In this article we will use Fathom, but the same principles could be applied to Google Analytics, Matomo or similar tools.
In the case of Fathom, I have since found a simpler solution. But the solution described here should still work and especially with adaptations for other tools.
The new app directory of Next.js 13 comes with a lot of exiting new features,
but the new router (next/navigation
) unfortunately does not support events.
So we have to track route changes by ourselves. My first idea was to use the history api, but the history api does not send events for route changes!
The last option we have now is to override the pushState method and send an custom event:
var pushState = history.pushState;history.pushState = function (state) { var result = pushState.apply(history, arguments); window.dispatchEvent(new Event("routeChange", state)); return result;};
The snippet above overrides the pushState
, calls the original function and
sends a routeChange
event afterwards.
Next.js
In Next.js we could use our method with the Script
tag:
And now we are able to notify fathom about the route changes.
We do this with a custom component (Analytics
), which is used in our root layout (see snippet above).
We have to use a custom component instead of a hook in order to annotate the component with use client.
Fathom SPA mode
The method described in this post can be significantly simplified for Fathom. I'm not sure if Fathom introduced the feature after writing the post or if I just missed it. Fathom has a SPA mode that does the same thing we did in our script. So we can remove the script from the layout and simplify our Analytics component as follows:
Posted in: nextjs, analytics, fathom, react