Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ AnalyticsDataset
```js
// 在网站底部插入以下代码即可集成网站分析仪表板
<script defer src="https://xxxxxx.pages.dev/tracker.min.js" data-website-id="自定义网站唯一标识"></script>
// 如果需要在统计页面显示访问的路径以及参数,如“/path/?hello=word”,则在网站底部插入以下代码
<script defer src="https://xxxxxx.pages.dev/tracker-all.min.js" data-website-id="自定义网站唯一标识"></script>
```

### 数据问题
Expand All @@ -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)
![Stargazers over time](https://starchart.cc/uxiaohan/HanAnalytics.svg?variant=adaptive)
81 changes: 81 additions & 0 deletions public/tracker-all.js
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions public/tracker-all.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.