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

Commit d0a82bf

Browse files
feat(directives): add v-ga
v-ga allows us to centralize all track events in one object and share it across the entire application without needs to add extra logic to our component methods
1 parent 04594d7 commit d0a82bf

File tree

10 files changed

+1909
-164
lines changed

10 files changed

+1909
-164
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ npm install vue-analytics
3333

3434
* [Get started](/docs/installation.md)
3535
* [How to load Google Analytics](/docs/script-loader.md)
36-
* [Untracked hits](/docs/untracked-hits.md)
3736
* [Page tracking](/docs/page-tracking.md)
38-
* [Cross-domain tracking](/docs/cross-domain-tracking.md)
3937
* [Event tracking](/docs/event-tracking.md)
38+
* [v-ga](/docs/v-ga.md)
39+
* [Cross-domain tracking](/docs/cross-domain-tracking.md)
4040
* [User timings](/docs/user-timings.md#user-timings)
4141
* [Exception tracking](/docs/exception-tracking.md)
4242
* [Require](/docs/require.md)
@@ -47,6 +47,7 @@ npm install vue-analytics
4747
* [On Analaytics script ready](/docs/when-google-analytics-is-loaded.md)
4848
* [Custom methods](/docs/custom-methods.md)
4949
* [Ecommerce](/docs/ecommerce.md)
50+
* [Untracked hits](/docs/untracked-hits.md)
5051
* [Debug](/docs/debug.md)
5152

5253
# Issues and features requests

config/base.config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ module.exports = {
1111
libraryTarget: 'umd'
1212
},
1313
resolve: {
14-
extensions: ['.js'],
14+
extensions: ['.js', '.vue'],
1515
alias: {
16-
lib: path.resolve(__dirname, '../src/lib')
16+
lib: path.resolve(__dirname, '../src/lib'),
17+
directives: path.resolve(__dirname, '../src/directives')
1718
}
1819
},
1920
module: {
@@ -26,6 +27,16 @@ module.exports = {
2627
presets: ['blue'],
2728
babelrc: false
2829
}
30+
},
31+
{
32+
test: /\.vue$/,
33+
exclude: /node_modules/,
34+
loader: 'vue-loader',
35+
options: {
36+
loaders: {
37+
js: 'babel-loader'
38+
}
39+
}
2940
}
3041
]
3142
}

docs/v-ga.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#v-ga
2+
This directive allows us to centralize all track events in one object and share it across the entire application without needs to add extra logic to our component methods
3+
4+
Taking as an example a button that only has to log a name in the console
5+
6+
```html
7+
<template>
8+
<button @click="logName">Log me</button>
9+
</template>
10+
11+
<script>
12+
export default {
13+
name: 'myComponent',
14+
15+
data () {
16+
return {
17+
name: 'John'
18+
}
19+
},
20+
21+
methods: {
22+
logName () {
23+
console.log(this.name)
24+
}
25+
}
26+
}
27+
</script>
28+
```
29+
30+
To start tracking the value of `name` we can start by adding a method in the commands object that handles this action
31+
32+
```js
33+
import Vue from 'vue'
34+
import VueAnalytics from 'vue-analytics'
35+
36+
Vue.use(VueAnalytics, {
37+
id: 'UA-XXX-X',
38+
commands: {
39+
trackName (name = 'unknown') {
40+
this.$ga.track('randomClicks', 'click', 'name', name)
41+
}
42+
}
43+
})
44+
```
45+
46+
then we only need to add the `v-ga` directive to your element and access the method from the `commands` list that now is shared in the `$ga` object
47+
48+
```html
49+
<template>
50+
<button
51+
v-ga="$ga.commands.trackName.bind(this, name)"
52+
@click="logName">
53+
Log me
54+
</button>
55+
</template>
56+
57+
<script>
58+
export default {
59+
name: 'myComponent',
60+
61+
data () {
62+
return {
63+
name: 'John'
64+
}
65+
},
66+
67+
methods: {
68+
logName () {
69+
console.log(this.name)
70+
}
71+
}
72+
}
73+
</script>
74+
```
75+
76+
If there's no need to pass any arguments, we could also just pass the name of the method as a string, and the plugin will look it up for us
77+
78+
```html
79+
<template>
80+
<button v-ga="'trackName'">Click me</button>
81+
</template>
82+
83+
<script>
84+
export default {
85+
name: 'myComponent'
86+
}
87+
</script>
88+
```

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const blueJest = require('blue-jest')
33
module.exports = Object.assign({}, blueJest, {
44
moduleNameMapper: Object.assign({}, blueJest.moduleNameMapper, {
55
'^lib/(.*)$': '<rootDir>/src/lib/$1',
6+
'^directives/(.*)$': '<rootDir>/src/directives/$1',
67
'vue$': 'vue/dist/vue.min.js'
78
})
89
})

0 commit comments

Comments
 (0)