UNPKG

overcentric

Version:

Overcentric watches your website, product, and users - and tells you what matters and what to do about it.

50 lines (49 loc) 2.01 kB
import { v4 as uuidv4 } from 'uuid'; const isBrowser = () => typeof window !== 'undefined'; export function generateUuid() { return uuidv4(); } export function storeDeviceId(deviceId) { if (!isBrowser()) return; try { // Determine if we should use Secure flag based on protocol const isSecure = window.location.protocol === 'https:'; const secureFlag = isSecure ? '; Secure' : ''; // First, set the cookie on the exact current domain (no domain attribute) // This ensures it works on the current domain regardless of subdomain structure document.cookie = `overcentric_device_id=${deviceId}; path=/; max-age=31536000; SameSite=Strict${secureFlag}`; // Then try to also set it at the parent domain level as before try { const domain = window.location.hostname.split('.').slice(-2).join('.'); document.cookie = `overcentric_device_id=${deviceId}; domain=.${domain}; path=/; max-age=31536000; SameSite=Strict${secureFlag}`; } catch (e) { // If setting on parent domain fails, we already have the cookie on current domain console.debug('Could not set cookie on parent domain'); } } catch (e) { console.warn('Failed to store device ID:', e); } } export function getDeviceId() { var _a, _b; if (!isBrowser()) return null; try { const deviceId = (_b = (_a = document.cookie .split('; ') .find(row => row.startsWith('overcentric_device_id='))) === null || _a === void 0 ? void 0 : _a.split('=')) === null || _b === void 0 ? void 0 : _b[1]; // If cookie is not available, try to get from window object as fallback if (!deviceId) { const windowDeviceId = window.overcentricDeviceId; return windowDeviceId || null; } return deviceId; } catch (e) { console.warn('Failed to get device ID:', e); return null; } }