A lowercase, hyphenated UUIDv7 string.
generateUuidV7(); // => "018f4f3e-1a2b-7c3d-8e4f-5a6b7c8d9e0f"
export function generateUuidV7(): string {
const bytes = new Uint8Array(16);
writeTimestampBytes(bytes, Date.now());
const random = crypto.getRandomValues(new Uint8Array(10));
bytes.set(random, 6);
// Version: top nibble of byte 6 becomes 0111 (7).
bytes[6] = (bytes[6] & 0x0f) | 0x70;
// Variant: top two bits of byte 8 become 10.
bytes[8] = (bytes[8] & 0x3f) | 0x80;
return bytesToUuidString(bytes);
}
Generates a UUIDv7: a 48-bit millisecond timestamp followed by 74 random bits (with the version and variant bits overlaid per RFC 9562). Unlike
crypto.randomUUID()(always v4, fully random), a v7 id's leading bytes sort chronologically and embed its creation time — required for PostHog's$session_idproperty, which PostHog validates against captured events' timestamps.