https://github.com/sergiodxa/next-ga
Next.js HOC to integrate Google Analytics on every page change
https://github.com/sergiodxa/next-ga
analytics ga google-analytics high-order-component hoc nextjs react
Last synced: 20 days ago
JSON representation
Next.js HOC to integrate Google Analytics on every page change
- Host: GitHub
- URL: https://github.com/sergiodxa/next-ga
- Owner: sergiodxa
- License: mit
- Archived: true
- Created: 2018-06-20T17:11:10.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-22T17:36:24.000Z (over 6 years ago)
- Last Synced: 2025-10-03T21:47:24.352Z (9 months ago)
- Topics: analytics, ga, google-analytics, high-order-component, hoc, nextjs, react
- Language: JavaScript
- Homepage:
- Size: 160 KB
- Stars: 231
- Watchers: 3
- Forks: 25
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-projects - next-ga - Next.js HOC to integrate Google Analytics on every page change ⭐231 `JavaScript` (📦 Legacy & Inactive Projects)
README
# next-ga
> 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: https://github.com/zeit/next.js/tree/canary/packages/next-plugin-google-analytics
Next.js High Order Component to integrate Google Analytics on every page change.
## Usage
Install it
```bash
yarn add next-ga
```
Import it inside your `pages/_app.js`;
```js
import Router from "next/router";
import withGA from "next-ga";
```
Wrap your [custom App container](https://nextjs.org/docs#custom-%3Capp%3E) with it
```js
// 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.