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.
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.