Skip to content

Commit 4d10416

Browse files
author
River@devbox
committed
Add Google Analytics tracking for page views and course clicks
1 parent a79bbf4 commit 4d10416

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
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>QPython+ - Personalized Programming Learning Community</title>
8+
<!-- Google tag (gtag.js) -->
9+
<script async src="https://www.googletagmanager.com/gtag/js?id=G-JH6WGHX4M2"></script>
10+
<script>
11+
window.dataLayer = window.dataLayer || [];
12+
function gtag(){dataLayer.push(arguments);}
13+
gtag('js', new Date());
14+
gtag('config', 'G-JH6WGHX4M2');
15+
</script>
816
</head>
917
<body>
1018
<div id="root"></div>

src/pages/CoursesPage.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ export function CoursesPage() {
291291
rel="noopener noreferrer"
292292
className="block bg-white rounded-lg shadow-lg overflow-hidden hover:shadow-xl transition-shadow"
293293
onClick={(e) => {
294+
if (typeof window.gtag !== 'undefined') {
295+
window.gtag('event', 'select_content', {
296+
content_type: 'course',
297+
item_id: course.id,
298+
})
299+
}
294300
if (typeof window !== 'undefined' && (window as any).milib) {
295301
e.preventDefault()
296302
;(window as any).milib.openUrl(course.url)

src/types/global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Window {
2+
gtag: (...args: any[]) => void
3+
}

0 commit comments

Comments
 (0)