Skip to content

Commit c116301

Browse files
authored
Add cookie consent banner (#155)
* Add `react-cookie-consent` * Add cookie consent banner * Revise code for responding to cookie consent * Revise code for responding to cookie consent * Add link to cookie policy * Create index.mdx * Create index.mdx
1 parent f700e22 commit c116301

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

package-lock.json

Lines changed: 22 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
@@ -32,6 +32,7 @@
3232
"docusaurus-theme-github-codeblock": "^2.0.2",
3333
"prism-react-renderer": "^2.3.0",
3434
"react": "^18.0.0",
35+
"react-cookie-consent": "9.0.0",
3536
"react-dom": "^18.0.0",
3637
"three": "^0.168.0"
3738
},

src/pages/cookies/index.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Cookie policy
2+
3+
This website, 080f53.com, uses cookies to improve your experience, enhance the functionality of my website, and personalize content. Cookies are small text files stored on your device by my website.
4+
5+
I use Microsoft Clarity and Google Tag Manager cookies for the following purposes:
6+
7+
- **Necessary cookies:** These cookies are essential for you to navigate my website and access its basic features.
8+
- **Performance cookies:** These cookies help me understand how visitors interact with my content, which improves my ability to create relevant and engaging blog posts, projects, and updates.
9+
- **Functionality cookies:** These cookies enable features like commenting, social sharing, and form submissions on my website.
10+
11+
## Consent
12+
13+
By visiting or using this website, you consent to the use of cookies by me.
14+
15+
If you do not consent to my use of cookies, please [contact me](/about#contact) to disable them.
16+
17+
Cookies typically stay active for a limited time, usually until you close your browser or delete the cookie. However, some cookies may remain active for longer periods, such as:
18+
19+
- **Session cookies:** These are deleted when you close your browser.
20+
- **Persistent cookies:** These remain active for a set period of time (usually up to 1 year).
21+
22+
## Changes to cookie policy
23+
24+
I reserve the right to update this cookie policy at any time. Changes will be effective immediately upon posting.
25+
26+
## Contact me
27+
28+
If you have any questions about my cookie policy or consent, please [contact me](/about#contact).

src/theme/Root.js

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import Clarity from '@microsoft/clarity';
3+
import CookieConsent from "react-cookie-consent";
34

4-
const projectId = "p9179dcazx"
5-
Clarity.init(projectId);
5+
// Initialize Clarity with the given project ID
6+
const projectId = "p9179dcazx";
67

7-
// Default implementation, that you can customize
8-
export default function Root({children}) {
9-
return <>{children}</>;
8+
export default function Root({ children }) {
9+
useEffect(() => {
10+
if (typeof window !== "undefined") {
11+
// Initialize Clarity only on the client-side
12+
Clarity.init(projectId);
13+
14+
// Add an event listener for the consent event
15+
window.addEventListener("CookieConsent", () => {
16+
if (window.clarity) {
17+
window.clarity('consent');
18+
console.log("Clarity consent event triggered.");
19+
}
20+
});
21+
}
22+
}, []);
23+
24+
const handleConsentAccept = () => {
25+
// Dispatch a custom "CookieConsent" event when the user accepts cookies
26+
const consentEvent = new Event("CookieConsent");
27+
window.dispatchEvent(consentEvent);
28+
console.log("CookieConsent event dispatched.");
29+
};
30+
31+
return (
32+
<>
33+
{children}
34+
<div>
35+
<CookieConsent
36+
location="bottom"
37+
buttonText="I understand"
38+
style={{
39+
backgroundColor: "#080f53",
40+
padding: "20px",
41+
}}
42+
buttonStyle={{
43+
backgroundColor: "#fff",
44+
color: "#000",
45+
fontWeight: "500",
46+
fontFamily:
47+
"system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'",
48+
}}
49+
expires={150}
50+
onAccept={handleConsentAccept}
51+
>
52+
This website uses cookies to enhance the user experience. By continuing to use this website, you acknowledge that you have read and understood the <a href="/cookies">cookie policy</a> and consent to the use of cookies to improve your browsing experience, personalize content, and analyze website traffic.
53+
</CookieConsent>
54+
</div>
55+
</>
56+
);
1057
}

0 commit comments

Comments
 (0)