Designer & Developer
I use Google Analytics for my own site and other sites I work on as well. But I never quite figured out how to get an overview with exit links in Google Analytics. Sure, there is the list of exit pages. But that doesn't tell you exactly where the user has gone to. I googled for a solution and found this wonderful piece of javascript from Danny Ng. I prefer working with the Prototype javascript framework and therefore I decided to rewrite it.
var ExitTracker = Class.create({ initialize: function() { this.domain = document.location.toString().toLowerCase().split("/")[2]; $$("a").each(function(item) { if (item.href && (item.href != (document.location + "#"))) { $(item).observe("click", this.track.bindAsEventListener(this)); } }.bind(this)); }, track: function(e) { var el = e.element(); var exit_domain = el.href.split("/")[2].toLowerCase(); if (this.domain.toLowerCase().indexOf(exit_domain) == -1) { if (typeof pageTracker != "undefined") { pageTracker._trackPageview("/exit/" + el.href); } } } });
Just save the code above as exittracker.js and insert the following right before the </body> tag and right after your Google Analytics code:
<!-- Google Analytics code here --> <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript" src="exittracker.js"></script> <script type="text/javascript"> new ExitTracker(); </script>
Don't forget to replace prototype.js in the snippet above with the location you saved prototype.js to. If you don't have prototype.js, you can get it here.
I am currently co-owner (and designer) at Superhero Cheesecake and used to work as Senior Designer Digital Media at MTV Networks. I live in Amsterdam, The Netherlands. I was born in 1983 and in 2007 I received my Bachelor of Engineering in Mediatechnology at the Hogeschool Utrecht, University of Applied Sciences. This website is part personal blog, part portfolio and part nonsense. If you would like to know more about me, drop me a line or add me on Facebook or LinkedIn.
JCH
January 29th, 2009 at 5:44 pm
Hi there,
If I understand this right then I should see the exit links tracked under the content tab by searching for content containing exit. If that’s right, then I’m not seeing anything though statcounter is showing clicks out from those pages. Can you suggest any common errors I may have made?
Jordi
January 31st, 2009 at 9:55 am
That’s strange, you should be able to see them under the Content Performance tab. They should all begin with /exit/ and then the URL of the exit link.
Send me a link to your website and I can take a look at it
JCH
February 3rd, 2009 at 11:31 am
Hi Jordi,
I tried to send via your contact form but there seems to be a problem with the send button?
The site is http://www.etenerife (dot) com and the code has been placed on the home page but is not recording any outgoing clicks.
Can you see what I have done wrong?
Jordi
February 3rd, 2009 at 12:19 pm
Oh I see what’s wrong now, you didn’t include Prototype! You need to do this first, otherwise it won’t work.
If you don’t want to include prototype then you can use the original version by Danny Ng: http://www.dannytalk.com/2008/08/30/tracking-exit-links-in-google-analytics/
BH
August 23rd, 2009 at 3:52 pm
Just a comment… It would be nice to show some sample code on including Prototype…. That way people coming here can get the whole picture.
Jordi
August 23rd, 2009 at 5:03 pm
@BH, you’re absolutely right. I updated the post and the example code, thanks!
Joe Price
February 11th, 2010 at 11:43 am
Hi,
I think I have implemented your code, however I am a bit confused as to where I can see the exit links being tracked. Can you please upload a screen grab of your analytics page?
Thanks!
Jordi
February 11th, 2010 at 11:51 am
Hi Joe, my Analytics is in Dutch so it probably won’t be very helpful. Anyway, they should appear under: Sources > Important exit pages (I’m not sure about the exact translations). All exit links should start with /exit/.
Hope that helps!
Joe Price
February 11th, 2010 at 2:44 pm
Thanks Jordi
I’ve found the links. A great script and I’ll find it very useful
Cheers
Joe
James
May 27th, 2010 at 8:37 pm
Hi Jordi,
Thanks for sharing! My one concern is that this will artificially inflate the number of pageviews for a site, as it is uses the pageTracker._trackPageview function.
Is this true? If so, I would recommend you add that users create an Advanced Segment (Page URL does not contain “/exit/”) to help filter out the data.
Thanks again for sharing this great bit of code!