auto-point 0.0.24 → 0.0.26

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