|
1 | 1 | ## Vuex and Google Analytics |
2 | 2 |
|
3 | | -Google Analytics it is supported also using the Vuex store, by installing the `analyticsMiddleware` plugin |
| 3 | +To be able to use vue-analytics from your Vuex store, just import the methods you need and fire it directly from the store |
4 | 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 | | -``` |
| 5 | +## First step |
| 6 | +Make sure to have your vue-analytics package installed **before** start using it in your store |
26 | 7 |
|
27 | 8 | ```js |
28 | 9 | // main.js |
29 | 10 | import Vue from 'vue' |
| 11 | +import store from './store' |
| 12 | +import App from './App' |
30 | 13 | import VueAnalytics from 'vue-analytics' |
31 | | -import store from './store.js' |
32 | 14 |
|
33 | 15 | Vue.use(VueAnalytics, { |
34 | | - id: 'UA-1234-5' |
| 16 | + id: 'UA-xxxx-1' |
35 | 17 | }) |
36 | 18 |
|
37 | 19 | new Vue({ |
38 | | - el: '#root', |
39 | 20 | store, |
40 | | - ... |
41 | | -}) |
| 21 | + render: h => h(App) |
| 22 | +}).$mount('#app') |
42 | 23 | ``` |
43 | 24 |
|
44 | | -### Usage |
| 25 | +## Second step |
| 26 | +Start using vue-analytics API in your store |
45 | 27 |
|
46 | | -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 |
| 28 | +```js |
| 29 | +// store.js |
| 30 | +import Vue from 'vue' |
| 31 | +import Vuex from 'vuex' |
| 32 | +import { page } from 'vue-analytics' |
47 | 33 |
|
48 | | -Example of a store action |
| 34 | +Vue.use(Vuex) |
49 | 35 |
|
50 | | -```js |
51 | | -const store = Vuex.Store({ |
| 36 | +export default new Vuex.Store({ |
52 | 37 | state: { |
53 | 38 | counter: 0 |
54 | 39 | }, |
55 | 40 | actions: { |
56 | | - increase ({ commit }) { |
57 | | - commit('increaseCounter', { |
58 | | - // Here some extra parameters to pass to the mutation |
59 | | - amount: 1, |
60 | | - |
61 | | - // The meta tag will be read by the plugin and fire |
62 | | - // the corresponding events |
63 | | - meta: { |
64 | | - analytics: [ |
65 | | - ['event', { |
66 | | - eventCategory: 'counter', |
67 | | - eventAction: 'increase', |
68 | | - eventLabel: 'counter experiment', |
69 | | - eventValue: 1 |
70 | | - }] |
71 | | - ] |
72 | | - } |
73 | | - |
74 | | - }) |
| 41 | + increase ({ commit, state }) { |
| 42 | + commit('increase', state.counter + 1) |
75 | 43 | } |
76 | | - } |
77 | | -}) |
78 | | -``` |
79 | | - |
80 | | -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 |
81 | | - |
82 | | -* event |
83 | | -* exception |
84 | | -* page |
85 | | -* query |
86 | | -* require |
87 | | -* set |
88 | | -* social |
89 | | -* time |
90 | | -* untracked |
91 | | -* ecommerce (*) |
92 | | -* commands |
93 | | - |
94 | | -**(*) pay attention to the ecommerce API at the bottom of this page, since it has a different structure** |
95 | | - |
96 | | -the second parameter will be our data constructed the same way as we were inside a component, for example: |
97 | | - |
98 | | -```js |
99 | | -export default { |
100 | | - name: 'MyComponent', |
101 | | - methods: { |
102 | | - clickMe () { |
103 | | - this.$ga.event({ |
104 | | - eventCategory: 'counter', |
105 | | - eventAction: 'increase', |
106 | | - eventLabel: 'counter experiment', |
107 | | - eventValue: 1 |
108 | | - }) |
109 | | - } |
110 | | - } |
111 | | -} |
112 | | -``` |
113 | | - |
114 | | -or |
115 | | - |
116 | | -```js |
117 | | -export default { |
118 | | - name: 'MyComponent', |
119 | | - methods: { |
120 | | - clickMe () { |
121 | | - this.$ga.event('counter', 'increase', 'counter experiment', 1) |
| 44 | + }, |
| 45 | + mutations: { |
| 46 | + increase (state, payload) { |
| 47 | + state.counter = payload |
| 48 | + event('user-click', 'increase', 'counter', state.counter) |
122 | 49 | } |
123 | 50 | } |
124 | | -} |
125 | | -``` |
126 | | - |
127 | | -then in our Vuex action we will write |
128 | | - |
129 | | -```js |
130 | | -commit('increaseCounter', { |
131 | | - meta: { |
132 | | - analytics: [ |
133 | | - ['event', { |
134 | | - eventCategory: 'counter', |
135 | | - eventAction: 'increase', |
136 | | - eventLabel: 'counter experiment', |
137 | | - eventValue: 1 |
138 | | - }] |
139 | | - ] |
140 | | - } |
141 | | -}) |
142 | | -``` |
143 | | - |
144 | | -or |
145 | | - |
146 | | -```js |
147 | | -commit('increaseCounter', { |
148 | | - meta: { |
149 | | - analytics: [ |
150 | | - ['event', 'counter', 'increase', 'counter experiment', 1] |
151 | | - ] |
152 | | - } |
153 | | -}) |
154 | | -``` |
155 | | - |
156 | | -### Multiple events |
157 | | - |
158 | | -The `analytics` property inside the `meta` object is an array, so it is possible to fire multiple events with one action |
159 | | - |
160 | | -```js |
161 | | -commit('someAction', { |
162 | | - meta: { |
163 | | - analytics: [ |
164 | | - ['event', 'counter', 'increase', 'counter experiment', 1], |
165 | | - ['page', '/about'], |
166 | | - ['set', 'userId', 12345] |
167 | | - ] |
168 | | - } |
169 | | -}) |
170 | | -``` |
171 | | - |
172 | | -### Ecommerce and Vuex |
173 | | - |
174 | | -Because of its differnet API nature, the ecommerce plugin needs to be called as it in the original Google Analytics documentation. |
175 | | - |
176 | | -```js |
177 | | -commit('someAction', { |
178 | | - meta: { |
179 | | - analytics: [ |
180 | | - ['ecommerce:addItem', { |
181 | | - id: '1234', |
182 | | - name: 'Fluffy Pink Bunnies', |
183 | | - sku: 'DD23444', |
184 | | - category: 'Party Toys', |
185 | | - price: '11.99', |
186 | | - quantity: '1' |
187 | | - }] |
188 | | - ] |
189 | | - } |
190 | 51 | }) |
191 | 52 | ``` |
0 commit comments