document.onclick = function(event) {
 
    event = event || window.event;
    var target = event.target || event.srcElement,
        targetElement = target.tagName.toLowerCase();
 
    if (targetElement == "a") {
        var href = target.getAttribute("href"),
            urlHost = document.domain.replace(/^www\./i,""),
            urlPattern = "^(?:https?:)?\/\/(?:(?:www)\.)?" + urlHost + "\/?";
          	var base = window.location.href.substring(0, window.location.href.lastIndexOf("/") + 1);
			href = href.replace(base, "");
        if (href) {
            eventCheck(href,urlPattern);
        }    
    }
 
    function eventCheck(href,urlPattern){
        if ((href.match(/^https?\:/i)) && (!href.match(urlPattern))){
            if (href.match(/^.*\.(pdf|jpg|png|gif|zip|mp3|txt|doc|rar|js|py)$/i)) {
                //_gaq.push(['_trackEvent', 'Download', 'click', href]);
                _gaq.push(['_trackPageview', href]);
            } else {
                _gaq.push(['_trackEvent', 'External', '', href]);
            }
        } else if (href.match(/^mailto\:/i)) {
            _gaq.push(['_trackEvent', 'Email', '', href.substr(7)]);
        } else if (href.match(/^.*\.(pdf|jpg|png|gif|zip|mp3|txt|doc|rar|js|py)$/i)) {
        	_gaq.push(['_trackPageview', href]);
            //_gaq.push(['_trackEvent', 'Download', 'click', href]);
        }
    }
};
