Skip to content

Instantly share code, notes, and snippets.

@shawn-crigger
Created October 19, 2014 18:49
Show Gist options
  • Save shawn-crigger/eb607b97ddeed37d2545 to your computer and use it in GitHub Desktop.
Save shawn-crigger/eb607b97ddeed37d2545 to your computer and use it in GitHub Desktop.
Very nicely used Google Analytics tracking code.
$("a").click(function() {
var href, href_lower;
href = ($(this).attr("href") === undefined ? "" : $(this).attr("href"));
href_lower = href.toLowerCase();
if (href_lower.substr(-3) === "pdf" || href_lower.substr(-3) === "xls" || href_lower.substr(-3) === "doc") {
_gaq.push(["_trackEvent", "document", "download", href_lower.substr(-3), $(this).text()]);
_gaq.push(["_trackPageview", href]);
}
if (href_lower.substr(0, 4).toLowerCase() === "http") {
_gaq.push(["_trackEvent", "external_link", "open", "href", $(this).text()]);
_gaq.push(["_trackPageview", href]);
}
if ($(this).attr("target") !== undefined && $(this).attr("target").toLowerCase() !== "_blank" && href_lower.substr(0, 10) !== "javascript") {
setTimeout((function() {
location.href = href;
}), 200);
return false;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment