auto-point 0.0.22 → 0.0.24
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 +35 -14
- package/README.md +11 -1
- package/package.json +1 -1
package/AP/auto_point.js
CHANGED
|
@@ -13,6 +13,16 @@
|
|
|
13
13
|
set server(url) {
|
|
14
14
|
this.#SERVER = url
|
|
15
15
|
}
|
|
16
|
+
#SEND_FUNC = function(data){
|
|
17
|
+
return fetch(this.#SERVER, {
|
|
18
|
+
method: 'POST',
|
|
19
|
+
headers: {
|
|
20
|
+
'Content-Type': 'application/json',
|
|
21
|
+
},
|
|
22
|
+
body: JSON.stringify(data),
|
|
23
|
+
keepalive: true
|
|
24
|
+
});
|
|
25
|
+
}
|
|
16
26
|
// 事件列表
|
|
17
27
|
#EVENTS = ['click']
|
|
18
28
|
// 发送数据间隔 单位毫秒 默认10秒
|
|
@@ -93,6 +103,9 @@
|
|
|
93
103
|
if (params.events && this.#isArray(params.events, "events must be an Array")) {
|
|
94
104
|
this.#EVENTS = params.events
|
|
95
105
|
}
|
|
106
|
+
if(params.send_func && this.#isFunction(params.send_func, "send_func must be a function")){
|
|
107
|
+
this.#SEND_FUNC = params.send_func.bind(this)
|
|
108
|
+
}
|
|
96
109
|
this.#POINT_SYS_INIT_TIME = new Date().getTime()
|
|
97
110
|
this.#mount_dom_event()
|
|
98
111
|
this.#timer_send()
|
|
@@ -167,13 +180,11 @@
|
|
|
167
180
|
})
|
|
168
181
|
// 挂载页面离开事件监听
|
|
169
182
|
window.addEventListener('visibilitychange', () => {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
this.#send()
|
|
176
|
-
}
|
|
183
|
+
let type = "visibilitychange"
|
|
184
|
+
let value = document.visibilityState
|
|
185
|
+
let describe = "Webpage visibility state is changed to: " + document.visibilityState
|
|
186
|
+
this.#add_point(this.#create_active_point_data(type, value, describe))
|
|
187
|
+
this.#send()
|
|
177
188
|
})
|
|
178
189
|
} else {
|
|
179
190
|
throw "<body></body> not found in DOM"
|
|
@@ -189,7 +200,7 @@
|
|
|
189
200
|
}
|
|
190
201
|
|
|
191
202
|
// 发送埋点数据
|
|
192
|
-
#send() {
|
|
203
|
+
async #send() {
|
|
193
204
|
if (this.#POINT_LIST.length) {
|
|
194
205
|
this.#MIDDLEWARE.map((item) => {
|
|
195
206
|
if (this.#isFunction(item)) {
|
|
@@ -198,16 +209,26 @@
|
|
|
198
209
|
})
|
|
199
210
|
this.#POINT_LAST_SEND_TIME = new Date().getTime() // 更新最后发送时间
|
|
200
211
|
navigator.sendBeacon(this.#SERVER, JSON.stringify(this.#POINT_LIST));
|
|
201
|
-
|
|
202
|
-
|
|
212
|
+
let res
|
|
213
|
+
try {
|
|
214
|
+
res = await this.#SEND_FUNC(this.#POINT_LIST)
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error("Error sending data:", error)
|
|
217
|
+
} finally {
|
|
218
|
+
// 发送后清空埋点列表数据
|
|
219
|
+
this.#POINT_LIST = new Array()
|
|
220
|
+
}
|
|
221
|
+
return res
|
|
203
222
|
}
|
|
204
223
|
}
|
|
205
224
|
|
|
206
|
-
//
|
|
225
|
+
// 根据间隔时间,定时发送埋点数据
|
|
207
226
|
#timer_send() {
|
|
208
227
|
setInterval(() => {
|
|
209
|
-
this.#
|
|
210
|
-
|
|
228
|
+
if(!this.#POINT_LAST_SEND_TIME || (new Date().getTime() - this.#POINT_LAST_SEND_TIME >= this.#INTERVAL_TIME)){
|
|
229
|
+
this.#send()
|
|
230
|
+
}
|
|
231
|
+
}, 200)
|
|
211
232
|
}
|
|
212
233
|
|
|
213
234
|
/**
|
|
@@ -228,7 +249,7 @@
|
|
|
228
249
|
|
|
229
250
|
// 手动发送数据
|
|
230
251
|
send() {
|
|
231
|
-
this.#send()
|
|
252
|
+
return this.#send()
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
/**
|
package/README.md
CHANGED
|
@@ -23,7 +23,17 @@ const ap = new AP().init({
|
|
|
23
23
|
server: 'https://your-server.com/api/track', // 替换为真实的服务端地址
|
|
24
24
|
interval_time: 5000, // 发送间隔:5秒
|
|
25
25
|
events: ['click'], // 监听的事件类型
|
|
26
|
-
point_sing: 'data-track-id' // 埋点标记属性名 如果元素上带有该属性名,AP会自动记录该属性值,否则不会记录
|
|
26
|
+
point_sing: 'data-track-id', // 埋点标记属性名 如果元素上带有该属性名,AP会自动记录该属性值,否则不会记录
|
|
27
|
+
send_func: function(data){ // 自定义发送函数,默认使用fetch发送数据
|
|
28
|
+
return fetch(this.#SERVER, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json',
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(data),
|
|
34
|
+
keepalive: true // 保持连接,确保数据发送完成
|
|
35
|
+
});
|
|
36
|
+
}
|
|
27
37
|
});
|
|
28
38
|
|
|
29
39
|
// 挂载中间件(在发送数据前执行)
|
package/package.json
CHANGED