This commit is contained in:
make
2026-02-22 19:42:47 +08:00
parent f4ee8dd16b
commit 6bb0953ab2
3 changed files with 63 additions and 0 deletions

View File

@@ -12,3 +12,40 @@
git restore .
git clean -fd
<script>
// =============================
// 停留时长统计U-Web 自定义事件)
// =============================
(function () {
var enterTs = Date.now(); // 页面进入时间
// 页面可见性变化(比 beforeunload 更稳定)
var totalStay = 0;
var lastVisibleTs = enterTs;
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === "hidden") {
totalStay += Date.now() - lastVisibleTs;
} else if (document.visibilityState === "visible") {
lastVisibleTs = Date.now();
}
});
// 页面关闭/刷新时上报
window.addEventListener("beforeunload", function () {
var now = Date.now();
totalStay += now - lastVisibleTs;
// 上报自定义事件
_czc.push([
"_trackEvent",
"page_duration", // 事件类别
location.pathname, // 事件名称:当前页面路径
"stay_time", // 标签
totalStay // value停留时长毫秒
]);
});
})();
</script>