Auto-tag Outbound Links for Google Analytics, Async Version, Event Variant


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

This uses the Event Tracking system rather than a '/outbound'


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.  
  16. _gaq.push(['_trackEvent', 'Outbound', 'Click', fixedLink]);
  17. };
  18. });
  19. };
  20. });
  21. });
  22. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.