Documentation
¶
Overview ¶
Package analytics provides privacy-first website analytics.
Index ¶
- func CleanReferrer(ref string) string
- func ExtractBotName(ua string) string
- func GenerateVisitorID(ip, userAgent string) string
- func HashIP(ip string) string
- func InitSalt(store *Store) error
- func IsBot(ua string) bool
- func ParseUserAgent(ua string) (browser, os, device string)
- func TruncateDate(t time.Time, period string) time.Time
- type BotStats
- type BotStatsResponse
- type BotVisit
- type CollectRequest
- type DailyView
- type DimensionStat
- type Handler
- func (h *Handler) Collect(c echo.Context) error
- func (h *Handler) Dashboard(c echo.Context) error
- func (h *Handler) DashboardHTML(c echo.Context) error
- func (h *Handler) GetBotStats(c echo.Context) error
- func (h *Handler) GetBotStatsFragment(c echo.Context) error
- func (h *Handler) GetSetupFragment(c echo.Context) error
- func (h *Handler) GetStats(c echo.Context) error
- func (h *Handler) GetStatsFragment(c echo.Context) error
- func (h *Handler) RegisterRoutes(e *echo.Echo, publicGroup *echo.Group, authMiddleware echo.MiddlewareFunc)
- type LatestPageVisit
- type PageStat
- type Stats
- type StatsResponse
- type Store
- func (s *Store) CleanupOldVisits(retentionDays int) error
- func (s *Store) Close() error
- func (s *Store) GetBotStats(from, to time.Time, hourly, monthly bool) (*BotStats, error)
- func (s *Store) GetRealtimeVisitors() (int, error)
- func (s *Store) GetSetting(key string) (string, error)
- func (s *Store) GetStats(from, to time.Time, hourly, monthly bool) (*Stats, error)
- func (s *Store) SaveBotVisit(bv *BotVisit) error
- func (s *Store) SaveVisit(v *Visit) error
- func (s *Store) SetSetting(key, value string) error
- func (s *Store) StartCleanupScheduler(retentionDays int, interval time.Duration) func()
- func (s *Store) UpdateVisitDuration(visitorID, path string, durationSec int) error
- type Visit
- type VisitRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CleanReferrer ¶
CleanReferrer extracts the domain from a referrer URL.
func ExtractBotName ¶
ExtractBotName extracts the bot name from User-Agent string.
func GenerateVisitorID ¶
GenerateVisitorID creates a salted visitor ID from IP and User-Agent.
func InitSalt ¶
InitSalt loads or generates a persistent salt for IP hashing. Must be called once at startup before any requests are served.
func ParseUserAgent ¶
ParseUserAgent extracts browser, OS, and device from User-Agent string.
Types ¶
type BotStats ¶
type BotStats struct {
Period string `json:"period"`
TotalVisits int `json:"total_visits"`
TopBots []DimensionStat `json:"top_bots"`
TopPages []PageStat `json:"top_pages"`
DailyVisits []DailyView `json:"daily_visits"`
}
BotStats holds aggregated bot analytics data.
type BotStatsResponse ¶
type BotStatsResponse struct {
Stats *BotStats `json:"stats"`
PeriodDays int `json:"period_days"`
Hourly bool `json:"hourly"`
Monthly bool `json:"monthly"`
}
BotStatsResponse is the JSON response for bot stats endpoint.
type BotVisit ¶
type BotVisit struct {
ID int64 `json:"-"`
BotName string `json:"bot_name"` // Name of the bot (e.g., "Googlebot")
IPHash string `json:"-"` // Hashed IP address
UserAgent string `json:"user_agent"` // Full user agent string
Path string `json:"path"` // Page path
Timestamp time.Time `json:"timestamp"`
}
BotVisit represents a single bot/crawler page view.
type CollectRequest ¶
type CollectRequest struct {
Path string `json:"path"`
Referrer string `json:"referrer"`
ScreenSize string `json:"screen_size"`
UserAgent string `json:"user_agent"`
DurationSec int `json:"duration_sec"`
}
CollectRequest is the expected request body for the collect endpoint.
type DimensionStat ¶
DimensionStat represents a dimension breakdown (browser, OS, etc.).
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles analytics HTTP requests.
func NewHandler ¶
NewHandler creates a new analytics handler. The collect endpoint is rate-limited to 60 requests per IP per minute.
func (*Handler) DashboardHTML ¶
DashboardHTML serves the standalone HTML dashboard using templ.
func (*Handler) GetBotStats ¶
GetBotStats returns bot analytics statistics as JSON.
func (*Handler) GetBotStatsFragment ¶
GetBotStatsFragment returns HTML fragment for bot stats (htmx)
func (*Handler) GetSetupFragment ¶
GetSetupFragment returns HTML fragment for setup tab (htmx)
func (*Handler) GetStatsFragment ¶
GetStatsFragment returns HTML fragment for visitor stats (htmx)
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(e *echo.Echo, publicGroup *echo.Group, authMiddleware echo.MiddlewareFunc)
RegisterRoutes registers analytics routes with the Echo router.
type LatestPageVisit ¶
type LatestPageVisit struct {
Path string `json:"path"`
Timestamp string `json:"timestamp"`
Browser string `json:"browser"`
}
LatestPageVisit represents a single recent page visit.
type Stats ¶
type Stats struct {
Period string `json:"period"`
UniqueVisitors int `json:"unique_visitors"`
TotalViews int `json:"total_views"`
AvgDuration int `json:"avg_duration_sec"`
TopPages []PageStat `json:"top_pages"`
LatestPages []LatestPageVisit `json:"latest_pages"`
BrowserStats []DimensionStat `json:"browsers"`
OSStats []DimensionStat `json:"os"`
DeviceStats []DimensionStat `json:"devices"`
ReferrerStats []DimensionStat `json:"referrers"`
DailyViews []DailyView `json:"daily_views"`
}
Stats holds aggregated analytics data.
type StatsResponse ¶
type StatsResponse struct {
Stats *Stats `json:"stats"`
Realtime int `json:"realtime_visitors"`
PeriodDays int `json:"period_days"`
Hourly bool `json:"hourly"`
Monthly bool `json:"monthly"`
}
StatsResponse is the JSON response for stats endpoint.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store provides database operations for analytics.
func (*Store) CleanupOldVisits ¶
CleanupOldVisits removes visits and bot visits older than the retention period.
func (*Store) GetBotStats ¶
GetBotStats returns aggregated bot statistics for the given time period.
func (*Store) GetRealtimeVisitors ¶
GetRealtimeVisitors returns the number of unique visitors in the last 5 minutes.
func (*Store) GetSetting ¶
GetSetting retrieves a setting value by key. Returns empty string if not found.
func (*Store) SaveBotVisit ¶
SaveBotVisit stores a new bot visit in the database.
func (*Store) SetSetting ¶
SetSetting stores a setting value by key (upsert).
func (*Store) StartCleanupScheduler ¶
StartCleanupScheduler runs periodic cleanup of old data. Returns a stop function.
type Visit ¶
type Visit struct {
ID int64 `json:"-"`
VisitorID string `json:"visitor_id"` // Anonymous fingerprint hash
SessionID string `json:"session_id"` // Session identifier
IPHash string `json:"-"` // Hashed IP address
Browser string `json:"browser"` // Browser name
OS string `json:"os"` // Operating system
Device string `json:"device"` // desktop, mobile, tablet
Path string `json:"path"` // Page path
Referrer string `json:"referrer"` // Referrer URL
ScreenSize string `json:"screen_size"` // e.g., "1920x1080"
Timestamp time.Time `json:"timestamp"`
DurationSec int `json:"duration_sec"` // Time spent on page (0 if not available)
}
Visit represents a single page view.