var a = document.getElementsByTagName('a'); for(i = 0; i < a.length; i++){ if (a[i].href.indexOf(location.host) == -1 && a[i].href.match(/^http:\/\//i)){ a[i].onclick = function(){ _gaq.push(['_trackEvent', 'outgoing_links', this.href.replace(/^http:\/\//i, '')]); } } }