analytics_int

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2026 License: GPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Package analytics_int provides built-in server-side analytics for oCMS. It tracks page views, unique visitors, referrers, browser/device stats, and geographic data with privacy-focused anonymization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountryName

func CountryName(code string) string

CountryName returns the full country name for a 2-letter country code.

Types

type BrowserStat

type BrowserStat struct {
	Browser string
	Views   int64
	Percent float64
}

BrowserStat represents browser breakdown for dashboard.

type CountryStat

type CountryStat struct {
	CountryCode string
	CountryName string
	Views       int64
	Percent     float64
}

CountryStat represents country breakdown for dashboard.

type DailyStat

type DailyStat struct {
	ID             int64
	Date           time.Time
	Path           string
	Views          int
	UniqueVisitors int
	Bounces        int
}

DailyStat represents daily aggregated statistics.

type DashboardData

type DashboardData struct {
	Overview     OverviewStats
	TopPages     []TopPage
	TopReferrers []TopReferrer
	Browsers     []BrowserStat
	Devices      []DeviceStat
	Countries    []CountryStat
	TimeSeries   []TimeSeriesPoint
	DateRange    string // "7d", "30d", "90d", "1y"
	Settings     Settings
}

DashboardData contains all data for the analytics dashboard.

type DeviceStat

type DeviceStat struct {
	DeviceType string
	Views      int64
	Percent    float64
}

DeviceStat represents device type breakdown for dashboard.

type GeoIPLookup

type GeoIPLookup struct {
	// contains filtered or unexported fields
}

GeoIPLookup handles IP to country lookup using MaxMind GeoLite2-Country database.

func NewGeoIPLookup

func NewGeoIPLookup() *GeoIPLookup

NewGeoIPLookup creates a new GeoIP lookup instance.

func (*GeoIPLookup) Close added in v0.1.0

func (g *GeoIPLookup) Close() error

Close closes the GeoIP database.

func (*GeoIPLookup) Init

func (g *GeoIPLookup) Init(dbPath string) error

Init initializes the GeoIP database from the given path. If path is empty, GeoIP lookups are disabled (graceful degradation). Returns an error if the database cannot be loaded (logs warning instead).

func (*GeoIPLookup) IsEnabled added in v0.1.0

func (g *GeoIPLookup) IsEnabled() bool

IsEnabled returns whether GeoIP lookups are available.

func (*GeoIPLookup) LookupCountry

func (g *GeoIPLookup) LookupCountry(ip string) string

LookupCountry returns the 2-letter ISO country code for an IP address. Returns empty string if: - GeoIP is not enabled/initialized - IP address is invalid - Country cannot be determined Returns "LOCAL" for private/local IPs.

func (*GeoIPLookup) Reload added in v0.1.0

func (g *GeoIPLookup) Reload() error

Reload reloads the GeoIP database if it has been updated. Safe to call periodically (e.g., from a cron job).

type GeoStat

type GeoStat struct {
	ID             int64
	Date           time.Time
	CountryCode    string
	Views          int
	UniqueVisitors int
}

GeoStat represents daily geographic statistics.

type HourlyStat

type HourlyStat struct {
	ID             int64
	HourStart      time.Time
	Path           string
	Views          int
	UniqueVisitors int
}

HourlyStat represents hourly aggregated statistics.

type Module

type Module struct {
	module.BaseModule
	// contains filtered or unexported fields
}

Module implements the internal analytics module.

func New

func New() *Module

New creates a new internal analytics module.

func (*Module) AdminURL

func (m *Module) AdminURL() string

AdminURL returns the admin dashboard URL.

func (*Module) CreateSessionHash

func (m *Module) CreateSessionHash(ip, userAgent string) string

CreateSessionHash creates a session proxy hash for grouping page views. Similar to visitor hash but with different suffix for distinct identification.

func (*Module) CreateVisitorHash

func (m *Module) CreateVisitorHash(ip, userAgent string) string

CreateVisitorHash creates an anonymized visitor fingerprint hash. The hash combines anonymized IP + user agent + date + salt. This allows counting unique visitors per day without tracking across days.

func (*Module) GetRealTimeVisitorCount

func (m *Module) GetRealTimeVisitorCount(minutes int) int

GetRealTimeVisitorCount returns the number of unique visitors in the last N minutes.

func (*Module) GetTrackingMiddleware

