Track outbound links with GA and jQuery


/ Published in: jQuery
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // Event Tracking for external Links
  2. //Add expression for external and mailto links
  3. $.expr[':'].external = function(obj){
  4. return !obj.href.match(/^mailto\:/)
  5. && (obj.hostname != location.hostname);
  6. };
  7.  
  8. function track_outbound_links(){
  9. // bind click function to all external links
  10. $('a:external').addClass('external').click(function(){
  11. var link = $(this).attr('href');
  12. _gaq.push(['_trackEvent', 'External Link', link]);
  13. window.location.href = link;
  14. return false;
  15. });
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.