feat: 接入 Vercel Analytics 与 Speed Insights 并控制采集量

This commit is contained in:
2026-02-23 23:08:01 +08:00
parent 16484827c9
commit 67bba0c207
3 changed files with 91 additions and 0 deletions
+24
View File
@@ -1,4 +1,6 @@
import type { Metadata } from "next"
import { Analytics } from "@vercel/analytics/next"
import { SpeedInsights } from "@vercel/speed-insights/next"
import "./globals.css"
export const metadata: Metadata = {
@@ -6,6 +8,19 @@ export const metadata: Metadata = {
description: "发现和探索全网优质 AI 项目",
}
const isVercelProduction = process.env.VERCEL_ENV === "production"
const isTrackablePath = (url: string): boolean => {
try {
const pathname = new URL(url).pathname
// Only keep user-facing locale pages to control usage on the Hobby plan.
return pathname === "/zh" || pathname === "/en" || pathname.startsWith("/zh/") || pathname.startsWith("/en/")
} catch {
return false
}
}
export default function RootLayout({
children,
}: Readonly<{
@@ -15,6 +30,15 @@ export default function RootLayout({
<html lang="zh" suppressHydrationWarning>
<body className="font-sans antialiased">
{children}
{isVercelProduction ? (
<>
<Analytics mode="production" beforeSend={(event) => (isTrackablePath(event.url) ? event : null)} />
<SpeedInsights
sampleRate={0.2}
beforeSend={(data) => (isTrackablePath(data.url) ? data : null)}
/>
</>
) : null}
</body>
</html>
)