;(function(){ let pageStartTime = performance.now(); let totalActiveTime = 0; let segmentStartTime = pageStartTime; let sendlist = []; //获取referer let referer = document.referrer; //获取pageurl和pagetitle let page_url = window.location.href; let page_title = document.title; //获取查询字符串 let query = window.location.search; let queryObject = {}; if (query) { const urlParams = new URLSearchParams(query); for (const [key, value] of urlParams.entries()) { queryObject[key] = value; } query = JSON.stringify(queryObject); } // let os = getOperatingSystem(); let browser = getBrowserInfo(); let device = getDeviceInfo(); let screen_resolution = getScreenResolution(); let site_referer = ""; //需要判断是否当前域名访问 if (referer.startsWith(window.location.origin)) { //这里获取的是上一个页面,除去当前域名的地址 site_referer = referer.replace(window.location.origin, ""); } let content_type = getMetaContent('content_type'); let content_id = getMetaContent('content_id'); //获取当前时间 需要2023-07-05 00:00:00 格式 let page_enter_time = getCurrentTimeString(); sendlist.push("referer="+encodeURIComponent(referer)); sendlist.push("page_url="+encodeURIComponent(page_url)); sendlist.push("page_title="+encodeURIComponent(page_title)); sendlist.push("query_params="+encodeURIComponent(query)); sendlist.push("client_os="+encodeURIComponent(os)); sendlist.push("client_browser="+encodeURIComponent(browser)); sendlist.push("client_device="+encodeURIComponent(device)); sendlist.push("client_screen_resolution="+encodeURIComponent(screen_resolution)); sendlist.push("site_referer="+encodeURIComponent(site_referer)); sendlist.push("page_enter_time="+encodeURIComponent(page_enter_time)); sendlist.push("content_type="+encodeURIComponent(content_type)); sendlist.push("content_id="+encodeURIComponent(content_id)); sendlist = sendlist.join('&'); let httpRequest = new XMLHttpRequest(); httpRequest.open("POST", "/api/statistics/visit/report/", true); httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); httpRequest.send(sendlist); httpRequest.onreadystatechange = () => { if (httpRequest.readyState === 4 && httpRequest.status === 200) { } else { if (httpRequest.status !== 200) { console.error(httpRequest.status, "data report error"); } } } document.addEventListener('visibilitychange', function() { if (document.hidden) { // 页面隐藏,暂停计时 totalActiveTime += (performance.now() - segmentStartTime); } else { // 页面再次可见,重新开始分段计时 segmentStartTime = performance.now(); } }); window.addEventListener('beforeunload', function() { totalActiveTime += (performance.now() - segmentStartTime); const visitDuration = Math.round(totalActiveTime); // 转换为毫秒整数 // 使用 Beacon API 发送数据 const data = new Blob([JSON.stringify({ visit_duration: visitDuration, })], { type: 'application/json' }); navigator.sendBeacon('/api/statistics/visit/report/update/', data); }); function getOperatingSystem() { const userAgent = navigator.userAgent; // Windows 11 在 User Agent 中可能仍然显示为 NT 10.0,但可以通过其他方式检测 // 目前,Windows 11 在 User Agent 中的显示方式可能不同,具体取决于浏览器 if (userAgent.includes('Windows NT 11.0')) { return 'Windows 11'; } // 一些情况下,Windows 11 仍显示为 NT 10.0,但可通过其他特征检测 else if (userAgent.includes('Windows NT 10.0')) { // 尝试检测 Windows 11 特有的特征 // 由于浏览器厂商实现不同,这种方法可能不完全准确 return 'Windows 10/11'; } if (userAgent.includes('Windows NT 6.3')) return 'Windows 8.1'; if (userAgent.includes('Windows NT 6.2')) return 'Windows 8'; if (userAgent.includes('Windows NT 6.1')) return 'Windows 7'; if (userAgent.includes('Mac OS X')) return 'Mac OS'; if (userAgent.includes('Android')) return 'Android'; if (userAgent.includes('iPhone') || userAgent.includes('iPad')) return 'iOS'; if (userAgent.includes('Linux')) return 'Linux'; return 'Unknown'; } function getBrowserInfo() { const userAgent = navigator.userAgent; if (userAgent.includes('Chrome') && !userAgent.includes('Edg')) return 'Chrome'; if (userAgent.includes('Firefox')) return 'Firefox'; if (userAgent.includes('Safari') && !userAgent.includes('Chrome')) return 'Safari'; if (userAgent.includes('Edg')) return 'Edge'; if (userAgent.includes('MSIE') || userAgent.includes('Trident')) return 'IE'; return 'Unknown'; } function getDeviceInfo() { const userAgent = navigator.userAgent.toLowerCase(); if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(userAgent)) { return 'Tablet'; } if (/mobile|android|iphone|ipod|ipad|iemobile|wpdesktop/i.test(userAgent)) { return 'Mobile'; } return 'Desktop'; } function getScreenResolution() { return `${window.screen.width}x${window.screen.height}`; } function getCurrentTimeString() { const now = new Date(); // 获取年月日时分秒 const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需+1 const day = String(now.getDate()).padStart(2, '0'); const hours = String(now.getHours()).padStart(2, '0'); const minutes = String(now.getMinutes()).padStart(2, '0'); const seconds = String(now.getSeconds()).padStart(2, '0'); // 拼接成目标格式 return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } function getMetaContent(name) { // 查找页面中name属性等于指定值的meta标签 const metaTag = document.querySelector(`meta[name="${name}"]`); // 如果找到meta标签,返回其content属性值;否则返回空字符串 return metaTag ? metaTag.getAttribute('content') || '' : ''; } })();