Yandex Metrika Plugin for Vue.js
Vue Yandex Metrika is a utility package designed to integrate Yandex Metrika tracking into Vue.js applications. It simplifies the process of sending page view data to Yandex Metrika, offering both automatic tracking when integrated with Vue Router and manual tracking capabilities. The current stable version is v1.8.3, released in September 2019, indicating a lack of active development since then. Its primary differentiator is its direct integration with the Vue ecosystem, abstracting the raw Yandex Metrika API calls. It allows configuration of tracking ID, environment-based tracking (e.g., only in production), script source, debug mode, route ignoring, and other Yandex Metrika counter options, providing a convenient wrapper for web analytics in Vue projects. Given its last update in 2019, it primarily supports older Vue 2 applications and may not be compatible with Vue 3 without community efforts or significant adaptations.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'hit')
cause The $metrika object is not available on the Vue instance, likely due to the plugin not being installed correctly or called before Vue.use().fixEnsure `Vue.use(VueYandexMetrika, { ... })` is called at the application's entry point before any component tries to access `this.$metrika`. -
Yandex Metrika counter ID is not set.
cause The `id` option, which specifies your Yandex Metrika counter ID, was not provided or was null/undefined during plugin initialization.fixProvide a valid Yandex Metrika counter ID in the plugin options: `Vue.use(VueYandexMetrika, { id: 'YOUR_COUNTER_ID', ... })`. -
ReferenceError: Vue is not defined
cause The Vue library itself has not been imported or made globally available before attempting to use the plugin.fixEnsure `import Vue from 'vue'` is present at the top of your main JavaScript file where the plugin is being installed.
Warnings
- breaking The `userParam` method was renamed to `userParams`. Calls using the old name will fail.
- gotcha API calls for Yandex Metrika are only performed if the `env` option matches 'production' by default. If you are testing in a development environment, you need to set `debug: true` or ensure `env` is not 'production'.
- gotcha The package has not been updated since September 2019 (v1.8.3). It is primarily designed for Vue 2 applications and may not be compatible with Vue 3 without significant manual adaptation or a community fork.
- gotcha The `skipSamePath` option is `true` by default, preventing tracking of consecutive page visits to the same URL. This might lead to undercounting if users refresh or navigate back to the same page.
Install
-
npm install vue-yandex-metrika -
yarn add vue-yandex-metrika -
pnpm add vue-yandex-metrika
Imports
- VueYandexMetrika
import { VueYandexMetrika } from 'vue-yandex-metrika'import VueYandexMetrika from 'vue-yandex-metrika'
- this.$metrika.hit
this.$metrika.reachGoal(goalName)
this.$metrika.hit(path, options)
- Vue.use
VueYandexMetrika.install(Vue, { id: XXXXXXXX })Vue.use(VueYandexMetrika, { id: XXXXXXXX, router })
Quickstart
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueYandexMetrika from 'vue-yandex-metrika';
const routes = [
{ path: '/', component: { template: '<div>Home Page</div>' }, name: 'home' },
{ path: '/about', component: { template: '<div>About Page</div>' }, name: 'about' },
{ path: '/contact', component: { template: '<div>Contact Page</div>' }, name: 'contact' }
];
const router = new VueRouter({ routes, mode: 'history' });
Vue.use(VueYandexMetrika, {
id: process.env.VUE_APP_YANDEX_METRIKA_ID ?? '12345678',
router: router,
env: process.env.NODE_ENV,
debug: process.env.NODE_ENV !== 'production',
options: {
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
webvisor: true
}
});
new Vue({
router,
template: `
<div id="app">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link> |
<router-link to="/contact">Contact</router-link>
<router-view></router-view>
</div>
`
}).$mount('#app');