I’ve been spending my time writing a script that will help track exit links (external links) in Google Analytics. This script will traverse through all anchor tags within the body and apply tracking codes on tags with the href attribute.
If the anchor tag has an onclick attribute already, it appends the tracking code to it and it shouldn’t affect the functionality.
I will try to post screenshots of how it looks in Google Analytics once I gather some data. To use it, simply paste the following code after the google analytics code and before the </body> tag.
<script type="text/javascript" src="<PATH-TO-SCRIPT>"></script> <script type="text/javascript"> dtalk_ga.applyVirtualExits(); </script>
You can view the source code here,
/** * @author: Danny Ng (https://www.dannyism.com) * @date: 30/08/08 * @updated: 26/09/08 * @notes: Free to use and distribute without altering this notice. Would appreciate a link back. * @usage: Place the following code before the </body> tag and after google analytics code, * * <script type="text/javascript" src="<PATH-TO-SCRIPT>"></script> * <script type="text/javascript"> * dtalk_ga.applyVirtualExits(); * </script> */ var dtalk_ga = { applyVirtualExits: function() { var trackPage = function(a) { var domain_name = document.location.toString().toLowerCase().split('/')[2]; var exit_domain = a.href.split('/')[2].toLowerCase(); if (domain_name.toLowerCase().indexOf(exit_domain) == -1) { if (typeof pageTracker != 'undefined') pageTracker._trackPageview('/exit/' + a.href); } }; var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { if (anchors[i].href && anchors[i].href != (document.location + '#')) { if (typeof anchors[i].onclick == 'undefined') anchors[i].onclick = function(e) { trackPage(this); }; else if (typeof anchors[i].onclick == 'function') { var old_onclick = anchors[i].onclick; anchors[i].onclick = function(e) { old_onclick(); trackPage(this); }; } } } } };
Hopefully I can find more ways of writing scripts that will help with Google Analytics.
Danny- great script. I had to make a small modification to get it to work.
Line 31– It looks like anchors[i].onclick is null in many cases and is therefore always defined as an object
Fixing this
if (typeof anchors[i].onclick == ‘undefined’)
to this
if (anchors[i].onclick == undefined || typeof anchors[i].onclick == ‘undefined’)
Seemed to do the trick. Thanks again for the great script!
Awesome that’s exactly the code I was looking for. I had been using Piwik analytics which already did this, but I’ve switched back to Google analytics and missed the functionality!
Actually, I set up a test script and I was (partially) wrong in the last post.
What had happened was, I was clicked the anchored link http://www.neptuneweb.com?id=1#. This then made document.location = http://www.neptuneweb.com?id=1#. Now the match doesn’t work and the callback gets attached to # links even though you are trying to exclude that.
e.g. now, anchors[i].href != (document.location + ‘#’) –> no longer applies, because document.location already includes the #. So after clicking the link, this logic breaks.
Neptune Support
Hi Danny,
There is a bug with this script.
If an a tag has href=# (anchor), then document.location will be something like http://www.neptuneweb.com?id=1#. Appending the ‘#’ to it will make document.location = http://www.neptuneweb.com?id=1##. You need something like the line below (top line is changed line):
if (anchors[i].href && anchors[i].href != (document.location + ‘#’))
Thanks for the script though!
Wicked Cool. Thank you. Your site just happened to be the first in my search for exit link tracking methods. Seems easy enough. I do, though, have a few sites using “google sites”. I guess my search is still on for a way to track those exit links. Thanks again.
Hello, I came across this post while searching for help with JavaScript. I’ve recently switched browsers from Chrome to Microsoft IE 6. After the change I seem to have a issue with loading JavaScript. Every time I go on a website that needs Javascript, my browser freezes and I get a “runtime error javascript.JSException: Unknown name”. I cannot seem to find out how to fix it. Any help is greatly appreciated! Thanks
Excuse my ignorance,Danny but I’m not sure what I need to do to implement this. You have said just cut and paste this small snippet of code:
1. <script type=”text/javascript” src=””>
2.
3. dtalk_ga.applyVirtualExits();
4.
But under that, I find a long version. I can’t see how using that small snippet would work. How does GA know where to put the exit links? So do I need to use the long version?
Yes you will need to import the long version either via a file or embedded in the source code.
Once imported, you can call dtalk_ga.applyVirtualExits() AFTER the import code.
Also, I thought you might be interested in the following solution: http://wordpress.org/extend/plugins/google-analytics-for-wordpress/
It can track clicks in text, in comments, on authors’ links, as well as track downloads.
Would be interesting to hear your opinion.
How accurate is this way of tracking exit links? A couple of days ago I have posted a link on my blog using CCounter script, and them implemented this script on the same day.
CCounter has registered 116 clicks, Google Analytics only 5 :(
Hi Karolis,
I’m on holidays at the moment but I will look into it once I get time and when I’m back from holidays.
Just a note, no two web analytics software records data the same way. So sometimes it’s not unusual to find differences in data say between Google Analytics and Omniture.