Skip to content

Commit a97c21d

Browse files
committed
Re-add custom page_view event
1 parent e52e6a7 commit a97c21d

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
function gtag(){dataLayer.push(arguments);}
99
gtag('js', new Date());
1010
gtag('config', 'G-S982VZS08T', {
11+
send_page_view: false,
1112
theme: localStorage.getItem('theme') || 'default',
1213
version: localStorage.getItem('schema_version') || '1.21.2',
1314
locale: localStorage.getItem('language') || 'en',

src/app/Analytics.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import type { VersionId } from './services/index.js'
44
export type Method = 'menu' | 'hotkey'
55

66
export namespace Analytics {
7+
export function pageview(url: string) {
8+
gtag('event', 'page_view', {
9+
page_location: url,
10+
page_title: document.title,
11+
})
12+
}
13+
714
export function setLocale(locale: string) {
815
gtag('set', {
916
locale,

src/app/App.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
import type { RouterOnChangeArgs } from 'preact-router'
12
import { Router } from 'preact-router'
23
import '../styles/global.css'
34
import '../styles/nodes.css'
5+
import { Analytics } from './Analytics.js'
46
import { Header } from './components/index.js'
57
import { Changelog, Convert, Customized, Generator, Generators, Guide, Guides, Home, LegacyPartners, Partners, Sounds, Transformation, Versions, WhatsNew, Worldgen } from './pages/index.js'
8+
import { cleanUrl } from './Utils.js'
69

710
export function App() {
11+
const changeRoute = (e: RouterOnChangeArgs) => {
12+
window.dispatchEvent(new CustomEvent('replacestate'))
13+
// Needs a timeout to ensure the title is set correctly
14+
setTimeout(() => Analytics.pageview(cleanUrl(e.url)))
15+
}
16+
817
return <>
918
<Header />
10-
<Router>
19+
<Router onChange={changeRoute}>
1120
<Home path="/" />
1221
<Generators path="/generators" />
1322
<Worldgen path="/worldgen" />

src/app/Utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function hashString(s: string) {
6464
}
6565

6666
export function cleanUrl(url: string) {
67-
return `/${url}/`.replaceAll('//', '/')
67+
return `/${url}/`.replaceAll(/\/\/+/g, '/')
6868
}
6969

7070
export function getPath(url: string) {

0 commit comments

Comments
 (0)