Page tracking

Page tracking is the most important feature of Google Analytics and you can achieve that in different ways

Enable page auto tracking

The most easy way to track your application, is to pass your VueRouter instance to the plugin and let it handle everything for you

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import VueAnalytics from 'vue-analytics'
  4. const router = new VueRouter({
  5. router: // your routes
  6. })
  7. Vue.use(VueAnalytics, {
  8. id: 'UA-XXX-X',
  9. router
  10. })

Manual page tracking

The standard way is just passing the current page path

  1. this.$ga.page('/')

passing as an object literal

  1. this.$ga.page({
  2. page: '/',
  3. title: 'Home page',
  4. location: window.location.href
  5. })

or you can even pass the VueRouter instance scoped in your component and the plugin will automatically detect the current route name, path and location: just be sure to add the name property in your route object

  1. this.$ga.page(this.$router)

Google Analytics docs: page tracking

Use screenview with autotracking

It is also possible to use autotracking and screen tracking by passing true to the screenview property in the autoTracking object

  1. import Vue from 'vue'
  2. import VueAnalytics from 'vue-analytics'
  3. Vue.use(VueAnalytics, {
  4. id: 'UA-XXX-X',
  5. autoTracking: {
  6. screenview: true
  7. }
  8. })

Disable pageview hit on page load

Page auto tracking sends a pageview event on page load, but it is possible to disable that

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import VueAnalytics from 'vue-analytics'
  4. const router = new VueRouter({
  5. router: // your routes
  6. })
  7. Vue.use(VueAnalytics, {
  8. id: 'UA-XXX-X',
  9. router,
  10. autoTracking: {
  11. pageviewOnLoad: false
  12. }
  13. })

Disable page auto tracking

To disable auto tracking we can just remove the VueRouter instance, but if you need to track only in specific environment or situations, it is also possible to disable page auto tracking like so

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. page: false
  6. }
  7. })

Ignore routes on page auto tracking

To disable auto tracking for specific routes, you need to pass an array of strings to the plugin options.The string needs to be the route name or the route path.

  1. Vue.use(VueAnalytics, {
  2. router,
  3. ignoreRoutes: ['home', '/contacts']
  4. })

Auto track with custom data

When auto-tracking is possible to pass a function with a custom object shape to use as a tracker.

The pageViewTemplate passes the current route as parameter

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. pageviewTemplate (route) {
  6. return {
  7. page: route.path,
  8. title: document.title,
  9. location: window.location.href
  10. }
  11. }
  12. }
  13. })

It is also possible to add custom data structure for each route, using the meta object

  1. import Vue from 'vue'
  2. import VueAnalytics from 'vue-analytics'
  3. import VueRouter from 'vue-router'
  4. const router = new VueRouter({
  5. routes: [
  6. {
  7. name: 'home',
  8. path: '/',
  9. component: {...},
  10. meta: {
  11. analytics: {
  12. pageviewTemplate (route) {
  13. return {
  14. title: 'This is my custom title',
  15. page: route.path,
  16. location: 'www.mydomain.com'
  17. }
  18. }
  19. }
  20. }
  21. }
  22. ]
  23. })

important: the route pageviewTemplate has always priority over the global one.

pageviewTemplate can return a falsy value to skip tracking, which can be useful for specific needs:

  • shouldRouterUpdate documented below is more appropriate for tracking control based on routing, but is not enough when you need to disable initial tracking on some pages, since it only applies to navigation after initial page load.
  • pageviewOnLoad: false is global and can’t depend on current route.

Avoid transforming route query object into querystring

It is possible to avoid route query to be sent as querystring using the transformQueryString property

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. transformQueryString: false
  6. }
  7. })

Remove vue-router base option

When a base path is added to the VueRouter instance, the path is merged to the actual router path during the automatic tracking: however it is still possible to remove this behaviour modifying the prependBase property in the configuration object

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. prependBase: false
  6. }
  7. })

Customize router updates

On every route change, the plugin will track the new route: when we change hashes, query strings or other parameters.

To avoid router to update and start tracking when a route has the same path of the previous one, it is possible to use the skipSamePath property in the autoTracking object

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. skipSamePath: true
  6. }
  7. })

For other use cases it is also possible to use the shouldRouterUpdate, accessible in the plugin configuration object, inside the autoTracking property.The methods has the previous and current route as parameters and it needs to return a truthy or falsy value.

  1. Vue.use(VueAnalytics, {
  2. id: 'UA-XXX-X',
  3. router,
  4. autoTracking: {
  5. shouldRouterUpdate (to, from) {
  6. // Here I'm allowing tracking only when
  7. // next route path is not the same as the previous
  8. return to.path !== from.path
  9. }
  10. }
  11. })