Track out-clicks with Google Analytics

One major feature that seems to be missing from Google Analytics is the ability to track out-clicks or clicks on external links.

With the Lucid Directory project, we wanted to be able to see which listings are being clicked on so we can provide statistics for this later down the track.

We have come up with the following solution to track all external clicks on the site while still linking directly to the sites so they get a link indexed in Google.

<script language="Javascript">
function lucid_clicks()
{
var SiteLinks = document.links;

for(var i=0;i < SiteLinks.length;i++)
{
var thehref = SiteLinks[i].href.toString();

if(thehref.indexOf("www.yourdomain.co.nz") == -1)
{
SiteLinks[i].setAttribute("onclick","javascript:urchinTracker('/Clicks/' + this.href)");
}
}
}
</script>

You can replace www.yourdomain.co.nz with your domain and /Clicks/etc with anything you wish to show in Google Analytics.

To use, simple call lucid_clicks() in your <body> tag as follows:

<body onload="lucid_clicks()">

Let us know if you have any suggestions on how this can be improved.

5 Responses to “Track out-clicks with Google Analytics”

  1. Aug 23
    05:53 AM
    Anthony

    If I already have an onload event, how to i make this work? <body> will that work?

  2. Aug 25
    04:34 PM

    Hi Anthony, you could try either:

    <body onload="lucid_clicks(); your_method();">
    Or you could create a new Javascript function that calls your method and the lucid_clicks() method and call that in your onload. Hope that makes sense. Let me know if it works.

  3. Mar 07
    04:36 AM

    Galen, this looks like it is going to be a very powerful script for several sites I maintain. Once I have installed it on the site, where would I look in Analytics to find the data? My analytics usage up to this point has been more basic tasks of finding weak landing pages or flaws in the checkout and tinkering with them. I haven't used custom scripts before. Any help would be much appreciated.

  4. Mar 08
    08:39 PM

    Hi Brian, you can access the stats for this in the content section of Analytics. You will look for "content" beginning with the path you specified in this little script ("/clicks/" in the case of my example). Hope that helps.

  5. May 07
    07:02 PM

    Thank you so much for this Brian. It's a very useful piece of script. I'm trying to make it work for my at the moment.

Comments