Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ffb60c97
authored
Oct 14, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add Umami and Google Analytics integration
parent
9ab5df93
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
18 deletions
+49
-18
docker-compose.yml
+7
-4
main.go
+40
-13
web/index.html
+2
-1
No files found.
docker-compose.yml
View file @
ffb60c97
...
@@ -30,11 +30,14 @@ services:
...
@@ -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
...
...
main.go
View file @
ffb60c97
...
@@ -150,6 +150,26 @@ func main() {
...
@@ -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() {
...
@@ -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
)
var
port
=
os
.
Getenv
(
"PORT"
)
if
port
==
""
{
port
=
strconv
.
Itoa
(
*
common
.
Port
)
}
// Log startup success message
common
.
LogStartupSuccess
(
startTime
,
port
)
err
=
server
.
Run
(
":"
+
port
)
func
InjectGoogleAnalytics
()
{
if
err
!=
nil
{
analyticsInjectBuilder
:=
&
strings
.
Builder
{}
common
.
FatalLog
(
"failed to start HTTP server: "
+
err
.
Error
())
if
os
.
Getenv
(
"GOOGLE_ANALYTICS_ID"
)
!=
""
{
gaID
:=
os
.
Getenv
(
"GOOGLE_ANALYTICS_ID"
)
// Google Analytics 4 (gtag.js)
analyticsInjectBuilder
.
WriteString
(
"<script async src=
\"
https://www.googletagmanager.com/gtag/js?id="
)
analyticsInjectBuilder
.
WriteString
(
gaID
)
analyticsInjectBuilder
.
WriteString
(
"
\"
></script>"
)
analyticsInjectBuilder
.
WriteString
(
"<script>"
)
analyticsInjectBuilder
.
WriteString
(
"window.dataLayer = window.dataLayer || [];"
)
analyticsInjectBuilder
.
WriteString
(
"function gtag(){dataLayer.push(arguments);}"
)
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
{
...
...
web/index.html
View file @
ffb60c97
...
@@ -10,7 +10,8 @@
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment