main.ts
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-24 17:28:02
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-20 22:15:08
* @FilePath: \rollup_demo\src\main.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import Request from './modules/request'
import type { BaseOptions, trackType } from './types/main'
import stayEvent from './modules/stayEvent'
import clickEvent from './modules/clickEvent'
import viewEvent from './modules/viewEvent'
import user from './modules/user'
const request = new Request('https://www.xxx.com', 'ajax')
class Tracker {
SDK: BaseOptions
/**
* SDK配置
*/
constructor(options: BaseOptions) {
this.SDK = Object.assign(this.initOptions(), options)
this.init()
}
/**
* 默认配置
*/
initOptions(): BaseOptions {
return {
track_type: 'image',
router_type: 'hash',
clickview: {
open_all_click: true,
collect_doms: ['p']
},
scrollview: {
collect_url: function () {
return true
},
scroll_switch: true,
delay_time: 4000
}
}
}
/**
* 初始化数据
*/
init() {
stayEvent.init(this.SDK.scrollview)
clickEvent.init(this.SDK.clickview)
viewEvent.init(this.SDK.router_type)
}
/**
* 代码埋点触发元素点击事件
*/
track(target: HTMLElement, prop: any, callback: Function) {
clickEvent.activeTrigger.call(clickEvent, target, prop, callback)
}
/**
* 手动触发路由事件
*/
autoTriggerView(prop: any) {
viewEvent.trigger.call(viewEvent, prop)
}
/**
* 添加上报公共属性
*/
setCommonAttribute(obj: object) {
user.addCommonData(obj)
}
/**
*更换上报方式发数据
*/
setSendType<T extends trackType>(type: T) {
this.SDK.track_type = type
}
/**
* 用户登录绑定唯一标识
*/
login(id: string|number) {
user.setLoginId(id)
}
/**
* 直接设置用户的属性,如果存在则覆盖。
*/
setAttribute(obj: object) {
user.setUser(obj)
}
}
export default Tracker