谷歌分析可选,条件展示

This commit is contained in:
david_bai
2025-06-17 19:44:15 +08:00
parent 6ebccb1c92
commit 4e592e1817
+24 -23
View File
@@ -1,16 +1,16 @@
import "./globals.css";
import Header from '@/components/web/Header'
import Footer from '@/components/web/Footer';
import Header from "@/components/web/Header";
import Footer from "@/components/web/Footer";
import { ThemeProvider } from "@/components/web/theme-provider";
import Script from 'next/script';
import { getDictionary } from '@/lib/dictionary';
import Script from "next/script";
import { getDictionary } from "@/lib/dictionary";
export default async function RootLayout({
children,
params: { lang }
params: { lang },
}: Readonly<{
children: React.ReactNode,
params: { lang: string }
children: React.ReactNode;
params: { lang: string };
}>) {
const messages = await getDictionary(lang);
const googleAnalyticsId = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS;
@@ -27,25 +27,26 @@ export default async function RootLayout({
storageKey="theme-preference"
>
<Header messages={messages} lang={lang} />
<div className="flex-1">
{children}
</div>
<div className="flex-1">{children}</div>
<Footer messages={messages} lang={lang} />
</ThemeProvider>
{/* Google Analytics */}
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}`}
strategy="afterInteractive" // 脚本在页面加载后执行
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${googleAnalyticsId}');
`}
</Script>
{googleAnalyticsId && (
<>
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}`}
strategy="afterInteractive" // 脚本在页面加载后执行
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${googleAnalyticsId}');
`}
</Script>
</>
)}
</body>
</html>
);