Automatic outbound link analytics with jQuery

Technology

I had the challenge of adding Google Analytics tracking code to all the outbound links on a site I’ve been working on. There are hundreds of these links scattered around the site, so rather than try and edit a bunch of links, manually adding onclick handlers in an error-prone fashion, I decided to get lazy and write some code to handle it for me.

First I was thinking about doing some sort of regular expression search and replace throughout the site and database, but that reminded me of CSS3 selectors and their ability to do simple pattern matching. I’ve seen people apply a special style to outbound links this way, so after a few minutes of monkeying around with things, I now have a chunk of jQuery that will automatically track clicks on all outbound links.

Here it is, in a nutshell:


jQuery(function($){

   // Match all anchor tags in the "maincontent" div with
   // urls that begin with "http" but don't contain the
   // string "yourwebsite.com"
   $('#maincontent a[href^="http"]').not('a[href*="yourwebsite.com"]').click(function(){

     try {

     // Get the href url and toss out the "http://"
     var href = $(this).attr('href');
     if ( href.indexOf("://") > 0 ) {

       // Track the page in Google Analytics as
       // "/tracking/outbound/www.somesite.com/foo"
       var outbound = '/tracking/outbound/' + href.split("://",2)[1];
       pageTracker._trackPageview(outbound);

       }
     } catch( e ) {}
   }
}

With this running, all of my internal pages get tracked as usual, and any external links will appear as pageviews that look like “/tracking/outbound/www.somesite.com/foo”.

If you link out to many different pages on several sites, keeping the full site url in the tracking code and building these deep paths is particularly useful. Google Analytics will allow you to drill down into the tree like it was normal content and quickly pull numbers on how many total outbound clicks you received (/tracking/outbound), how many went to www.somesite.com (/tracking/outbound/www.somesite.com), and how many people clicked out to a particular page on the site.

This saved me quite a bit of time and is immensely more flexible than any other outbound tracking method I’ve used. I hope this helps someone else. Drop me a line in the comments if this works out for you.

Update: it looks like I wasn’t the first to do this. An article by Rebecca Murphey shows how to do something similar, while also adding the referring post title to the tracking code. Pretty cool stuff, I must say.

0 thoughts on “Automatic outbound link analytics with jQuery

  1. Dale says:

    This is not a complete replacement, it only outlines changing the activated carbon. The instructable also doesn’t outline what mesh/nominal pore size the original carbon media is so if you use a carbon media with a larger size, it can result in more turbidity (cloudier water)depending on you water’s turbidity.

    There are other additives like ion exchange resins in a Brita filter that remove hardness as well as metals. That are also no included in this Instructable.

    FYI activate carbon is really a polisher, it works on absorbtion, and it is a very good breeding ground for bacteria.

  2. Beth Terry says:

    For those not inclined to opening up the filter and figuring out how to refill it (which is not the best solution in the first place) we are asking Clorox to create a take-back recycling program so the used plastic cartridges don’t end up in the landfill or incinerator.

    Please visit http://www.takebackthefilter.org to sign the petition and send us your used filters!

    The original BRITA company in Europe has already created a recycling program for used cartridges, but here in North America, Brita is owned by Clorox, which has not provided a way to recycle them.

  3. brian says:

    you can theoretically “re-exchange” the minerals caught in the ion exchange polymer by washing with salt water. This allows the sodium to replace those minerals which previously replaced the sodium ions

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK