2026-08-12 14:02:25 +03:00
|
|
|
/**
|
|
|
|
|
* Device ID provider — generates and persists a unique device identifier.
|
|
|
|
|
*
|
2026-09-17 15:26:42 +03:00
|
|
|
* Generates a UUID v4 and stores the generated UUID in localStorage under `rudder_device_id`.
|
2026-08-12 14:02:25 +03:00
|
|
|
*/
|
|
|
|
|
|
2026-09-17 15:26:42 +03:00
|
|
|
import { uuidv4 } from '../core/uuid.js';
|
|
|
|
|
|
2026-08-12 14:02:25 +03:00
|
|
|
const STORAGE_KEY = 'rudder_device_id';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the stored device ID or creates a new one if none exists.
|
|
|
|
|
* The device ID is persisted in localStorage and survives page reloads.
|
|
|
|
|
*/
|
|
|
|
|
export function getOrCreateDeviceId(): string {
|
|
|
|
|
try {
|
|
|
|
|
const existing = localStorage.getItem(STORAGE_KEY);
|
|
|
|
|
if (existing) return existing;
|
|
|
|
|
} catch {
|
|
|
|
|
// localStorage unavailable — generate ephemeral ID.
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-17 15:26:42 +03:00
|
|
|
const id = uuidv4();
|
2026-08-12 14:02:25 +03:00
|
|
|
try {
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, id);
|
|
|
|
|
} catch {
|
|
|
|
|
// Storage unavailable — ID will be ephemeral for this session.
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|