@yext/analytics
Version:
An analytics library for Yext
29 lines • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrSetupSessionId = void 0;
const ulidx_1 = require("ulidx");
const SESSION_ID_KEY = 'yext_analytics_session_id';
/**
* Retrieves session id from session storage, or generates a new ULID to use as session id.
* The new id is then stored in session storage. Returns null if the sessionStorage API is
* unavailable (e.g. The function is running on the server for SSR).
*/
function getOrSetupSessionId() {
if (typeof window === 'undefined') {
return null;
}
try {
let sessionId = window.sessionStorage.getItem(SESSION_ID_KEY);
if (!sessionId) {
sessionId = (0, ulidx_1.ulid)();
window.sessionStorage.setItem(SESSION_ID_KEY, sessionId);
}
return sessionId;
}
catch (err) {
console.warn('Unable to use browser sessionStorage for sessionId.\n', err);
return null;
}
}
exports.getOrSetupSessionId = getOrSetupSessionId;
//# sourceMappingURL=setupSessionId.js.map