Add external link tracking with jQuery and Google Analytics

Quick and easy way to setup Google Analytics tracking for all links that open in a new window (typically external links that wouldn’t normally be tracked)… Note, this should be called from within $(document).ready or after all anchor tags have been added to the DOM.

1
2
3
4
5
6
$('a[target=_blank]').click(function(){
    try{
        _gaq.push(['_trackEvent', 'External Links', 'Click', $(this).attr('href')]);
    } catch(err) {}
    return true;
});

Leave a Reply