From c8d40b5cbd3aa66c534f593e6d1684e0f1b14df9 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 22 Sep 2025 22:27:53 +0200 Subject: [PATCH 1/2] fix umami analytics --- apps/web/index.html | 6 ++++-- apps/web/src/analytics.ts | 22 ++++++++-------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/apps/web/index.html b/apps/web/index.html index 5f821e469..6caaaf677 100644 --- a/apps/web/index.html +++ b/apps/web/index.html @@ -5,9 +5,11 @@ TrustSwap diff --git a/apps/web/src/analytics.ts b/apps/web/src/analytics.ts index 9cfc2fab8..759dc651d 100644 --- a/apps/web/src/analytics.ts +++ b/apps/web/src/analytics.ts @@ -1,41 +1,35 @@ declare global { interface Window { umami?: { - // Custom events track: (eventName?: string, data?: Record) => void; - // Official pageview API trackView: (url?: string, referrer?: string | null, websiteId?: string) => void; }; } } -// Avoid duplicate tracking when rapid route updates occur +// Debounce last route let lastPathname: string | null = null; -// Poll until the Umami script is loaded +// Wait for Umami to load function whenUmamiReady(cb: () => void, tries = 20) { if (typeof window !== "undefined" && window.umami) return cb(); if (tries <= 0) return; setTimeout(() => whenUmamiReady(cb, tries - 1), 250); } +const WEBSITE_ID = "13760d0b-e30d-43c2-9026-1920a86720c8"; + export function trackPageView(pathname: string) { if (!pathname || pathname === lastPathname) return; lastPathname = pathname; whenUmamiReady(() => { if (window.umami?.trackView) { - // Proper pageview: shows up in the "Pageviews" metrics - window.umami.trackView(pathname); + // Official pageview -> shows in "Pageviews" + window.umami.trackView(pathname, document.referrer || null, WEBSITE_ID); } else if (window.umami?.track) { - // Fallback as a custom event (visible under "Events", not "Pageviews") - window.umami.track("pageview", { url: pathname }); + // Fallback as custom event (visible under "Events") + window.umami.track("pageview", { url: pathname, website: WEBSITE_ID }); } }); } - -export function trackEvent(name: string, data?: Record) { - whenUmamiReady(() => { - window.umami?.track?.(name, data); - }); -} From 9e10d7db08ce5bb9945f92e367bd939b9b9ca10f Mon Sep 17 00:00:00 2001 From: James Date: Mon, 22 Sep 2025 23:02:28 +0200 Subject: [PATCH 2/2] fix umami analytics --- apps/web/index.html | 4 ++-- apps/web/src/analytics.ts | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/apps/web/index.html b/apps/web/index.html index 6caaaf677..27385b378 100644 --- a/apps/web/index.html +++ b/apps/web/index.html @@ -7,9 +7,9 @@ diff --git a/apps/web/src/analytics.ts b/apps/web/src/analytics.ts index 759dc651d..a3e9cf8bd 100644 --- a/apps/web/src/analytics.ts +++ b/apps/web/src/analytics.ts @@ -7,10 +7,10 @@ declare global { } } -// Debounce last route +// Prevent duplicate for same route let lastPathname: string | null = null; -// Wait for Umami to load +// Wait for Umami script function whenUmamiReady(cb: () => void, tries = 20) { if (typeof window !== "undefined" && window.umami) return cb(); if (tries <= 0) return; @@ -25,10 +25,8 @@ export function trackPageView(pathname: string) { whenUmamiReady(() => { if (window.umami?.trackView) { - // Official pageview -> shows in "Pageviews" window.umami.trackView(pathname, document.referrer || null, WEBSITE_ID); } else if (window.umami?.track) { - // Fallback as custom event (visible under "Events") window.umami.track("pageview", { url: pathname, website: WEBSITE_ID }); } });