main.ts
2.65 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
92
93
94
95
96
97
98
99
100
101
102
103
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-24 17:28:02
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-22 10:52:37
* @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, ClickOption, trackType, ScrollOption } from './types/main'
import stayEvent from './modules/stayEvent'
import clickEvent from './modules/clickEvent'
import viewEvent from './modules/viewEvent'
import report from './modules/report'
import User from './modules/user'
// const request = new Request('https://www.xxx.com', 'ajax')
class Tracker extends User {
SDK: BaseOptions
/**
* SDK配置
*/
constructor(options: BaseOptions) {
super()
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']
},
reportConfig: {
send_count: 2,
send_time: 10,
switch_start: true,
post_way: 'ajax', // ajax image
server_url: 'http://192.168.1.126:3000/log',
server_img_url: 'http://192.168.1.126:3000/read-image/log.gif',
track_data_name: 'track_data'
},
scrollview: {
collect_url: function () {
return true
},
scroll_switch: true,
delay_time: 4000
}
}
}
/**
* 初始化数据
*/
init() {
stayEvent.init(this.SDK.scrollview as ScrollOption)
clickEvent.init(this.SDK.clickview as ClickOption)
viewEvent.init(this.SDK.router_type)
report.init(this.SDK.reportConfig)
}
/**
* 代码埋点触发元素点击事件
*/
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) {
this.addCommonData(obj)
}
/**
*更换上报方式发数据
*/
setSendType<T extends trackType>(type: T) {
this.SDK.track_type = type
}
/**
* 用户登录绑定唯一标识
*/
login(id: string | number) {
this.setLoginId(id)
}
/**
* 直接设置用户的属性,如果存在则覆盖。
*/
setAttribute(obj: object) {
this.setUser(obj)
}
}
export default Tracker