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 ¶
const ( KindPageView = "pageview" KindAIReferral = "ai_referral" )
Rollup kinds stored in hatamoto_analytics_daily.kind.
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 Handlers ¶
type Handlers struct {
// contains filtered or unexported fields
}
Handlers wires the public analytics route.
func NewHandlers ¶
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.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads the daily rollup.
func (*Reader) AIReferrals ¶
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).
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) Flush ¶
Flush writes the buffered counts. A failed write is put back so the next flush retries it.
func (*Service) RecordAIReferral ¶
RecordAIReferral buffers one visit referred by the AI assistant source (chatgpt, claude, ...).
func (*Service) RecordPageViews ¶
RecordPageViews buffers n pageviews for today.