auto-point 0.0.11 → 0.0.12
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/AP/auto_point.js +71 -71
- package/package.json +1 -1
package/AP/auto_point.js
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
1
|
class auto_point {
|
|
2
2
|
// 初始化状态
|
|
3
|
-
|
|
3
|
+
INIT_STATE = true
|
|
4
4
|
// 服务端地址
|
|
5
|
-
|
|
5
|
+
SERVER = void (0)
|
|
6
6
|
get server() {
|
|
7
|
-
return this
|
|
7
|
+
return this.SERVER
|
|
8
8
|
}
|
|
9
9
|
set server(url) {
|
|
10
|
-
this
|
|
10
|
+
this.SERVER = url
|
|
11
11
|
}
|
|
12
12
|
// 事件列表
|
|
13
|
-
|
|
13
|
+
EVENTS = ['click']
|
|
14
14
|
// 发送数据间隔 单位毫秒 默认10秒
|
|
15
|
-
|
|
15
|
+
SEND_TIME = 10_000
|
|
16
16
|
// sing挂载事件元素标记
|
|
17
|
-
|
|
17
|
+
POINT_SING = "auto_point"
|
|
18
18
|
// 埋点队列
|
|
19
|
-
|
|
19
|
+
POINT_LIST = new Array()
|
|
20
20
|
get point_list() {
|
|
21
|
-
return this
|
|
21
|
+
return this.POINT_LIST
|
|
22
22
|
}
|
|
23
23
|
set point_list(data){
|
|
24
|
-
this
|
|
24
|
+
this.POINT_LIST = data
|
|
25
25
|
}
|
|
26
26
|
// 埋点系统时间
|
|
27
|
-
|
|
27
|
+
POINT_SYS_INIT_TIME = void(0)
|
|
28
28
|
get point_sys_init_time(){
|
|
29
|
-
return this
|
|
29
|
+
return this.POINT_SYS_INIT_TIME
|
|
30
30
|
}
|
|
31
31
|
// 上次发送时间
|
|
32
|
-
|
|
32
|
+
POINT_LAST_SEND_TIME = void(0)
|
|
33
33
|
get point_last_send_time(){
|
|
34
|
-
return this
|
|
34
|
+
return this.POINT_LAST_SEND_TIME
|
|
35
35
|
}
|
|
36
36
|
// 中间件列表
|
|
37
|
-
|
|
37
|
+
MIDDLEWARE = new Array()
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
ERROR(msg) {
|
|
40
40
|
console.error(msg)
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
ISFUNCTION(obj, message) {
|
|
43
43
|
if (typeof obj === 'function') {
|
|
44
44
|
return true
|
|
45
45
|
} else {
|
|
46
|
-
message && this
|
|
46
|
+
message && this.ERROR(message)
|
|
47
47
|
return false
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
NOTARROWFUNCTION(obj,message){
|
|
51
51
|
if (typeof obj === 'function' && obj.prototype) {
|
|
52
52
|
return true
|
|
53
53
|
} else {
|
|
54
|
-
message && this
|
|
54
|
+
message && this.ERROR(message)
|
|
55
55
|
return false
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
ISARRAY(array, message) {
|
|
59
59
|
if (Array.isArray(array)) {
|
|
60
60
|
return true
|
|
61
61
|
} else {
|
|
62
|
-
message && this
|
|
62
|
+
message && this.ERROR(message)
|
|
63
63
|
return false
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
|
|
66
|
+
ISOBJECT(obj, message) {
|
|
67
67
|
if (Object.prototype.toString.call(obj) === '[object Object]') {
|
|
68
68
|
return true
|
|
69
69
|
} else {
|
|
70
|
-
message && this
|
|
70
|
+
message && this.ERROR(message)
|
|
71
71
|
return false
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
|
|
74
|
+
PARAMS_HASH_SERVER(server) {
|
|
75
75
|
if (server) {
|
|
76
76
|
return true
|
|
77
77
|
} else {
|
|
@@ -81,31 +81,31 @@ class auto_point {
|
|
|
81
81
|
|
|
82
82
|
constructor() { }
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
if (this
|
|
86
|
-
this
|
|
87
|
-
this
|
|
88
|
-
this
|
|
89
|
-
if (params.events && this
|
|
90
|
-
this
|
|
84
|
+
INIT(params) {
|
|
85
|
+
if (this.INIT_STATE && this.ISOBJECT(params, "Init only accepts object type data") && this.PARAMS_HASH_SERVER(params.server)) {
|
|
86
|
+
this.SERVER = params.server
|
|
87
|
+
this.SEND_TIME = params.send_time || this.SEND_TIME
|
|
88
|
+
this.POINT_SING = params.point_sing || this.POINT_SING
|
|
89
|
+
if (params.events && this.ISARRAY(params.events, "events must be an Array")) {
|
|
90
|
+
this.EVENTS = params.events
|
|
91
91
|
}
|
|
92
|
-
this
|
|
93
|
-
this
|
|
94
|
-
this
|
|
92
|
+
this.POINT_SYS_INIT_TIME = new Date().getTime()
|
|
93
|
+
this.MOUNT_DOM_EVENT()
|
|
94
|
+
this.TIMER_SEND()
|
|
95
95
|
// 初始化成功埋点
|
|
96
96
|
let type = "init"
|
|
97
97
|
let value = "success"
|
|
98
98
|
let describe = "Webpage initialization succeeded"
|
|
99
|
-
this
|
|
100
|
-
this
|
|
99
|
+
this.ADD_POINT(this.CREATE_ACTIVE_POINT_DATA(type, value, describe))
|
|
100
|
+
this.INIT_STATE = false
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
CREATE_POINT_DATA_COMMON_PARAMS(){
|
|
105
105
|
let point_item = new Object()
|
|
106
106
|
point_item.location = JSON.parse(JSON.stringify(window.location))
|
|
107
|
-
point_item.point_sys_init_time = this
|
|
108
|
-
point_item.point_last_send_time = this
|
|
107
|
+
point_item.point_sys_init_time = this.POINT_SYS_INIT_TIME
|
|
108
|
+
point_item.point_last_send_time = this.POINT_LAST_SEND_TIME
|
|
109
109
|
return point_item
|
|
110
110
|
}
|
|
111
111
|
|
|
@@ -115,14 +115,14 @@ class auto_point {
|
|
|
115
115
|
* @param {Element} element 接收一个DOM元素节点对象
|
|
116
116
|
* @returns {object} 返回一个埋点数据对象point_item
|
|
117
117
|
*/
|
|
118
|
-
|
|
119
|
-
let point_item = this
|
|
118
|
+
CREATE_EVENT_POINT_DATA(element) {
|
|
119
|
+
let point_item = this.CREATE_POINT_DATA_COMMON_PARAMS()
|
|
120
120
|
point_item.point_type = "event"
|
|
121
|
-
point_item.point_sing = this
|
|
121
|
+
point_item.point_sing = this.POINT_SING
|
|
122
122
|
point_item.elment = element.target
|
|
123
123
|
point_item.event_type = element.type
|
|
124
|
-
point_item.point_attr = element.target.getAttribute(this
|
|
125
|
-
point_item.point_value = element.target.getAttribute(this
|
|
124
|
+
point_item.point_attr = element.target.getAttribute(this.POINT_SING)
|
|
125
|
+
point_item.point_value = element.target.getAttribute(this.POINT_SING)
|
|
126
126
|
return point_item
|
|
127
127
|
}
|
|
128
128
|
|
|
@@ -133,8 +133,8 @@ class auto_point {
|
|
|
133
133
|
* @param {string} activeValue 埋点动作记录值
|
|
134
134
|
* @returns
|
|
135
135
|
*/
|
|
136
|
-
|
|
137
|
-
let point_item = this
|
|
136
|
+
CREATE_ACTIVE_POINT_DATA(activeType, activeAttr, activeValue){
|
|
137
|
+
let point_item = this.CREATE_POINT_DATA_COMMON_PARAMS()
|
|
138
138
|
point_item.point_type = "active"
|
|
139
139
|
point_item.point_sing = activeType
|
|
140
140
|
point_item.point_attr = activeAttr
|
|
@@ -143,14 +143,14 @@ class auto_point {
|
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
// 挂载全局监听
|
|
146
|
-
|
|
146
|
+
MOUNT_DOM_EVENT() {
|
|
147
147
|
if (window && document && document.body) {
|
|
148
148
|
let body = document.body
|
|
149
149
|
// 挂载各个类型事件监听
|
|
150
|
-
this
|
|
150
|
+
this.EVENTS.map((item) => {
|
|
151
151
|
body.addEventListener(item, (e) => {
|
|
152
|
-
if (e.target.getAttribute(this
|
|
153
|
-
this
|
|
152
|
+
if (e.target.getAttribute(this.POINT_SING)) {
|
|
153
|
+
this.ADD_POINT(this.CREATE_EVENT_POINT_DATA(e))
|
|
154
154
|
}
|
|
155
155
|
})
|
|
156
156
|
})
|
|
@@ -159,7 +159,7 @@ class auto_point {
|
|
|
159
159
|
let type = "hashchange"
|
|
160
160
|
let value = window.location.hash
|
|
161
161
|
let describe = "Webpage hash value is changed to: " + window.location.hash
|
|
162
|
-
this
|
|
162
|
+
this.ADD_POINT(this.CREATE_ACTIVE_POINT_DATA(type, value, describe))
|
|
163
163
|
})
|
|
164
164
|
// 挂载页面离开事件监听
|
|
165
165
|
window.addEventListener('visibilitychange', () => {
|
|
@@ -167,8 +167,8 @@ class auto_point {
|
|
|
167
167
|
let type = "pagehide"
|
|
168
168
|
let value = "hidden"
|
|
169
169
|
let describe = "Webpage hidden point"
|
|
170
|
-
this
|
|
171
|
-
this
|
|
170
|
+
this.ADD_POINT(this.CREATE_ACTIVE_POINT_DATA(type, value, describe))
|
|
171
|
+
this.SEND()
|
|
172
172
|
}
|
|
173
173
|
})
|
|
174
174
|
} else {
|
|
@@ -177,34 +177,34 @@ class auto_point {
|
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
// 添加埋点记录
|
|
180
|
-
|
|
181
|
-
if (this
|
|
180
|
+
ADD_POINT(params) {
|
|
181
|
+
if (this.ISOBJECT(params, "Point data must be an object")) {
|
|
182
182
|
params.time = new Date().getTime()
|
|
183
|
-
this
|
|
183
|
+
this.POINT_LIST.push(params)
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
|
|
187
187
|
// 发送埋点数据
|
|
188
|
-
|
|
189
|
-
if (this
|
|
190
|
-
this
|
|
191
|
-
if (this
|
|
188
|
+
SEND() {
|
|
189
|
+
if (this.POINT_LIST.length) {
|
|
190
|
+
this.MIDDLEWARE.map((item) => {
|
|
191
|
+
if (this.ISFUNCTION(item)) {
|
|
192
192
|
item.apply(this)
|
|
193
193
|
}
|
|
194
194
|
})
|
|
195
|
-
navigator.sendBeacon(this
|
|
195
|
+
navigator.sendBeacon(this.SERVER, JSON.stringify(this.POINT_LIST));
|
|
196
196
|
// 发送后清空埋点列表数据
|
|
197
|
-
this
|
|
197
|
+
this.POINT_LIST = new Array()
|
|
198
198
|
} else {
|
|
199
199
|
console.log("无新埋点数据")
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
// 定时发送
|
|
204
|
-
|
|
204
|
+
TIMER_SEND() {
|
|
205
205
|
setInterval(() => {
|
|
206
|
-
this
|
|
207
|
-
}, this
|
|
206
|
+
this.SEND()
|
|
207
|
+
}, this.SEND_TIME)
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
/**
|
|
@@ -219,13 +219,13 @@ class auto_point {
|
|
|
219
219
|
* @param {string} [params.point_sing]
|
|
220
220
|
*/
|
|
221
221
|
init(params) {
|
|
222
|
-
this
|
|
222
|
+
this.INIT(params)
|
|
223
223
|
return this
|
|
224
224
|
}
|
|
225
225
|
|
|
226
226
|
// 手动发送数据
|
|
227
227
|
send() {
|
|
228
|
-
this
|
|
228
|
+
this.SEND()
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
/**
|
|
@@ -234,15 +234,15 @@ class auto_point {
|
|
|
234
234
|
* @return this
|
|
235
235
|
*/
|
|
236
236
|
middleware(func) {
|
|
237
|
-
if (this
|
|
238
|
-
this
|
|
237
|
+
if (this.NOTARROWFUNCTION(func, "Middleware requires a non-arrow function parameter")) {
|
|
238
|
+
this.MIDDLEWARE.push(func)
|
|
239
239
|
}
|
|
240
240
|
return this
|
|
241
241
|
}
|
|
242
242
|
|
|
243
243
|
// 手动添加埋点记录
|
|
244
244
|
add_point(params) {
|
|
245
|
-
this
|
|
245
|
+
this.ADD_POINT(params)
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
}
|