Skip to content

Instantly share code, notes, and snippets.

@krijnhoetmer
Created April 14, 2016 09:17
Show Gist options
  • Save krijnhoetmer/1833138b9f1bd23effd9a80f97ce585c to your computer and use it in GitHub Desktop.
Save krijnhoetmer/1833138b9f1bd23effd9a80f97ce585c to your computer and use it in GitHub Desktop.
Thanks to this “nifty” script, async GA tracking on amsterdam.nl links makes sure you have to click certain links twice if you block trackers and stuff. Yay.
// Thanks to this “nifty” script, async GA tracking on amsterdam.nl
// links makes sure you have to click certain links twice if you
// block trackers and stuff. Yay.
$.fn.analyticsPushEventClick = function(obj, href, category, async) {
$(this).on("click", function(event) {
return analyticsPushEvent(event, obj, href, category, async);
});
};
analyticsPushEvent = function(event, obj, href, category, async) {
var clicked = obj;
if (clicked.hasClass("guaPrevent")) {
clicked.removeClass("guaPrevent");
return true;
} else {
if (async) {
event.stopImmediatePropagation();
clicked.addClass("guaPrevent");
}
for (var c = 0; c < analytics_ids.length && analytics_enabled; c++) {
var analytics_id = analytics_ids[c];
var send = analytics_id == "" ? "send" : (analytics_id + ".send");
var last = (c == analytics_ids.length - 1);
if (last && async) {
ga(send, 'event', {
'eventCategory': category,
'eventAction': "click",
'eventLabel': href,
'hitCallback': function() {
clicked[0].click();
},
'hitCallbackFail': function() {
clicked[0].click();
}
});
} else {
ga(send, 'event', {
'eventCategory': category,
'eventAction': "click",
'eventLabel': href
});
}
}
return !async;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment