nerdware/apps/web/src/lib/analytics.ts
AyoubBenziza 1bda0e122e feat(web): optional Umami analytics (env-driven)
- inject the Umami script at runtime only when VITE_UMAMI_SRC + VITE_UMAMI_WEBSITE_ID
  are set (so dev/local traffic isn't tracked); website-id is public, not a secret
- web Dockerfile passes them as build args; docker-compose.prod.yml + env examples
  document them (optional, empty = disabled)

Verified: build bakes the values in when set; skipped when unset.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 08:40:35 +02:00

20 lines
672 B
TypeScript

// Umami analytics : on injecte le script seulement si les variables de build
// sont présentes (donc pas en dev → pas de trafic local dans les stats).
// L'ID de site est public (visible dans le HTML), ce n'est pas un secret.
const SRC = import.meta.env.VITE_UMAMI_SRC
const WEBSITE_ID = import.meta.env.VITE_UMAMI_WEBSITE_ID
export function initAnalytics(): void {
if (!SRC || !WEBSITE_ID) {
return
}
if (document.querySelector("script[data-website-id]")) {
return
}
const script = document.createElement("script")
script.defer = true
script.src = SRC
script.setAttribute("data-website-id", WEBSITE_ID)
document.head.appendChild(script)
}