Add clarity cookie consent to docs site (#1908)

* Add clarity cookie consent to docs site

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Sharon Hart
2026-03-16 17:24:32 +02:00
committed by GitHub
parent 97ffb3a79d
commit 2450561bc7

View File

@@ -1,12 +1,129 @@
{% extends "base.html" %}
{% block analytics %}
<!-- Microsoft Clarity with Consent Mode -->
<script type="text/javascript">
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "5pd40fk720");
(function () {
// Determine the current consent state before loading Clarity
var storedConsent = null;
try {
storedConsent = window.localStorage && window.localStorage.getItem("clarity_consent");
} catch (e) {
// If localStorage is unavailable (e.g., privacy mode), fall back to denied
storedConsent = null;
}
// Default to "denied" when there is no prior explicit "granted" choice
var defaultConsent = storedConsent === "granted" ? "granted" : "denied";
(function (c, l, a, r, i, t, y) {
c[a] = c[a] || function () {
(c[a].q = c[a].q || []).push(arguments);
};
// Set consentv2 state before loading the Clarity script so it honors consent immediately
c[a]("consentv2", {
ad_Storage: defaultConsent,
analytics_Storage: defaultConsent
});
t = l.createElement(r);
t.async = 1;
t.src = "https://www.clarity.ms/tag/" + i;
y = l.getElementsByTagName(r)[0];
y.parentNode.insertBefore(t, y);
})(window, document, "clarity", "script", "5pd40fk720");
})();
</script>
<!-- Cookie consent banner -->
<style>
#cookie-consent-banner {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: var(--md-default-bg-color, #fff);
border-top: 1px solid var(--md-default-fg-color--lightest, #e0e0e0);
padding: 1rem 1.5rem;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 1rem;
z-index: 9999;
box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
font-size: 0.82rem;
}
#cookie-consent-banner p {
margin: 0;
flex: 1 1 300px;
color: var(--md-default-fg-color, #333);
}
#cookie-consent-banner a {
color: var(--md-accent-fg-color, #448aff);
}
#cookie-consent-banner .consent-buttons {
display: flex;
gap: 0.5rem;
flex-shrink: 0;
}
#cookie-consent-banner button {
padding: 0.4rem 1.2rem;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 0.82rem;
font-weight: 500;
}
#cookie-consent-accept {
background: var(--md-primary-fg-color, #4051b5);
color: var(--md-primary-bg-color, #fff);
}
#cookie-consent-decline {
background: var(--md-default-fg-color--lightest, #e0e0e0);
color: var(--md-default-fg-color, #333);
}
</style>
<script type="text/javascript">
(function() {
if (localStorage.getItem("clarity_consent")) return;
var banner = document.createElement("div");
banner.id = "cookie-consent-banner";
banner.setAttribute("role", "dialog");
banner.setAttribute("aria-label", "Cookie consent");
banner.innerHTML =
'<p>This site uses cookies via Microsoft Clarity to understand how you use our documentation. ' +
'<a href="https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-cookies" ' +
'target="_blank" rel="noopener">Learn more</a></p>' +
'<div class="consent-buttons">' +
'<button id="cookie-consent-decline">Decline</button>' +
'<button id="cookie-consent-accept">Accept</button>' +
'</div>';
document.addEventListener("DOMContentLoaded", function() {
document.body.appendChild(banner);
document.getElementById("cookie-consent-accept").addEventListener("click", function() {
localStorage.setItem("clarity_consent", "granted");
window.clarity("consentv2", {
ad_Storage: "granted",
analytics_Storage: "granted"
});
banner.remove();
});
document.getElementById("cookie-consent-decline").addEventListener("click", function() {
localStorage.setItem("clarity_consent", "denied");
window.clarity("consentv2", {
ad_Storage: "denied",
analytics_Storage: "denied"
});
banner.remove();
});
});
})();
</script>
{% endblock %}