Auto-tag Outbound Links for Google Analytics, Async Version


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



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $('body a').filter(function() {
  4. var theHref = this;
  5.  
  6. if (theHref.hostname && theHref.hostname !== location.hostname) {
  7. $(theHref).not(".noAutoLink").attr('target', '_blank').bind('click keypress', function(event) {
  8. var code = event.charCode || event.keyCode;
  9.  
  10. if (!code || (code && code == 13)) {
  11. var fixedLink = this.href;
  12. var _gaq = _gaq || [];
  13.  
  14. fixedLink = fixedLink.replace(/https?:\/\/(.*)/, "$1");
  15. fixedLink = '/outgoing/' + fixedLink;
  16.  
  17. _gaq.push(['_trackPageview', fixedLink]);
  18. };
  19. });
  20. };
  21. });
  22. });
  23. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.