auto-point 0.0.25 → 0.0.27
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 +43 -43
- package/README.md +5 -7
- package/package.json +1 -1
package/AP/auto_point.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
set server(url) {
|
|
14
14
|
this.#SERVER = url
|
|
15
15
|
}
|
|
16
|
-
#SEND_FUNC = function(data){
|
|
16
|
+
#SEND_FUNC = function (data) {
|
|
17
17
|
return fetch(this.#SERVER, {
|
|
18
18
|
method: 'POST',
|
|
19
19
|
headers: {
|
|
@@ -34,21 +34,24 @@
|
|
|
34
34
|
get point_list() {
|
|
35
35
|
return this.#POINT_LIST
|
|
36
36
|
}
|
|
37
|
-
set point_list(data){
|
|
37
|
+
set point_list(data) {
|
|
38
38
|
this.#POINT_LIST = data
|
|
39
39
|
}
|
|
40
40
|
// 埋点系统时间
|
|
41
|
-
#POINT_SYS_INIT_TIME = void(0)
|
|
42
|
-
get point_sys_init_time(){
|
|
41
|
+
#POINT_SYS_INIT_TIME = void (0)
|
|
42
|
+
get point_sys_init_time() {
|
|
43
43
|
return this.#POINT_SYS_INIT_TIME
|
|
44
44
|
}
|
|
45
45
|
// 上次发送时间
|
|
46
|
-
#POINT_LAST_SEND_TIME = void(0)
|
|
47
|
-
get point_last_send_time(){
|
|
46
|
+
#POINT_LAST_SEND_TIME = void (0)
|
|
47
|
+
get point_last_send_time() {
|
|
48
48
|
return this.#POINT_LAST_SEND_TIME
|
|
49
49
|
}
|
|
50
50
|
// 中间件列表
|
|
51
51
|
#MIDDLEWARE = new Array()
|
|
52
|
+
get middleware() {
|
|
53
|
+
return this.#MIDDLEWARE
|
|
54
|
+
}
|
|
52
55
|
|
|
53
56
|
#ERROR(msg) {
|
|
54
57
|
console.error(msg)
|
|
@@ -61,7 +64,7 @@
|
|
|
61
64
|
return false
|
|
62
65
|
}
|
|
63
66
|
}
|
|
64
|
-
#NotArrowFunction(obj,message){
|
|
67
|
+
#NotArrowFunction(obj, message) {
|
|
65
68
|
if (typeof obj === 'function' && obj.prototype) {
|
|
66
69
|
return true
|
|
67
70
|
} else {
|
|
@@ -86,24 +89,34 @@
|
|
|
86
89
|
}
|
|
87
90
|
}
|
|
88
91
|
#params_hash_server(server) {
|
|
89
|
-
if (server) {
|
|
92
|
+
if (server && /^https?:\/\//.test(server)) { // server 是一个url
|
|
90
93
|
return true
|
|
91
94
|
} else {
|
|
92
95
|
throw "The server in params cannot be empty"
|
|
93
96
|
}
|
|
94
97
|
}
|
|
95
98
|
|
|
96
|
-
|
|
99
|
+
/**
|
|
100
|
+
* @param {object} params
|
|
101
|
+
* @param {string} params.server server - 服务端地址 未自定义send_func时必填
|
|
102
|
+
* @param {Array.<string>} [params.events] - 事件列表 默认['click']
|
|
103
|
+
* @param {number} [params.interval_time] - 发送数据间隔 单位毫秒 默认10_000毫秒
|
|
104
|
+
* @param {string} [params.point_sing] - 默认"auto_point",添加元素点击事件的埋点标记,被标记的元素点击时触发埋点事件
|
|
105
|
+
* @param {function} [params.send_func] - 自定义发送函数,默认使用fetch发送POST请求, 使用自定义发送函数时, 可以不填写server参数
|
|
106
|
+
*/
|
|
107
|
+
constructor(params) {
|
|
108
|
+
this.#init(params)
|
|
109
|
+
}
|
|
97
110
|
|
|
98
111
|
#init(params) {
|
|
99
|
-
if (this.#INIT_STATE && this.#isObject(params, "Init only accepts object type data") && this.#params_hash_server(params.server)) {
|
|
112
|
+
if (this.#INIT_STATE && this.#isObject(params, "Init only accepts object type data") && (this.#params_hash_server(params.server) || this.#isFunction(params.send_func, "The server and send_func must receive an argument."))) {
|
|
100
113
|
this.#SERVER = params.server
|
|
101
114
|
this.#INTERVAL_TIME = params.interval_time || this.#INTERVAL_TIME
|
|
102
115
|
this.#POINT_SING = params.point_sing || this.#POINT_SING
|
|
103
116
|
if (params.events && this.#isArray(params.events, "events must be an Array")) {
|
|
104
117
|
this.#EVENTS = params.events
|
|
105
118
|
}
|
|
106
|
-
if(params.send_func && this.#isFunction(params.send_func, "send_func must be a function")){
|
|
119
|
+
if (params.send_func && this.#isFunction(params.send_func, "send_func must be a function")) {
|
|
107
120
|
this.#SEND_FUNC = params.send_func
|
|
108
121
|
}
|
|
109
122
|
this.#POINT_SYS_INIT_TIME = new Date().getTime()
|
|
@@ -115,10 +128,12 @@
|
|
|
115
128
|
let describe = "Webpage initialization succeeded"
|
|
116
129
|
this.#add_point(this.#create_active_point_data(type, value, describe))
|
|
117
130
|
this.#INIT_STATE = false
|
|
131
|
+
} else {
|
|
132
|
+
throw "Init failed"
|
|
118
133
|
}
|
|
119
134
|
}
|
|
120
135
|
|
|
121
|
-
#create_point_data_common_params(){
|
|
136
|
+
#create_point_data_common_params() {
|
|
122
137
|
let point_item = new Object()
|
|
123
138
|
point_item.location = JSON.parse(JSON.stringify(window.location))
|
|
124
139
|
point_item.point_sys_init_time = this.#POINT_SYS_INIT_TIME
|
|
@@ -150,7 +165,7 @@
|
|
|
150
165
|
* @param {string} activeValue 埋点动作记录值
|
|
151
166
|
* @returns
|
|
152
167
|
*/
|
|
153
|
-
#create_active_point_data(activeType, activeAttr, activeValue){
|
|
168
|
+
#create_active_point_data(activeType, activeAttr, activeValue) {
|
|
154
169
|
let point_item = this.#create_point_data_common_params()
|
|
155
170
|
point_item.point_type = "active"
|
|
156
171
|
point_item.point_sing = activeType
|
|
@@ -172,7 +187,7 @@
|
|
|
172
187
|
})
|
|
173
188
|
})
|
|
174
189
|
// 挂载hash监听
|
|
175
|
-
window.addEventListener("hashchange",()=>{
|
|
190
|
+
window.addEventListener("hashchange", () => {
|
|
176
191
|
let type = "hashchange"
|
|
177
192
|
let value = window.location.hash
|
|
178
193
|
let describe = "Webpage hash value is changed to: " + window.location.hash
|
|
@@ -202,20 +217,21 @@
|
|
|
202
217
|
// 发送埋点数据
|
|
203
218
|
async #send() {
|
|
204
219
|
if (this.#POINT_LIST.length) {
|
|
205
|
-
this.#
|
|
206
|
-
|
|
207
|
-
|
|
220
|
+
let point_list = this.#POINT_LIST
|
|
221
|
+
for(let middleware of this.#MIDDLEWARE){
|
|
222
|
+
if (this.#isFunction(middleware)) {
|
|
223
|
+
point_list = await middleware(point_list)
|
|
208
224
|
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
let res
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let res
|
|
213
228
|
try {
|
|
214
|
-
res = await this.#SEND_FUNC(
|
|
229
|
+
res = await this.#SEND_FUNC(point_list)
|
|
215
230
|
} catch (error) {
|
|
216
231
|
console.error("Error sending data:", error)
|
|
217
232
|
} finally {
|
|
218
|
-
//
|
|
233
|
+
// 发送后清空埋点列表数据, 并更新最后发送时间
|
|
234
|
+
this.#POINT_LAST_SEND_TIME = new Date().getTime() // 更新最后发送时间
|
|
219
235
|
this.#POINT_LIST = new Array()
|
|
220
236
|
}
|
|
221
237
|
return res
|
|
@@ -225,28 +241,12 @@
|
|
|
225
241
|
// 根据间隔时间,定时发送埋点数据
|
|
226
242
|
#timer_send() {
|
|
227
243
|
setInterval(() => {
|
|
228
|
-
if(!this.#POINT_LAST_SEND_TIME || (new Date().getTime() - this.#POINT_LAST_SEND_TIME >= this.#INTERVAL_TIME)){
|
|
244
|
+
if (!this.#POINT_LAST_SEND_TIME || (new Date().getTime() - this.#POINT_LAST_SEND_TIME >= this.#INTERVAL_TIME)) {
|
|
229
245
|
this.#send()
|
|
230
246
|
}
|
|
231
247
|
}, 200)
|
|
232
248
|
}
|
|
233
249
|
|
|
234
|
-
/**
|
|
235
|
-
* @brief 埋点记录初始化
|
|
236
|
-
* @description server (必填) - 服务端地址
|
|
237
|
-
* @description send_time - 发送数据间隔 单位毫秒 默认10_000毫秒
|
|
238
|
-
* @description point_sing - 默认"auto_point",添加元素点击事件的埋点标记,被标记的元素点击时触发埋点事件
|
|
239
|
-
* @param {object} params
|
|
240
|
-
* @param {string} params.server
|
|
241
|
-
* @param {Array.<string>} [params.events]
|
|
242
|
-
* @param {number} [params.send_time]
|
|
243
|
-
* @param {string} [params.point_sing]
|
|
244
|
-
*/
|
|
245
|
-
init(params) {
|
|
246
|
-
this.#init(params)
|
|
247
|
-
return this
|
|
248
|
-
}
|
|
249
|
-
|
|
250
250
|
// 手动发送数据
|
|
251
251
|
send() {
|
|
252
252
|
return this.#send()
|
|
@@ -254,11 +254,11 @@
|
|
|
254
254
|
|
|
255
255
|
/**
|
|
256
256
|
* @description 挂载中间件
|
|
257
|
-
* @param {function(
|
|
258
|
-
* @return
|
|
257
|
+
* @param {function(point_list)} func 接收一个函数参数,该函数接收一个point_list参数,返回一个point_list
|
|
258
|
+
* @return {point_list} 处理后的埋点数据列表
|
|
259
259
|
*/
|
|
260
260
|
middleware(func) {
|
|
261
|
-
if (this.#
|
|
261
|
+
if (this.#isFunction(func, "Middleware requires a function parameter")) {
|
|
262
262
|
this.#MIDDLEWARE.push(func)
|
|
263
263
|
}
|
|
264
264
|
return this
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ npm i -S auto-point
|
|
|
19
19
|
// 假设AP已经初始化完成
|
|
20
20
|
// 添加一个自定义埋点
|
|
21
21
|
import AP from "auto-point";
|
|
22
|
-
const ap = new AP(
|
|
22
|
+
const ap = new AP({
|
|
23
23
|
server: 'https://your-server.com/api/track', // 替换为真实的服务端地址
|
|
24
24
|
interval_time: 5000, // 发送间隔:5秒
|
|
25
25
|
events: ['click'], // 监听的事件类型
|
|
@@ -29,12 +29,10 @@ const ap = new AP().init({
|
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
//
|
|
33
|
-
ap.middleware(function() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// 注意:中间件函数中的 this 指向 AP 实例
|
|
37
|
-
// 可以在这里修改 this.#POINT_LIST 中的数据
|
|
32
|
+
// 挂载中间件(在发送数据前执行), 中间件函数接收一个参数,即埋点数据列表,返回处理后的埋点数据列表
|
|
33
|
+
ap.middleware(function(point_list) {
|
|
34
|
+
console.log('执行中间件:准备发送埋点数据', point_list);
|
|
35
|
+
return point_list
|
|
38
36
|
});
|
|
39
37
|
|
|
40
38
|
// 添加自定义埋点数据
|
package/package.json
CHANGED