Skip to content

Instantly share code, notes, and snippets.

@dustinrecko
Last active February 17, 2019 08:15
Show Gist options
  • Save dustinrecko/7f8e331c9e2dd960df58a67009327eba to your computer and use it in GitHub Desktop.
Save dustinrecko/7f8e331c9e2dd960df58a67009327eba to your computer and use it in GitHub Desktop.
Deduping Google Analytics Transaction Hits - customTask
function () {
return function (model) {
var proxyDomain = "https://<yourdomain.com>/collect";
var proxySendHitTask = function () {
function pixel(hitPayload) {
var result = false;
try {
document.createElement("img").src = proxyDomain + "?" + hitPayload, result = true
} catch (e) {}
return result
}
return {
send: function (hitPayload) {
var result;
if (!(result = 2036 >= hitPayload.length && pixel(hitPayload))) {
result = false;
try {
result = navigator.sendBeacon && navigator.sendBeacon(proxyDomain,hitPayload)
} catch (e) {}
}
if (!result) {
var xhr;
try {
window.XMLHttpRequest && "withCredentials" in (xhr = new XMLHttpRequest) && (xhr.open("GET", proxyDomain, true), xhr.setRequestHeader("Content-Type", "text/plain"), xhr.send(hitPayload), result = true)
} catch (e) {}
}
return result || pixel(hitPayload)
}
}
}();
tracker.set("sendHitTask", function (model) {
proxySendHitTask.send(model.get("hitPayload"));
}, true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment