Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Latest commit

 

History

History
52 lines (43 loc) · 1009 Bytes

vuex.md

File metadata and controls

52 lines (43 loc) · 1009 Bytes

Vuex and Google Analytics

To be able to use vue-analytics from your Vuex store, just import the methods you need and fire it directly from the store

First step

Make sure to have your vue-analytics package installed before start using it in your store

// main.js
import Vue from 'vue'
import store from './store'
import App from './App'
import VueAnalytics from 'vue-analytics'

Vue.use(VueAnalytics, {
  id: 'UA-xxxx-1'
})

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

Second step

Start using vue-analytics API in your store

// store.js
import Vue from 'vue'
import Vuex from 'vuex'
import { event } from 'vue-analytics'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    counter: 0
  },
  actions: {
    increase ({ commit, state }) {
      commit('increase', state.counter + 1)
    }
  },
  mutations: {
    increase (state, payload) {
      state.counter = payload
      event('user-click', 'increase', 'counter', state.counter)
    }
  }
})