mirror of
https://github.com/UiO-Gaming/uiogaming.no.git
synced 2026-11-05 13:56:47 +01:00
reintroduce umami analytics support
This commit is contained in:
4 files changed
+95
No files matched your search
@@ -2,3 +2,6 @@ NEXT_PUBLIC_SANITY_PROJECT_ID=mmqlu667
|
|||||||
NEXT_PUBLIC_SANITY_DATASET=production
|
NEXT_PUBLIC_SANITY_DATASET=production
|
||||||
|
|
||||||
SANITY_REVALIDATE_SECRET=your-super-secret-webhook-token-here
|
SANITY_REVALIDATE_SECRET=your-super-secret-webhook-token-here
|
||||||
|
|
||||||
|
NEXT_PUBLIC_UMAMI_SRC=https://umami.uiogaming.no/script.js
|
||||||
|
NEXT_PUBLIC_UMAMI_WEBSITE_ID=1e985053-9158-4d62-be07-0fe83a9dce6e
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
import "@/app/globals.css"
|
import "@/app/globals.css"
|
||||||
|
|
||||||
|
import UmamiAnalytics from "@/components/umamiAnalytics"
|
||||||
|
|
||||||
|
import { getAnalyticsConfig } from "@/lib/analyticsConfig"
|
||||||
|
|
||||||
import type { Metadata } from "next"
|
import type { Metadata } from "next"
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
@@ -18,10 +22,19 @@ export default function RootLayout({
|
|||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}>) {
|
}>) {
|
||||||
|
const analyticsConfig = getAnalyticsConfig()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<html lang="no">
|
<html lang="no">
|
||||||
<body>
|
<body>
|
||||||
<main>{children}</main>
|
<main>{children}</main>
|
||||||
|
{analyticsConfig?.enabled && (
|
||||||
|
<UmamiAnalytics
|
||||||
|
websiteId={analyticsConfig.websiteId}
|
||||||
|
src={analyticsConfig.src}
|
||||||
|
domains={analyticsConfig.domains}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import Script from "next/script"
|
||||||
|
|
||||||
|
interface UmamiAnalyticsProps {
|
||||||
|
websiteId: string
|
||||||
|
src?: string
|
||||||
|
domains?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Script
|
||||||
|
src={src}
|
||||||
|
data-website-id={websiteId}
|
||||||
|
data-domains={domains?.join(",")}
|
||||||
|
strategy="afterInteractive"
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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