Tracker env is read at request time and the CSP already allows the analytics host, so staging can set a rule admin, an ops bearer token, and the cookieless Umami site without rebuilding.
25 lines
802 B
TypeScript
25 lines
802 B
TypeScript
/**
|
|
* Optional Umami tracker. Unset env vars mean no script ships.
|
|
* Cookie-free mode is an Umami server setting, not this snippet.
|
|
*
|
|
* Values are read by key at request time so Cloudron `env set` works
|
|
* without rebuilding the image (`NEXT_PUBLIC_*` is otherwise inlined
|
|
* at `next build`).
|
|
*/
|
|
function readEnv(name: string): string {
|
|
return process.env[name]?.trim() ?? "";
|
|
}
|
|
|
|
export function umamiTracker(): { src: string; websiteId: string } | null {
|
|
const src = readEnv("NEXT_PUBLIC_UMAMI_SRC");
|
|
const websiteId = readEnv("NEXT_PUBLIC_UMAMI_WEBSITE_ID");
|
|
if (!src || !websiteId) return null;
|
|
try {
|
|
const url = new URL(src);
|
|
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
|
|
return { src: url.href, websiteId };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|