|
1 | | -import React from 'react'; |
| 1 | +import React, { useEffect } from 'react'; |
2 | 2 | import Clarity from '@microsoft/clarity'; |
| 3 | +import CookieConsent from "react-cookie-consent"; |
3 | 4 |
|
4 | | -const projectId = "p9179dcazx" |
5 | | -Clarity.init(projectId); |
| 5 | +// Initialize Clarity with the given project ID |
| 6 | +const projectId = "p9179dcazx"; |
6 | 7 |
|
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 | + ); |
10 | 57 | } |
0 commit comments