Extract visitor ID management into visitor.ts
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
import { getConsentCookie, setConsentCookie } from "./cookie";
|
import { getConsentCookie, setConsentCookie } from "./cookie";
|
||||||
import { NotFoundError } from "./errors";
|
import { NotFoundError } from "./errors";
|
||||||
import { fetchJSON } from "./http";
|
import { fetchJSON } from "./http";
|
||||||
|
import { getOrCreateVisitorId } from "./visitor";
|
||||||
|
|
||||||
export interface CookieItem {
|
export interface CookieItem {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -60,31 +61,6 @@ export interface CookieBannerClientOptions {
|
|||||||
baseUrl: string;
|
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 {
|
export class CookieBannerClient {
|
||||||
private readonly baseUrl: string;
|
private readonly baseUrl: string;
|
||||||
private readonly bannerId: string;
|
private readonly bannerId: string;
|
||||||
|
|||||||
38
packages/cookie-banner/src/visitor.ts
Normal file
38
packages/cookie-banner/src/visitor.ts
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user