Last active
February 17, 2019 08:15
-
-
Save dustinrecko/7f8e331c9e2dd960df58a67009327eba to your computer and use it in GitHub Desktop.
Deduping Google Analytics Transaction Hits - customTask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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