@dash0/sdk-web
Version:
Dash0's Web SDK to collect telemetry from end-users' web browsers
30 lines (29 loc) • 1.03 kB
JavaScript
import { getItem, isSupported, setItem } from "./session-storage";
import { debug, warn } from "./debug";
import { generateUniqueId, TAB_ID_BYTES } from "./id";
const TAB_ID_STORAGE_KEY = "d0_tab";
export let tabId = null;
/**
* We want to be able to identify what browser tab the user is looking at. We do this by identifying tabs through
* random-generated IDs that are kept in session storage. To quote MDN:
*
* > sessionStorage is partitioned by both origin and browser tabs (top-level browsing contexts)
*/
export function initializeTabId() {
if (!isSupported) {
debug("Storage API is not available and tab tracking is therefore not supported.");
return;
}
try {
const storedValue = getItem(TAB_ID_STORAGE_KEY);
if (storedValue) {
tabId = storedValue;
return;
}
tabId = generateUniqueId(TAB_ID_BYTES);
setItem(TAB_ID_STORAGE_KEY, tabId);
}
catch (e) {
warn("Failed to record tab ID information", e);
}
}