Skip to content

Commit dc99758

Browse files
committed
Add analytics support
1 parent dab1096 commit dc99758

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ ui:
7070
enabled: false
7171
# Logo URL to display in the header
7272
logo: https://raw.githubusercontent.com/henrywhitaker3/prompage/refs/heads/main/internal/resources/static/icon.png
73+
analytics:
74+
# Type of analytics (default: none)
75+
# Options:
76+
# - none: No Tracking
77+
# - umami: umami.is tracking
78+
type: umami
79+
80+
# umami settings
81+
umami:
82+
# Script for umami
83+
script: http://localhost:3000/script.js
84+
# Website ID for umami
85+
websiteID: 3FD6E4BD-2CE9-4D9F-BD8B-65B3A2B870C9
7386
```
7487
7588
## Installation

internal/config/config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ type UIHeader struct {
4646
Logo string `yaml:"logo"`
4747
}
4848

49+
type Analytics struct {
50+
Type string `yaml:"type"`
51+
Umami AnalyticsUmami `yaml:"umami"`
52+
}
53+
54+
type AnalyticsUmami struct {
55+
Script string `yaml:"script"`
56+
WebsiteID string `yaml:"websiteID"`
57+
}
58+
4959
type Metrics struct {
5060
Enabled bool `yaml:"enabled"`
5161
Port int `yaml:"port"`
@@ -76,6 +86,8 @@ type Config struct {
7686
Refresh time.Duration `yaml:"refresh"`
7787

7888
UI UI `yaml:"ui"`
89+
90+
Analytics Analytics `yaml:"analytics"`
7991
}
8092

8193
func Load(path string) (*Config, error) {
@@ -159,6 +171,10 @@ func setDefaults(conf *Config) {
159171
if conf.UI.Theme == "" {
160172
conf.UI.Theme = "light"
161173
}
174+
175+
if conf.Analytics.Type == "" {
176+
conf.Analytics.Type = "none"
177+
}
162178
}
163179

164180
func setDefaultQueryValues(q *Query) {
@@ -216,6 +232,30 @@ func (c *Config) Validate() error {
216232
}
217233
}
218234

235+
analyticsTypeValid := false
236+
analyticsTypes := []string{
237+
"none",
238+
"umami",
239+
}
240+
241+
for _, val := range analyticsTypes {
242+
if val == c.Analytics.Type {
243+
analyticsTypeValid = true
244+
}
245+
}
246+
if !analyticsTypeValid {
247+
return errors.New("unsupportet analytics type")
248+
}
249+
250+
if c.Analytics.Type == "umami" {
251+
if c.Analytics.Umami.Script == "" {
252+
return errors.New("analytics umami script must be set")
253+
}
254+
if c.Analytics.Umami.WebsiteID == "" {
255+
return errors.New("analytics umami websiteID must be set")
256+
}
257+
}
258+
219259
return nil
220260
}
221261

internal/resources/views/base.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<link rel="stylesheet" href="/static/tailwind.css?v={{ .Version }}" />
88
<link rel="icon" type="image/png" href="/static/icon.png" />
99
<title>{{ .Config.UI.PageTitle }}</title>
10+
{{ if eq .Config.Analytics.Type "umami" }}
11+
<script defer src="{{ .Config.Analytics.Umami.Script }}" data-website-id="{{ .Config.Analytics.Umami.WebsiteID }}"></script>
12+
{{ end }}
1013
</head>
1114

1215
<body

0 commit comments

Comments
 (0)