Skip to content

Commit ca4a67b

Browse files
author
wilovy
committed
feat: Add Clarity Cookies
1 parent 7db87f3 commit ca4a67b

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

src/components/CookieBanner.astro

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
const projectId = import.meta.env.VITE_CLARITY_PROJECT_ID
3+
---
4+
5+
<div
6+
id="cookie-banner"
7+
data-clarity-id={projectId}
8+
class="hidden fixed bottom-0 left-0 right-0 z-50 p-4 bg-editor-bg border-t border-stroke-color shadow-lg"
9+
>
10+
<div class="max-w-4xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-4">
11+
<p class="text-sm text-secondary/80">
12+
Usamos cookies de analítica para mejorar tu experiencia. ¿Aceptas?
13+
</p>
14+
<div class="flex gap-3 shrink-0">
15+
<button
16+
id="reject-cookies"
17+
class="px-4 py-2 text-sm text-secondary/70 border border-stroke-color rounded hover:bg-stroke-color/30 transition-colors cursor-pointer"
18+
>
19+
Rechazar
20+
</button>
21+
<button
22+
id="accept-cookies"
23+
class="px-4 py-2 text-sm bg-yellow text-primary font-semibold rounded hover:opacity-90 transition-opacity cursor-pointer"
24+
>
25+
Aceptar
26+
</button>
27+
</div>
28+
</div>
29+
</div>
30+
31+
<script>
32+
import Clarity from "@microsoft/clarity"
33+
34+
const banner = document.getElementById("cookie-banner")
35+
const projectId = banner?.dataset.clarityId ?? ""
36+
37+
function loadClarity() {
38+
if (projectId) {
39+
Clarity.init(projectId)
40+
Clarity.consentV2({ ad_Storage: "granted", analytics_Storage: "granted" })
41+
}
42+
}
43+
44+
const consent = localStorage.getItem("cookiesAccepted")
45+
46+
if (consent === "true") {
47+
loadClarity()
48+
} else if (consent === null) {
49+
banner?.classList.remove("hidden")
50+
}
51+
52+
document.getElementById("accept-cookies")?.addEventListener("click", () => {
53+
localStorage.setItem("cookiesAccepted", "true")
54+
loadClarity()
55+
banner?.classList.add("hidden")
56+
})
57+
58+
document.getElementById("reject-cookies")?.addEventListener("click", () => {
59+
localStorage.setItem("cookiesAccepted", "false")
60+
banner?.classList.add("hidden")
61+
})
62+
</script>

src/layouts/Layout.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { ClientRouter } from "astro:transitions"
33
import "~/styles/global.css"
44
import favicon from "~/assets/images/webp/favicon.webp"
5+
import CookieBanner from "~/components/CookieBanner.astro"
56
67
const {
78
title = "RustLings Web",
@@ -23,5 +24,6 @@ const {
2324
class="h-screen flex flex-col bg-primary text-secondary font-family-poppins"
2425
>
2526
<slot />
27+
<CookieBanner />
2628
</body>
2729
</html>

src/pages/index.astro

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
---
2-
import Clarity from "@microsoft/clarity"
32
import Footer from "~/components/footer/Footer.astro"
43
import Community from "~/features/home/components/Community.astro"
54
import Hero from "~/features/home/components/Hero.astro"
65
import Playground from "~/features/home/components/Playground.astro"
76
import Layout from "~/layouts/Layout.astro"
8-
9-
const projectId = import.meta.env.VITE_CLARITY_PROJECT_ID
10-
11-
Clarity.init(projectId)
127
---
138

149
<Layout>

0 commit comments

Comments
 (0)