Skip to content

Commit 5db2fe1

Browse files
committed
Update Clarity tracking and consent
1 parent bc4d501 commit 5db2fe1

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@astrojs/rss": "4.0.17",
14+
"@microsoft/clarity": "^1.0.2",
1415
"@tailwindcss/vite": "4.2.1",
1516
"astro": "5.18.0",
1617
"astro-consent": "1.0.17",

src/layouts/Layout.astro

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,53 @@ const ogImage = image ? new URL(image, Astro.site ?? 'https://devproxy.net').hre
4646
}
4747
})();
4848
</script>
49-
<script is:inline>
50-
(function() {
51-
function loadClarity() {
52-
(function(c,l,a,r,i,t,y){
53-
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
54-
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
55-
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
56-
})(window, document, "clarity", "script", "vr3ajkmgm3");
49+
<script>
50+
import Clarity from '@microsoft/clarity';
51+
52+
type AstroConsent = {
53+
get(): { categories?: { analytics?: boolean } } | null;
54+
set(categories: Record<string, boolean>): void;
55+
reset(): void;
56+
};
57+
58+
const w = window as Window & { astroConsent?: AstroConsent };
59+
60+
let clarityLoaded = false;
61+
function loadClarity() {
62+
if (clarityLoaded) return;
63+
clarityLoaded = true;
64+
Clarity.init("vr3ajkmgm3");
65+
}
66+
67+
function checkConsent() {
68+
if (w.astroConsent) {
69+
const consent = w.astroConsent.get();
70+
if (consent?.categories?.analytics) loadClarity();
5771
}
58-
function checkConsent() {
59-
if (window.astroConsent) {
60-
var consent = window.astroConsent.get();
61-
if (consent && consent.categories && consent.categories.analytics) loadClarity();
62-
}
63-
}
64-
checkConsent();
65-
document.addEventListener('cookie-consent-updated', checkConsent);
66-
})();
72+
}
73+
74+
// Check on load (returning visitor who already accepted)
75+
checkConsent();
76+
77+
// Wrap astroConsent.set to detect consent changes without a page reload
78+
if (w.astroConsent) {
79+
const originalSet = w.astroConsent.set.bind(w.astroConsent);
80+
w.astroConsent.set = (categories: Record<string, boolean>) => {
81+
originalSet(categories);
82+
if (categories.analytics) loadClarity();
83+
};
84+
}
85+
86+
// Rename "Reject" button to "Essential only" (not configurable in astro-consent)
87+
const rejectBtn = document.querySelector<HTMLButtonElement>('#astro-consent-banner .cb-reject');
88+
if (rejectBtn) {
89+
rejectBtn.textContent = 'Essential only';
90+
} else {
91+
new MutationObserver((_mutations, obs) => {
92+
const btn = document.querySelector<HTMLButtonElement>('#astro-consent-banner .cb-reject');
93+
if (btn) { btn.textContent = 'Essential only'; obs.disconnect(); }
94+
}).observe(document.body, { childList: true, subtree: true });
95+
}
6796
</script>
6897
</head>
6998
<body class="min-h-screen flex flex-col">

0 commit comments

Comments
 (0)