Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit ccbd63a

Browse files
Merge branch 'develop'
2 parents e186d75 + ff38a02 commit ccbd63a

File tree

3 files changed

+98
-175
lines changed

3 files changed

+98
-175
lines changed

docs/installation.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Get started
22

3-
Install the package
3+
### Installation
44
```bash
55
npm install vue-analytics
66
```
@@ -17,14 +17,63 @@ Vue.use(VueAnalytics, {
1717

1818
**Important**
1919

20-
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
20+
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
2121

2222
```js
2323
const VueAnalytics = require('vue-analytics').default
2424

2525
Vue.use(VueAnalytics, { ... })
2626
```
2727

28+
### Usage
29+
it is possible to use the api in two different ways:
30+
- within the component scope
31+
- importing methods separately
32+
33+
#### Component scope
34+
35+
```js
36+
export default {
37+
name: 'MyComponent',
38+
39+
methods: {
40+
track () {
41+
this.$ga.page('/')
42+
}
43+
}
44+
}
45+
```
46+
47+
#### Import methods
48+
49+
To be able to use methods import, make sure you install vue-analytics **before** you want to use them
50+
51+
```js
52+
import { page } from 'vue-analytics'
53+
54+
export default {
55+
name: 'MyComponent',
56+
57+
methods: {
58+
track () {
59+
page('/')
60+
}
61+
}
62+
}
63+
```
64+
65+
#### API
66+
- event
67+
- ecommerce
68+
- set
69+
- page
70+
- query
71+
- screenview
72+
- time
73+
- require
74+
- exception
75+
- social
76+
2877
## Track multiple accounts
2978

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

docs/vuex.md

Lines changed: 24 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,52 @@
11
## Vuex and Google Analytics
22

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
44

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
267

278
```js
289
// main.js
2910
import Vue from 'vue'
11+
import store from './store'
12+
import App from './App'
3013
import VueAnalytics from 'vue-analytics'
31-
import store from './store.js'
3214

3315
Vue.use(VueAnalytics, {
34-
id: 'UA-1234-5'
16+
id: 'UA-xxxx-1'
3517
})
3618

3719
new Vue({
38-
el: '#root',
3920
store,
40-
...
41-
})
21+
render: h => h(App)
22+
}).$mount('#app')
4223
```
4324

44-
### Usage
25+
## Second step
26+
Start using vue-analytics API in your store
4527

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'
4733

48-
Example of a store action
34+
Vue.use(Vuex)
4935

50-
```js
51-
const store = Vuex.Store({
36+
export default new Vuex.Store({
5237
state: {
5338
counter: 0
5439
},
5540
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)
7543
}
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)
12249
}
12350
}
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-
}
19051
})
19152
```

src/index.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
import bootstrap from './bootstrap'
22
import lib from './lib'
33
import { update } from './config'
4-
import { onAnalyticsReady } from './helpers'
4+
import * as helpers from './helpers'
55
import ga from 'directives/ga'
6-
import * as exception from 'lib/exception'
7-
import analyticsMiddleware from './vuex-middleware'
6+
import { autotracking as expectionAutotracking } from 'lib/exception'
7+
import vuexMiddleware from './vuex-middleware'
88

99
export default function install (Vue, options = {}) {
1010
update({ ...options, $vue: Vue })
1111

1212
Vue.directive('ga', ga)
13-
1413
Vue.prototype.$ga = Vue.$ga = lib
1514

16-
exception.autotracking(Vue)
17-
15+
expectionAutotracking(Vue)
1816
bootstrap()
1917
}
2018

21-
export {
22-
onAnalyticsReady,
23-
analyticsMiddleware
24-
}
19+
// Vuex middleware
20+
export const analyticsMiddleware = vuexMiddleware
21+
22+
// Helpers
23+
export const onAnalyticsReady = helpers.onAnalyticsReady
24+
25+
// Event library
26+
export const event = lib.event
27+
export const ecommerce = lib.ecommerce
28+
export const set = lib.set
29+
export const page = lib.page
30+
export const query = lib.query
31+
export const screenview = lib.screenview
32+
export const time = lib.time
33+
export const require = lib.require
34+
export const exception = lib.exception
35+
export const social = lib.social
36+
37+

0 commit comments

Comments
 (0)