Disable Azure Application Insights if a Do Not Track request is sent

This commit is contained in:
Thomas A. Christensen II 2020-02-20 17:51:08 -08:00
parent 269d6a8521
commit 52a6f60cea
Signed by: millironx
GPG key ID: 139C07724802BC5D

View file

@ -25,15 +25,43 @@
<!-- Azure Application Insights -->
<script type='text/javascript'>
// Enables app insights IF AND ONLY IF user does not send a DNT request
// Shamelessly stolen from https://dev.to/spekulatius1984/how-to-check-do-not-track-dnt-in-javascript-57g1
let dntActive = () => {
// get the value from the various browser implementations.
let dnt_active = parseInt(
// Internet Explorer 9 and 10 vendor prefix
navigator.msDoNotTrack ||
// IE 11 uses window.doNotTrack
window.doNotTrack ||
// W3C
navigator.doNotTrack,
10
);
// If this comes to exactly 1 DNT is set.
return (dnt_active === 1);
}
// Check if DNT is active.
if (dntActive()) {
// active
console.log("Azure Application Insights has been disabled due to user's Do Not Track request")
} else {
// inactive
console.log("Azure Application Insights is active")
var appInsights = window.appInsights || function (config) {
function r(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } }
var t = { config: config }, u = document, e = window, o = 'script', s = u.createElement(o), i, f; for (s.src = config.url || '//az416426.vo.msecnd.net/scripts/a/ai.0.js', u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ['Event', 'Exception', 'Metric', 'PageView', 'Trace', 'Ajax']; i.length;)r('track' + i.pop()); return r('setAuthenticatedUserContext'), r('clearAuthenticatedUserContext'), config.disableExceptionTracking || (i = 'onerror', r('_' + i), f = e[i], e[i] = function (config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o), s }), t
function r(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } }
var t = { config: config }, u = document, e = window, o = 'script', s = u.createElement(o), i, f; for (s.src = config.url || '//az416426.vo.msecnd.net/scripts/a/ai.0.js', u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ['Event', 'Exception', 'Metric', 'PageView', 'Trace', 'Ajax']; i.length;)r('track' + i.pop()); return r('setAuthenticatedUserContext'), r('clearAuthenticatedUserContext'), config.disableExceptionTracking || (i = 'onerror', r('_' + i), f = e[i], e[i] = function (config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o), s }), t
}({
instrumentationKey: '6a3cb8a6-9523-4f84-8237-915370d025ef'
});
window.appInsights = appInsights;
appInsights.trackPageView(null, null, { urlReferrer: document.referrer });
}
</script>
</head>
<body>