docs: clarify default and optional data-storage options in ScriptSetupBlock and script.js for improved user understanding
This commit is contained in:
@@ -90,7 +90,7 @@ export default function ScriptSetupBlock({
|
|||||||
{`<script defer data-domain="${site.domain}" data-api="${API_URL}" src="${APP_URL}/script.js"></script>`}
|
{`<script defer data-domain="${site.domain}" data-api="${API_URL}" src="${APP_URL}/script.js"></script>`}
|
||||||
</code>
|
</code>
|
||||||
<p className="mt-2 text-xs text-neutral-500 dark:text-neutral-400">
|
<p className="mt-2 text-xs text-neutral-500 dark:text-neutral-400">
|
||||||
Optional: <code className="rounded px-1 bg-neutral-200 dark:bg-neutral-700">data-storage="session"</code> for per-tab (ephemeral) visitor counting.
|
Default: cross-tab (localStorage). Optional: <code className="rounded px-1 bg-neutral-200 dark:bg-neutral-700">data-storage="session"</code> to opt out (per-tab, ephemeral).
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Pulse - Privacy-First Tracking Script
|
* Pulse - Privacy-First Tracking Script
|
||||||
* Lightweight, no cookies, GDPR compliant.
|
* Lightweight, no cookies, GDPR compliant.
|
||||||
* Default: cross-tab visitor ID (localStorage, optional data-storage-ttl in hours).
|
* Default: cross-tab visitor ID (localStorage), optional data-storage-ttl in hours.
|
||||||
* Optional: data-storage="session" for per-tab (ephemeral) counting.
|
* Optional: data-storage="session" for per-tab (ephemeral) counting.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -116,8 +116,8 @@
|
|||||||
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
// * Returns session/visitor ID. Default: ephemeral (sessionStorage, per-tab).
|
// * Returns session/visitor ID. Default: persistent (localStorage, cross-tab), optional TTL in hours.
|
||||||
// * With data-storage="local": persistent (localStorage, cross-tab), optional TTL in hours.
|
// * With data-storage="session": ephemeral (sessionStorage, per-tab).
|
||||||
function getSessionId() {
|
function getSessionId() {
|
||||||
if (cachedSessionId) {
|
if (cachedSessionId) {
|
||||||
return cachedSessionId;
|
return cachedSessionId;
|
||||||
@@ -158,6 +158,20 @@
|
|||||||
}
|
}
|
||||||
} catch (e2) {}
|
} catch (e2) {}
|
||||||
}
|
}
|
||||||
|
// * Final re-read immediately before write to avoid overwriting a fresher ID from another tab
|
||||||
|
var rawBeforeWrite = localStorage.getItem(key);
|
||||||
|
if (rawBeforeWrite) {
|
||||||
|
try {
|
||||||
|
var parsedBefore = JSON.parse(rawBeforeWrite);
|
||||||
|
if (parsedBefore && typeof parsedBefore.id === 'string') {
|
||||||
|
var expBefore = ttlMs > 0 && typeof parsedBefore.created === 'number' && (Date.now() - parsedBefore.created > ttlMs);
|
||||||
|
if (!expBefore) {
|
||||||
|
cachedSessionId = parsedBefore.id;
|
||||||
|
return cachedSessionId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e3) {}
|
||||||
|
}
|
||||||
localStorage.setItem(key, JSON.stringify({ id: cachedSessionId, created: Date.now() }));
|
localStorage.setItem(key, JSON.stringify({ id: cachedSessionId, created: Date.now() }));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
cachedSessionId = generateId();
|
cachedSessionId = generateId();
|
||||||
|
|||||||
Reference in New Issue
Block a user