by sergiodxa

sergiodxa / next-ga

Next.js HOC to integrate Google Analytics on every page change

226 Stars 27 Forks Last release: almost 2 years ago (2.3.4) MIT License 56 Commits 15 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


The Next.js team has an official plugin being developed, while plugin supports it's still in canary only, the way the plugin works it's simpler than next-ga and I recommend you to check it and do it manually in your project until it's stable.

Plugin link:

Next.js High Order Component to integrate Google Analytics on every page change.


Install it

yarn add next-ga

Import it inside your

import Router from "next/router";
import withGA from "next-ga";

Wrap your custom App container with it

// pass your GA code as first argument
export default withGA("UA-xxxxxxxxx-1", Router)(MyApp);

That's it, now when the user access a page it will log a pageview to Google Analytics, each page change after that will also trigger a pageview on Google Analytics.

Note: This module detects if it's running in localhost and do nothing there to avoid polluting your analytics with local data.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.