How to track user actions and custom events with Google Analytics and jQuery
This is a customization of Rebecca Murphey’s script:
1 $('a').each(function() { 2 var $a = $(this); 3 var href = $a.attr('href'); 4 5 if(typeof pageTracker == 'undefined') { return; } 6 7 // Link is external 8 if (href.match(/^http/) && !href.match(document.domain)) { 9 $a.click(function() { 10 pageTracker._trackPageview('/external/' + href); 11 }); 12 } else { 13 $a.click(function() { 14 pageTracker._trackPageview('/internal' + href); 15 }); 16 } 17 });
Note that clicks are shown as page views in reports, so you should exclude them from all reports. A future version of Google Analytics will allow you to track events, such as mouse clicks, without affecting page view reporting, see this page on the new event tracking beta feature for more information.
How to exclude your own traffic from Google Analytics reports and other JavaScript based analytics software
Option 1: Changing your browser’s user agent
Open the about:config page in Firefox by typing about:config in the address bar and pressing enter. Now change the general.useragent.extra.firefox setting to an easily identifiable string, for example the following:
1 Firefox/3.0 disable-tracking
Then in your code check that the user-agent string doesn’t contain disable-tracking>
1 <% if !request.user_agent.include?('disable-tracking') %> 2 TRACKING CODE GOES HERE 3 <% end %>
Option 2:
Use one of Google Analytics native ways of excluding traffic from certain domains, IPs, user-agents or users having a specific browser cookie.