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

Commit f1b1568

Browse files
Merge pull request #98 from brunosabot/feature/directive_event_modifier
Add the possibility to choose other events than just click
2 parents 9ae46af + a2f82ef commit f1b1568

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

docs/v-ga.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ Then we only need to add the `v-ga` directive to your element and access the met
7171
</script>
7272
```
7373

74+
By default, the directive is executed when the element is clicked. However, if you want to change the event type for the logging, you can add the proper event as a modifier.
75+
76+
```html
77+
<template>
78+
<input
79+
v-model="name"
80+
v-ga.focus="$ga.commands.trackFocus.bind(this, 'name')" />
81+
</template>
82+
83+
<script>
84+
export default {
85+
name: 'myComponent',
86+
data () {
87+
return {
88+
name: 'John'
89+
}
90+
},
91+
methods: {
92+
logName () {
93+
console.log(this.name)
94+
}
95+
}
96+
}
97+
</script>
98+
```
99+
74100
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
75101

76102
```html

src/directives/ga.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import config from '../config'
22

33
export default {
4-
inserted: function (el, { value }, vnode) {
5-
el.addEventListener('click', function () {
6-
let fn = typeof value === 'string'
7-
? config.commands[value]
8-
: value
4+
inserted: function (el, binding, vnode) {
5+
const events = Object.keys(binding.modifiers)
96

10-
if (!fn) {
11-
throw new Error('[vue-analytics] The value passed to v-ga is not defined in the commands list.')
12-
}
7+
if (events.length === 0) {
8+
events.push('click')
9+
}
1310

14-
fn.apply(vnode.context)
11+
events.forEach(event => {
12+
el.addEventListener(event, function () {
13+
let fn = typeof binding.value === 'string'
14+
? config.commands[binding.value]
15+
: binding.value
16+
17+
if (!fn) {
18+
throw new Error('[vue-analytics] The value passed to v-ga is not defined in the commands list.')
19+
}
20+
21+
fn.apply(vnode.context)
22+
})
1523
})
1624
}
1725
}

0 commit comments

Comments
 (0)