Skip to content

Commit 352daab

Browse files
committed
microsoft clarity
1 parent 7480af0 commit 352daab

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

hugo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ disableKinds = ["taxonomy", "term"]
2020
paginate = 10
2121
env = "production"
2222
google_analytics_id = "G-VM01Q3R43D"
23+
microsoft_clarity_id = "mxr4hxioh4"
2324
# Add static/images/og-default.png (1200×630) for better social sharing; omit or leave empty to use logo
2425
default_og_image = "images/og-default.png"
2526

layouts/partials/cookie-consent.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
{{- warnf "google_analytics_id not set in hugo.toml params. Please configure it." -}}
55
{{- $gaId = "G-VM01Q3R43D" -}}
66
{{- end -}}
7-
<div id="cookie-consent-banner" class="cookie-consent-banner" role="dialog" aria-labelledby="cookie-consent-title" aria-live="polite" style="display: none;" data-ga-id="{{ $gaId }}">
7+
{{- $clarityId := .Site.Params.microsoft_clarity_id | default "" -}}
8+
<div id="cookie-consent-banner" class="cookie-consent-banner" role="dialog" aria-labelledby="cookie-consent-title" aria-live="polite" style="display: none;" data-ga-id="{{ $gaId }}"{{ with $clarityId }} data-clarity-id="{{ . }}"{{ end }}>
89
<div class="cookie-consent-content">
910
<div class="cookie-consent-text">
1011
<h2 id="cookie-consent-title" class="cookie-consent-title">Cookie Consent</h2>

layouts/partials/googlegtag.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{{- /* Preconnect to Google Analytics for improved performance */ -}}
2-
{{- /* Note: Google Analytics will only load after user consent via cookie-consent.js */ -}}
1+
{{- /* Preconnect to analytics hosts for improved performance */ -}}
2+
{{- /* Note: GA and Clarity load only after user consent via cookie-consent.js */ -}}
33
<link rel="preconnect" href="https://www.googletagmanager.com" crossorigin>
4-
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
4+
<link rel="dns-prefetch" href="https://www.googletagmanager.com">
5+
<link rel="preconnect" href="https://www.clarity.ms" crossorigin>
6+
<link rel="dns-prefetch" href="https://www.clarity.ms">

static/js/cookie-consent.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Cookie Consent Manager
3-
* Handles GDPR/CCPA compliance for Google Analytics
3+
* Handles GDPR/CCPA compliance for Google Analytics and Microsoft Clarity
44
*/
55

66
(function() {
@@ -21,6 +21,36 @@
2121
return 'G-VM01Q3R43D'; // Last resort fallback
2222
}
2323

24+
function getClarityProjectId() {
25+
const banner = document.getElementById('cookie-consent-banner');
26+
if (banner && banner.dataset.clarityId) {
27+
return banner.dataset.clarityId;
28+
}
29+
return '';
30+
}
31+
32+
function loadMicrosoftClarity() {
33+
const clarityId = getClarityProjectId();
34+
if (!clarityId || window.__cfdClarityLoadedId === clarityId) {
35+
return;
36+
}
37+
38+
(function (c, l, a, r, i, t, y) {
39+
c[a] =
40+
c[a] ||
41+
function () {
42+
(c[a].q = c[a].q || []).push(arguments);
43+
};
44+
t = l.createElement(r);
45+
t.async = 1;
46+
t.src = 'https://www.clarity.ms/tag/' + i;
47+
y = l.getElementsByTagName(r)[0];
48+
y.parentNode.insertBefore(t, y);
49+
})(window, document, 'clarity', 'script', clarityId);
50+
51+
window.__cfdClarityLoadedId = clarityId;
52+
}
53+
2454
// Check if consent has been given
2555
function getConsentStatus() {
2656
return localStorage.getItem(COOKIE_CONSENT_KEY);
@@ -110,6 +140,8 @@
110140
event_category: 'privacy',
111141
});
112142
}
143+
144+
loadMicrosoftClarity();
113145
}
114146

115147
// Handle accept button click

0 commit comments

Comments
 (0)