From b57d7e69399e1246ba67952757933d1e4970780d Mon Sep 17 00:00:00 2001 From: Miley Chandonnet Date: Thu, 12 Feb 2026 11:41:38 -0500 Subject: [PATCH] Add Google Analytics tracking to portfolio Track page views across the site including blog navigation with gtag.js and send page_view events on route changes for home, blog list, and individual blog posts. Co-Authored-By: Claude Haiku 4.5 --- src/commonMain/resources/index.html | 8 ++++++++ src/jsMain/kotlin/codes/miley/frontend/App.kt | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/commonMain/resources/index.html b/src/commonMain/resources/index.html index 9ae50a2..98ad48e 100644 --- a/src/commonMain/resources/index.html +++ b/src/commonMain/resources/index.html @@ -4,6 +4,14 @@ Miley Chandonnet • Portfolio + + + diff --git a/src/jsMain/kotlin/codes/miley/frontend/App.kt b/src/jsMain/kotlin/codes/miley/frontend/App.kt index bbbdbdf..19e7203 100644 --- a/src/jsMain/kotlin/codes/miley/frontend/App.kt +++ b/src/jsMain/kotlin/codes/miley/frontend/App.kt @@ -15,6 +15,20 @@ import kotlinx.browser.window import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.* +private fun trackPageView(route: Map) { + val path = when { + route["page"] == "blog" && route.containsKey("post") -> "/blog/${route["post"]}" + route["page"] == "blog" -> "/blog" + else -> "/" + } + val gtag = window.asDynamic().gtag + if (gtag != null) { + val params = js("{}") + params.page_path = path + gtag("event", "page_view", params) + } +} + fun RenderContext.header() { var lastScrollY = window.scrollY div("categories") { @@ -72,6 +86,7 @@ fun main() { .data .distinctUntilChanged() .render { route -> + trackPageView(route) val page = route["page"] header()