This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
04594d7
commit d0a82bf
Showing
10 changed files
with
1,909 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#v-ga | ||
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 | ||
|
||
Taking as an example a button that only has to log a name in the console | ||
|
||
```html | ||
<template> | ||
<button @click="logName">Log me</button> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'myComponent', | ||
data () { | ||
return { | ||
name: 'John' | ||
} | ||
}, | ||
methods: { | ||
logName () { | ||
console.log(this.name) | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
To start tracking the value of `name` we can start by adding a method in the commands object that handles this action | ||
|
||
```js | ||
import Vue from 'vue' | ||
import VueAnalytics from 'vue-analytics' | ||
|
||
Vue.use(VueAnalytics, { | ||
id: 'UA-XXX-X', | ||
commands: { | ||
trackName (name = 'unknown') { | ||
this.$ga.track('randomClicks', 'click', 'name', name) | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
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 | ||
|
||
```html | ||
<template> | ||
<button | ||
v-ga="$ga.commands.trackName.bind(this, name)" | ||
@click="logName"> | ||
Log me | ||
</button> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'myComponent', | ||
data () { | ||
return { | ||
name: 'John' | ||
} | ||
}, | ||
methods: { | ||
logName () { | ||
console.log(this.name) | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
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 | ||
|
||
```html | ||
<template> | ||
<button v-ga="'trackName'">Click me</button> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'myComponent' | ||
} | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.