1.1.0 • Published 5 years ago
next-simple-google-analytics v1.1.0
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 MainDocumentimport 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-analyticsContribute
- 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