Help us shape the future of Go! Take the 2026 Go Developer Survey

analytics

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package analytics keeps the only first-party analytics Hatamoto needs: pageviews and AI-assistant referrals, rolled up per day. There are no visitors, sessions or per-event rows; crawler hits live in internal/agents.

Index

Constants

View Source
const (
	KindPageView   = "pageview"
	KindAIReferral = "ai_referral"
)

Rollup kinds stored in hatamoto_analytics_daily.kind.

View Source
const EventTypePageView = "page_view"

EventTypePageView is the only client-reported event type /track counts.

Variables

This section is empty.

Functions

This section is empty.

Types

type DayCount

type DayCount struct {
	Day   string `json:"day"`
	Views int64  `json:"views"`
}

DayCount is the pageviews of one day.

type Handlers

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

Handlers wires the public analytics route.

func NewHandlers

func NewHandlers(svc *Service) *Handlers

NewHandlers constructs the analytics handlers.

func (*Handlers) Track

func (h *Handlers) Track(w http.ResponseWriter, r *http.Request)

Track handles POST /api/v1/track. It counts a page_view (one per request) into the daily rollup's buffer and ignores every other type; nothing about the visitor is stored. Known crawlers are dropped (they are recorded by the agent capture instead).

type PostgresRepository

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

PostgresRepository is the Repository backed by hatamoto_analytics_daily.

func NewPostgresRepository

func NewPostgresRepository(db *database.DB) *PostgresRepository

NewPostgresRepository creates the Postgres-backed repository.

func (*PostgresRepository) Add

func (r *PostgresRepository) Add(ctx context.Context, day time.Time, kind, key string, n int64) error

Add implements Repository.

type Reader

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

Reader reads the daily rollup.

func NewReader

func NewReader(db *database.DB) *Reader

NewReader creates the Postgres-backed rollup reader.

func (*Reader) AIReferrals

func (r *Reader) AIReferrals(ctx context.Context, from, to time.Time) ([]ReferralDay, error)

AIReferrals returns visits per assistant per day from..to (inclusive, UTC dates), oldest day first. The rollup is per assistant and day only: it keeps no page path, by design (no visitor profiles).

func (*Reader) Pageviews

func (r *Reader) Pageviews(ctx context.Context, from, to time.Time) ([]DayCount, error)

Pageviews returns the pageviews per day from..to (inclusive, UTC dates), oldest day first. Days without a row are absent.

type ReferralDay

type ReferralDay struct {
	Day       string `json:"day"`
	Assistant string `json:"assistant"`
	Visits    int64  `json:"visits"`
}

ReferralDay is the visits one AI assistant sent on one day.

type Repository

type Repository interface {
	// Add increments the (day, kind, key) counter by n.
	Add(ctx context.Context, day time.Time, kind, key string, n int64) error
}

Repository adds to the daily rollup.

type Service

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

Service records pageviews and AI referrals. Counts are buffered in memory and written by Flush (every flushInterval once Start is called, and on Close), so a busy site does not hammer one hot rollup row per request.

func NewService

func NewService(repo Repository, logger *slog.Logger) *Service

NewService creates the recording service.

func (*Service) Close

func (s *Service) Close(ctx context.Context)

Close stops the periodic flush and writes what is still buffered.

func (*Service) Flush

func (s *Service) Flush(ctx context.Context) error

Flush writes the buffered counts. A failed write is put back so the next flush retries it.

func (*Service) RecordAIReferral

func (s *Service) RecordAIReferral(source string)

RecordAIReferral buffers one visit referred by the AI assistant source (chatgpt, claude, ...).

func (*Service) RecordPageViews

func (s *Service) RecordPageViews(n int64)

RecordPageViews buffers n pageviews for today.

func (*Service) Start

func (s *Service) Start()

Start runs the periodic flush until Close.

Jump to

Keyboard shortcuts

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