From 89793e35508b301226eb526501bdff513a672114 Mon Sep 17 00:00:00 2001 From: starryrbs <1322096624@qq.com> Date: Thu, 12 Mar 2020 19:19:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E7=BB=9F=E8=AE=A1=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++- src/directives/track-event.js | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 934d0c5..e2c2f2e 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ vue-ba 提供 track-event,track-pageview 两个指令,开发者可以直接 可通过逗号分隔的字符串或对象字面量传递参数,以字符串传递时请注意参数顺序,可参考trackEvent API。 +统计自定义事件使用 v-track-event.someEvent.custom + #### 用法 ``` @@ -92,6 +94,8 @@ vue-ba 提供 track-event,track-pageview 两个指令,开发者可以直接 // 统计keypress事件 + // 统计someEvent事件,someEvent是自定义事件 + // 以对象字面量传递参数 @@ -112,4 +116,4 @@ vue-ba 提供 track-event,track-pageview 两个指令,开发者可以直接
// 以字符串指定受访页面和来源
// 以对象字面量指定受访页面和来源 -``` \ No newline at end of file +``` diff --git a/src/directives/track-event.js b/src/directives/track-event.js index 6a9a346..8212c1e 100644 --- a/src/directives/track-event.js +++ b/src/directives/track-event.js @@ -1,7 +1,7 @@ import ba from '../index' import { notChanged, isEmpty } from './utils' -export default function (el, binding) { +export default function (el, binding, vnode) { if (notChanged(binding) || isEmpty(binding)) { return } @@ -26,7 +26,13 @@ export default function (el, binding) { if (!events.length) events.push('click') // default listen click - events.forEach((event) => { - el.addEventListener(event, () => ba.trackEvent(...args), false) + events.forEach((eventValue) => { + const customTag = 'custom' + let [event, custom] = eventValue.split(':') + if (custom === customTag) { + vnode.componentInstance.$on(event, () => ba.trackEvent(...args), false) + } else { + el.addEventListener(event, () => ba.trackEvent(...args), false) + } }) }