Skip to content

Commit c42918a

Browse files
committed
add gtag
1 parent 4a97310 commit c42918a

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>@servicestack/react - Component Gallery</title>
88
<meta name="description" content="A comprehensive React component library for building modern web applications with ServiceStack" />
9+
10+
<!-- Google Analytics -->
11+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-1F83FL4LLC"></script>
12+
<script>
13+
window.dataLayer = window.dataLayer || [];
14+
function gtag(){dataLayer.push(arguments);}
15+
gtag('js', new Date());
16+
gtag('config', 'G-1F83FL4LLC');
17+
</script>
918
</head>
1019
<body>
1120
<div id="root"></div>

src/analytics.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Google Analytics utility functions
2+
declare global {
3+
interface Window {
4+
gtag?: (...args: any[]) => void;
5+
}
6+
}
7+
8+
export const trackPageView = (url: string) => {
9+
if (typeof window.gtag !== 'undefined') {
10+
window.gtag('config', 'G-1F83FL4LLC', {
11+
page_path: url,
12+
});
13+
}
14+
};

src/main.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React from 'react'
1+
import React, { useEffect } from 'react'
22
import ReactDOM from 'react-dom/client'
3-
import { BrowserRouter, Routes, Route } from 'react-router-dom'
3+
import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom'
44
import { GalleryProvider } from './gallery/context'
5+
import { trackPageView } from './analytics'
56
import Gallery from './gallery'
67
import AlertsPage from './gallery/pages/Alerts'
78
import DataGridPage from './gallery/pages/DataGrid'
@@ -42,10 +43,22 @@ if (colorScheme === 'dark') {
4243

4344
console.log('HTML classList after:', document.documentElement.classList.toString())
4445

46+
// Analytics component to track page views
47+
function Analytics() {
48+
const location = useLocation()
49+
50+
useEffect(() => {
51+
trackPageView(location.pathname + location.search)
52+
}, [location])
53+
54+
return null
55+
}
56+
4557
ReactDOM.createRoot(document.getElementById('root')!).render(
4658
<React.StrictMode>
4759
<GalleryProvider>
4860
<BrowserRouter>
61+
<Analytics />
4962
<Routes>
5063
<Route path="/" element={<Gallery />} />
5164
<Route path="/gallery" element={<Gallery />} />

0 commit comments

Comments
 (0)