@gctrack/vue 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/index.cjs +89 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +19 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +88 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yinuo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @gctrack/vue
|
|
2
|
+
|
|
3
|
+
Vue 插件 —— 自动接入路由 PV 与全局错误捕获(`Vue.config.errorHandler` → `vue_error`),并提供 `v-track` / `v-track-view` / `v-track-stay` 指令与 `$track` mixin。
|
|
4
|
+
|
|
5
|
+
> ⚠️ **仅兼容 Vue 2**(基于 `Vue.prototype` / `Vue.directive` 的 `bind` 钩子)。Vue 3 项目请等待后续的 `@gctrack/vue3`。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm i @gctrack/vue @gctrack/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 用法
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import Vue from 'vue'
|
|
17
|
+
import { createTracker } from '@gctrack/core'
|
|
18
|
+
import { GCTrackPlugin } from '@gctrack/vue'
|
|
19
|
+
import router from './router'
|
|
20
|
+
|
|
21
|
+
const tracker = createTracker({ reportUrl: '...', appKey: '...' })
|
|
22
|
+
Vue.use(GCTrackPlugin, { tracker, router, trackPageView: true, trackErrors: true })
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
模板指令:
|
|
26
|
+
|
|
27
|
+
```html
|
|
28
|
+
<button v-track="'btn_click'">点我</button>
|
|
29
|
+
<div v-track-view="'card_expose'">曝光埋点</div>
|
|
30
|
+
<div v-track-stay="'card_stay'">停留埋点</div>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT (c) yinuo
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* GCTrack Vue 插件对象
|
|
5
|
+
*/
|
|
6
|
+
const GCTrackPlugin = { install(Vue, options) {
|
|
7
|
+
const { tracker, router, trackPageView = true, trackErrors = true } = options;
|
|
8
|
+
Vue.prototype.$tracker = tracker;
|
|
9
|
+
if (router && trackPageView) router.afterEach((to, from) => {
|
|
10
|
+
tracker.track("page_view", {
|
|
11
|
+
from: from.path,
|
|
12
|
+
to: to.path,
|
|
13
|
+
title: to.meta?.title || document.title,
|
|
14
|
+
query: to.query,
|
|
15
|
+
params: to.params
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
if (trackErrors) Vue.config.errorHandler = (err, vm, info) => {
|
|
19
|
+
console.error("Vue Error:", err);
|
|
20
|
+
tracker.track("vue_error", {
|
|
21
|
+
message: err.message,
|
|
22
|
+
stack: err.stack,
|
|
23
|
+
componentName: vm?.$options?.name || "Anonymous",
|
|
24
|
+
info,
|
|
25
|
+
timestamp: Date.now()
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
registerDirectives(Vue, tracker);
|
|
29
|
+
} };
|
|
30
|
+
/**
|
|
31
|
+
* 注册全局指令
|
|
32
|
+
*/
|
|
33
|
+
function registerDirectives(Vue, tracker) {
|
|
34
|
+
Vue.directive("track", { bind(el, binding) {
|
|
35
|
+
el.addEventListener("click", () => {
|
|
36
|
+
const eventName = binding.value;
|
|
37
|
+
const eventData = binding.arg ? { [binding.arg]: binding.modifiers } : {};
|
|
38
|
+
tracker.track(eventName, eventData);
|
|
39
|
+
});
|
|
40
|
+
} });
|
|
41
|
+
Vue.directive("track-view", { bind(el, binding) {
|
|
42
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
43
|
+
const observer = new IntersectionObserver((entries) => {
|
|
44
|
+
entries.forEach((entry) => {
|
|
45
|
+
if (entry.isIntersecting) {
|
|
46
|
+
const eventName = binding.value;
|
|
47
|
+
tracker.track(`${eventName}_view`, { element: el.tagName });
|
|
48
|
+
observer.unobserve(el);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
observer.observe(el);
|
|
53
|
+
} });
|
|
54
|
+
Vue.directive("track-stay", { bind(el, binding) {
|
|
55
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
56
|
+
const startTime = Date.now();
|
|
57
|
+
new IntersectionObserver((entries) => {
|
|
58
|
+
entries.forEach((entry) => {
|
|
59
|
+
if (!entry.isIntersecting) {
|
|
60
|
+
const stayTime = Date.now() - startTime;
|
|
61
|
+
const eventName = binding.value;
|
|
62
|
+
tracker.track(`${eventName}_stay`, { stayTime });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}).observe(el);
|
|
66
|
+
} });
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Vue Mixin
|
|
70
|
+
*/
|
|
71
|
+
const GCTrackMixin = { methods: {
|
|
72
|
+
/**
|
|
73
|
+
* 追踪事件
|
|
74
|
+
*/
|
|
75
|
+
$track(eventType, data) {
|
|
76
|
+
if (this.$tracker) this.$tracker.track(eventType, data);
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* 追踪页面浏览
|
|
80
|
+
*/
|
|
81
|
+
$trackPageView(data) {
|
|
82
|
+
if (this.$tracker) this.$tracker.track("page_view", data);
|
|
83
|
+
}
|
|
84
|
+
} };
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.GCTrackMixin = GCTrackMixin;
|
|
87
|
+
exports.GCTrackPlugin = GCTrackPlugin;
|
|
88
|
+
|
|
89
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Tracker } from '@gctrack/core'\n\n/**\n * Vue 插件选项\n */\nexport interface VuePluginOptions {\n tracker: Tracker\n router?: any\n trackPageView?: boolean\n trackErrors?: boolean\n}\n\n/**\n * GCTrack Vue 插件对象\n */\nexport const GCTrackPlugin = {\n install(Vue: any, options: VuePluginOptions): void {\n const { tracker, router, trackPageView = true, trackErrors = true } = options\n\n // 将 tracker 挂载到 Vue 原型\n Vue.prototype.$tracker = tracker\n\n // 路由变化时追踪页面浏览\n if (router && trackPageView) {\n router.afterEach((to: any, from: any) => {\n tracker.track('page_view', {\n from: from.path,\n to: to.path,\n title: to.meta?.title || document.title,\n query: to.query,\n params: to.params,\n })\n })\n }\n\n // 全局错误处理\n if (trackErrors) {\n Vue.config.errorHandler = (err: Error, vm: any, info: string) => {\n console.error('Vue Error:', err)\n\n tracker.track('vue_error', {\n message: err.message,\n stack: err.stack,\n componentName: vm?.$options?.name || 'Anonymous',\n info,\n timestamp: Date.now(),\n })\n }\n }\n\n // 注册全局指令\n registerDirectives(Vue, tracker)\n },\n}\n\n/**\n * 注册全局指令\n */\nfunction registerDirectives(Vue: any, tracker: Tracker) {\n // 点击埋点指令\n Vue.directive('track', {\n bind(el: HTMLElement, binding: any) {\n el.addEventListener('click', () => {\n const eventName = binding.value\n const eventData = binding.arg ? { [binding.arg]: binding.modifiers } : {}\n\n tracker.track(eventName, eventData)\n })\n },\n })\n\n // 曝光埋点指令\n Vue.directive('track-view', {\n bind(el: HTMLElement, binding: any) {\n if (typeof IntersectionObserver === 'undefined') {\n return\n }\n\n const observer = new IntersectionObserver((entries) => {\n entries.forEach((entry) => {\n if (entry.isIntersecting) {\n const eventName = binding.value\n tracker.track(`${eventName}_view`, {\n element: el.tagName,\n })\n\n // 只上报一次\n observer.unobserve(el)\n }\n })\n })\n\n observer.observe(el)\n },\n })\n\n // 停留时长埋点指令\n Vue.directive('track-stay', {\n bind(el: HTMLElement, binding: any) {\n if (typeof IntersectionObserver === 'undefined') {\n return\n }\n\n const startTime = Date.now()\n\n const observer = new IntersectionObserver((entries) => {\n entries.forEach((entry) => {\n if (!entry.isIntersecting) {\n const stayTime = Date.now() - startTime\n const eventName = binding.value\n\n tracker.track(`${eventName}_stay`, {\n stayTime,\n })\n }\n })\n })\n\n observer.observe(el)\n },\n })\n}\n\n/**\n * Vue Mixin\n */\nexport const GCTrackMixin = {\n methods: {\n /**\n * 追踪事件\n */\n $track(eventType: string, data?: Record<string, any>): void {\n if (this.$tracker) {\n this.$tracker.track(eventType, data)\n }\n },\n\n /**\n * 追踪页面浏览\n */\n $trackPageView(data?: Record<string, any>): void {\n if (this.$tracker) {\n this.$tracker.track('page_view', data)\n }\n },\n },\n}\n\n/**\n * 类型声明\n */\ndeclare module 'vue/types/vue' {\n interface Vue {\n $tracker: Tracker\n $track: (eventType: string, data?: Record<string, any>) => void\n $trackPageView: (data?: Record<string, any>) => void\n }\n}\n"],"mappings":";;;;;AAeA,MAAa,gBAAgB,EAC3B,QAAQ,KAAU,SAAiC;CACjD,MAAM,EAAE,SAAS,QAAQ,gBAAgB,MAAM,cAAc,SAAS;CAGtE,IAAI,UAAU,WAAW;CAGzB,IAAI,UAAU,eACZ,OAAO,WAAW,IAAS,SAAc;EACvC,QAAQ,MAAM,aAAa;GACzB,MAAM,KAAK;GACX,IAAI,GAAG;GACP,OAAO,GAAG,MAAM,SAAS,SAAS;GAClC,OAAO,GAAG;GACV,QAAQ,GAAG;GACZ,CAAC;GACF;CAIJ,IAAI,aACF,IAAI,OAAO,gBAAgB,KAAY,IAAS,SAAiB;EAC/D,QAAQ,MAAM,cAAc,IAAI;EAEhC,QAAQ,MAAM,aAAa;GACzB,SAAS,IAAI;GACb,OAAO,IAAI;GACX,eAAe,IAAI,UAAU,QAAQ;GACrC;GACA,WAAW,KAAK,KAAK;GACtB,CAAC;;CAKN,mBAAmB,KAAK,QAAQ;GAEnC;;;;AAKD,SAAS,mBAAmB,KAAU,SAAkB;CAEtD,IAAI,UAAU,SAAS,EACrB,KAAK,IAAiB,SAAc;EAClC,GAAG,iBAAiB,eAAe;GACjC,MAAM,YAAY,QAAQ;GAC1B,MAAM,YAAY,QAAQ,MAAM,GAAG,QAAQ,MAAM,QAAQ,WAAW,GAAG,EAAE;GAEzE,QAAQ,MAAM,WAAW,UAAU;IACnC;IAEL,CAAC;CAGF,IAAI,UAAU,cAAc,EAC1B,KAAK,IAAiB,SAAc;EAClC,IAAI,OAAO,yBAAyB,aAClC;EAGF,MAAM,WAAW,IAAI,sBAAsB,YAAY;GACrD,QAAQ,SAAS,UAAU;IACzB,IAAI,MAAM,gBAAgB;KACxB,MAAM,YAAY,QAAQ;KAC1B,QAAQ,MAAM,GAAG,UAAU,QAAQ,EACjC,SAAS,GAAG,SACb,CAAC;KAGF,SAAS,UAAU,GAAG;;KAExB;IACF;EAEF,SAAS,QAAQ,GAAG;IAEvB,CAAC;CAGF,IAAI,UAAU,cAAc,EAC1B,KAAK,IAAiB,SAAc;EAClC,IAAI,OAAO,yBAAyB,aAClC;EAGF,MAAM,YAAY,KAAK,KAAK;EAe5B,IAbqB,sBAAsB,YAAY;GACrD,QAAQ,SAAS,UAAU;IACzB,IAAI,CAAC,MAAM,gBAAgB;KACzB,MAAM,WAAW,KAAK,KAAK,GAAG;KAC9B,MAAM,YAAY,QAAQ;KAE1B,QAAQ,MAAM,GAAG,UAAU,QAAQ,EACjC,UACD,CAAC;;KAEJ;IAGI,CAAC,QAAQ,GAAG;IAEvB,CAAC;;;;;AAMJ,MAAa,eAAe,EAC1B,SAAS;;;;CAIP,OAAO,WAAmB,MAAkC;EAC1D,IAAI,KAAK,UACP,KAAK,SAAS,MAAM,WAAW,KAAK;;;;;CAOxC,eAAe,MAAkC;EAC/C,IAAI,KAAK,UACP,KAAK,SAAS,MAAM,aAAa,KAAK;;CAG3C,EACF"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Tracker } from "@gctrack/core";
|
|
2
|
+
export interface VuePluginOptions {
|
|
3
|
+
tracker: Tracker;
|
|
4
|
+
router?: any;
|
|
5
|
+
trackPageView?: boolean;
|
|
6
|
+
trackErrors?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const GCTrackPlugin: {install(Vue: any, options: VuePluginOptions): void};
|
|
9
|
+
export declare const GCTrackMixin: {methods: {
|
|
10
|
+
$track(eventType: string, data?: Record<string, any>): void;
|
|
11
|
+
$trackPageView(data?: Record<string, any>): void;
|
|
12
|
+
}};
|
|
13
|
+
declare module "vue/types/vue" {
|
|
14
|
+
interface Vue {
|
|
15
|
+
$tracker: Tracker;
|
|
16
|
+
$track: (eventType: string, data?: Record<string, any>) => void;
|
|
17
|
+
$trackPageView: (data?: Record<string, any>) => void;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Tracker } from "@gctrack/core";
|
|
2
|
+
export interface VuePluginOptions {
|
|
3
|
+
tracker: Tracker;
|
|
4
|
+
router?: any;
|
|
5
|
+
trackPageView?: boolean;
|
|
6
|
+
trackErrors?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const GCTrackPlugin: {install(Vue: any, options: VuePluginOptions): void};
|
|
9
|
+
export declare const GCTrackMixin: {methods: {
|
|
10
|
+
$track(eventType: string, data?: Record<string, any>): void;
|
|
11
|
+
$trackPageView(data?: Record<string, any>): void;
|
|
12
|
+
}};
|
|
13
|
+
declare module "vue/types/vue" {
|
|
14
|
+
interface Vue {
|
|
15
|
+
$tracker: Tracker;
|
|
16
|
+
$track: (eventType: string, data?: Record<string, any>) => void;
|
|
17
|
+
$trackPageView: (data?: Record<string, any>) => void;
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
//#region src/index.ts
|
|
3
|
+
/**
|
|
4
|
+
* GCTrack Vue 插件对象
|
|
5
|
+
*/
|
|
6
|
+
const GCTrackPlugin = { install(Vue, options) {
|
|
7
|
+
const { tracker, router, trackPageView = true, trackErrors = true } = options;
|
|
8
|
+
Vue.prototype.$tracker = tracker;
|
|
9
|
+
if (router && trackPageView) router.afterEach((to, from) => {
|
|
10
|
+
tracker.track("page_view", {
|
|
11
|
+
from: from.path,
|
|
12
|
+
to: to.path,
|
|
13
|
+
title: to.meta?.title || document.title,
|
|
14
|
+
query: to.query,
|
|
15
|
+
params: to.params
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
if (trackErrors) Vue.config.errorHandler = (err, vm, info) => {
|
|
19
|
+
console.error("Vue Error:", err);
|
|
20
|
+
tracker.track("vue_error", {
|
|
21
|
+
message: err.message,
|
|
22
|
+
stack: err.stack,
|
|
23
|
+
componentName: vm?.$options?.name || "Anonymous",
|
|
24
|
+
info,
|
|
25
|
+
timestamp: Date.now()
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
registerDirectives(Vue, tracker);
|
|
29
|
+
} };
|
|
30
|
+
/**
|
|
31
|
+
* 注册全局指令
|
|
32
|
+
*/
|
|
33
|
+
function registerDirectives(Vue, tracker) {
|
|
34
|
+
Vue.directive("track", { bind(el, binding) {
|
|
35
|
+
el.addEventListener("click", () => {
|
|
36
|
+
const eventName = binding.value;
|
|
37
|
+
const eventData = binding.arg ? { [binding.arg]: binding.modifiers } : {};
|
|
38
|
+
tracker.track(eventName, eventData);
|
|
39
|
+
});
|
|
40
|
+
} });
|
|
41
|
+
Vue.directive("track-view", { bind(el, binding) {
|
|
42
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
43
|
+
const observer = new IntersectionObserver((entries) => {
|
|
44
|
+
entries.forEach((entry) => {
|
|
45
|
+
if (entry.isIntersecting) {
|
|
46
|
+
const eventName = binding.value;
|
|
47
|
+
tracker.track(`${eventName}_view`, { element: el.tagName });
|
|
48
|
+
observer.unobserve(el);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
observer.observe(el);
|
|
53
|
+
} });
|
|
54
|
+
Vue.directive("track-stay", { bind(el, binding) {
|
|
55
|
+
if (typeof IntersectionObserver === "undefined") return;
|
|
56
|
+
const startTime = Date.now();
|
|
57
|
+
new IntersectionObserver((entries) => {
|
|
58
|
+
entries.forEach((entry) => {
|
|
59
|
+
if (!entry.isIntersecting) {
|
|
60
|
+
const stayTime = Date.now() - startTime;
|
|
61
|
+
const eventName = binding.value;
|
|
62
|
+
tracker.track(`${eventName}_stay`, { stayTime });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}).observe(el);
|
|
66
|
+
} });
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Vue Mixin
|
|
70
|
+
*/
|
|
71
|
+
const GCTrackMixin = { methods: {
|
|
72
|
+
/**
|
|
73
|
+
* 追踪事件
|
|
74
|
+
*/
|
|
75
|
+
$track(eventType, data) {
|
|
76
|
+
if (this.$tracker) this.$tracker.track(eventType, data);
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* 追踪页面浏览
|
|
80
|
+
*/
|
|
81
|
+
$trackPageView(data) {
|
|
82
|
+
if (this.$tracker) this.$tracker.track("page_view", data);
|
|
83
|
+
}
|
|
84
|
+
} };
|
|
85
|
+
//#endregion
|
|
86
|
+
export { GCTrackMixin, GCTrackPlugin };
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Tracker } from '@gctrack/core'\n\n/**\n * Vue 插件选项\n */\nexport interface VuePluginOptions {\n tracker: Tracker\n router?: any\n trackPageView?: boolean\n trackErrors?: boolean\n}\n\n/**\n * GCTrack Vue 插件对象\n */\nexport const GCTrackPlugin = {\n install(Vue: any, options: VuePluginOptions): void {\n const { tracker, router, trackPageView = true, trackErrors = true } = options\n\n // 将 tracker 挂载到 Vue 原型\n Vue.prototype.$tracker = tracker\n\n // 路由变化时追踪页面浏览\n if (router && trackPageView) {\n router.afterEach((to: any, from: any) => {\n tracker.track('page_view', {\n from: from.path,\n to: to.path,\n title: to.meta?.title || document.title,\n query: to.query,\n params: to.params,\n })\n })\n }\n\n // 全局错误处理\n if (trackErrors) {\n Vue.config.errorHandler = (err: Error, vm: any, info: string) => {\n console.error('Vue Error:', err)\n\n tracker.track('vue_error', {\n message: err.message,\n stack: err.stack,\n componentName: vm?.$options?.name || 'Anonymous',\n info,\n timestamp: Date.now(),\n })\n }\n }\n\n // 注册全局指令\n registerDirectives(Vue, tracker)\n },\n}\n\n/**\n * 注册全局指令\n */\nfunction registerDirectives(Vue: any, tracker: Tracker) {\n // 点击埋点指令\n Vue.directive('track', {\n bind(el: HTMLElement, binding: any) {\n el.addEventListener('click', () => {\n const eventName = binding.value\n const eventData = binding.arg ? { [binding.arg]: binding.modifiers } : {}\n\n tracker.track(eventName, eventData)\n })\n },\n })\n\n // 曝光埋点指令\n Vue.directive('track-view', {\n bind(el: HTMLElement, binding: any) {\n if (typeof IntersectionObserver === 'undefined') {\n return\n }\n\n const observer = new IntersectionObserver((entries) => {\n entries.forEach((entry) => {\n if (entry.isIntersecting) {\n const eventName = binding.value\n tracker.track(`${eventName}_view`, {\n element: el.tagName,\n })\n\n // 只上报一次\n observer.unobserve(el)\n }\n })\n })\n\n observer.observe(el)\n },\n })\n\n // 停留时长埋点指令\n Vue.directive('track-stay', {\n bind(el: HTMLElement, binding: any) {\n if (typeof IntersectionObserver === 'undefined') {\n return\n }\n\n const startTime = Date.now()\n\n const observer = new IntersectionObserver((entries) => {\n entries.forEach((entry) => {\n if (!entry.isIntersecting) {\n const stayTime = Date.now() - startTime\n const eventName = binding.value\n\n tracker.track(`${eventName}_stay`, {\n stayTime,\n })\n }\n })\n })\n\n observer.observe(el)\n },\n })\n}\n\n/**\n * Vue Mixin\n */\nexport const GCTrackMixin = {\n methods: {\n /**\n * 追踪事件\n */\n $track(eventType: string, data?: Record<string, any>): void {\n if (this.$tracker) {\n this.$tracker.track(eventType, data)\n }\n },\n\n /**\n * 追踪页面浏览\n */\n $trackPageView(data?: Record<string, any>): void {\n if (this.$tracker) {\n this.$tracker.track('page_view', data)\n }\n },\n },\n}\n\n/**\n * 类型声明\n */\ndeclare module 'vue/types/vue' {\n interface Vue {\n $tracker: Tracker\n $track: (eventType: string, data?: Record<string, any>) => void\n $trackPageView: (data?: Record<string, any>) => void\n }\n}\n"],"mappings":";;;;;AAeA,MAAa,gBAAgB,EAC3B,QAAQ,KAAU,SAAiC;CACjD,MAAM,EAAE,SAAS,QAAQ,gBAAgB,MAAM,cAAc,SAAS;CAGtE,IAAI,UAAU,WAAW;CAGzB,IAAI,UAAU,eACZ,OAAO,WAAW,IAAS,SAAc;EACvC,QAAQ,MAAM,aAAa;GACzB,MAAM,KAAK;GACX,IAAI,GAAG;GACP,OAAO,GAAG,MAAM,SAAS,SAAS;GAClC,OAAO,GAAG;GACV,QAAQ,GAAG;GACZ,CAAC;GACF;CAIJ,IAAI,aACF,IAAI,OAAO,gBAAgB,KAAY,IAAS,SAAiB;EAC/D,QAAQ,MAAM,cAAc,IAAI;EAEhC,QAAQ,MAAM,aAAa;GACzB,SAAS,IAAI;GACb,OAAO,IAAI;GACX,eAAe,IAAI,UAAU,QAAQ;GACrC;GACA,WAAW,KAAK,KAAK;GACtB,CAAC;;CAKN,mBAAmB,KAAK,QAAQ;GAEnC;;;;AAKD,SAAS,mBAAmB,KAAU,SAAkB;CAEtD,IAAI,UAAU,SAAS,EACrB,KAAK,IAAiB,SAAc;EAClC,GAAG,iBAAiB,eAAe;GACjC,MAAM,YAAY,QAAQ;GAC1B,MAAM,YAAY,QAAQ,MAAM,GAAG,QAAQ,MAAM,QAAQ,WAAW,GAAG,EAAE;GAEzE,QAAQ,MAAM,WAAW,UAAU;IACnC;IAEL,CAAC;CAGF,IAAI,UAAU,cAAc,EAC1B,KAAK,IAAiB,SAAc;EAClC,IAAI,OAAO,yBAAyB,aAClC;EAGF,MAAM,WAAW,IAAI,sBAAsB,YAAY;GACrD,QAAQ,SAAS,UAAU;IACzB,IAAI,MAAM,gBAAgB;KACxB,MAAM,YAAY,QAAQ;KAC1B,QAAQ,MAAM,GAAG,UAAU,QAAQ,EACjC,SAAS,GAAG,SACb,CAAC;KAGF,SAAS,UAAU,GAAG;;KAExB;IACF;EAEF,SAAS,QAAQ,GAAG;IAEvB,CAAC;CAGF,IAAI,UAAU,cAAc,EAC1B,KAAK,IAAiB,SAAc;EAClC,IAAI,OAAO,yBAAyB,aAClC;EAGF,MAAM,YAAY,KAAK,KAAK;EAe5B,IAbqB,sBAAsB,YAAY;GACrD,QAAQ,SAAS,UAAU;IACzB,IAAI,CAAC,MAAM,gBAAgB;KACzB,MAAM,WAAW,KAAK,KAAK,GAAG;KAC9B,MAAM,YAAY,QAAQ;KAE1B,QAAQ,MAAM,GAAG,UAAU,QAAQ,EACjC,UACD,CAAC;;KAEJ;IAGI,CAAC,QAAQ,GAAG;IAEvB,CAAC;;;;;AAMJ,MAAa,eAAe,EAC1B,SAAS;;;;CAIP,OAAO,WAAmB,MAAkC;EAC1D,IAAI,KAAK,UACP,KAAK,SAAS,MAAM,WAAW,KAAK;;;;;CAOxC,eAAe,MAAkC;EAC/C,IAAI,KAAK,UACP,KAAK,SAAS,MAAM,aAAa,KAAK;;CAG3C,EACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gctrack/vue",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vue plugin for GCTrack",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"vue",
|
|
21
|
+
"plugin",
|
|
22
|
+
"monitoring",
|
|
23
|
+
"tracking"
|
|
24
|
+
],
|
|
25
|
+
"author": "yinuo",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"vue": "^2.6.0",
|
|
32
|
+
"@gctrack/core": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown",
|
|
36
|
+
"dev": "tsdown --watch"
|
|
37
|
+
}
|
|
38
|
+
}
|