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
+2
View File
@@ -19,6 +19,8 @@
"@radix-ui/react-navigation-menu": "^1.2.2",
"@radix-ui/react-separator": "^1.1.1",
"@radix-ui/react-slot": "^1.1.1",
"@vercel/analytics": "^1.6.1",
"@vercel/speed-insights": "^1.3.1",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"lucide-react": "^0.468.0",
+65
View File
@@ -23,6 +23,12 @@ importers:
'@radix-ui/react-slot':
specifier: ^1.1.1
version: 1.2.4(@types/react@19.2.7)(react@19.2.3)
'@vercel/analytics':
specifier: ^1.6.1
version: 1.6.1(next@15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)
'@vercel/speed-insights':
specifier: ^1.3.1
version: 1.3.1(next@15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)
class-variance-authority:
specifier: ^0.7.1
version: 0.7.1
@@ -1586,6 +1592,55 @@ packages:
cpu: [x64]
os: [win32]
'@vercel/analytics@1.6.1':
resolution: {integrity: sha512-oH9He/bEM+6oKlv3chWuOOcp8Y6fo6/PSro8hEkgCW3pu9/OiCXiUpRUogDh3Fs3LH2sosDrx8CxeOLBEE+afg==}
peerDependencies:
'@remix-run/react': ^2
'@sveltejs/kit': ^1 || ^2
next: '>= 13'
react: ^18 || ^19 || ^19.0.0-rc
svelte: '>= 4'
vue: ^3
vue-router: ^4
peerDependenciesMeta:
'@remix-run/react':
optional: true
'@sveltejs/kit':
optional: true
next:
optional: true
react:
optional: true
svelte:
optional: true
vue:
optional: true
vue-router:
optional: true
'@vercel/speed-insights@1.3.1':
resolution: {integrity: sha512-PbEr7FrMkUrGYvlcLHGkXdCkxnylCWePx7lPxxq36DNdfo9mcUjLOmqOyPDHAOgnfqgGGdmE3XI9L/4+5fr+vQ==}
peerDependencies:
'@sveltejs/kit': ^1 || ^2
next: '>= 13'
react: ^18 || ^19 || ^19.0.0-rc
svelte: '>= 4'
vue: ^3
vue-router: ^4
peerDependenciesMeta:
'@sveltejs/kit':
optional: true
next:
optional: true
react:
optional: true
svelte:
optional: true
vue:
optional: true
vue-router:
optional: true
'@vitejs/plugin-react@4.7.0':
resolution: {integrity: sha512-gUu9hwfWvvEDBBmgtAowQCojwZmJ5mcLn3aufeCsitijs3+f2NsrPtlAWIR6OPiqljl96GVCUbLe0HyqIpVaoA==}
engines: {node: ^14.18.0 || >=16.0.0}
@@ -5066,6 +5121,16 @@ snapshots:
'@unrs/resolver-binding-win32-x64-msvc@1.11.1':
optional: true
'@vercel/analytics@1.6.1(next@15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)':
optionalDependencies:
next: 15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
react: 19.2.3
'@vercel/speed-insights@1.3.1(next@15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(react@19.2.3)':
optionalDependencies:
next: 15.1.11(@babel/core@7.28.5)(@playwright/test@1.57.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
react: 19.2.3
'@vitejs/plugin-react@4.7.0(vite@5.4.21(@types/node@22.19.3))':
dependencies:
'@babel/core': 7.28.5
+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>
)