Fathom Analytics tracking email links, telephone links, external links and download links

Its now possible to track events in Fathom using dynamic variables - which means it is now possible to track

  • Email mailto links

  • Telephone links

  • External Links

  • Download File Links

Here is some sample code

<script>
document.addEventListener('DOMContentLoaded', (event) => {
    // Function to determine the type of link
    const getLinkType = (href) => {
        if (href.startsWith('mailto:')) {
            return 'Email';
        } else if (href.startsWith('tel:')) {
            return 'Telephone';
        } else if (href.startsWith('http') && !href.includes(location.hostname)) {
            return 'External';
        } else if (/\.(pdf|docx|xlsx|pptx|zip|rar)$/i.test(href)) {
            return 'Download';
        }
        return false;
    };

    // Event handler for all links
    const handleLinkClick = (e) => {
        const linkType = getLinkType(e.target.href);

        if (linkType) {
            fathom.trackEvent(`${linkType} : ` + e.target.href);
        }
    };

    // Loop over all links and add the event listener
    const links = document.querySelectorAll('a');
    links.forEach(link => {
        link.addEventListener('click', handleLinkClick);
    });
});
</script>

About Fraser Clark

I've been a professional developer for over 10 years. I've been consulting and developing websites & software for small businesses, multi-nationals & governments.

I'm an expert in WordPress, Drupal, Laravel & a whole host of other platforms.

More about Fraser | Get in touch