Place this code before the </body> tag and it’ll track all outbound links that aren’t part of your website.
<script type=”text/javascript”>
if (document.getElementsByTagName) {
var ahrefs = document.getElementsByTagName(‘a’);
for (var i=0; i<ahrefs.length;i++) {
if (ahrefs[i].href.indexOf(‘http://jhuskisson.com‘) == -1 && !ahrefs[i].onclick) {
ahrefs[i].onclick = function () { var track = this.href + ”; urchinTracker (‘/outgoing/’+track.substring(7)); }
}
}
}
</script>
Simply replace http://jhuskisson.com with your own site URL and hey presto – all outbound links that aren’t internal are being tracked by Google Analytics.
Dan —
Will it give me stats of of how many hits a page I have got on another site that came from my site?
Jamie Huskisson —
Yes Dan it will. This will allow Google Analytics to track how many times a link that points to outside of your own has been clicked.
MR —
Will this work if the links on the site change. e.g if someone performs a search on my site the results will be different each time but i want to track which of these search result links have been followed.
Jamie Huskisson —
MR – So long as there’s no links below the code, then this will work just fine.
Curious —
How do I do I use this code so I can make the outbound link a “goal” within Google Analytics?
Ross —
Thanks for the plugin! One question: where will the results appear in the report ? Under Top Content ?
LKamoku —
i entered this info in my footer.php and part of the code turns blue…(using dreamweaver) what does that mean:
turns blue from ‘for var …all the way to…
i have the code there now and when testing my outbound links i dont see any stats (looking in the content drilldown section)
am i doing something wrong?
Konrad —
Thanks for this, it works no probs. I can see the clicks in the site overlay but is there any more straight forward way to see the clicks in Analytics (preferably in list form).
Thanks,
Konrad
Yunus —
This is great and simple so thanks! When I add the code to my site however it doesn’t seem to add the onclick funcion to my links. I’d be grateful for any suggestions as to why this might not be working. Pls see this link as an example
Chris —
if (document.getElementsByTagName)
{
var ahrefs = document.getElementsByTagName('a');
for (var i=0; i
This will work , there were a few funny characters within the script for some reason, when you put this in dreamweaver it WONT show blue anymore.
Great code though , thanks for posting it
Chris —
Ok, I cant seem to enclose this in javascript tags , so please put these around the document, if someone can edit this as it wont like the code in comments and make this shown then that would be great.
Jamie Huskisson —
Seems to be displaying just fine for me here Chris, albeit without any indenting?
Chris —
Hi Jamie,
Ok thanks for letting me know,
I just had to change some of the charaters within the script and it showed the code correctly in dreamweaver, I am still waiting to see if the results clarify.
Brad —
A couple of questions about implementing this. Since analytics takes hours to update it takes forever to try to figure this out.
1- does this code work with both old and new style analytics code?
2- should this code go above or below the regular analytics code?
3- with the outbound link tracking code that google gives (where you have to tag each link individually), you have to put the main analytics code just after the opening (not closing ) since it has to execute first before the link tracking code. With your code you don’t have to do that?
Thanks. If I can get this to work it will save a ton of time.
Ryota —
Brad – This is what I’ve done to have it work with ga.js. It should still go below ga.js. Google suggests that the new ga.js should go above any calls to its methods, so usually paste the ga.js code right before .
if (document.getElementsByTagName) {
var ahrefs = document.getElementsByTagName(‘a’);
for (var i=0; i<ahrefs.length;i++) {
if (ahrefs[i].href.indexOf(‘http://www.domain.com’) == -1 && !ahrefs[i].onclick) {
ahrefs[i].onclick = function () {
pageTracker._trackPageview(‘/outbound/’+this.href.substring(7));}
}
}
}
Ryota —
I paste the ga.js code right before the closing head tag.
Brad W. —
I have the ga,js version installed in my footer under the Google analytics ga.js code. The anlystics code works on my php site however I am not seeking any reporting of outbound clicks in analytics? Can any one offer advice ? (I do get a 404 error on site overlay)
Thanks Brad
brandon —
do you put your code before or after the code
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
var pageTracker = _gat._getTracker(“########”);
pageTracker._trackPageview();
I’m trying to track several hyper links that go to external sites. How would I go about that?
Henrietta —
Hi,
Do you mean that it will tack all the links which you send to other websites?
Thanks
Henrietta
Gavin Doolan —
This is great thanks for writing this I was going to consider writing one of my own scripts like this, however you have already done it.
For all those wondering if you go to Content -> Top Content report you should be able to filter by /outgoing/ to see all your outbound pageviews.
Please note that this script will inflate your pageviews, so you may consider creating another profile and using a filter in Analytics to exclude pageviews in /outgoing/ if accurate pageviews is extremely important.
Cheers,
Gavin.
Man —
Works in current analytics version!
jimb —
Jamie
Thanks for the code, i added it yesterday to my new website.
How do I find the results though? I have tried ‘Content’>’Top Content’>Filter with ‘Outgoing’ but still nothing.
Can you help?
best,
Jim
Genesis —
Thanks for this code> i will try it right away.
Cee —
is this technique still working? I wanna try this stuff because we are having a hard time tracking those banner ad clicks from our pages. One question though, if we have more than a thousand webpages with banner ads (which I believe is considered outgoing links), will i use that code for each of those webpage? =D thanks!
Abdul hakeem —
Thanks for sharing it, I have been looking for the same technique for a long time.
Giles —
Or, if you’re using jQuery and want a more controllable solution, mark the links as rel=”external” and:
$(function(){
$(“a[rel=external]”).click(function(){
var domain = window.location.protocol + “://” + window.location.host;
var url = “/outbound/” + $(this).attr(“href”).replace(domain, ”).replace(“http://www.”, “”).replace(“http://”);
_gaq.push([‘_trackPageview’, url]);
});
});
This is using the asynchronous tracking code.
Tommy —
Thanks for the code, i added it yesterday to my new website.Thanks for this, it works no probs
Jay —
Kindly please let me know on what section can I see the report of outbound links that are tracked using this script on Google A.?
Nima Heydarian —
Thanks for the post Jamie. Where would the result show up in Analytics?