diff --git a/README.md b/README.md index 65ae221..ef90b69 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ AnalyticsDataset ```js // 在网站底部插入以下代码即可集成网站分析仪表板 +// 如果需要在统计页面显示访问的路径以及参数,如“/path/?hello=word”,则在网站底部插入以下代码 + ``` ### 数据问题 @@ -65,4 +67,4 @@ https://www.vvhan.com/article/han-analytics ## Stargazers over time -![Stargazers over time](https://starchart.cc/uxiaohan/HanAnalytics.svg?variant=adaptive) \ No newline at end of file +![Stargazers over time](https://starchart.cc/uxiaohan/HanAnalytics.svg?variant=adaptive) diff --git a/public/tracker-all.js b/public/tracker-all.js new file mode 100644 index 0000000..703cd89 --- /dev/null +++ b/public/tracker-all.js @@ -0,0 +1,81 @@ +((window) => { + const { location, document, navigator } = window; + const { hostname: host, pathname } = location; + const { currentScript, referrer } = document; + const currentRef = !referrer.includes(host) ? referrer : ""; + if (!currentScript || navigator.userAgent.indexOf("Electron") > 0) return; + const attr = currentScript.getAttribute.bind(currentScript); + const website = attr("data-website-id"); + const sendURL = new URL(currentScript.src).origin + "/send"; + let visitor = true; + let visit = true; + // 访问间隔 (分钟) + const AccessInterval = 30; + // 页面访问 + let PathVisit = {}; + try { + PathVisit = JSON.parse(localStorage.getItem("_vhLastVisit")) || {}; + if (typeof PathVisit != "object") PathVisit = {}; + } catch (error) { + localStorage.removeItem("_vhLastVisit"); + } + const vhLastVisit = PathVisit[pathname] || 0; + // 访客访问 + const vhLastVisitor = localStorage.getItem("_vhLastVisitor") || 0; + // 是否是今天缓存 + const nowVisitDate = new Date(); + const vhLastVisitorDate = new Date(Number(vhLastVisitor)); + const vhLastVisitDate = new Date(Number(vhLastVisit)); + // 访客访问 + if (nowVisitDate.getFullYear() === vhLastVisitorDate.getFullYear() && nowVisitDate.getMonth() === vhLastVisitorDate.getMonth() && nowVisitDate.getDate() === vhLastVisitorDate.getDate()) { + // 今天缓存即老用户 + visitor = false; + } else { + // 否则更新缓存时间 + localStorage.setItem("_vhLastVisitor", Date.now()); + } + // 访问次数访问 + if (Date.now() - vhLastVisitDate.getTime() < AccessInterval * 60 * 1000) { + // 超过访问间隔即老访客 + visit = false; + } else { + // 否则更新缓存时间 + PathVisit[pathname] = Date.now(); + localStorage.setItem("_vhLastVisit", JSON.stringify(PathVisit)); + } + + // URL转码 + const encode = (str) => { + if (!str) return ""; + try { + if (decodeURI(str) !== str) return decodeURI(str); + } catch (e) { + return str; + } + return encodeURI(str); + }; + + // 上报 + const send = async () => { + if (!website) return; + try { + await fetch(sendURL, { + method: "POST", + body: JSON.stringify({ + website, + host, + path: encode(pathname + location.search), //上报带参数的路径 + referrer: encode(currentRef), + visitor, + visit + }), + headers: { + "Content-Type": "application/json" + } + }); + } catch (e) { + /* empty */ + } + }; + send(); +})(window); diff --git a/public/tracker-all.min.js b/public/tracker-all.min.js new file mode 100644 index 0000000..b8c1b74 --- /dev/null +++ b/public/tracker-all.min.js @@ -0,0 +1 @@ +(a=>{const{location:b,document:c,navigator:d}=a,{hostname:e,pathname:f}=b,{currentScript:g,referrer:h}=c,i=h.includes(e)?"":h;if(!g||0{if(!a)return"";try{if(decodeURI(a)!==a)return decodeURI(a)}catch(b){return a}return encodeURI(a)},v=async()=>{if(k)try{await fetch(l,{method:"POST",body:JSON.stringify({website:k,host:e,path:u(f+b.search),referrer:u(i),visitor:m,visit:n}),headers:{"Content-Type":"application/json"}})}catch(a){}};v()})(window);