56 lines
1.6 KiB
HTML
56 lines
1.6 KiB
HTML
<script>
|
|
(function () {
|
|
// API URL injected from MkDocs config.extra (set by env_config_hook.py)
|
|
var apiUrl = "{{ config.extra.api_url }}";
|
|
if (!apiUrl) return;
|
|
|
|
var trackUrl = apiUrl + "/api/docs-analytics/track";
|
|
|
|
// Anonymous session UUID (sessionStorage — dies with tab close, no cookies)
|
|
function getSessionHash() {
|
|
var key = "__docs_sh";
|
|
var hash = sessionStorage.getItem(key);
|
|
if (!hash) {
|
|
hash = crypto.randomUUID
|
|
? crypto.randomUUID()
|
|
: "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
var r = (Math.random() * 16) | 0;
|
|
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
|
|
});
|
|
sessionStorage.setItem(key, hash);
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
function trackPageView(path) {
|
|
var payload = JSON.stringify({
|
|
path: path,
|
|
referrer: document.referrer || undefined,
|
|
sessionHash: getSessionHash(),
|
|
});
|
|
|
|
// Prefer sendBeacon for reliability (works during tab close)
|
|
if (navigator.sendBeacon) {
|
|
navigator.sendBeacon(trackUrl, new Blob([payload], { type: "application/json" }));
|
|
} else {
|
|
fetch(trackUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: payload,
|
|
keepalive: true,
|
|
}).catch(function () {});
|
|
}
|
|
}
|
|
|
|
// Track initial page load
|
|
trackPageView(location.pathname);
|
|
|
|
// Subscribe to Material's SPA navigation observable (instant loading)
|
|
if (typeof document$ !== "undefined") {
|
|
document$.subscribe(function () {
|
|
trackPageView(location.pathname);
|
|
});
|
|
}
|
|
})();
|
|
</script>
|