feat: implement memory caching for session ID with fallback to sessionStorage

This commit is contained in:
Usman Baig
2026-01-17 14:11:14 +01:00
parent 08b59c0362
commit 134ceebca1

View File

@@ -20,15 +20,32 @@
const domain = script.getAttribute('data-domain');
const apiUrl = script.getAttribute('data-api') || 'https://analytics-api.ciphera.net';
// * Memory cache for session ID (fallback if sessionStorage is unavailable)
let cachedSessionId = null;
// * Generate ephemeral session ID (not persistent)
function getSessionId() {
const key = 'plausible_session_' + domain;
let sessionId = sessionStorage.getItem(key);
if (!sessionId) {
sessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
sessionStorage.setItem(key, sessionId);
if (cachedSessionId) {
return cachedSessionId;
}
return sessionId;
const key = 'plausible_session_' + domain;
try {
cachedSessionId = sessionStorage.getItem(key);
} catch (e) {
// * Access denied or unavailable - ignore
}
if (!cachedSessionId) {
cachedSessionId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
try {
sessionStorage.setItem(key, cachedSessionId);
} catch (e) {
// * Storage full or unavailable - ignore, will use memory cache
}
}
return cachedSessionId;
}
// * Track pageview