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

refactor: moved analytics checks to a separate file

This commit is contained in:
2024-08-21 00:18:17 +00:00
parent 272049c71a
commit d7500e861c
4 changed files with 20 additions and 3 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
import Package from "@/../package.json"; import Package from "@/../package.json";
import { footerImage, nextjsLogo } from "@/_assets/illustrations"; import { footerImage, nextjsLogo } from "@/_assets/illustrations";
import { analyticsEnabled } from "@/_utils/analytics/server";
import Image from "next/image"; import Image from "next/image";
import Link from "next/link"; import Link from "next/link";
import React from "react"; import React from "react";
@@ -14,7 +15,7 @@ const Footer: React.FC = () => (
<p> <p>
This site was created with help of some third-party tools and services. This site was created with help of some third-party tools and services.
</p> </p>
{ process.env.CLARITY_ID && { analyticsEnabled() &&
<p> <p>
This site is using Microsoft Clarity for analytics purposes.<br aria-hidden /> This site is using Microsoft Clarity for analytics purposes.<br aria-hidden />
By using this site you agree that we and Microsoft can collect and use this data. By using this site you agree that we and Microsoft can collect and use this data.
+14
View File
@@ -0,0 +1,14 @@
import { headers } from "next/headers";
/**
* Check if Clarity is enabled
* @returns true if Clarity is enabled
*/
export const analyticsEnabled = (): boolean => !!process.env.CLARITY_ID;
/**
* Check if Clarity is enabled and the browser didn't send a DNT signal
* @returns true if Clarity is enabled and the browser didn't send a DNT signal
*/
export const canLoadAnalytics = (): boolean =>
analyticsEnabled() && headers().get("Dnt") !== "1";
+2 -1
View File
@@ -2,6 +2,7 @@ import Package from "@/../package.json";
import Button from "@/_components/Button"; import Button from "@/_components/Button";
import { canonicalName, getTitle } from "@/_data/metadata"; import { canonicalName, getTitle } from "@/_data/metadata";
import ThirdPartyAttribution from "@/_data/ThirdPartyAttributiont"; import ThirdPartyAttribution from "@/_data/ThirdPartyAttributiont";
import { analyticsEnabled } from "@/_utils/analytics/server";
import { ArrowLeft24Regular, ArrowRight24Regular } from "@fluentui/react-icons"; import { ArrowLeft24Regular, ArrowRight24Regular } from "@fluentui/react-icons";
import { Metadata } from "next"; import { Metadata } from "next";
import React from "react"; import React from "react";
@@ -23,7 +24,7 @@ const AttributionPage: React.FC = () => (
<article> <article>
<h1>Attrubtions &amp; information</h1> <h1>Attrubtions &amp; information</h1>
{ process.env.CLARITY_ID && { analyticsEnabled() &&
<section> <section>
<h2>Privacy policy</h2> <h2>Privacy policy</h2>
<p> <p>
+2 -1
View File
@@ -5,6 +5,7 @@ import Script from "next/script";
import { PropsWithChildren } from "react"; import { PropsWithChildren } from "react";
import Footer from "./_components/Footer"; import Footer from "./_components/Footer";
import Header from "./_components/Header"; import Header from "./_components/Header";
import { canLoadAnalytics } from "./_utils/analytics/server";
import fonts from "./fonts"; import fonts from "./fonts";
import "./globals.scss"; import "./globals.scss";
@@ -26,7 +27,7 @@ export default function RootLayout(props: PropsWithChildren)
return ( return (
<html lang="en" className={ fonts.map(i => i.variable).join(" ") }> <html lang="en" className={ fonts.map(i => i.variable).join(" ") }>
{ headers.get("Dnt") !== "1" && process.env.CLARITY_ID && { canLoadAnalytics() &&
// If "Do Not Track" is enabled, or there's no CLARITY_ID set up, we don't load any analytics // If "Do Not Track" is enabled, or there's no CLARITY_ID set up, we don't load any analytics
<Script id="ms-clarity" src="/clarity.js" data-id={ process.env.CLARITY_ID } /> <Script id="ms-clarity" src="/clarity.js" data-id={ process.env.CLARITY_ID } />
} }