Code Monkey home page Code Monkey logo

google-gtag-module's Introduction

@nuxtjs/google-gtag

npm version npm downloads Circle CI Codecov License

Google official gtagjs for Nuxt.js

๐Ÿ“– Release Notes

Attention

This project is looking for maintainers. Please see RFC for future of module.

Features

The module includes Google googletagmanager.com/gtag/js into your project and enables it with config you pass in as options.

  • Check the official reference gtagjs

Setup

  1. Add @nuxtjs/google-gtag dependency to your project
yarn add @nuxtjs/google-gtag # or npm install @nuxtjs/google-gtag
  1. Add @nuxtjs/google-gtag to the modules section of nuxt.config.js
{
  modules: [
    // Simple usage
    '@nuxtjs/google-gtag',

    // With options
    ['@nuxtjs/google-gtag', { /* module options */ }]
  ]
}

Using top level options

{
  modules: [
    '@nuxtjs/google-gtag'
  ],
  'google-gtag': {
    id: 'UA-XXXX-XX',
    config: {
      anonymize_ip: true, // anonymize IP 
      send_page_view: false, // might be necessary to avoid duplicated page track on page reload
      linker: {
        domains: ['domain.com','domain.org']
      }
    },
    debug: true, // enable to track in dev mode
    disableAutoPageTrack: false, // disable if you don't want to track each page route with router.afterEach(...).
    additionalAccounts: [{
      id: 'AW-XXXX-XX', // required if you are adding additional accounts
      config: {
        send_page_view: false // optional configurations
      }
    }]
  }
}

Options

id (required)

Google Analytics property ID.

config

  • Default: {}

Config options for gtagjs

debug

  • Default: false

Enable to track in dev mode.

disableAutoPageTrack

  • Default: false

Disable if you don't want to track each page route with router.afterEach(...).

additionalAccounts

  • Default: []

You can add more configuration like AdWords

Usage

This module includes Google gtag in your NuxtJs project and enables every page tracking by default. You can use gtag inside of your components/functions/methods like follow:

this.$gtag('event', 'your_event', { /* track something awesome */})

To make sure that every page is tracked correctly

As the router code sometimes runs before head data is set correctly you can use following approach to make sure that everything is set correctly:

// make sure to set disableAutoPageTrack: true inside of nuxt.config.js
// inside of your Page.vue/Layout.vue file
 mounted() {
    if (process.browser) {
      this.$gtag('config', 'UA-XXXX-XXX', {
        page_title: this.$metaInfo.title,
        page_path: this.$route.fullPath,
      })
    }
  }

See official docs:

Check functionalities

Install Google Tag Assistant and see if your page is being tracked.

Development

  • Clone this repository
  • Install dependencies using yarn install or npm install
  • Start development server using npm run dev

License

MIT License

Copyright (c) Nuxt Community

google-gtag-module's People

Contributors

dohomi avatar dependabot[bot] avatar ricardogobbosouza avatar juno-w avatar mehidi258 avatar pi0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.