Skip to content

Commit 67c321c

Browse files
committed
feat: add Umami and Google Analytics integration
1 parent b3f50e9 commit 67c321c

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

docker-compose.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ services:
3030
# - SQL_DSN=root:123456@tcp(mysql:3306)/new-api # Point to the mysql service, uncomment if using MySQL
3131
- REDIS_CONN_STRING=redis://redis
3232
- TZ=Asia/Shanghai
33-
- ERROR_LOG_ENABLED=true # 是否启用错误日志记录
34-
- BATCH_UPDATE_ENABLED=true # 是否启用批量更新 batch update enabled
35-
# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions
36-
# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! multi-node deployment, set this to a random string!!!!!!!
33+
- ERROR_LOG_ENABLED=true # 是否启用错误日志记录 (Whether to enable error log recording)
34+
- BATCH_UPDATE_ENABLED=true # 是否启用批量更新 (Whether to enable batch update)
35+
# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions
36+
# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! multi-node deployment, set this to a random string!!!!!!!
3737
# - SYNC_FREQUENCY=60 # Uncomment if regular database syncing is needed
38+
# - GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX # Google Analytics 的测量 ID (Google Analytics Measurement ID)
39+
# - UMAMI_WEBSITE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Umami 网站 ID (Umami Website ID)
40+
# - UMAMI_SCRIPT_URL=https://analytics.umami.is/script.js # Umami 脚本 URL,默认为官方地址 (Umami Script URL, defaults to official URL)
3841

3942
depends_on:
4043
- redis

main.go

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,26 @@ func main() {
150150
})
151151
server.Use(sessions.Sessions("session", store))
152152

153+
InjectUmamiAnalytics()
154+
InjectGoogleAnalytics()
155+
156+
// 设置路由
157+
router.SetRouter(server, buildFS, indexPage)
158+
var port = os.Getenv("PORT")
159+
if port == "" {
160+
port = strconv.Itoa(*common.Port)
161+
}
162+
163+
// Log startup success message
164+
common.LogStartupSuccess(startTime, port)
165+
166+
err = server.Run(":" + port)
167+
if err != nil {
168+
common.FatalLog("failed to start HTTP server: " + err.Error())
169+
}
170+
}
171+
172+
func InjectUmamiAnalytics() {
153173
analyticsInjectBuilder := &strings.Builder{}
154174
if os.Getenv("UMAMI_WEBSITE_ID") != "" {
155175
umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID")
@@ -164,21 +184,28 @@ func main() {
164184
analyticsInjectBuilder.WriteString("\"></script>")
165185
}
166186
analyticsInject := analyticsInjectBuilder.String()
167-
indexPage = bytes.ReplaceAll(indexPage, []byte("<analytics></analytics>\n"), []byte(analyticsInject))
168-
169-
router.SetRouter(server, buildFS, indexPage)
170-
var port = os.Getenv("PORT")
171-
if port == "" {
172-
port = strconv.Itoa(*common.Port)
173-
}
174-
175-
// Log startup success message
176-
common.LogStartupSuccess(startTime, port)
187+
indexPage = bytes.ReplaceAll(indexPage, []byte("<!--umami-->\n"), []byte(analyticsInject))
188+
}
177189

178-
err = server.Run(":" + port)
179-
if err != nil {
180-
common.FatalLog("failed to start HTTP server: " + err.Error())
190+
func InjectGoogleAnalytics() {
191+
analyticsInjectBuilder := &strings.Builder{}
192+
if os.Getenv("GOOGLE_ANALYTICS_ID") != "" {
193+
gaID := os.Getenv("GOOGLE_ANALYTICS_ID")
194+
// Google Analytics 4 (gtag.js)
195+
analyticsInjectBuilder.WriteString("<script async src=\"https://www.googletagmanager.com/gtag/js?id=")
196+
analyticsInjectBuilder.WriteString(gaID)
197+
analyticsInjectBuilder.WriteString("\"></script>")
198+
analyticsInjectBuilder.WriteString("<script>")
199+
analyticsInjectBuilder.WriteString("window.dataLayer = window.dataLayer || [];")
200+
analyticsInjectBuilder.WriteString("function gtag(){dataLayer.push(arguments);}")
201+
analyticsInjectBuilder.WriteString("gtag('js', new Date());")
202+
analyticsInjectBuilder.WriteString("gtag('config', '")
203+
analyticsInjectBuilder.WriteString(gaID)
204+
analyticsInjectBuilder.WriteString("');")
205+
analyticsInjectBuilder.WriteString("</script>")
181206
}
207+
analyticsInject := analyticsInjectBuilder.String()
208+
indexPage = bytes.ReplaceAll(indexPage, []byte("<!--Google Analytics-->\n"), []byte(analyticsInject))
182209
}
183210

184211
func InitResources() error {

web/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
content="OpenAI 接口聚合管理,支持多种渠道包括 Azure,可用于二次分发管理 key,仅单可执行文件,已打包好 Docker 镜像,一键部署,开箱即用"
1111
/>
1212
<title>New API</title>
13-
<analytics></analytics>
13+
<!--umami-->
14+
<!--Google Analytics-->
1415
</head>
1516

1617
<body>

0 commit comments

Comments
 (0)