Click here to Skip to main content
15,888,157 members
Articles / Programming Languages / Javascript

Link Tracking using jQuery and the Google Analytics Asynchronous Tracking Code

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Oct 2011CPOL2 min read 9.9K   7  
Here is some jQuery you can add to your pages that will allow you to track when people click on external links.

The Google Analytics tracking code is triggered when pages that include the code are called. But what about when users request PDFs and other documents that aren't web pages or click on links to external web pages on other websites?  How do we track these events also?

Here is some jQuery you can add to your pages that will allow you to track when people click on these links. You will have to add the Google Analytics Asynchronous Tracking code (found here: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html) and add jQuery to your page (see here: http://docs.jquery.com/How_jQuery_Works) to use this code:

275824/Untitleddrawing.png

JavaScript
$(document).ready(function(){

 $('a').click(function(){

  href = ($(this).attr('href') == undefined) ? ('') : ($(this).attr('href'));
  href_lower = href.toLowerCase();
  
  if(href_lower.substr(-3) == "pdf" || href_lower.substr(-3) == 
     "xls" || href_lower.substr(-3) == "doc") {
   _gaq.push(['_trackEvent', 'document', 'download', 
             href_lower.substr(-3), $(this).text()]);
   _gaq.push(['_trackPageview', href]);
  }
 
  if(href_lower.substr(0, 4).toLowerCase() == "http") {
   _gaq.push(['_trackEvent', 'external_link', 'open', 'href', $(this).text()]);
   _gaq.push(['_trackPageview', href]);
  }
  
  if ($(this).attr('target') != undefined && $(this).attr('target').toLowerCase() != 
     '_blank' && href_lower.substr(0,10) != "javascript") {
   setTimeout(function() { location.href = href; }, 200);
   return false;
  }

 });

});

So, the example above will track clicks on internal links opening files with the extensions pdf, xls, and doc and will categorize them in your Google Analytics event tracking reports as "documents". It will also track outgoing links (links with URLs beginning with "http") and categorize them as "external_links".

If you need more categories, add more if statements. In the method above, I create one click event that is attached to every link on the page, rather than just attaching specific events to the links that require them. If a link satisfies more than one criteria (e.g. it's a link both to a PDF, and it's on an external website) then both events will be created. In the end, I thought this approach was probably more customizable and efficient overall, especially if there are several event categories.

The setTimeout bit is to allow links that replace the page content (i.e. regular links) a moment before calling the page; so that the GA function has time to execute before opening the page. Be careful when using plugins like Fancybox; add links to such plugins to the exception list in the condition wrapped around the setTimeout function as these functions remove the href attribute and the combination of that and the setTimeout can cause unpredictable behaviour.

You may want to track events for clicks made by specific links like the nav or featured content. You can achieve this using a similar method:

JavaScript
$("#main_menu a").mouseup(function(){
if($(this).attr('href').toLowerCase() != "javascript:;" && $(this).attr('href') != "#") {
_gaq.push(['_trackEvent', 'main_menu', 'open', $(this).attr('href')]);
}
});

Change the ID in bold to whatever you call the container that your menu is kept in. I added the check for JavaScript:; and # just in case there are links that don't open pages (e.g., links that reveal further options).

Leave comments if you have other suggestions for mixing jQuery and GA for better tracking.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder QR Loyalty Cards
Canada Canada
Founder of QR Loyalty Cards, Father, Husband and Space Cowboy.

https://qrloyalty.cards

Comments and Discussions

 
-- There are no messages in this forum --