fix(monitoring): route Umami events to the endpoint the tracker was built with

The tracker posts to `${data-host-url}${collect path}`, and the collect
path is compiled into the operator's Umami build: /api/send by default,
anything else when the instance sets COLLECT_API_ENDPOINT. The tag
pointed data-host-url at the app origin and the proxy listened only on
/api/send, so a renamed endpoint sent every page view and event to an
app path that does not exist and got a full 404 page back each time.

Point data-host-url at /api/monitoring/umami and answer every path under
it with one proxy route. The route reads the collect path from the
configured tracker (cached for an hour), forwards only that path to the
Umami host, and answers anything else, or an unreadable tracker, with an
empty response. The admin test event posts to the same learnt path. The
send-URL resolver lives in one module instead of two copies.
This commit is contained in:
Marc-André Bombeck committed 2026-10-04 15:13:52 +02:00
1 parent 2e150e97cf
commit 1b1da652d3
12 files changed
+494 -275

No files matched your search

+10 -2
View File
@@ -2,6 +2,8 @@
import { useEffect } from "react";
import { UMAMI_PROXY_PREFIX } from "@/lib/monitoring/umami-paths";
interface UmamiScriptProps {
enabled: boolean;
websiteId: string | null;
@@ -22,8 +24,14 @@ export function UmamiScript({ enabled, websiteId }: UmamiScriptProps) {
script.defer = true;
script.src = "/api/monitoring/umami-script";
script.setAttribute("data-website-id", websiteId);
// Force same-origin tracking endpoint so CSP stays strict.
script.setAttribute("data-host-url", window.location.origin);
// Send events to the same-origin proxy so CSP stays strict. The tracker
// appends its own collect path (`/api/send`, or whatever the operator's
// Umami build renamed it to), and every path under the prefix reaches
// the proxy route.
script.setAttribute(
"data-host-url",
`${window.location.origin}${UMAMI_PROXY_PREFIX}`,
);
if (existing) {
existing.replaceWith(script);