With this simple snippet, all clicks on page-internal anchors, external links (http…) and mailto-links are tracked as events in Google Analytics:
$(function(){
$("a[href*='http']").each(function() {
$(this).click(function (ev) {
var pageView = '/outgoing/' + $(this).attr('href');
_gat._getTrackerByName()._trackEvent('Outbound Links', pageView);
var _href = $(this).attr('href');
setTimeout(function() {
location.href = _href;
}, 100);
ev.preventDefault();
return false;
});
});
$("a[href*='mailto']").each(function() {
$(this).click(function (ev) {
var pageView = '/mailto/' + $(this).attr('href').substring(7);
_gat._getTrackerByName()._trackEvent('Mailto', pageView);
});
});
$("a[href*='#']").each(function() {
$(this).click(function (ev) {
var pageView = '/anchor/' + $(this).attr('href').substring(1);
_gat._getTrackerByName()._trackEvent('Anchors', pageView);
});
});
});
Hint: If you do not have JQuery embedded already, also embed this before the script i provided above.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

This worked great for me. I just used the outbound link function. Thanks so much!
Hi Lars,
Looks very clean. Will this work for any webpage? Can you show a simple one page example with an external link?
thanks in advance,
Phil
Hi Lars,
Love what you have here but our email service provider bans email marketing that contains any script. Do you know of a way to easily track the “mailto:” we have built into an email? – Thanks. Christa