Track outbound links for Google Analytics automatically

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(‘‘) == -1 && !ahrefs[i].onclick) {
ahrefs[i].onclick = function () { var track = this.href + ”; urchinTracker (‘/outgoing/’+track.substring(7)); }

Simply replace with your own site URL and hey presto – all outbound links that aren’t internal are being tracked by Google Analytics.

29 Responses

  1. Dan

    Will it give me stats of of how many hits a page I have got on another site that came from my site?

  2. 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.

  3. 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.

  4. Jamie Huskisson

    MR – So long as there’s no links below the code, then this will work just fine.

  5. Curious

    How do I do I use this code so I can make the outbound link a “goal” within Google Analytics?

  6. Ross

    Thanks for the plugin! One question: where will the results appear in the report ? Under Top Content ?

  7. 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?

  8. 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).



  9. 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

  10. 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

  11. 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.

  12. Jamie Huskisson

    Seems to be displaying just fine for me here Chris, albeit without any indenting?

  13. 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.

  14. 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.

  15. 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(‘’) == -1 && !ahrefs[i].onclick) {
    ahrefs[i].onclick = function () {

  16. Ryota

    I paste the ga.js code right before the closing head tag.

  17. 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

  18. 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 + “’ type=’text/javascript’%3E%3C/script%3E”));

    var pageTracker = _gat._getTracker(“########”);

    I’m trying to track several hyper links that go to external sites. How would I go about that?

  19. Henrietta


    Do you mean that it will tack all the links which you send to other websites?


  20. 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.


  21. Man

    Works in current analytics version!

  22. jimb

    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?

  23. Genesis

    Thanks for this code> i will try it right away.

  24. 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!

  25. Abdul hakeem

    Thanks for sharing it, I have been looking for the same technique for a long time.

  26. Giles

    Or, if you’re using jQuery and want a more controllable solution, mark the links as rel=”external” and:

    var domain = window.location.protocol + “://” +;
    var url = “/outbound/” + $(this).attr(“href”).replace(domain, ”).replace(“http://www.”, “”).replace(“http://”);
    _gaq.push([‘_trackPageview’, url]);

    This is using the asynchronous tracking code.

  27. Tommy

    Thanks for the code, i added it yesterday to my new website.Thanks for this, it works no probs

  28. 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.?

  29. Nima Heydarian

    Thanks for the post Jamie. Where would the result show up in Analytics?

Comments are closed, but feel free to @jhuskisson on Twitter with your thoughts.