-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.js
More file actions
87 lines (77 loc) · 2.84 KB
/
Copy pathanalytics.js
File metadata and controls
87 lines (77 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script>
// analytics.js - Custom event tracking for GA4
(function() {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Track PDF downloads
document.addEventListener('click', function(e) {
const target = e.target.closest('a');
if (!target) return;
const href = target.getAttribute('href');
if (href && href.endsWith('.pdf')) {
gtag('event', 'file_download', {
file_name: href.split('/').pop(),
link_text: target.textContent.trim()
});
}
});
// Track external link clicks
document.addEventListener('click', function(e) {
const target = e.target.closest('a');
if (!target) return;
const href = target.getAttribute('href');
if (href && (href.startsWith('http://') || href.startsWith('https://'))) {
const isExternal = !href.includes(window.location.hostname);
if (isExternal) {
gtag('event', 'click', {
event_category: 'outbound',
event_label: href,
link_domain: new URL(href).hostname
});
}
}
});
// Track course expansion
const courseToggles = document.querySelectorAll('.course-toggle');
courseToggles.forEach(function(btn) {
btn.addEventListener('click', function() {
const courseItem = btn.closest('.course-item');
const courseTitle = courseItem ? courseItem.querySelector('.course-title, h3, h4') : null;
const isExpanding = !courseItem.classList.contains('open');
gtag('event', isExpanding ? 'course_expand' : 'course_collapse', {
event_category: 'engagement',
event_label: courseTitle ? courseTitle.textContent.trim() : 'Unknown Course'
});
});
});
// Track coursework filters
const filterButtons = document.querySelectorAll('.cat-filter, .program-filter');
filterButtons.forEach(function(btn) {
btn.addEventListener('click', function() {
const filterType = btn.classList.contains('cat-filter') ? 'category' : 'program';
const filterValue = btn.dataset.cat || btn.dataset.program;
gtag('event', 'filter_apply', {
event_category: 'coursework_filters',
filter_type: filterType,
filter_value: filterValue
});
});
});
// Track scroll depth
let scrollMarks = { 25: false, 50: false, 75: false, 100: false };
window.addEventListener('scroll', function() {
const winHeight = window.innerHeight;
const docHeight = document.documentElement.scrollHeight - winHeight;
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const scrollPercent = Math.round((scrollTop / docHeight) * 100);
[25, 50, 75, 100].forEach(function(mark) {
if (scrollPercent >= mark && !scrollMarks[mark]) {
scrollMarks[mark] = true;
gtag('event', 'scroll', {
percent_scrolled: mark
});
}
});
});
})();
</script>