Tracking outgoing clicks with Google Analytics

I wanted my first post as a .eduGuru to be something spectacular, unfortunately all the ideas I had were taking too long to write, so here is something short and sweet.

Building on my last postTracking Flash Interaction with Google Analytics, I showed how to track flash actions with Google Analytics. The pageTracker._trackPageview() function is key to this tracking, but it can also be expanded past flash and into the uncharted territory of “outgoing links.”

In the analytics world outgoing links are the black hole of tracking, people are on your site, they click, and are gone forever. After stumbling on the article Google Analytics Tip by the Texas A&M Webmasters I decided to expand on it.

Instead of having the javascript called inline for every link, with one simple jquery (or your favorite javascript library) call it can be added to all the anchor tags with an external reference. Right now the example below is limited to just “https://” for proof of concept.

Here is how it works

1. Load the newest version of Google Analytics on your page. (this include must be loaded before any clicks can be tracked)

<script type="text/javascript"></script>
<script type="text/javascript"></script>

2. From there include jquery

<script src="https://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"
type="text/javascript"></script>

3. Run this script on document load to attach the “click” to all the external links

<script type="text/javascript">
  $(document).ready( function() {
    $('a[@href^="https://"]').click( function() {
        pageTracker._trackPageview('/outgoing/' + $(this).attr('href'));
        return true;
    });
  });
</script>

Voilà! Check out your Google Analytics and search for “/outgoing” in your Top Content area to see the most popular outgoing links.

Later Expansion

To expand on this example for production use, it is wise to also include “https://”, “ftp://” and exclude any “https://[yourdomain.edu]/” from tracking since those will go to internal pages even though they have a fully qualified URL in the attribute.

Warning

These new “/outgoing” links WILL show up as “visits” in your site on Google Analytics potentially skewing visitor hits. I recommend setting up an addition Profile for “Outgoing Tracking” and include only traffic from “^/outgoing/”. In addition exclude “^/outgoing/” from your main profile.

20 Responses to “Tracking outgoing clicks with Google Analytics”

  1. Says:

    Also check out the Google AJAX API for common javascript APIs (such as jQuery) at https://code.google.com/apis/ajaxlibs/. It uses Google’s Content Delivery Network (CDN) to serve up the javascript file instead of using your local network bandwidth.

  2. Says:

    Genius! Quit simple, but incredibly effective. Of course you have to do some extra work in GA to get your filters and profiles setup properly, but talk about a whole new wave of premium intelligence.

    I’d recommend setting up a profile that filters out the “outgoing” subdirectory.

  3. Says:

    Good post. We did something almost identical this year at missouri.edu (https://is.gd/ap0W) and have been very pleased with the results. Founderis correct in that you have to set up filters to really get a better sense of the data.

    Setting up a second profile to record this data is a great idea that I hadn’t considered.

  4. Says:

    @Ben Good point, we have just begun working with the new AJAX API, its super nice. Will eventually do a followup post on it.

    @FounderSimple and effective for sure! I just realized how many external clicks we were getting. Now I know where all those “bounces” are going.

    @Gilzow Nice work man! That is some good work. You find anything interesting with the results?

  5. Says:

    Excellent tip, I can see a lot of uses for this.

    See what type of content users are interested in.

    See if any ill placed external link could be decreasing your conversion rate in the conversion funnel.

    probably more.

  6. Says:

    Nice post Nick. Also have a look at the following: https://www.advanced-web-metrics.com/blog/2008/06/08/updated-tracking-script-for-gajs/

    (originally from Sep 2007!)

    Info there for both urchin.js and ga.js users

    I would be interested in your comments.

    Best regards, Brian Clifton

  7. Says:

    @Brian Nice article! I am surprised I did not stumble on it before. I like your approach, it is definitely more robust than my proof of concept.

    I like that you supplied some analytics with your post. We just started doing this and are just acquiring analytics.

    With your results have you made any changes to the way you post or how you link to external sites?

  8. Says:

    This really is a handy metric that people need to measure. I have a client that has a site which is basically just an online directory, so it’s nothing but outgoing links.

    This will be the only real analytics of interest - seeing where we’re sending them. Can’t wait to get it up and running.

  9. Says:

    @Associate Directorthe most interesting thing we have seen so far is that students use our main page frequently to get to the student webmail portal instead of just typing webmail.mizzou.edu in their browser.

    @Brian Clifton you have XSS vulnerabilities at your website ;-)

  10. Says:

    @Gilzow exactly the same thing we saw when we put it on our front page.

  11. Says:

    Hi, tks for this great tip! Would you be able to provide some guidance on how to set up new profiles and filtering on Analytics to make sure the right traffic is tracked? I’ve no idea how to do that. TKs!

  12. Says:

    Thanks for the tutorial. Exactly what I was looking for. I appreciate your time for the post!

  13. Says:

    uncaught exception: Syntax error, unrecognized expression: [@href^="https://"] I got this guy. It works if you remove the @ as of jquery 1.3.2

  14. Says:

    That is great information. Im glad that I can relate to this sorta stuff. I know if you have any cell phone tracking software problems you can just use this Cell Phone Tracker

  15. Says:

    To track clicks on outbound links can also be used _trackEvent. It is much more comfortable and functional ( i posted about it here https://chenado.net/en/1029.html )

  16. Says:

    If you want to make sure that the script filters out your domain from the selection, modify the selector to:

    $(“a[href*='https://']:not([href*='"+window.location.hostname+"'])”)

    source: https://www.thewhyandthehow.com/tracking-outbound-links-with-jquery-and-analytics/

    • Says:

      Where are my manners? Great article, by the way. Glad I read through it.

  17. Says:

    Brad,
    Thanks for the additional code! Glad you enjoyed the article :-)

Trackbacks/Pingbacks

  1. Trending Upward | Poorly Performing Pages - How Do We Know? --> says:

    [...] actual exit links (as opposed to someone closing out of your site via the browser), check out the great post by Associate Director on .eduGuru about tracking exit links in Google [...]

  2. [...] Tracking Outgoing Clicks with Google Analytics [...]