Get started

vue-gtag documentation page

The global site tag (gtag.js) is a JavaScript tagging framework and API that allows you to send event data to Google Analytics, Google Ads, and Google Marketing Platform. For general gtag.js documentation, read the gtag.js developer guide.

Install

Requires Vue 3 installed

$ npm add vue-gtag

Add plugin to your application

This is the basic installation required to start using the plugin

import { createApp } from "vue";
import App from "./App.vue";
import VueGtag from "vue-gtag";

createApp(App).use(VueGtag, {
  config: { id: "GA_MEASUREMENT_ID" }
}).mount("#app");

After this, you will be able to use the gtag the library inside your components contextually by accessing it like this example

export default {
  name: 'MyComponent',

  methods: {
    login () {
      this.$gtag.event('login', { method: 'Google' })
    }
  }
}

Since in Vue 3, the setup method doesn't have access to this.$gtag but you can import the method you need like this

import { event } from 'vue-gtag'

export default {
  name: 'MyComponent',

  setup() {
    const login = () => {
      event('login', { method: 'Google' })
    }
    
    return {
      login
    }
  }
}

Initial config parameters

The config object also accepts a params property that will add additional parameters to your initial config call

import { createApp } from "vue";
import App from "./App.vue";
import VueGtag from "vue-gtag";

createApp(App).use(VueGtag, {
  config: { 
    id: "GA_MEASUREMENT_ID",
    params: {
      anonymize_ip: true
    }
  }
}).mount("#app");

Last updated

Was this helpful?