Skip to content

Commit b07c38c

Browse files
committed
feat(analytics): 添加 Google Analytics 和 51.la 统计支持
在环境配置中新增 GA_ID 和 LA_ID 配置项,并在应用根组件中集成对应的统计脚本。
1 parent 29382e3 commit b07c38c

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

.env.example

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ DENO_ENV=development
7878
# Redis 连接字符串(如需缓存)
7979
# REDIS_URL=
8080

81-
# 分析/统计 ID(如 Google Analytics)
82-
# GA_ID=
81+
# Google Analytics ID
82+
GA_ID=
83+
84+
# 51.la 统计 ID
85+
LA_ID=L1NaKSoU1jvMh9mE
8386

8487
# Sentry DSN(错误追踪)
8588
# SENTRY_DSN=

config/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ export const env = {
7171
// 可选配置
7272
databaseUrl: getEnv("DATABASE_URL", ""),
7373
redisUrl: getEnv("REDIS_URL", ""),
74+
75+
// 统计分析
7476
gaId: getEnv("GA_ID", ""),
77+
laId: getEnv("LA_ID", ""),
78+
79+
// 错误追踪
7580
sentryDsn: getEnv("SENTRY_DSN", ""),
7681
} as const;
7782

routes/_app.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
1+
// deno-lint-ignore-file react-no-danger
12
import { type PageProps } from "$fresh/server.ts";
3+
import { env } from "../config/env.ts";
4+
25
export default function App({ Component }: PageProps) {
6+
const gaId = env.gaId;
7+
const laId = env.laId;
8+
39
return (
410
<html>
511
<head>
612
<meta charset="utf-8" />
713
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
814
<title>HaloLight</title>
915
<link rel="stylesheet" href="/styles.css" />
16+
17+
{/* 51.la 统计 */}
18+
{laId && (
19+
<script
20+
id="LA_COLLECT"
21+
src={`https://web.51.la/go?id=${laId}`}
22+
defer
23+
/>
24+
)}
25+
26+
{/* Google Analytics */}
27+
{gaId && (
28+
<>
29+
<script
30+
async
31+
src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}
32+
/>
33+
{/* deno-lint-ignore react-no-danger */}
34+
<script
35+
dangerouslySetInnerHTML={{
36+
__html: `
37+
window.dataLayer = window.dataLayer || [];
38+
function gtag(){dataLayer.push(arguments);}
39+
gtag('js', new Date());
40+
gtag('config', '${gaId}');
41+
`,
42+
}}
43+
/>
44+
</>
45+
)}
1046
</head>
1147
<body>
1248
<Component />

0 commit comments

Comments
 (0)