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.
Before starting, make sure to know the basics of Google global site tag (gtag.js) already. Important parts of the plugin API have their link to the official documentation to help you out.
Whenever you don't understand something, and the documentation lacks information, create an issue on Github.

Install

Requires Vue 3 installed
1
$ npm add vue-gtag
Copied!

Add plugin to your application

This is the basic installation required to start using the plugin
1
import { createApp } from "vue";
2
import App from "./App.vue";
3
import VueGtag from "vue-gtag";
4
5
createApp(App).use(VueGtag, {
6
config: { id: "GA_MEASUREMENT_ID" }
7
}).mount("#app");
Copied!
This is just an example by using the Google Analytics domain ID as a configuration ID, but it is possible to use things like Google Adwords as well, so make sure to read the gtag documentation if you need more insights about what type of tracking code you need to use
After this, you will be able to use the gtag the library inside your components contextually by accessing it like this example
1
export default {
2
name: 'MyComponent',
3
4
methods: {
5
login () {
6
this.$gtag.event('login', { method: 'Google' })
7
}
8
}
9
}
Copied!
Since in Vue 3, the setup method doesn't have access to this.$gtag but you can import the method you need like this
1
import { event } from 'vue-gtag'
2
3
export default {
4
name: 'MyComponent',
5
6
setup() {
7
const login = () => {
8
event('login', { method: 'Google' })
9
}
10
11
return {
12
login
13
}
14
}
15
}
Copied!

Initial config parameters

The config object also accepts a params property that will add additional parameters to your initial config call
1
import { createApp } from "vue";
2
import App from "./App.vue";
3
import VueGtag from "vue-gtag";
4
5
createApp(App).use(VueGtag, {
6
config: {
7
id: "GA_MEASUREMENT_ID",
8
params: {
9
anonymize_ip: true
10
}
11
}
12
}).mount("#app");
Copied!
Last modified 3mo ago