Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-tag-manager

cajames4.7kMIT0.1.2TypeScript support: included

Easy to use Google Tag Manager implementation for Vue

vue, tag manager, google tag manager, analytics, gtm

readme

Vue Tag Manager

Build Status Coverage Status

Simple Google Tag Manager implementation for Vue

Usage

Simple setup:

import Vue from "vue"
import VueTagManager from "vue-tag-manager"

// Do this before creating the Vue app
Vue.use(VueTagManager, {
    gtmId: 'GTM-XXXXXX'
})

new Vue({...})

Later in app:

Vue.gtm.push({ event: 'some-event' })

// or in a component
this.$gtm.push({ event: 'some-event' })

or in a template:

<button @click="$gtm.push({ event: 'some-event' })">

Configuration Options

Option Description Type Default Is Required?
gtmId Google Tag Manager ID string - Yes
queryParams Additional parameters to add to the script URL, including gtm_preview and gtm_auth for environment switching object {} No
dataLayer Initialise the GTM datalayer with variables. object undefined No
dataLayerName The Data Layer variable name string "dataLayer" No

Usage

push(entry: Object)

const entry = {event: 'something'}
this.$gtm.push(entry)
// or
Vue.gtm.push(entry)

On a TagManager instance, this method allows you to push events onto the initialised data layer. See the Google Tag Manager documentation for more details on event tracking with GTM.

Native Implementation

While this library is mainly for Vue, the core Tag Manager is just a javascript object, so it's been exported too. It can be used as such:

<head>
    <script src="https://unpkg.com/vue-tag-manager@x.x.x/lib/TagManager.js"></script>
    <script>
        VueTagManager.initialize({
            gtmId: 'GTM-KG7LSMH'
        })
    </script>
</head>

<!-- In code later -->
<script>
    window.TagManager.push({event: "something"})
</script>

To-do

  • <input checked="" disabled="" type="checkbox"> Initital implementation from React GTM
  • <input checked="" disabled="" type="checkbox"> Basic Vue Plugin
  • <input checked="" disabled="" type="checkbox"> Shell out tests
  • <input checked="" disabled="" type="checkbox"> Simplify the URL + snippet generation and injection
  • <input checked="" disabled="" type="checkbox"> Vue prototype property
  • <input disabled="" type="checkbox"> Basic implementation - don't break on SSR
  • <input disabled="" type="checkbox"> Vue auto-track router
  • <input disabled="" type="checkbox"> Vue track directive
  • <input disabled="" type="checkbox"> More through docs with examples
  • <input disabled="" type="checkbox"> SSR support

Why need another GTM library?

Initially used vue-gtm which is a great library, but wanted to be able to programatically configure the GTMID and initial DataLayer.

Really liked the React GTM script injection dev experience, and liked the Vue GTM dev and track experience (as a Vue Plugin, with the $gtm interface). So this plugin aims to combine both of them into this simple library.

Inspired by

changelog

0.1.2 (2018-08-14)

Bug Fixes

  • Fixed esm input and output paths (24e7847)
  • updating rollup config to use input (0480c94)

0.1.1 (2018-08-03)

Bug Fixes

  • fixed esm export. Changed from webpack to rollup (57d5439)

0.1.0 (2018-08-02)

Bug Fixes

  • adding baseurl for config (ac51640)
  • gtmId should initialise as null (71e1321)
  • injector (c8b285f)
  • Make sure using umd to export lib (510665c)

Features

  • Upgrade both native and vue plugin entries (#3) (648da7e)

0.0.3 (2018-07-25)

Features

  • add root umd export for Tag Manager (d3ee16a)

0.0.2 (2018-07-25)

Features

  • add types generation (75e12b5)
  • initial library based on React google tag manager (29583ae)
  • Separate export of the tag manager itself (1755c00)
  • written the basic vue plugin wrapper (353e30f)