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"),
            target = target.getAttribute("target"),
            urlHost = document.domain.replace(/^www\./i, ""),
            urlPattern = "^(?:https?:)?\/\/(?:(?:www)\.)?" + urlHost + "\/?";
        if (href) {
            eventCheck(href, urlPattern, target);
        }
    }

    function eventCheck(href, urlPattern, target) {
        if ((href.match(/^https?\:/i)) && (!href.match(urlPattern))) {
            if (href.match(/^.*\.(pdf|zip|mp3|txt|doc|rar|js|xlsx|docx|ppt|pptx|keynote)$/i)) {
                _gaq.push(['_trackEvent', 'Download', 'click', href]);
                if (target != "_blank") {
	                setTimeout(function() { location.href = href; }, 200);
	            }
                return false;
            } else {
                _gaq.push(['_trackEvent', 'External', 'click', href]);
                if (target != "_blank") {
                	setTimeout(function() { location.href = href; }, 200);
                }
                return false;
            }
        } else if (href.match(/^mailto\:/i)) {
            _gaq.push(['_trackEvent', 'Email', 'click', href.substr(7)]);
        } else if (href.match(/^.*\.(pdf|zip|mp3|txt|doc|rar|js|xlsx|docx|ppt|pptx|keynote)$/i)) {
            _gaq.push(['_trackEvent', 'Download', 'click', href]);
            if (target != "_blank") {
                setTimeout(function() { location.href = href; }, 200);
            }
            return false;

        }
    }
};
