Analytics with Next.js 13

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.

Th…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sebastian Sdorra

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.

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:

import { FC, PropsWithChildren } from "react";
import Script from "next/script";
import Analytics from "./Analytics";

const RootLayout: FC<PropsWithChildren> = ({ children }) => (
  <html>
    <body>
      <main>{children}</main>
      <Analytics />
      <Script id="onRouteChange">{`
        (function (history) {
          var pushState = history.pushState;
          history.pushState = function(state){
            var result = pushState.apply(history, arguments);
            window.dispatchEvent(new Event("routeChange", state));
            return result;
          };
        })(window.history);
      `}</Script>
    </body>
  </html>
);

export default RootLayout;

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).

"use client";

import { useEffect } from "react";
import * as Fathom from "fathom-client";

const Analytics = () => {
  useEffect(() => {
    Fathom.load("YOUR_FATHOM_TRACKING_CODE", {
      includedDomains: ["yourdomain.com"],
    });

    const onRouteChange = () => Fathom.trackPageview();

    window.addEventListener("routeChange", onRouteChange);
    return () => window.removeEventListener("routeChange", onRouteChange);
  }, []);

  return null;
};

export default Analytics;

We have to use a custom component instead of a hook in order to annotate the component with use client.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sebastian Sdorra


Print Share Comment Cite Upload Translate Updates
APA

Sebastian Sdorra | Sciencx (2022-12-05T21:42:04+00:00) Analytics with Next.js 13. Retrieved from https://www.scien.cx/2022/12/05/analytics-with-next-js-13/

MLA
" » Analytics with Next.js 13." Sebastian Sdorra | Sciencx - Monday December 5, 2022, https://www.scien.cx/2022/12/05/analytics-with-next-js-13/
HARVARD
Sebastian Sdorra | Sciencx Monday December 5, 2022 » Analytics with Next.js 13., viewed ,<https://www.scien.cx/2022/12/05/analytics-with-next-js-13/>
VANCOUVER
Sebastian Sdorra | Sciencx - » Analytics with Next.js 13. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/05/analytics-with-next-js-13/
CHICAGO
" » Analytics with Next.js 13." Sebastian Sdorra | Sciencx - Accessed . https://www.scien.cx/2022/12/05/analytics-with-next-js-13/
IEEE
" » Analytics with Next.js 13." Sebastian Sdorra | Sciencx [Online]. Available: https://www.scien.cx/2022/12/05/analytics-with-next-js-13/. [Accessed: ]
rf:citation
» Analytics with Next.js 13 | Sebastian Sdorra | Sciencx | https://www.scien.cx/2022/12/05/analytics-with-next-js-13/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.