阅览记录监听
This commit is contained in:
parent
7c77c92c33
commit
e40d7b8dbf
@ -643,6 +643,128 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 用户停留时间追踪器
|
||||||
|
const stayTimeTracker = {
|
||||||
|
totalStayTime: 0, // 总有效停留时间(秒)
|
||||||
|
intervalStartTime: 0, // 当前间隔开始时间戳
|
||||||
|
isActive: true, // 是否处于活跃状态
|
||||||
|
checkInterval: 5000, // 活动检查间隔(毫秒)
|
||||||
|
|
||||||
|
// 初始化
|
||||||
|
init() {
|
||||||
|
this.intervalStartTime = Date.now();
|
||||||
|
|
||||||
|
// 页面可见性变化监听
|
||||||
|
document.addEventListener('visibilitychange', () => {
|
||||||
|
if (document.hidden) {
|
||||||
|
this.pauseTracking();
|
||||||
|
} else {
|
||||||
|
this.resumeTracking();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 用户活动监听(鼠标移动、点击、滚动、键盘等)
|
||||||
|
const activityEvents = ['mousemove', 'click', 'scroll', 'keydown', 'touchstart'];
|
||||||
|
activityEvents.forEach(event => {
|
||||||
|
window.addEventListener(event, this.resetTimer.bind(this), { passive: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
// 开始检查
|
||||||
|
this.startChecking();
|
||||||
|
|
||||||
|
// 页面卸载前保存数据
|
||||||
|
window.addEventListener('beforeunload', this.handleUnload.bind(this));
|
||||||
|
},
|
||||||
|
|
||||||
|
// 开始检查活动状态
|
||||||
|
startChecking() {
|
||||||
|
setInterval(() => {
|
||||||
|
if (!this.isActive) return;
|
||||||
|
|
||||||
|
const currentTime = Date.now();
|
||||||
|
const elapsedSeconds = Math.floor((currentTime - this.intervalStartTime) / 1000);
|
||||||
|
|
||||||
|
// 达到60秒有效间隔
|
||||||
|
if (elapsedSeconds >= 10) {
|
||||||
|
this.totalStayTime += 10;
|
||||||
|
//this.intervalStartTime = currentTime;
|
||||||
|
this.reportStayTime(10,this.intervalStartTime,currentTime,this.isActive);
|
||||||
|
}
|
||||||
|
}, this.checkInterval);
|
||||||
|
},
|
||||||
|
|
||||||
|
// 重置计时器(用户有活动时调用)
|
||||||
|
resetTimer() {
|
||||||
|
if (!this.isActive) {
|
||||||
|
this.resumeTracking();
|
||||||
|
}
|
||||||
|
this.intervalStartTime = Date.now();
|
||||||
|
},
|
||||||
|
|
||||||
|
// 暂停追踪
|
||||||
|
pauseTracking() {
|
||||||
|
this.isActive = false;
|
||||||
|
console.log('停留追踪已暂停');
|
||||||
|
},
|
||||||
|
|
||||||
|
// 恢复追踪
|
||||||
|
resumeTracking() {
|
||||||
|
this.isActive = true;
|
||||||
|
this.intervalStartTime = Date.now();
|
||||||
|
console.log('停留追踪已恢复');
|
||||||
|
},
|
||||||
|
|
||||||
|
// 报告停留时间
|
||||||
|
reportStayTime(duration,intervalStartTime,currentTime,isActive) {
|
||||||
|
console.log(`有效停留时间: ${duration}秒 | 总计: ${this.totalStayTime}秒 ${currentTime} ${intervalStartTime}`);
|
||||||
|
|
||||||
|
// 实际应用中这里发送数据到服务器
|
||||||
|
$.ajax({
|
||||||
|
type : "POST",
|
||||||
|
url : "./w?sid=" + encodeURIComponent($('#sid').val()) + "&cmd=com.awspaas.user.apps.browsing_data.service.insertReadingLog",
|
||||||
|
data : "userId="+userId+"&userName="+userName+"&ruuid="+ruuid+"&fileName="+fileName+"&intervalStartTime="+intervalStartTime+"¤tTime="+currentTime+"&browserId="+browserId+"&isActive="+isActive,
|
||||||
|
success : function(msg) {
|
||||||
|
if(msg.result==ok){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
// 发送数据到服务器(使用sendBeacon确保页面关闭时也能发送)
|
||||||
|
sendToServer(duration,intervalStartTime,currentTime,isActive) {
|
||||||
|
// 实际应用中这里发送数据到服务器
|
||||||
|
$.ajax({
|
||||||
|
type : "POST",
|
||||||
|
url : "./w?sid=" + encodeURIComponent($('#sid').val()) + "&cmd=com.awspaas.user.apps.browsing_data.service.insertReadingLog",
|
||||||
|
data : "userId="+userId+"&userName="+userName+"&ruuid="+ruuid+"&fileName="+fileName+"&intervalStartTime="+intervalStartTime+"¤tTime="+currentTime+"&browserId="+browserId+"&isActive="+isActive,
|
||||||
|
success : function(msg) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
// 页面卸载处理
|
||||||
|
handleUnload() {
|
||||||
|
// 如果有部分停留时间但不足60秒
|
||||||
|
const partialTime = Math.floor((Date.now() - this.intervalStartTime) / 1000);
|
||||||
|
if (partialTime > 0) {
|
||||||
|
console.log(`未完成间隔的停留时间: ${partialTime}秒`);
|
||||||
|
}
|
||||||
|
const currentTime = Date.now();
|
||||||
|
// 发送总停留时间
|
||||||
|
if (this.totalStayTime > 0) {
|
||||||
|
this.sendToServer(this.totalStayTime,this.intervalStartTime,currentTime,false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 启动追踪器
|
||||||
|
stayTimeTracker.init();
|
||||||
|
|
||||||
//初始化封面模板
|
//初始化封面模板
|
||||||
function initCoverTpl(){
|
function initCoverTpl(){
|
||||||
$.ajax({
|
$.ajax({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user