Extract visitor ID management into visitor.ts

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-15 18:17:17 +04:00
parent 5cdf39c4f7
commit 0abde83a88
2 changed files with 39 additions and 25 deletions

View File

@@ -15,6 +15,7 @@
import { getConsentCookie, setConsentCookie } from "./cookie";
import { NotFoundError } from "./errors";
import { fetchJSON } from "./http";
import { getOrCreateVisitorId } from "./visitor";
export interface CookieItem {
name: string;
@@ -60,31 +61,6 @@ export interface CookieBannerClientOptions {
baseUrl: string;
}
const STORAGE_KEY_PREFIX = "probo_consent";
function getOrCreateVisitorId(bannerId: string): string {
const key = `${STORAGE_KEY_PREFIX}:${bannerId}:vid`;
try {
const stored = localStorage.getItem(key);
if (stored) {
return stored;
}
} catch {
// localStorage unavailable
}
const id = crypto.randomUUID();
try {
localStorage.setItem(key, id);
} catch {
// localStorage unavailable
}
return id;
}
export class CookieBannerClient {
private readonly baseUrl: string;
private readonly bannerId: string;

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
const STORAGE_KEY_PREFIX = "probo_consent";
export function getOrCreateVisitorId(bannerId: string): string {
const key = `${STORAGE_KEY_PREFIX}:${bannerId}:vid`;
try {
const stored = localStorage.getItem(key);
if (stored) {
return stored;
}
} catch {
// localStorage unavailable
}
const id = crypto.randomUUID();
try {
localStorage.setItem(key, id);
} catch {
// localStorage unavailable
}
return id;
}