feat: add Umami and Google Analytics integration
This commit is contained in:
+7
-4
@@ -30,11 +30,14 @@ services:
|
|||||||
# - SQL_DSN=root:123456@tcp(mysql:3306)/new-api # Point to the mysql service, uncomment if using MySQL
|
# - SQL_DSN=root:123456@tcp(mysql:3306)/new-api # Point to the mysql service, uncomment if using MySQL
|
||||||
- REDIS_CONN_STRING=redis://redis
|
- REDIS_CONN_STRING=redis://redis
|
||||||
- TZ=Asia/Shanghai
|
- TZ=Asia/Shanghai
|
||||||
- ERROR_LOG_ENABLED=true # 是否启用错误日志记录
|
- ERROR_LOG_ENABLED=true # 是否启用错误日志记录 (Whether to enable error log recording)
|
||||||
- BATCH_UPDATE_ENABLED=true # 是否启用批量更新 batch update enabled
|
- BATCH_UPDATE_ENABLED=true # 是否启用批量更新 (Whether to enable batch update)
|
||||||
# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions
|
# - STREAMING_TIMEOUT=300 # 流模式无响应超时时间,单位秒,默认120秒,如果出现空补全可以尝试改为更大值 (Streaming timeout in seconds, default is 120s. Increase if experiencing empty completions)
|
||||||
# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! multi-node deployment, set this to a random string!!!!!!!
|
# - SESSION_SECRET=random_string # 多机部署时设置,必须修改这个随机字符串!! (multi-node deployment, set this to a random string!!!!!!!)
|
||||||
# - SYNC_FREQUENCY=60 # Uncomment if regular database syncing is needed
|
# - SYNC_FREQUENCY=60 # Uncomment if regular database syncing is needed
|
||||||
|
# - GOOGLE_ANALYTICS_ID=G-XXXXXXXXXX # Google Analytics 的测量 ID (Google Analytics Measurement ID)
|
||||||
|
# - UMAMI_WEBSITE_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx # Umami 网站 ID (Umami Website ID)
|
||||||
|
# - UMAMI_SCRIPT_URL=https://analytics.umami.is/script.js # Umami 脚本 URL,默认为官方地址 (Umami Script URL, defaults to official URL)
|
||||||
|
|
||||||
depends_on:
|
depends_on:
|
||||||
- redis
|
- redis
|
||||||
|
|||||||
@@ -150,6 +150,26 @@ func main() {
|
|||||||
})
|
})
|
||||||
server.Use(sessions.Sessions("session", store))
|
server.Use(sessions.Sessions("session", store))
|
||||||
|
|
||||||
|
InjectUmamiAnalytics()
|
||||||
|
InjectGoogleAnalytics()
|
||||||
|
|
||||||
|
// 设置路由
|
||||||
|
router.SetRouter(server, buildFS, indexPage)
|
||||||
|
var port = os.Getenv("PORT")
|
||||||
|
if port == "" {
|
||||||
|
port = strconv.Itoa(*common.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log startup success message
|
||||||
|
common.LogStartupSuccess(startTime, port)
|
||||||
|
|
||||||
|
err = server.Run(":" + port)
|
||||||
|
if err != nil {
|
||||||
|
common.FatalLog("failed to start HTTP server: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func InjectUmamiAnalytics() {
|
||||||
analyticsInjectBuilder := &strings.Builder{}
|
analyticsInjectBuilder := &strings.Builder{}
|
||||||
if os.Getenv("UMAMI_WEBSITE_ID") != "" {
|
if os.Getenv("UMAMI_WEBSITE_ID") != "" {
|
||||||
umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID")
|
umamiSiteID := os.Getenv("UMAMI_WEBSITE_ID")
|
||||||
@@ -164,21 +184,28 @@ func main() {
|
|||||||
analyticsInjectBuilder.WriteString("\"></script>")
|
analyticsInjectBuilder.WriteString("\"></script>")
|
||||||
}
|
}
|
||||||
analyticsInject := analyticsInjectBuilder.String()
|
analyticsInject := analyticsInjectBuilder.String()
|
||||||
indexPage = bytes.ReplaceAll(indexPage, []byte("<analytics></analytics>\n"), []byte(analyticsInject))
|
indexPage = bytes.ReplaceAll(indexPage, []byte("<!--umami-->\n"), []byte(analyticsInject))
|
||||||
|
}
|
||||||
|
|
||||||
router.SetRouter(server, buildFS, indexPage)
|
func InjectGoogleAnalytics() {
|
||||||
var port = os.Getenv("PORT")
|
analyticsInjectBuilder := &strings.Builder{}
|
||||||
if port == "" {
|
if os.Getenv("GOOGLE_ANALYTICS_ID") != "" {
|
||||||
port = strconv.Itoa(*common.Port)
|
gaID := os.Getenv("GOOGLE_ANALYTICS_ID")
|
||||||
}
|
// Google Analytics 4 (gtag.js)
|
||||||
|
analyticsInjectBuilder.WriteString("<script async src=\"https://www.googletagmanager.com/gtag/js?id=")
|
||||||
// Log startup success message
|
analyticsInjectBuilder.WriteString(gaID)
|
||||||
common.LogStartupSuccess(startTime, port)
|
analyticsInjectBuilder.WriteString("\"></script>")
|
||||||
|
analyticsInjectBuilder.WriteString("<script>")
|
||||||
err = server.Run(":" + port)
|
analyticsInjectBuilder.WriteString("window.dataLayer = window.dataLayer || [];")
|
||||||
if err != nil {
|
analyticsInjectBuilder.WriteString("function gtag(){dataLayer.push(arguments);}")
|
||||||
common.FatalLog("failed to start HTTP server: " + err.Error())
|
analyticsInjectBuilder.WriteString("gtag('js', new Date());")
|
||||||
|
analyticsInjectBuilder.WriteString("gtag('config', '")
|
||||||
|
analyticsInjectBuilder.WriteString(gaID)
|
||||||
|
analyticsInjectBuilder.WriteString("');")
|
||||||
|
analyticsInjectBuilder.WriteString("</script>")
|
||||||
}
|
}
|
||||||
|
analyticsInject := analyticsInjectBuilder.String()
|
||||||
|
indexPage = bytes.ReplaceAll(indexPage, []byte("<!--Google Analytics-->\n"), []byte(analyticsInject))
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitResources() error {
|
func InitResources() error {
|
||||||
|
|||||||
+2
-1
@@ -10,7 +10,8 @@
|
|||||||
content="OpenAI 接口聚合管理,支持多种渠道包括 Azure,可用于二次分发管理 key,仅单可执行文件,已打包好 Docker 镜像,一键部署,开箱即用"
|
content="OpenAI 接口聚合管理,支持多种渠道包括 Azure,可用于二次分发管理 key,仅单可执行文件,已打包好 Docker 镜像,一键部署,开箱即用"
|
||||||
/>
|
/>
|
||||||
<title>New API</title>
|
<title>New API</title>
|
||||||
<analytics></analytics>
|
<!--umami-->
|
||||||
|
<!--Google Analytics-->
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user