Re-add custom page_view event
Some checks are pending
Deploy to GitHub Pages / build (push) Waiting to run
Deploy to GitHub Pages / deploy (push) Blocked by required conditions

This commit is contained in:
Misode
2025-01-04 20:42:04 +01:00
parent e52e6a791c
commit a97c21d9de
4 changed files with 19 additions and 2 deletions

View File

@@ -8,6 +8,7 @@
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-S982VZS08T', {
send_page_view: false,
theme: localStorage.getItem('theme') || 'default',
version: localStorage.getItem('schema_version') || '1.21.2',
locale: localStorage.getItem('language') || 'en',

View File

@@ -4,6 +4,13 @@ import type { VersionId } from './services/index.js'
export type Method = 'menu' | 'hotkey'
export namespace Analytics {
export function pageview(url: string) {
gtag('event', 'page_view', {
page_location: url,
page_title: document.title,
})
}
export function setLocale(locale: string) {
gtag('set', {
locale,

View File

@@ -1,13 +1,22 @@
import type { RouterOnChangeArgs } from 'preact-router'
import { Router } from 'preact-router'
import '../styles/global.css'
import '../styles/nodes.css'
import { Analytics } from './Analytics.js'
import { Header } from './components/index.js'
import { Changelog, Convert, Customized, Generator, Generators, Guide, Guides, Home, LegacyPartners, Partners, Sounds, Transformation, Versions, WhatsNew, Worldgen } from './pages/index.js'
import { cleanUrl } from './Utils.js'
export function App() {
const changeRoute = (e: RouterOnChangeArgs) => {
window.dispatchEvent(new CustomEvent('replacestate'))
// Needs a timeout to ensure the title is set correctly
setTimeout(() => Analytics.pageview(cleanUrl(e.url)))
}
return <>
<Header />
<Router>
<Router onChange={changeRoute}>
<Home path="/" />
<Generators path="/generators" />
<Worldgen path="/worldgen" />

View File

@@ -64,7 +64,7 @@ export function hashString(s: string) {
}
export function cleanUrl(url: string) {
return `/${url}/`.replaceAll('//', '/')
return `/${url}/`.replaceAll(/\/\/+/g, '/')
}
export function getPath(url: string) {