Get started

Installation

  1. npm install vue-analytics

Start using it your Vue application

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

Important

For all the ES5 users out there, this package uses a default export so if you want to use require instead of import you should import the plugin like this

  1. const VueAnalytics = require('vue-analytics').default
  2. Vue.use(VueAnalytics, { ... })

Usage

it is possible to use the api in two different ways:

  • within the component scope
  • importing methods separately

Component scope

  1. export default {
  2. name: 'MyComponent',
  3. methods: {
  4. track () {
  5. this.$ga.page('/')
  6. }
  7. }
  8. }

Import methods

To be able to use methods import, make sure you install vue-analytics before you want to use them

  1. import { page } from 'vue-analytics'
  2. export default {
  3. name: 'MyComponent',
  4. methods: {
  5. track () {
  6. page('/')
  7. }
  8. }
  9. }

API

  • event
  • ecommerce
  • set
  • page
  • query
  • screenview
  • time
  • require
  • exception
  • social

Track multiple accounts

Pass an array of strings for a multiple tracking system. Every hit will be fired twice: each time with a different tracker name

  1. import Vue from 'vue'
  2. import VueAnalytics from 'vue-analytics'
  3. Vue.use(VueAnalytics, {
  4. id: ['UA-XXX-A', 'UA-XXX-B']
  5. })

Use functions or/and Promises

It is also possible to pass a function, a Promise or a function that returns a Promise: as soon as it returns always a string or an array of strings

  1. import Vue from 'vue'
  2. import VueAnalytics from 'vue-analytics'
  3. import axios from 'axios'
  4. // a function
  5. Vue.use(VueAnalytics, {
  6. id () {
  7. return 'UA-XXX-A'
  8. }
  9. })
  10. // a Promise
  11. Vue.use(VueAnalytics, {
  12. id: axios.get('/api/foo').then(response => {
  13. return response.data
  14. })
  15. })
  16. // a function that returns a Promise
  17. Vue.use(VueAnalytics, {
  18. id: () => axios.get('/api/foo').then(response => {
  19. return response.data
  20. })
  21. })