Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TrustSwap</title>
<script
async defer
async
defer
src="https://cloud.umami.is/script.js"
data-website-id="13760d0b-e30d-43c2-9026-1920a86720c8"
data-host-url="https://cloud.umami.is"
data-domains="trustswap.intuition.box"
data-auto-track="false">
</script>
Expand Down
20 changes: 6 additions & 14 deletions apps/web/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
declare global {
interface Window {
umami?: {
// Custom events
track: (eventName?: string, data?: Record<string, any>) => void;
// Official pageview API
trackView: (url?: string, referrer?: string | null, websiteId?: string) => void;
};
}
}

// Avoid duplicate tracking when rapid route updates occur
// Prevent duplicate for same route
let lastPathname: string | null = null;

// Poll until the Umami script is loaded
// Wait for Umami script
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);
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 });
window.umami.track("pageview", { url: pathname, website: WEBSITE_ID });
}
});
}

export function trackEvent(name: string, data?: Record<string, any>) {
whenUmamiReady(() => {
window.umami?.track?.(name, data);
});
}
Loading