Skip to content

Commit 2bea211

Browse files
Add GA tracking (#22)
1 parent ca15041 commit 2bea211

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>E-Bikes Portal</title>
8+
9+
<!-- Google Analytics -->
10+
<script async src="https://www.googletagmanager.com/gtag/js?id=%VITE_GA_MEASUREMENT_ID%"></script>
11+
<script>
12+
window.dataLayer = window.dataLayer || [];
13+
function gtag(){dataLayer.push(arguments);}
14+
gtag('js', new Date());
15+
gtag('config', '%VITE_GA_MEASUREMENT_ID%');
16+
</script>
817
</head>
918
<body>
1019
<div id="root"></div>

src/client/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { LicenseType, User } from '../db/users';
1111
import Analyze from './components/analytics/Analyze';
1212
import AIAssistent from './components/analytics/AIAssistent';
1313
import DemoScript from './components/auth/DemoScript';
14-
import { useMobile } from './hooks/useMobile';
1514
import MobileRouteGuard from './components/MobileRouteGuard';
15+
import { useGA } from './hooks/useGA';
1616

1717
interface AppContextType {
1818
notifications: NotificationItem[];
@@ -76,6 +76,8 @@ export const UserProvider = ({ children }: { children: ReactNode }) => {
7676
}
7777

7878
function App() {
79+
// Initialize Google Analytics tracking
80+
useGA();
7981

8082
return (
8183
<div className={styles.root}>

src/client/hooks/useGA.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
4+
declare global {
5+
interface Window {
6+
gtag: (command: string, targetId: string, config?: any) => void;
7+
}
8+
}
9+
10+
export const useGA = () => {
11+
const location = useLocation();
12+
13+
useEffect(() => {
14+
// Track page view when location changes
15+
if (window.gtag && import.meta.env.VITE_GA_MEASUREMENT_ID) {
16+
window.gtag('config', import.meta.env.VITE_GA_MEASUREMENT_ID, {
17+
page_path: location.pathname + location.search,
18+
});
19+
}
20+
}, [location]);
21+
};

0 commit comments

Comments
 (0)