next-simple-google-analytics
v1.1.0
Published
Add Google Analytics to your Next.js site in a jiffy
Downloads
105
Maintainers
Readme
next-simple-google-analytics
Add Google Analytics to your Next.js site in a jiffy
Table of Contents
About
This module is heavily based on the official example.
Usage
Add the default export to the Head
of _document.js
:
import Document, { Html, Head, Main, NextScript } from 'next/document'
import GoogleAnalytics from 'next-simple-google-analytics'
class MainDocument extends Document {
static async getInitialProps (ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render () {
return (
<Html lang='en'>
<Head>
<GoogleAnalytics id="UA-X00XXX0X-X"/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MainDocument
import GoogleAnalytics from 'next-simple-google-analytics'
GoogleAnalytics.pageview('/foo/bar', {
foo: true
})
GoogleAnaltyics.event('didthing', {
category, label, value
})
Route changes are required using routeChangeComplete
.
Install
This project uses node and npm.
$ npm install next-simple-google-analytics
$ # OR
$ yarn add next-simple-google-analytics
Contribute
- Fork it and create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am "Add some feature"
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
License
MIT © Tiaan du Plessis