Skip to content

Commit 6bfe783

Browse files
committed
Add GA4 click tracking for project and contact links
1 parent 698f98d commit 6bfe783

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

site/app.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,37 @@
5757

5858
sections.forEach(s => io.observe(s));
5959
})();
60+
61+
// GA4: track project link + contact clicks
62+
(function trackClicks() {
63+
if (typeof gtag !== 'function') return;
64+
const clean = (s) => (s || '').replace(/\s*\s*$/, '').trim();
65+
66+
document.addEventListener('click', (e) => {
67+
const link = e.target.closest('a');
68+
if (!link) return;
69+
const href = link.getAttribute('href') || '';
70+
71+
// Project links inside a card
72+
const cardLink = link.closest('.card-links');
73+
if (cardLink) {
74+
const card = link.closest('.card');
75+
const project = card && card.querySelector('h3') ? card.querySelector('h3').textContent.trim() : 'Unknown';
76+
gtag('event', 'project_link_click', {
77+
project_name: project,
78+
link_type: clean(link.textContent),
79+
link_url: href
80+
});
81+
return;
82+
}
83+
84+
// Contact / social links
85+
let method = null;
86+
if (href.startsWith('mailto:')) method = 'email';
87+
else if (href.includes('linkedin.com')) method = 'linkedin';
88+
else if (href.includes('github.com')) method = 'github';
89+
if (method) {
90+
gtag('event', 'contact_click', { method: method, link_url: href });
91+
}
92+
});
93+
})();

0 commit comments

Comments
 (0)