/ Published in: jQuery
This will automatically tag external links and prefix the URL with '/outgoing' for tracking in Google Analytics. The pageTracker is assumed to be added to the page elsewhere in the markup. By default it is applied to all links contained in the BODY tag, excluding those with the class 'noAutoLink.' This snippet is not an original work of mine, but I do not know its author.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script type="text/javascript"> $(document).ready(function() { $('body a').filter(function() { var theHref = this; if (theHref.hostname && theHref.hostname !== location.hostname) { $(theHref).not(".noAutoLink").attr('target', '_blank').bind('click keypress', function(event) { var code = event.charCode || event.keyCode; if (!code || (code && code == 13)) { if (pageTracker) { var fixedLink = this.href; fixedLink = fixedLink.replace(/https?:\/\/(.*)/, "$1"); fixedLink = '/outgoing/' + fixedLink; pageTracker._trackPageview(fixedLink); } }; }); }; }); }); </script>