|
| 1 | +## Vuex and Google Analytics |
| 2 | + |
| 3 | +Google Analytics it is now supported also using the Vuex store, by installing the `analyticsMiddleware` plugin |
| 4 | + |
| 5 | +### Install |
| 6 | + |
| 7 | +```js |
| 8 | +// store.js |
| 9 | +import Vuex from 'vuex' |
| 10 | +import Vue from 'vue' |
| 11 | +import { analyticsMiddleware } from 'vue-analytics' |
| 12 | + |
| 13 | +Vue.use(Vuex) |
| 14 | + |
| 15 | +const store = new Vuex.Store({ |
| 16 | + state: {}, |
| 17 | + actions: {}, |
| 18 | + mutations: {}, |
| 19 | + plugins: [ |
| 20 | + analyticsMiddleware |
| 21 | + ] |
| 22 | +}) |
| 23 | + |
| 24 | +export default store |
| 25 | +``` |
| 26 | + |
| 27 | +```js |
| 28 | +// main.js |
| 29 | +import Vue from 'vue' |
| 30 | +import VueAnalytics from 'vue-analytics' |
| 31 | +import store from './store.js' |
| 32 | + |
| 33 | +Vue.use(VueAnalytics, { |
| 34 | + id: 'UA-1234-5' |
| 35 | +}) |
| 36 | + |
| 37 | +new Vue({ |
| 38 | + ... |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +### Usage |
| 43 | + |
| 44 | +In the action, when sending a payload, we need to specify a `meta` object with an `analytics` property that will help us send all the data we need to the trackers |
| 45 | + |
| 46 | +Example of a store action |
| 47 | + |
| 48 | +```js |
| 49 | +const store = Vuex.Store({ |
| 50 | + state: { |
| 51 | + counter: 0 |
| 52 | + }, |
| 53 | + actions: { |
| 54 | + increase ({ commit }) { |
| 55 | + commit('increaseCounter', { |
| 56 | + // Here some extra parameters to pass to the mutation |
| 57 | + amount: 1, |
| 58 | + |
| 59 | + // The meta tag will be read by the plugin and fire |
| 60 | + // the corresponding events |
| 61 | + meta: { |
| 62 | + analytics: [ |
| 63 | + ['event', { |
| 64 | + eventCategory: 'counter', |
| 65 | + eventAction: 'increase', |
| 66 | + eventLabel: 'counter experiment', |
| 67 | + eventValue: 1 |
| 68 | + }] |
| 69 | + ] |
| 70 | + } |
| 71 | + |
| 72 | + }) |
| 73 | + } |
| 74 | + } |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +The way we can construct the track method is just by creating an array and the first argument will be one of the methods available in `vue-analytics` API |
| 79 | + |
| 80 | +* event |
| 81 | +* exception |
| 82 | +* page |
| 83 | +* query |
| 84 | +* require |
| 85 | +* set |
| 86 | +* social |
| 87 | +* time |
| 88 | +* untracked |
| 89 | +* ecommerce |
| 90 | +* commands |
| 91 | + |
| 92 | +the second parameter will be our data as usual constructed in a normal call, so if in a component we will have |
| 93 | + |
| 94 | +```js |
| 95 | +export default { |
| 96 | + name: 'MyComponent', |
| 97 | + methods: { |
| 98 | + clickMe () { |
| 99 | + this.$ga.event({ |
| 100 | + eventCategory: 'counter', |
| 101 | + eventAction: 'increase', |
| 102 | + eventLabel: 'counter experiment', |
| 103 | + eventValue: 1 |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +or |
| 111 | + |
| 112 | +```js |
| 113 | +export default { |
| 114 | + name: 'MyComponent', |
| 115 | + methods: { |
| 116 | + clickMe () { |
| 117 | + this.$ga.event('counter', 'increase', 'counter experiment', 1) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +then in our Vuex action we will write |
| 124 | + |
| 125 | +```js |
| 126 | +commit('increaseCounter', { |
| 127 | + meta: { |
| 128 | + analytics: [ |
| 129 | + ['event', { |
| 130 | + eventCategory: 'counter', |
| 131 | + eventAction: 'increase', |
| 132 | + eventLabel: 'counter experiment', |
| 133 | + eventValue: 1 |
| 134 | + }] |
| 135 | + ] |
| 136 | + } |
| 137 | +}) |
| 138 | +``` |
| 139 | + |
| 140 | +or |
| 141 | + |
| 142 | +```js |
| 143 | +commit('increaseCounter', { |
| 144 | + meta: { |
| 145 | + analytics: [ |
| 146 | + ['event', 'counter', 'increase', 'counter experiment', 1] |
| 147 | + ] |
| 148 | + } |
| 149 | +}) |
| 150 | +``` |
| 151 | + |
| 152 | +### Multiple events |
| 153 | + |
| 154 | +The `analytics` property inside the `meta` object is an array, so it is possible to fire multiple events with one action |
| 155 | + |
| 156 | +```js |
| 157 | +commit('someAction', { |
| 158 | + meta: { |
| 159 | + analytics: [ |
| 160 | + ['event', 'counter', 'increase', 'counter experiment', 1], |
| 161 | + ['page', '/about'], |
| 162 | + ['set', 'userId', 12345] |
| 163 | + ] |
| 164 | + } |
| 165 | +}) |
| 166 | +``` |
0 commit comments