Return to Snippet

Revision: 29024
at July 21, 2010 07:04 by rgsmith007


Updated Code
<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>

Revision: 29023
at July 20, 2010 01:57 by rgsmith007


Initial Code
<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>

Initial URL


Initial Description
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.

Initial Title
Auto-tag Outbound Links for Google Analytics

Initial Tags
link, google

Initial Language
jQuery