auto-point 0.0.21 → 0.0.23

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,10 +13,20 @@
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秒
19
- #SEND_TIME = 10000
29
+ #INTERVAL_TIME = 10000
20
30
  // sing挂载事件元素标记
21
31
  #POINT_SING = "auto_point"
22
32
  // 埋点队列
@@ -88,11 +98,14 @@
88
98
  #init(params) {
89
99
  if (this.#INIT_STATE && this.#isObject(params, "Init only accepts object type data") && this.#params_hash_server(params.server)) {
90
100
  this.#SERVER = params.server
91
- this.#SEND_TIME = params.send_time || this.#SEND_TIME
101
+ this.#INTERVAL_TIME = params.interval_time || this.#INTERVAL_TIME
92
102
  this.#POINT_SING = params.point_sing || this.#POINT_SING
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
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
- if (navigator && navigator.sendBeacon) {
171
- let type = "visibilitychange"
172
- let value = document.visibilityState
173
- let describe = "Webpage visibility state is changed to: " + document.visibilityState
174
- this.#add_point(this.#create_active_point_data(type, value, describe))
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,20 @@
198
209
  })
199
210
  this.#POINT_LAST_SEND_TIME = new Date().getTime() // 更新最后发送时间
200
211
  navigator.sendBeacon(this.#SERVER, JSON.stringify(this.#POINT_LIST));
212
+ let res = await this.#SEND_FUNC(this.#POINT_LIST)
201
213
  // 发送后清空埋点列表数据
202
214
  this.#POINT_LIST = new Array()
215
+ return res
203
216
  }
204
217
  }
205
218
 
206
- // 定时发送
219
+ // 根据间隔时间,定时发送埋点数据
207
220
  #timer_send() {
208
221
  setInterval(() => {
209
- this.#send()
210
- }, this.#SEND_TIME)
222
+ if(!this.#POINT_LAST_SEND_TIME || (new Date().getTime() - this.#POINT_LAST_SEND_TIME >= this.#INTERVAL_TIME)){
223
+ this.#send()
224
+ }
225
+ }, 200)
211
226
  }
212
227
 
213
228
  /**
@@ -228,7 +243,7 @@
228
243
 
229
244
  // 手动发送数据
230
245
  send() {
231
- this.#send()
246
+ return this.#send()
232
247
  }
233
248
 
234
249
  /**
package/README.md CHANGED
@@ -21,7 +21,7 @@ npm i -S auto-point
21
21
  import AP from "auto-point";
22
22
  const ap = new AP().init({
23
23
  server: 'https://your-server.com/api/track', // 替换为真实的服务端地址
24
- send_time: 5000, // 发送间隔:5秒
24
+ interval_time: 5000, // 发送间隔:5秒
25
25
  events: ['click'], // 监听的事件类型
26
26
  point_sing: 'data-track-id' // 埋点标记属性名 如果元素上带有该属性名,AP会自动记录该属性值,否则不会记录
27
27
  });
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.21"
16
+ "version": "0.0.23"
17
17
  }