Vue Gtag Next
vue-gtag-next is a plugin designed for Vue 3 to integrate Google's Global Site Tag (gtag.js), enabling event data collection for services like Google Analytics and Google Ads. The package's current stable version is 1.14.0, last released in October 2020. According to its README from that period, it explicitly stated "it's not production ready" and was developed when "Vue 3 is not out yet." This indicates that development ceased early in Vue 3's lifecycle, and the package has not received updates to keep pace with subsequent Vue 3 releases or changes in gtag.js. Its release cadence has effectively stopped. While it aimed to provide a convenient wrapper for gtag.js within a Vue 3 application, its unmaintained status and early development phase mean it lacks long-term support and may have compatibility issues with current Vue 3 versions, making it a potentially risky choice for modern applications compared to actively maintained alternatives.
Common errors
-
Error: "gtag" is not defined
cause The gtag.js script was not correctly loaded or initialized before the VueGtagNext plugin attempted to use it, or the plugin itself failed to initialize.fixEnsure the VueGtagNext plugin is correctly installed with `app.use()` and that the `property.id` configuration is valid. Check network requests to confirm `gtag.js` is loading. -
TypeError: app.use is not a function
cause This typically indicates that `app` is not a valid Vue 3 application instance, or you are attempting to use `app.use` on a non-Vue 3 environment.fixEnsure you are importing `createApp` from 'vue' and calling `createApp(App)` to create your application instance before calling `app.use()`. -
Cannot read properties of undefined (reading 'event') when using useGtag()
cause The `useGtag` composable was called outside of a component's setup context or before the `VueGtagNext` plugin was installed on the Vue application instance.fixEnsure `app.use(VueGtagNext, { ... })` is called before your component that uses `useGtag` is mounted. Call `useGtag()` only within the `setup()` function of a Vue component.
Warnings
- breaking The package explicitly states it is 'not production ready' and was developed when 'Vue 3 is not out yet' (as of October 2020). It has not received updates since 2020, making it highly likely to be incompatible with modern Vue 3 versions and best practices.
- gotcha Lack of maintenance since October 2020 means the package may not include updates to the gtag.js specification, potentially leading to outdated or incorrect tracking implementations for newer Google Analytics features (e.g., GA4 specific events).
- gotcha Due to its abandoned status, there is no ongoing security auditing or bug fixes. Using unmaintained tracking libraries can pose supply chain security risks if vulnerabilities are discovered and not patched.
Install
-
npm install vue-gtag-next -
yarn add vue-gtag-next -
pnpm add vue-gtag-next
Imports
- default
const VueGtagNext = require('vue-gtag-next')import VueGtagNext from 'vue-gtag-next'
- useGtag
import { useGtag } from 'vue-gtag-next' - gtag
import { gtag } from 'vue-gtag-next'
Quickstart
import { createApp } from 'vue';
import VueGtagNext, { useGtag } from 'vue-gtag-next';
const App = {
template: `
<div>
<h1>Google Analytics Demo</h1>
<button @click="sendPageView">Send Page View</button>
<button @click="sendEvent">Send Custom Event</button>
</div>
`,
setup() {
const { event } = useGtag();
const sendPageView = () => {
event('page_view', {
page_title: 'Home Page',
page_path: '/',
});
console.log('Page view sent!');
};
const sendEvent = () => {
event('button_click', {
event_category: 'engagement',
event_label: 'Call to Action',
value: 1,
});
console.log('Custom event sent!');
};
return { sendPageView, sendEvent };
},
};
const app = createApp(App);
app.use(VueGtagNext, {
property: {
id: process.env.VUE_APP_GA_ID ?? 'G-XXXXXXXXXX' // Replace with your actual GA ID
},
enabled: true, // Enable or disable tracking
bootstrap: true, // Inject gtag.js script automatically
});
app.mount('#app');