diff --git a/packages/cookie-banner/src/client.ts b/packages/cookie-banner/src/client.ts index e829e7ea4..8c2f49293 100644 --- a/packages/cookie-banner/src/client.ts +++ b/packages/cookie-banner/src/client.ts @@ -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; diff --git a/packages/cookie-banner/src/visitor.ts b/packages/cookie-banner/src/visitor.ts new file mode 100644 index 000000000..90c3f7e93 --- /dev/null +++ b/packages/cookie-banner/src/visitor.ts @@ -0,0 +1,38 @@ +// Copyright (c) 2026 Probo Inc . +// +// 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; +}