next-simple-google-analytics

1.1.0 • Public • Published

next-simple-google-analytics

package version package downloads standard-readme compliant package license make a pull request

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

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am "Add some feature"
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT © Tiaan du Plessis

Package Sidebar

Install

npm i next-simple-google-analytics

Weekly Downloads

40

Version

1.1.0

License

MIT

Unpacked Size

239 kB

Total Files

9

Last publish

Collaborators

  • tiaanduplessis