func (m *Module) GetTrackingMiddleware() func(next http.Handler) http.Handler

GetTrackingMiddleware returns the tracking middleware for use in router setup. This should be called after Init() to ensure settings are loaded.

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init initializes the module.

func (*Module) IsEnabled

func (m *Module) IsEnabled() bool

IsEnabled returns whether analytics tracking is enabled.

func (*Module) Migrations

func (m *Module) Migrations() []module.Migration

Migrations returns database migrations.

func (*Module) RegisterAdminRoutes

func (m *Module) RegisterAdminRoutes(r chi.Router)

RegisterAdminRoutes registers admin routes.

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(_ chi.Router)

RegisterRoutes registers public routes (none for this module).

func (*Module) ReloadSettings

func (m *Module) ReloadSettings() error

ReloadSettings reloads settings from the database.

func (*Module) RunAggregationNow

func (m *Module) RunAggregationNow() error

RunAggregationNow runs all aggregation jobs immediately (for testing).

func (*Module) Shutdown

func (m *Module) Shutdown() error

Shutdown cleans up resources.

func (*Module) SidebarLabel

func (m *Module) SidebarLabel() string

SidebarLabel returns the display label for the admin sidebar.

func (*Module) StartAggregator

func (m *Module) StartAggregator()

StartAggregator starts background aggregation jobs.

func (*Module) TemplateFuncs

func (m *Module) TemplateFuncs() template.FuncMap

TemplateFuncs returns template functions provided by the module.

func (*Module) TrackingMiddleware

func (m *Module) TrackingMiddleware() func(http.Handler) http.Handler

TrackingMiddleware returns middleware that tracks page views.

func (*Module) TranslationsFS

func (m *Module) TranslationsFS() embed.FS

TranslationsFS returns module translations.

type OverviewStats

type OverviewStats struct {
	TotalViews       int64
	UniqueVisitors   int64
	BounceRate       float64 // percentage
	ViewsToday       int64
	ViewsYesterday   int64
	TrendPercent     float64 // change from yesterday
	RealTimeVisitors int     // visitors in last 5 minutes
}

OverviewStats contains summary statistics for the dashboard.

type PageView

type PageView struct {
	ID             int64
	VisitorHash    string    // Anonymized visitor fingerprint (daily rotating)
	Path           string    // URL path (e.g., "/about")
	PageID         *int64    // FK to pages.id (nullable for non-page routes)
	ReferrerDomain string    // Extracted referrer domain
	CountryCode    string    // 2-letter ISO country code
	Browser        string    // Browser name (Chrome, Firefox, Safari, etc.)
	OS             string    // Operating system
	DeviceType     string    // desktop, mobile, tablet
	Language       string    // Accept-Language primary language
	SessionHash    string    // Session proxy (IP+UA+date hash)
	CreatedAt      time.Time // When the view occurred
}

PageView represents a single page view event stored in the database.

type ParsedUA

type ParsedUA struct {
	Browser    string
	OS         string
	DeviceType string // "desktop", "mobile", "tablet"
}

ParsedUA holds parsed user agent information.

type ReferrerStat

type ReferrerStat struct {
	ID             int64
	Date           time.Time
	ReferrerDomain string
	Views          int
	UniqueVisitors int
}

ReferrerStat represents daily referrer statistics.

type Settings

type Settings struct {
	Enabled           bool
	RetentionDays     int
	ExcludePaths      []string
	CurrentSalt       string
	SaltCreatedAt     time.Time
	SaltRotationHours int
}

Settings holds module configuration.

type TechStat

type TechStat struct {
	ID         int64
	Date       time.Time
	Browser    string
	OS         string
	DeviceType string
	Views      int
}

TechStat represents daily browser/device statistics.

type TimeSeriesPoint

type TimeSeriesPoint struct {
	Date   string // YYYY-MM-DD
	Views  int64
	Unique int64
}

TimeSeriesPoint represents a data point for time series charts.

type TopPage

type TopPage struct {
	Path           string
	PageID         *int64
	PageTitle      string
	Views          int64
	UniqueVisitors int64
	BounceRate     float64
}

TopPage represents a page in the top pages list.

type TopReferrer

type TopReferrer struct {
	Domain         string
	Views          int64
	UniqueVisitors int64
}

TopReferrer represents a referrer in the top referrers list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL