// Function to send click data to Google Analytics
function trackClick(event) {
// Check if Google Analytics is available
if (typeof gtag === 'function') {
// Get the clicked element details
let clickID = event.target.id || "No ID";
let clickClasses = event.target.className || "No Classes";
let clickText = event.target.innerText || "No Text";
let clickURL = event.target.href || "No URL";
// Send event data to Google Analytics (GA4)
gtag('event', 'click', {
'event_category': 'Click',
'event_label': clickText,
'click_id': clickID,
'click_classes': clickClasses,
'click_text': clickText,
'click_url': clickURL
});
} else if (typeof ga === 'function') {
// Fallback for Universal Analytics (GA3)
ga('send', 'event', 'Click', clickText, clickURL);
} else {
console.warn("Google Analytics not detected.");
}
}
// Attach the click tracking function to the document
document.addEventListener('click', trackClick);