Skip to content

Commit 30ce291

Browse files
committed
Added Google Analytics Tracker
1 parent 248e94f commit 30ce291

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
function gtag() { dataLayer.push(arguments); }
1111
gtag('js', new Date());
1212

13-
gtag('config', 'G-MED2RQS20T');
13+
gtag('config', 'G-XXXXXXXXXX', {
14+
send_page_view: false
15+
});
1416
</script>
1517

1618
<meta charset="UTF-8" />

src/analytics/AnalyticsTracker.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router-dom';
3+
import { sendPageView } from '../analytics/analytics';
4+
5+
export const AnalyticsTracker = () => {
6+
const location = useLocation();
7+
8+
useEffect(() => {
9+
const fullPath = location.pathname + location.search + location.hash;
10+
sendPageView(fullPath);
11+
}, [location]);
12+
13+
return null;
14+
};

src/analytics/analytics.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare global {
2+
interface Window {
3+
gtag?: (...args: any[]) => void;
4+
}
5+
}
6+
7+
export const sendPageView = (url: string) => {
8+
if (typeof window.gtag === 'function') {
9+
window.gtag('event', 'page_view', {
10+
page_path: url,
11+
});
12+
} else {
13+
console.warn('gtag is not defined');
14+
}
15+
};

src/layout/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Container, CssBaseline } from "@mui/material"
22
import Header from "./Header"
33
import { Outlet } from "react-router-dom"
44
import { AnimatePresence } from "framer-motion";
5+
import { AnalyticsTracker } from "../analytics/AnalyticsTracker";
56

67
function App() {
78

@@ -11,6 +12,7 @@ function App() {
1112
<Header />
1213
<AnimatePresence initial={false} mode={"wait"}>
1314
<Container sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', m: 0, p: 0, minHeight: '100vh', minWidth: '100vw' }}>
15+
<AnalyticsTracker />
1416
<Outlet key={window.location.pathname} />
1517
</Container>
1618
</AnimatePresence>

0 commit comments

Comments
 (0)