From 43f6251f7a764eaa0f470cf20e194d475bfc47ab Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Sat, 24 Jan 2026 19:34:54 -0800 Subject: [PATCH] feat: track page views in gtm --- global.d.ts | 1 + src/router.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/global.d.ts b/global.d.ts index 7f7dd832f..ec455707f 100644 --- a/global.d.ts +++ b/global.d.ts @@ -30,6 +30,7 @@ interface Window { badge?: string } } + dataLayer?: Array> } interface Navigator { diff --git a/src/router.ts b/src/router.ts index b489d2257..88f651899 100644 --- a/src/router.ts +++ b/src/router.ts @@ -36,6 +36,18 @@ function getBasePath(): string { const basePath = getBasePath() +function pushPageView(to: RouteLocationNormalized): void { + if (!isCloud) return + + const dataLayer = window.dataLayer ?? (window.dataLayer = []) + dataLayer.push({ + event: 'page_view', + page_location: window.location.href, + page_title: document.title, + page_path: to.fullPath + }) +} + const router = createRouter({ history: isFileProtocol ? createWebHashHistory() @@ -93,6 +105,10 @@ installPreservedQueryTracker(router, [ } ]) +router.afterEach((to) => { + pushPageView(to) +}) + if (isCloud) { const { flags } = useFeatureFlags() const PUBLIC_ROUTE_NAMES = new Set([