Google Analytics: track outgoing clicks with jQuery

The script below allows you to track clicks to external links to your website.

In addition, I leave you the code to track PNG image openings and zip file downloads.

jQuery('a').click(function(){
    var linktype = '';
    // Get click href attribut
    var hreflink = jQuery(this).attr('href');
    var myClass = jQuery(this).attr("class");
    
    // Debug
    console.log('Link clicked : ' + hreflink);
    console.log('Class clicked : ' + myClass);

    linktype = hrefType(hreflink);

    // Debug
    console.log('Link type : ' + linktype);

    if( typeof gtag == "undefined" ){
        // Debug
        console.log('GA not loaded');
    }else{
        if(linktype == 'external'){
            gtag ('event', 'clic', {
                'event_category': 'Outlink',
                'event_label': hreflink,
                'transport_type': 'beacon'
              });
        }else if(linktype == 'internal_image'){
            //Debug
            console.log('Click on image');
            gtag ('event', 'clic', {
                'event_category': 'Images',
                'event_label': hreflink,
                'transport_type': 'beacon'
            });
        }else if(linktype == 'internal_download'){
            //Debug
            console.log('Click on file');
            gtag ('event', 'Download', {
                'event_category': hreflink.substr(hreflink.length - 3),
                'event_label': hreflink,
                'transport_type': 'beacon'
            });
        }
    }    
});

function hrefType(link){
    var url = link;
    var sourceString = url.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];

    if(sourceString.charAt(0) == ''){
        return "Anchor";
    }

    if(sourceString !== window.location.host){
        return 'external';
    }

    if(sourceString == window.location.host){

        if(link.substr(link.length - 3) == 'png'){
            return 'internal_image';
        }else if(link.substr(link.length - 3) == 'zip'){
            return 'internal_download'
        }

        return 'internal';
    }

    return 'unknow';
}
Categories Uncategorized


Related Posts


GLPI: basic installation and configuration.
In this article, we will see step by step how to install GLPI (Free Management of IT Park). GLPI is an open source solution that offers businesses a complete solution for managing the information system. Features: Help-desk : Incident and request man

Veeam ONE: installation and configuration
Veeam ONE is a "free" software package offered by Veeam for virtual infrastructure supervision. Veeam ONE consists of software: Veeam ONE Monitor which allows the supervision of hypervisors (Hyper-V and VMware) and virtual machines. It is also possib

Windows 10: Using the sandbox
Presentation Windows 10 Sandbox allows you to launch an isolated Windows 10 (build 18342) environment to perform tests in all security. Prerequisites Enable virtualization in the BIOS. Have Windows 10 Pro or Enterprise. Installation Open the Control