Pārlūkot izejas kodu

feat:增加页面停留时长埋点统计

yangfeng 4 mēneši atpakaļ
vecāks
revīzija
bc1b3320ea
2 mainītis faili ar 249 papildinājumiem un 232 dzēšanām
  1. 36 1
      src/easy-use-track.js
  2. 213 231
      yarn.lock

+ 36 - 1
src/easy-use-track.js

@@ -34,11 +34,13 @@ class EasyUse {
       selfTrackURL,
       selfTrackSupportEnabled,
       clabTrackSupportEnabled,
-      upWaitStep: 200
+      upWaitStep: 200,
+      minStayDuration: 2000 // 最小有效停留时间(2秒)
     }, options)
     this.json = []
     this.params = {}
     this._running = false
+    this.enterTime = Date.now() // 记录初始进入时间
     return this.init()
   }
 
@@ -275,6 +277,11 @@ class EasyUse {
         this.upPageInfo()
       }, this.config.upWaitStep)
     }, false)
+
+    // 新增beforeunload监听
+    window.addEventListener('beforeunload', (e) => {
+      this.reportStayDuration()
+    })
   }
 
   /**
@@ -339,6 +346,8 @@ class EasyUse {
       return
     }
 
+    this.reportStayDuration()  // 新增停留统计
+    this.enterTime = Date.now() // 重置进入时间
     this.manualReportPV()
   }
 
@@ -507,6 +516,32 @@ class EasyUse {
       return null
   }
 
+  // 上报页面停留时长
+  reportStayDuration () {
+    const duration = Date.now() - this.enterTime
+    if (duration < this.config.minStayDuration) return
+    const params = this.getNowPageInfo()
+    // 将差值从毫秒换算为秒
+    const diffInSeconds = Math.abs(duration / 1000)
+    params.duration = diffInSeconds
+
+    // 自有埋点上报(统计页面停留时长)
+    if (this.config.selfTrackSupportEnabled) {
+      this.sendSelfTrack({
+        action_id: 'c_jy_stay_duration',
+        action_type: 'events',
+        ...params,
+        date: new Date()
+      })
+    }
+
+    // 荟聚SDK上报
+    if (this.config.clabTrackSupportEnabled) {
+      const tracker = this.useClabTracker()
+      tracker?.track?.('c_jy_stay_duration', params)
+    }
+  }
+
 }
 
 export default EasyUse

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 213 - 231
yarn.lock


Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels