Tracking mailto, anchors and external links with Google Analytics

With this simple snippet, all clicks on page-internal anchors, external links (http…) and mailto-links are tracked as events in Google Analytics:

$(function(){
    $("a[href*='http']").each(function() {
                    $(this).click(function (ev) {
                        var pageView = '/outgoing/' + $(this).attr('href');
                        _gat._getTrackerByName()._trackEvent('Outbound Links', pageView);
                        var _href = $(this).attr('href');
                        setTimeout(function() {
                            location.href = _href;
                        }, 100);
                        ev.preventDefault();
                        return false;
                    });
        });
      $("a[href*='mailto']").each(function() {
                    $(this).click(function (ev) {
                        var pageView = '/mailto/' + $(this).attr('href').substring(7);
                        _gat._getTrackerByName()._trackEvent('Mailto', pageView);
                    });
        });
      $("a[href*='#']").each(function() {
                    $(this).click(function (ev) {
                        var pageView = '/anchor/' + $(this).attr('href').substring(1);
                        _gat._getTrackerByName()._trackEvent('Anchors', pageView);
                    });
        });
});

27 thoughts on “Tracking mailto, anchors and external links with Google Analytics

  1. This is great! Thanks for putting this snippet together. I’ve been searching forever for something. May I copy/paste or will I need to customize it before dropping it in?

  2. To clairify, I would paste this below the already established (function({ code before the script end code? Or am I replacing the (function({ code entirely? I’m new to the world of analytics and don’t want to break my tracker (even though it isn’t currently tracking all those things you’ve listed yet)

  3. Dear Lars,

    I placed it in the section, at the bottom of the GA Asynchonous tag, just before the .
    However it does not seem to work.
    What am I doing wrong?

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-123456-1’]);
    _gaq.push([‘_trackPageview’]);

    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();

    $(function(){
    $(“a[href*=’http’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/outgoing/’ + $(this).attr(‘href’);
    _gat._getTrackerByName()._trackEvent(‘Outbound Links’, pageView);
    var _href = $(this).attr(‘href’);
    setTimeout(function() {
    location.href = _href;
    }, 100);
    ev.preventDefault();
    return false;
    });
    });
    $(“a[href*=’mailto’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/mailto/’ + $(this).attr(‘href’).substring(7);
    _gat._getTrackerByName()._trackEvent(‘Mailto’, pageView);
    });
    });
    $(“a[href*=’#’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/anchor/’ + $(this).attr(‘href’).substring(1);
    _gat._getTrackerByName()._trackEvent(‘Anchors’, pageView);
    });
    });
    });

  4. Hi Lars:

    Thank you for this. I have been wanting something like this for years. I have a wordpress site and my theme has seo and integration options that add code to the body of every page. The theme developers had indicated that this is the section for analytics code, which I installed and analytics works perfectly. I just added your snippet in the following manner and was hoping that you would be able to confirm that it is done correctly?

    Thanks again

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-880302-1’]);
    _gaq.push([‘_setCustomVar’, 1, ‘internal’, ‘Yes’, 1]);
    _gaq.push([‘_trackPageview’]);

    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();

    $(function(){
    $(“a[href*=’http’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/outgoing/’ + $(this).attr(‘href’);
    _gat._getTrackerByName()._trackEvent(‘Outbound Links’, pageView);
    var _href = $(this).attr(‘href’);
    setTimeout(function() {
    location.href = _href;
    }, 100);
    ev.preventDefault();
    return false;
    });
    });
    $(“a[href*=’mailto’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/mailto/’ + $(this).attr(‘href’).substring(7);
    _gat._getTrackerByName()._trackEvent(‘Mailto’, pageView);
    });
    });
    $(“a[href*=’#’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/anchor/’ + $(this).attr(‘href’).substring(1);
    _gat._getTrackerByName()._trackEvent(‘Anchors’, pageView);
    });
    });
    });

  5. Sorry – I forgot to ask how to set up goal details in analytics. I think I just set the goal label to “Mailto” in the goal details?

    Thanks again!

  6. Correction – I think that I just set up the goal “category” as “Mailto” for the mailto event, etc..?

    Sorry – I don’t mean to ask a million questions…

  7. I have added your code like this, but i don’t know why it is not working in my website. Can you please look in to it and please suggest me if anything wrong with the code.

    My Code
    —————-

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-34831893-1’]);
    _gaq.push([‘_trackPageview’]);

    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();

    Added your code from here…
    —————————————
    $(function(){
    $(“a[href*=’http’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/outgoing/’ + $(this).attr(‘href’);
    _gat._getTrackerByName()._trackEvent(‘Outbound Links’, pageView);
    var _href = $(this).attr(‘href’);
    setTimeout(function() {
    location.href = _href;
    }, 100);
    ev.preventDefault();
    return false;
    });
    });
    $(“a[href*=’mailto’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/mailto/’ + $(this).attr(‘href’).substring(7);
    _gat._getTrackerByName()._trackEvent(‘Mailto’, pageView);
    });
    });
    $(“a[href*=’#’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/anchor/’ + $(this).attr(‘href’).substring(1);
    _gat._getTrackerByName()._trackEvent(‘Anchors’, pageView);
    });
    });
    });

    Complete code
    ——————–

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-34831893-1’]);
    _gaq.push([‘_trackPageview’]);

    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();
    $(function(){
    $(“a[href*=’http’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/outgoing/’ + $(this).attr(‘href’);
    _gat._getTrackerByName()._trackEvent(‘Outbound Links’, pageView);
    var _href = $(this).attr(‘href’);
    setTimeout(function() {
    location.href = _href;
    }, 100);
    ev.preventDefault();
    return false;
    });
    });
    $(“a[href*=’mailto’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/mailto/’ + $(this).attr(‘href’).substring(7);
    _gat._getTrackerByName()._trackEvent(‘Mailto’, pageView);
    });
    });
    $(“a[href*=’#’]”).each(function() {
    $(this).click(function (ev) {
    var pageView = ‘/anchor/’ + $(this).attr(‘href’).substring(1);
    _gat._getTrackerByName()._trackEvent(‘Anchors’, pageView);
    });
    });
    });

    Please suggest me..

  8. Hi Lars

    I just wanted to confirm that your ‘function’ declaration at the start of the script requires the leading ‘$’ symbol. I am not any means used to javascript but noticed that the function declaration in the script supplied by Google analytics does not include a leading ‘$’ symbol. I am not a programmer but I know enough that code has a grammar attached to it and also noticed that others have included the code with a leading ‘$’ symbol and then reported that there are problems so thought I would ask. Sorry if my question is a stupid one.

  9. I think I have been able to answer my own question but other people may find this useful as well. Your script requires jquery and so the following code should already have been executed. If it hasn’t simply add it to the code snippet above the google analytics code and the tracking will start almost immediately.

  10. I am very new to muse and coding. Please help!!
    When I place this (Google Analytics coding that you provided above) in the page/page property/metadata/HTML for : it shows up at the top of the website in preview. How or where do I need to place this code so it is properly placed and and not showing on live view?

  11. Hi,
    Does this only work with ga.js or does it also work with the new analytics.js?

    I’m not very technical and I’m struggling with this. Any help is much appreciated.

    Carl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s