google统计添加

This commit is contained in:
谢宇宁
2026-10-16 19:02:39 +07:00
parent 4afd5f9143
commit fe52aefe4a
7 changed files with 30 additions and 1 deletions
+3 -1
View File
@@ -38,6 +38,8 @@ import "@/styles/pages/playback.css";
import "@/styles/overrides/pwa-safe-area.css";
import type { ReactNode } from "react";
import { PwaProvider } from "@/components/pwa/PwaProvider";
import { GoogleAnalytics } from "@/components/analytics/GoogleAnalytics";
import { serverConfig } from "@/server/config";
import { pwaViewport } from "@/lib/pwa/config";
import { getRequestPage } from "@/server/react/request-page";
import { toNextMetadata } from "@/server/seo";
@@ -81,7 +83,7 @@ export default async function RootLayout({
/>
)}
</head>
<body className={presentation.bodyClass}><PwaProvider />{children}</body>
<body className={presentation.bodyClass}><PwaProvider />{children}<GoogleAnalytics id={serverConfig().googleAnalyticsID} /></body>
</html>
);
}
@@ -0,0 +1,17 @@
import Script from "next/script";
/** Google tag (gtag.js); loads after hydration so it never blocks rendering. */
export function GoogleAnalytics({ id }: { id: string }) {
if (!id) return null;
return (
<>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${id}`}
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config',${JSON.stringify(id)});`}
</Script>
</>
);
}
+2
View File
@@ -25,6 +25,8 @@ export function serverConfig() {
timeout: Math.max(100, Number(process.env.UPSTREAM_TIMEOUT_MS) || 20000),
m3u8KeyFile: process.env.M3U8_KEY_FILE || './config/secrets/m3u8.key',
navigationOnly: process.env.NAVIGATION_ONLY === 'true',
/** Google Analytics 4 measurement ID; empty or invalid disables the Google tag. */
googleAnalyticsID: /^G-[A-Z0-9]{4,20}$/.test((process.env.GOOGLE_ANALYTICS_ID || '').trim()) ? (process.env.GOOGLE_ANALYTICS_ID || '').trim() : '',
/** Every Telegram contact entry points here, overriding the upstream tgGroup/telegram/telegramAdd. */
contactTelegramURL: installationURL(process.env.CONTACT_TELEGRAM_URL) || 'https://t.me/jbd125',
installation: {