diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 54c40c0..ea9c0f7 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -7,14 +7,10 @@ import { GetMetadata } from "@/apis/auth"; import "./globals.scss"; import Loading from "./loading"; import CombinedProviders from "@/contexts/CombinedProviders"; +import GoogleAnalytics from "@/components/GoogleAnalytics"; const inter = Inter({ subsets: ["latin"] }); -// export const metadata: Metadata = { -// title: "typeframes", -// description: "Generated by typeframes", -// }; -// 将metadata改为异步函数,获取动态SEO数据 export async function generateMetadata(): Promise { const res = await GetMetadata(); // 调用API获取SEO数据 if (res.code === 200) { @@ -50,6 +46,7 @@ async function RootLayout({ {children} + ); diff --git a/src/components/GoogleAnalytics.tsx b/src/components/GoogleAnalytics.tsx new file mode 100644 index 0000000..db65753 --- /dev/null +++ b/src/components/GoogleAnalytics.tsx @@ -0,0 +1,42 @@ +"use client"; +import { useEffect } from "react"; +import Script from "next/script"; +import { usePathname } from "next/navigation"; + +const GA_TRACKING_ID = "G-JGPW372ZX7"; // 替换为你的Tracking ID + +declare global { + interface Window { + gtag: (...args: any[]) => void; + } +} +const GoogleAnalytics = () => { + const pathname = usePathname(); + + useEffect(() => { + if (typeof window !== "undefined" && GA_TRACKING_ID) { + window.gtag("config", GA_TRACKING_ID, { + page_path: pathname, + }); + } + }, [pathname]); + + return ( + <> + + + ); +}; + +export default GoogleAnalytics;