1
0
mirror of https://github.com/XFox111/my-website.git synced 2026-04-22 07:28:01 +03:00

feat!: added cookie consent banner and management system

This commit is contained in:
2024-08-21 00:22:49 +00:00
parent dceb2e44b7
commit 67222999a9
10 changed files with 330 additions and 17 deletions
+55
View File
@@ -0,0 +1,55 @@
/* eslint-disable no-unused-vars */
declare global
{
interface Window
{
/**
* Notify Clarity about user's cookie consent
* @param cmd - command
* @param consent - consent (default: true)
* @see https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-api
*/
clarity?(cmd: "consent", consent?: boolean): void;
/**
* Set custom Clarity user identifier
* @param cmd - command
* @param customId - user identifier
* @param customSessionId - session identifier
* @param customPageId - page identifier
* @param friendlyName - user friendly name
* @see https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-api
*/
clarity?(cmd: "identify", customId: string, customSessionId?: string, customPageId?: string, friendlyName?: string): void;
/**
* Add custom tag to Clarity session recording
* @param cmd - command
* @param key - tag
* @param value - value
* @see https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-api
*/
clarity?(cmd: "set", key: string, value: string): void;
/**
* Add custom event to Clarity session recording
* @param cmd - command
* @param value - event name
* @see https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-api
*/
clarity?(cmd: "event", value: string): void;
/**
* Prioritize current Clarity session recording
* @param cmd - command
* @param reason - reason for upgrade
* @see https://learn.microsoft.com/en-us/clarity/setup-and-installation/clarity-api
*/
clarity?(cmd: "upgrade", reason: string): void;
}
}
export type ClarityProps =
{ cmd: "consent", consent?: boolean; } |
{ cmd: "identify", customId: string, customSessionId?: string, customPageId?: string, friendlyName?: string; } |
{ cmd: "set", key: string, value: string; } |
{ cmd: "event", value: string; } |
{ cmd: "upgrade", reason: string; };
export default global;
+49
View File
@@ -0,0 +1,49 @@
"use client";
export const acceptCookies = (): void =>
{
setCookie("CC", "1", 5184000); // 60 days
window.clarity?.("consent", true);
};
export const rejectCookies = (): void =>
{
setCookie("CC", "0", 86400); // 1 day
window.clarity?.("consent", false);
};
export const dismissCookies = (): void =>
{
setCookie("CC", "", 1209600); // 14 days
};
export const requireExcplicitConsent: boolean = process.env.NEXT_PUBLIC_CLARITY_CONSENT === "1";
export const getCookieChoice = (): "accepted" | "rejected" | "acknowledged" | "none" =>
{
switch (getCookie("CC"))
{
case "1": return "accepted";
case "0": return "rejected";
case "": return "acknowledged";
default: return "none";
}
};
function setCookie(name: string, value: string | number, maxAge: number): void
{
window.document.cookie = `${name}=${value}; max-age=${maxAge}; path=/`;
}
function getCookie(name: string): string | undefined
{
let cookieName = name + "=";
let rawCookies = decodeURIComponent(window.document.cookie);
let cookies = rawCookies.split(";");
for (const cookie of cookies)
if (cookie.trim().startsWith(cookieName))
return cookie.substring(cookieName.length);
return undefined;
}