Explorar el Código

feature:新增自定义事件发送和销毁钩子

luoy hace 1 mes
padre
commit
9c51c2c25f
Se han modificado 1 ficheros con 6 adiciones y 2 borrados
  1. 6 2
      buriedPiont.js

+ 6 - 2
buriedPiont.js

@@ -7,11 +7,13 @@ class Tracker {
     this.visitCookieTimeout = options.visitCookieTimeout || 1; // 默认访问次数cookie有效期1分钟
     this.maxVisitCount = options.maxVisitCount || 5; // 默认最大访问次数5次
     this.blockCookieTimeout = options.blockCookieTimeout || 180; // 默认封锁cookie有效期180分钟
+    this.myEventSend = options.myEventSend || (() => {}); // 是否开启自定义事件上报
+    this.beforeDestroy = options.beforeDestroy || (() => {}); // 销毁前的回调函数
   }
 
 init() {
-  const visitCookieName = 'userVisitCount372';
-  const blockCookieName = 'userBlocked372';
+  const visitCookieName = 'userVisitCount';
+  const blockCookieName = 'userBlocked';
   const isBlocked = getCookie(blockCookieName);
   const doneFN = () => {
     this.startTime = Date.now();
@@ -44,6 +46,7 @@ init() {
   bindEvents() {
     window.addEventListener('beforeunload', this.handleBeforeUnload);
     window.addEventListener('click', this.handleClickEvent);
+    this.myEventSend(this.sendData.bind(this)); // 绑定自定义事件上报
   }
 
   startHeartbeat() {
@@ -90,6 +93,7 @@ init() {
   destroy() {
     window.removeEventListener('beforeunload', this.handleBeforeUnload);
     window.removeEventListener('click', this.handleClickEvent);
+    this.beforeDestroy();
     if (this.timer) {
       clearInterval(this.timer);
     }