Detalhes do pacote

@nuxtjs/gtm

nuxt-community186kMIT2.4.0

Google Tag Manager Module for Nuxt.js

readme (leia-me)

@nuxtjs/gtm

npm version npm downloads Checks Codecov License

Google Tag Manager Module for Nuxt.js

📖 Release Notes

ℹ️ If coming from v1 (@nuxtjs/google-tag-manager) please read v2 release notes.

Setup

  1. Add @nuxtjs/gtm dependency to your project
yarn add @nuxtjs/gtm # or npm install @nuxtjs/gtm
  1. Add @nuxtjs/gtm to the modules section of nuxt.config.js
export default {
  modules: [
    '@nuxtjs/gtm',
  ],
  gtm: {
    id: 'GTM-XXXXXXX'
  }
}

Runtime Config

You can use runtime config if need to use dynamic environment variables in production. Otherwise, the options will be hardcoded during the build and won't be read from nuxt.config anymore.

export default {
  modules: [
    '@nuxtjs/gtm'
  ],

  gtm: {
    id: 'GTM-XXXXXXX', // Used as fallback if no runtime config is provided
  },

  publicRuntimeConfig: {
    gtm: {
      id: process.env.GOOGLE_TAG_MANAGER_ID
    }
  },
}

Options

Defaults:

export default {
  gtm: {
    enabled: undefined, /* see below */
    debug: false,

    id: undefined,
    layer: 'dataLayer',
    variables: {},

    pageTracking: false,
    pageViewEventName: 'nuxtRoute',

    autoInit: true,
    respectDoNotTrack: true,

    scriptId: 'gtm-script',
    scriptDefer: false,
    scriptURL: 'https://www.googletagmanager.com/gtm.js',
    crossOrigin: false,

    noscript: true,
    noscriptId: 'gtm-noscript',
    noscriptURL: 'https://www.googletagmanager.com/ns.html'
  }
}

enabled

GTM module uses a debug-only version of $gtm during development (nuxt dev).

You can explicitly enable or disable it using enabled option:

export default {
  gtm: {
    // Always send real GTM events (also when using `nuxt dev`)
    enabled: true
  }
}

debug

Whether $gtm API calls like init and push are logged to the console.

Manual GTM Initialization

There are several use cases that you may need more control over initialization:

  • Block Google Tag Manager before user directly allows (GDPR realisation or other)
  • Dynamic ID based on request path or domain
  • Initialize with multi containers
  • Enable GTM on page level

nuxt.config.js:

export default {
 modules: [
  '@nuxtjs/gtm'
 ],
 plugins: [
  '~/plugins/gtm'
 ]
}

plugins/gtm.js:

export default function({ $gtm, route }) {
  $gtm.init('GTM-XXXXXXX')
}
  • Note: All events will be still buffered in data layer but won't send until init() method getting called.
  • Note: Please consult with Google Tag Manager Docs for more information caveats using multiple containers.

Router Integration

You can optionally set pageTracking option to true to track page views.

Note: This is disabled by default to prevent double events when using alongside with Google Analytics so take care before enabling this option.

The default event name for page views is nuxtRoute, you can change it by setting the pageViewEventName option.

Usage

Pushing events

You can push events into the configured layer:

this.$gtm.push({ event: 'myEvent', ...someAttributes })

Development

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Start development server using yarn dev or GTM_ID=<your gtm id> yarn dev if you want to provide custom GTM_ID.

License

MIT License

Copyright (c) Nuxt.js Community

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.4.0 (2020-11-10)

Features

  • support crossOrigin option (#78) (e843176)
  • module: add support for runtimeConfig (#53) (2bc1735)
  • plugin: add debug option and disable by default (#61) (37f13e5)

Bug Fixes

  • plugin.mock: handle event.eventCallback (#76) (cdb0c5c)
  • fix doNotTrack polyfill (#87) (c69d1cf)
  • module: allow client side push before init (#60) (c9b073a)

2.3.2 (2020-07-23)

Bug Fixes

  • plugin: load SSR events before container loaded (#51) (498f9bd)

2.3.1 (2020-07-23)

Bug Fixes

2.3.0 (2020-06-01)

Features

  • require nuxt > 2.12 to avoid ssr memory leak (43f41c8)
  • use mock version for nuxt dev (ec85699), closes #32

Bug Fixes

  • check respectDoNotTrack option value (#37) (b1a938c)

2.2.3 (2020-03-17)

Bug Fixes

  • prepend router.base when sending page track events (#20) (2940d85)
  • module: warn about head as a function in nuxt config is not supported (#14) (8b37f8d)

2.2.2 (2020-02-25)

Bug Fixes

  • always render noscript block (a921b42)

2.2.1 (2020-02-24)

2.2.0 (2020-02-23)

Features

Bug Fixes

  • add id to noscript iframe (b345db7)
  • call startPageTracking on client only (774fccf)
  • pageTitle is not available on SSR (228d796)

2.1.1 (2020-02-11)

Bug Fixes

  • prevent calling init with initial id when autoInit enabled (24744a5)

2.1.0 (2020-02-11)

Features

  • allow manually init with multiple ids (closes #1, #4, #5) (accfa77)

2.0.1 (2020-02-10)

Bug Fixes

  • guard against double script executation (#3) (970e0e6)

2.0.0 (2020-02-09)

Features

2.3.1 (2019-11-01)

Bug Fixes

  • google-tag-manager: fixed noscript content sanitization (#322) (7531faf)

2.3.0 (2019-10-07)

Features

  • google-tag-manager: add <noscript> fallback (#263) (#300) (f0dd65f)
  • google-tag-manager: add pageViewEventName option (#314) (f718a6a)

2.2.1 (2019-07-13)

Bug Fixes

  • google-tag-manager: fix respectDoNotTrack (#295) (56cb078)

2.2.0 (2019-04-15)

Features

  • google-tag-manager: refactor, dev option and $gtm.pushEvent (#274) (d0ba40f)

2.1.4 (2019-02-12)

Bug Fixes

  • google-tag-manager: fix broken syntax (#262) (0e9e552)

2.1.3 (2019-02-12)

Note: Version bump only for package @nuxtjs/google-tag-manager

2.1.2 (2019-01-11)

Note: Version bump only for package @nuxtjs/google-tag-manager

2.1.1 (2018-12-19)

Bug Fixes

  • google-tag-manager: delay for route changing before send (#221) (a7e1634)

2.1.0 (2018-10-01)

Features

  • google-tag-manager: Allow GTM ID to be function (#219) (e4bf538)

2.0.0 (2018-05-15)

Features

  • google-tag-manager: rework on options and customization (50c1f27)

BREAKING CHANGES

  • google-tag-manager: pageTracking is disabled by default

1.2.0 (2018-04-27)

Features

1.1.0 (2017-11-20)

Features

  • google-tag-manager: removed lodash dependency (#160) (3c5f674)

1.0.1 (2017-07-24)

Bug Fixes

  • google-tag-manager: default 'layer' should be set to 'dataLayer' (#82) (1ed8b76)

0.0.5 (2017-06-06)

0.0.4 (2017-06-02)

0.0.3 (2017-05-31)

0.0.2 (2017-05-30)

0.0.1 (2017-05-29)