Skip to content

Commit d4a1c54

Browse files
authored
Add component for Microsoft Clarity and cookie consent banner (#805)
* Create Root.js * Add Microsoft Clarity code * Add `react-cookie-consent` * Add feature to show cookie consent message * Increase font size of `I understand` button * Revise cookie banner message
1 parent 2f02ca9 commit d4a1c54

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
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
@@ -34,6 +34,7 @@
3434
"docusaurus-plugin-image-zoom": "^2.0.0",
3535
"prism-react-renderer": "^2.3.0",
3636
"react": "^18.0.0",
37+
"react-cookie-consent": "9.0.0",
3738
"react-dom": "^18.0.0"
3839
},
3940
"devDependencies": {

src/theme/Root.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React, { useEffect } from 'react';
2+
import Clarity from '@microsoft/clarity';
3+
import CookieConsent from "react-cookie-consent";
4+
5+
// Initialize Clarity with the given project ID
6+
const projectId = "p90sis7r0o";
7+
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+
fontSize: "15px",
47+
fontFamily:
48+
"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'",
49+
}}
50+
expires={150}
51+
onAccept={handleConsentAccept}
52+
>
53+
This website uses cookies to enhance the visitor experience. By continuing to use this website, you acknowledge that you have read and understood our <a href="https://www.scalar-labs.com/privacy-policy" style={{color: "white", textDecorationLine: "underline"}} target="_blank">privacy policy</a> and consent to the use of cookies to help improve your browsing experience and provide you with personalized content.
54+
</CookieConsent>
55+
</div>
56+
</>
57+
);
58+
}

0 commit comments

Comments
 (0)