vue-gtag
Github
v3
v3
  • Get started
  • gtag.js consent management
  • Page tracker
  • Multiple domain tracking
  • Plugin hooks
  • Global namespace
  • Access gtag instance directly
  • Plugin options
  • Migration v2 to v3
  • Methods
    • pageview
    • event
    • consent
    • consentGrantedAll
    • consentDeniedAll
    • query
    • screenview
    • customMap
    • ecommerce
    • linker
    • set
    • time
    • config
    • exception
Powered by GitBook
On this page
  • installation
  • Multiple account tracking
  • Plugin hooks: onReady and onError
  • Auto tracking
  • Deferred plugin initialization

Migration v2 to v3

PreviousPlugin optionsNextpageview

Last updated 1 month ago

All functionality from version v2 is preserved in v3, although most options have been renamed and organized by their scope. The primary focus has been on rewriting the entire codebase in TypeScript to enhance the developer experience and ensure better type safety.

In addition, new methods have been added like , , , , and the composable.

vue-gtag is ESM-only. Check out article if you want to know more about the reasons behind ESM-only builds.

installation

v2

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");

v3

I suggest using the `configure` method to install vue-gtag, as it has a smaller initial bundle size and won't add all methods to the Vue instance. While you won't be able to use $gtag in your templates, you can still import each method individually as needed.

import { configure } from "vue-gtag";

configure({
  tagId: "GA_MEASUREMENT_ID"
})

Use the `createGtag` method if you wish to install global properties. This will increase the bundle size because all methods will be bundled together when injected into the Vue instance. You will be able to use $gtag in your template.

import { createApp } from 'vue'
import { createGtag } from "vue-gtag";

const gtag = createGtag({
  tagId: "GA_MEASUREMENT_ID"
})

const app = createApp({ ... })
app.use(gtag)

The installation no longer requires the App instance, and it can be triggered anywhere in your app if you don't need global events.


Multiple account tracking

v2

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

createApp(App).use(VueGtag, {
  includes: [
    { id: 'GA_MEASUREMENT_ID_2' },
    { 
      id: 'GA_MEASUREMENT_ID_3',
      params: {
        anonymize_ip: true
      }
    }
  ],
  config: { id: "GA_MEASUREMENT_ID_1" }
}).mount("#app");

v3

import { createGtag } from "vue-gtag";

createGtag({
  tagId: "GA_MEASUREMENT_ID",
  additionalAccounts: [
    { tagId: 'GA_MEASUREMENT_ID_2' },
    { 
      tagId: 'GA_MEASUREMENT_ID_3',
      config: {
        anonymize_ip: true
      }
    }
  ],
})

Plugin hooks: onReady and onError

v2

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

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

v3

import { createGtag } from "vue-gtag";

createGtag({
  config: { id: "GA_MEASUREMENT_ID" },
  hooks: {
    "script:loaded": () => {},
    "script:error": () => {}
  }
})

Auto tracking

v2

import { createApp } from "vue";
import VueGtag from "vue-gtag";
import App from "./App.vue";
import router from './router'
​
const app = createApp(App);
​
app.use(router);
​
app.use(VueGtag, {
  config: { 
    id: "GA_MEASUREMENT_ID",
  },
}, router); // <----- add your router here
​
app.mount("#app");

v3

import { createGtag } from "vue-gtag";
import router from './router'

​createGtag({
  tagId: 'GA_MEASUREMENT_ID',
  pageTracker: {
    router, // <----- add your router here
  }
})

Deferred plugin initialization

v2

import { createApp } from "vue";
import VueGtag from "vue-gtag";
import App from "./App.vue";
​
const app = createApp(App);
​
app.use(VueGtag, {
  bootstrap: false,
  config: { 
    id: "GA_MEASUREMENT_ID",
  },
})
​
app.mount("#app");
// component.vue
<script lang="ts" setup>
  import { bootstrap } from 'vue-gtag'
</script>

<template>
  <button @click="bootstrap">Add tracking</button>
</template>

v3

// main.ts
import { createGtag } from "vue-gtag";
import router from './router'

​createGtag({
  tagId: 'GA_MEASUREMENT_ID',
  initMode: 'manual'
})
// component.vue
<script lang="ts" setup>
  import { addGtag } from 'vue-gtag'
</script>

<template>
  <button @click="addGtag">Add tracking</button>
</template>

With v3, it is now possible to use the built-in composable, which will bootstrap gtag.js and provide methods to directly accept or deny user consent.

For more details about hooks, check the page

The auto-tracking feature moved to a separate object. Check the page

Instead of manually calling addGtag, a more GDPR-compliant approach is to use the composable. This will facilitate consent handling, cookie checks, and adding gtag to your app.

consent
consentGrantedAll
consentDeniedAll
ecommerce
useConsent
this
useConsent
Plugin hooks
Page tracker
useConsent