mirror of
https://github.com/UiO-Gaming/uiogaming.no.git
synced 2026-11-05 13:56:47 +01:00
Simplify Umami analytics into one server component
The config was split between lib/analyticsConfig and a client component that repeated the production check and default URL, and checked window during render. Umami's data-domains already restricts tracking to our domains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5c612d995d
commit
0eb47846e7
3 files changed
+10
-76
No files matched your search
@@ -7,7 +7,6 @@ import { notFound } from "next/navigation"
|
||||
import LanguageSelector from "@components/ui/languageSelector"
|
||||
import UmamiAnalytics from "@/components/umamiAnalytics"
|
||||
import { routing } from "@/i18n/routing"
|
||||
import { getAnalyticsConfig } from "@/lib/analyticsConfig"
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "UiO Gaming",
|
||||
@@ -38,21 +37,13 @@ export default async function RootLayout({
|
||||
|
||||
const messages = (await import(`../../../messages/${locale}.json`)).default
|
||||
|
||||
const analyticsConfig = getAnalyticsConfig()
|
||||
|
||||
return (
|
||||
<html lang={locale}>
|
||||
<body>
|
||||
<NextIntlClientProvider locale={locale} messages={messages}>
|
||||
<LanguageSelector />
|
||||
<main>{children}</main>
|
||||
{analyticsConfig?.enabled && (
|
||||
<UmamiAnalytics
|
||||
websiteId={analyticsConfig.websiteId}
|
||||
src={analyticsConfig.src}
|
||||
domains={analyticsConfig.domains}
|
||||
/>
|
||||
)}
|
||||
<UmamiAnalytics />
|
||||
</NextIntlClientProvider>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,49 +1,21 @@
|
||||
"use client"
|
||||
|
||||
import Script from "next/script"
|
||||
|
||||
interface UmamiAnalyticsProps {
|
||||
websiteId: string
|
||||
src?: string
|
||||
domains?: string[]
|
||||
}
|
||||
// Umami ignores visits from other domains, so local and preview deployments
|
||||
// aren't counted
|
||||
const DOMAINS = ["uiogaming.no", "www.uiogaming.no"]
|
||||
|
||||
export default function UmamiAnalytics({
|
||||
websiteId,
|
||||
src = "https://analytics.umami.is/script.js",
|
||||
domains,
|
||||
}: UmamiAnalyticsProps) {
|
||||
if (!websiteId) {
|
||||
console.warn("Umami: Website ID is required")
|
||||
return null
|
||||
}
|
||||
export default function UmamiAnalytics() {
|
||||
const websiteId = process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID
|
||||
const src =
|
||||
process.env.NEXT_PUBLIC_UMAMI_SRC || "https://analytics.umami.is/script.js"
|
||||
|
||||
// Only load analytics in production or if explicitly enabled
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
return null
|
||||
}
|
||||
|
||||
// Domain validation (client-side check as additional layer)
|
||||
if (domains && typeof window !== "undefined") {
|
||||
const currentDomain = window.location.hostname
|
||||
const isAllowedDomain = domains.some(
|
||||
domain => currentDomain === domain || currentDomain.endsWith(`.${domain}`)
|
||||
)
|
||||
|
||||
if (!isAllowedDomain) {
|
||||
console.warn(
|
||||
`Umami: Domain ${currentDomain} not in allowed list:`,
|
||||
domains
|
||||
)
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (!websiteId || process.env.NODE_ENV !== "production") return null
|
||||
|
||||
return (
|
||||
<Script
|
||||
src={src}
|
||||
data-website-id={websiteId}
|
||||
data-domains={domains?.join(",")}
|
||||
data-domains={DOMAINS.join(",")}
|
||||
strategy="afterInteractive"
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
interface AnalyticsConfig {
|
||||
websiteId: string
|
||||
src: string
|
||||
domains: string[]
|
||||
enabled: boolean
|
||||
}
|
||||
|
||||
export function getAnalyticsConfig(): AnalyticsConfig | null {
|
||||
const websiteId = process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID
|
||||
const src =
|
||||
process.env.NEXT_PUBLIC_UMAMI_SRC || "https://analytics.umami.is/script.js"
|
||||
|
||||
if (!websiteId) {
|
||||
return null
|
||||
}
|
||||
|
||||
// Define allowed domains
|
||||
const domains = ["uiogaming.no", "www.uiogaming.no"]
|
||||
|
||||
// Additional environment-based checks
|
||||
const enabled = process.env.NODE_ENV === "production"
|
||||
|
||||
return {
|
||||
websiteId,
|
||||
src,
|
||||
domains,
|
||||
enabled,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user