Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-8287: fix(web): stop sending workspace titles to Google Analytics (#9595)

Multica Eve317e17f12322
diff --git a/apps/web/components/google-analytics.test.tsx b/apps/web/components/google-analytics.test.tsx
index 6263190171..256e76e9db 100644
--- a/apps/web/components/google-analytics.test.tsx
+++ b/apps/web/components/google-analytics.test.tsx
@@ -57,3 +57,31 @@ it("stops before a signed download click, but not for clean or blob links", () =
click("https://cdn.example.invalid/a.pdf?Signature=s");
expect(disabled()).toBe(true);
});
+
+// Each queued gtag call, as a plain array.
+const gtagCalls = () => ((w.dataLayer as IArguments[] | undefined) ?? []).map((a) => Array.from(a));
+const pageTitles = () =>
+ gtagCalls().flatMap((c) =>
+ c[0] === "set" ? [(c[1] as { page_title?: string }).page_title] : [],
+ );
+
+it("pins page_title to route labels instead of the workspace document title", () => {
+ document.title = "Fix the secret launch · Multica";
+ start("/acme/issues/MUL-1");
+ window.history.pushState(null, "", "/acme/projects");
+ window.history.replaceState(null, "", "/changelog");
+ window.history.pushState(null, "", "/acme/inbox");
+ window.dispatchEvent(new PopStateEvent("popstate"));
+ expect(pageTitles()).toEqual([
+ "Workspace · Issues · Detail",
+ "Workspace · Projects",
+ "Changelog",
+ "Workspace · Inbox",
+ "Workspace · Inbox",
+ ]);
+ expect(JSON.stringify(gtagCalls())).not.toContain("secret launch");
+ // config parameters outrank `set`, so a page_title in config would pin the
+ // first label for every later page view.
+ const config = gtagCalls().find((c) => c[0] === "config");
+ expect(config?.[2]).not.toHaveProperty("page_title");
+});
diff --git a/apps/web/config/google-analytics.test.ts b/apps/web/config/google-analytics.test.ts
index 46d735776a..2cebe62617 100644
--- a/apps/web/config/google-analytics.test.ts
+++ b/apps/web/config/google-analytics.test.ts
@@ -1,7 +1,11 @@
// @vitest-environment node
import { expect, it } from "vitest";
-import { resolveGoogleAnalyticsId, sanitizeAnalyticsUrl } from "./google-analytics";
+import {
+ analyticsPageTitle,
+ resolveGoogleAnalyticsId,
+ sanitizeAnalyticsUrl,
+} from "./google-analytics";
it("enables only a valid GA4 measurement ID", () => {
expect(resolveGoogleAnalyticsId({})).toBeUndefined();
@@ -17,3 +21,19 @@ it("strips credentials from URLs but keeps campaign params", () => {
expect(sanitizeAnalyticsUrl("https://multica.ai/?utm_source=x&code=c")).toBe("https://multica.ai/?utm_source=x");
expect(sanitizeAnalyticsUrl("javascript:alert(1)")).toBe("");
});
+
+it("reports route labels as page_title, never workspace content", () => {
+ const t = (path: string) => analyticsPageTitle(`https://multica.ai${path}`);
+ expect(t("/")).toBe("Home");
+ expect(t("/changelog")).toBe("Changelog");
+ expect(t("/contact-sales")).toBe("Contact sales");
+ expect(t("/invite/inv_1")).toBe("Invite");
+ expect(t("/lark/bind")).toBe("Lark");
+ expect(t("/acme")).toBe("Workspace");
+ expect(t("/acme/issues")).toBe("Workspace · Issues");
+ expect(t("/acme/issues/MUL-123")).toBe("Workspace · Issues · Detail");
+ expect(t("/acme/my-issues")).toBe("Workspace · My issues");
+ // Unknown segments are not echoed back.
+ expect(t("/acme/secret-roadmap")).toBe("Workspace · Page");
+ expect(analyticsPageTitle("not a url")).toBe("Multica");
+});
diff --git a/apps/web/config/google-analytics.ts b/apps/web/config/google-analytics.ts
index 0758e27990..762c9e275f 100644
--- a/apps/web/config/google-analytics.ts
+++ b/apps/web/config/google-analytics.ts
@@ -1,3 +1,5 @@
+import { RESERVED_SLUGS } from "@multica/core/paths";
+
type RuntimeEnv = Record<string, string | undefined>;
// GA4 Measurement IDs look like `G-RQVF209EHD`. The value is interpolated
@@ -65,6 +67,65 @@ export function isSensitiveAnalyticsUrl(raw: string | undefined | null): boolean
return !parts || parts.sensitive;
}
+// Top-level routes that are not workspace slugs. RESERVED_SLUGS covers most of
+// them; the IM bind and join routes predate it and are listed here directly.
+const GLOBAL_ROUTES_OUTSIDE_RESERVED = new Set(["join", "lark", "slack", "telegram", "wecom"]);
+
+// Sections under /{workspaceSlug}/. Anything else is reported as a generic
+// workspace page rather than echoing an unknown segment.
+const WORKSPACE_SECTIONS = new Set([
+ "agents",
+ "attachments",
+ "autopilots",
+ "billing",
+ "chat",
+ "inbox",
+ "issues",
+ "members",
+ "my-issues",
+ "projects",
+ "runtimes",
+ "settings",
+ "skills",
+ "squads",
+ "usage",
+]);
+
+/**
+ * The page_title reported to Google Analytics for a URL.
+ *
+ * GA4 sends `document.title` as page_title on every event by default, and in
+ * a workspace that title is user content (Issue titles, project names, agent
+ * names). This returns a fixed label built only from route names Multica
+ * defines — never from IDs, slugs, or the live title — so the reported title
+ * carries nothing a workspace member wrote. The path itself is still reported
+ * via page_location.
+ */
+export function analyticsPageTitle(raw: string | undefined | null): string {
+ let pathname: string;
+ try {
+ pathname = new URL(raw ?? "").pathname;
+ } catch {
+ return "Multica";
+ }
+ const segments = pathname.split("/").filter(Boolean);
+ const first = segments[0]?.toLowerCase();
+ if (!first) return "Home";
+ if (RESERVED_SLUGS.has(first) || GLOBAL_ROUTES_OUTSIDE_RESERVED.has(first)) {
+ return humanizeRouteSegment(first);
+ }
+ const section = segments[1]?.toLowerCase();
+ if (!section) return "Workspace";
+ if (!WORKSPACE_SECTIONS.has(section)) return "Workspace · Page";
+ const label = `Workspace · ${humanizeRouteSegment(section)}`;
+ return segments.length > 2 ? `${label} · Detail` : label;
+}
+
+function humanizeRouteSegment(segment: string): string {
+ const words = segment.replace(/-/g, " ");
+ return words.charAt(0).toUpperCase() + words.slice(1);
+}
+
function analyticsUrlParts(
raw: string | undefined | null,
): { sanitized: string; sensitive: boolean } | null {
diff --git a/apps/web/lib/google-analytics.ts b/apps/web/lib/google-analytics.ts
index a72f9bb063..760f0d625c 100644
--- a/apps/web/lib/google-analytics.ts
+++ b/apps/web/lib/google-analytics.ts
@@ -1,4 +1,8 @@
-import { isSensitiveAnalyticsUrl, sanitizeAnalyticsUrl } from "../config/google-analytics";
+import {
+ analyticsPageTitle,
+ isSensitiveAnalyticsUrl,
+ sanitizeAnalyticsUrl,
+} from "../config/google-analytics";
type Gtag = (...args: unknown[]) => void;
type GaWindow = Window & {
@@ -20,6 +24,11 @@ type GaWindow = Window & {
* A fresh document can start again, with its landing referrer sanitized.
* The same applies to activating a credential-bearing link or form (e.g. a
* signed attachment download), which GA reports without a URL change.
+ *
+ * page_title is pinned the same way. GA would otherwise read document.title,
+ * which in a workspace is user content (Issue titles, project names). Every
+ * event instead carries a route label from analyticsPageTitle, set before
+ * GA's own history listener observes a navigation.
*/
export function installGoogleAnalytics(measurementId: string) {
const w = window as unknown as GaWindow;
@@ -45,6 +54,10 @@ export function installGoogleAnalytics(measurementId: string) {
w.dataLayer!.push(arguments);
};
w.gtag("js", new Date());
+ // page_title lives in the global `set` scope only, never in `config`:
+ // config parameters take precedence over set, so a config page_title
+ // would freeze the first label and ignore pinPageTitle on navigation.
+ w.gtag("set", { page_title: analyticsPageTitle(w.location.href) });
// The landing referrer may be a same-origin secret URL (full-page hop
// off /login?cli_state=…); pin a sanitized copy instead.
w.gtag("config", measurementId, {
@@ -58,6 +71,14 @@ export function installGoogleAnalytics(measurementId: string) {
document.head.appendChild(script);
};
+ // Re-pin page_title for the URL GA is about to observe. gtag.js wraps the
+ // history methods after this module does, so its wrapper calls ours first
+ // and sees the label already set when it reports the navigation.
+ const pinPageTitle = (url: string) => {
+ if (!loaded || stopped || !w.gtag) return;
+ w.gtag("set", { page_title: analyticsPageTitle(url) });
+ };
+
const maybeLoad = () => {
if (stopped || loaded) return;
if (isSensitiveAnalyticsUrl(w.location.href)) {
@@ -77,6 +98,7 @@ export function installGoogleAnalytics(measurementId: string) {
if (loaded && (isSensitiveAnalyticsUrl(w.location.href) || isSensitiveAnalyticsUrl(target))) {
stop();
}
+ pinPageTitle(target);
const result = original.call(this, data, unused, url);
// Initial loading must happen after a successful navigation, with the
// real location already clean. No tag has observed earlier URLs yet.
@@ -89,6 +111,7 @@ export function installGoogleAnalytics(measurementId: string) {
// Capture phase, registered before gtag.js: runs ahead of gtag's listener.
const onPopState = () => {
if (loaded && isSensitiveAnalyticsUrl(w.location.href)) stop();
+ pinPageTitle(w.location.href);
maybeLoad();
};
w.addEventListener("popstate", onPopState, true);