场景:因为有需求,在ios微信,打开h5预览页面开始记录预览开始时间,然后点击左上角 x 关闭h5时,调用埋点接口,统计用户在页面留存时间多数
问题:在安卓、浏览器内一切正常,但是ios内会存在问题,点击左上角 x 关闭h5时,调用埋点接口时,有时会发出,有时不会发出
查询了好多资料,都没有找到解决问题,所以发出帖子来,请大家一起帮忙研究研究
发帖求助,先感谢官方及各路大佬的解答
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
document.addEventListener("visibilitychange", function logData() {
if (document.visibilityState === "hidden") {
navigator.sendBeacon("https://xxx/save", JSON.stringify({
"userId": "11111",
"startTime": "2024-02-27 16:11:26.35",
"endTime": "2024-02-27 16:11:31.587",
"content": "{\"OS\":\"iphone os\",\"OSVersion\":\"17\",\"browser\":\"applewebkit\"}"
}));
}
});
// 监听网页关闭或刷新
window.addEventListener('unload', logData, false);
function logData() {
navigator.sendBeacon("https://xxx/save", JSON.stringify({
"userId": "22222",
"startTime": "2024-02-27 16:11:26.35",
"endTime": "2024-02-27 16:11:31.587",
"content": "{\"OS\":\"iphone os\",\"OSVersion\":\"17\",\"browser\":\"applewebkit\"}"
}));
}
document.addEventListener("pagehide", function logData() {
navigator.sendBeacon("https://xxx/save", JSON.stringify({
"userId": "33333",
"startTime": "2024-02-27 16:11:26.35",
"endTime": "2024-02-27 16:11:31.587",
"content": "{\"OS\":\"iphone os\",\"OSVersion\":\"17\",\"browser\":\"applewebkit\"}"
}));
});
</script>
</body>
</html>
顶帖,求助,还是没有发现解决办法