collect.ts
2.17 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
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 17:05:34
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-21 09:28:02
* @FilePath: \glsx-tracker-javascript\src\modules\collect.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import type { DefaultInfoDescriptor, EventType, ReportInfoDescriptor } from '../types/collect'
import utils from '../utils/helper'
import { EVENT_TYPE } from '../utils/constants'
class Collect {
getUUID: string
user_login_id?: string | number
constructor() {
this.getUUID = utils.UUID()
}
/**
* 采集默认信息数据
*/
get getDefaultData(): DefaultInfoDescriptor {
return {
$page_title: document.title,
$page_url: location.href,
$page_path: location.pathname
}
}
/**
* 收集点击事件数据
* 判断需要监听哪些dom元素
*/
getClickEvent<T extends object>(para: T) {
this.sendTracker(para, EVENT_TYPE[0] as EventType)
}
/**
* 收集浏览事件数据
*/
getViewEvent<T extends object>(para: T) {
this.sendTracker(para, EVENT_TYPE[1] as EventType)
}
/**
* 收集停留事件数据
*/
getStayEvent<T extends object>(para: T) {
this.sendTracker(para, EVENT_TYPE[2] as EventType)
}
/**
* 设置登录用户id
*/
getLoginId(id: string | number) {
this.user_login_id = id
}
/**
* 发送特殊的埋点数据
*/
sendCommonTracker(obj: object, name: string) {
const reportInfo = {
...obj,
eventName: name,
report_time: utils.dateFormat(Date.now()),
anonymous_id: this.getUUID
}
console.log(reportInfo)
}
/**
* 发送埋点数据
*/
sendTracker<T>(para: T, type: EventType) {
const reportInfo: ReportInfoDescriptor = {
properties: Object.assign(this.getDefaultData, para),
eventName: type,
anonymous_id: this.getUUID,
report_time: utils.dateFormat(Date.now())
}
if (this.user_login_id) {
reportInfo.user_login_id = this.user_login_id
}
console.log(reportInfo)
}
}
export default new Collect()