analyticsync

package
v0.110.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package analyticsync copies what each connected analytics tool reports into Postgres, one row per site, report, day and key, so people and agents can query history, compare tools for the same website and add numbers up without calling the providers each time.

A connection's first run stores the last 90 days of traffic and the last 7 days of top pages, sources, search queries and events. Later runs re-read the last few days, because providers keep revising recent numbers (Search Console lags by two to three days). Runs are spaced so each tool stays well inside its rate limits.

Index

Constants

View Source
const (
	JobSync = "analytics.sync"

	// MaxSites is how many of a login's sites sync without being switched
	// on one by one; the rest are listed but off.
	MaxSites = 20
)

Variables

This section is empty.

Functions

func AskedNow added in v0.110.0

func AskedNow(ctx context.Context) context.Context

AskedNow marks a sync someone asked for, rather than a scheduled one: it also re-reads what a scheduled run reads once a day (the sitemaps).

func BotToken

func BotToken(userAgent string) string

BotToken names the AI crawler a user agent belongs to, or "".

func Enqueue

func Enqueue(ctx context.Context, q jobs.Querier, ws, integrationID, key string) (int64, error)

Enqueue asks for a sync of one connection. Key de-duplicates; "" is a sync someone asked for now.

func Host

func Host(raw string) string

Host turns a site address into the domain that lines it up across tools: "https://www.Example.com/" and "sc-domain:example.com" are both example.com. It returns "" when the text is not a domain.

func HostOf

func HostOf(site connectors.AnalyticsSite) string

HostOf finds the domain of a tool's site from its address, id or name.

Types

type Config

type Config struct {
	Site   string       `json:"site"`
	Domain string       `json:"domain"`
	Sites  []SiteConfig `json:"sites"`
}

Config is the part of a connection's config about sites. Site is the default for live reports; Domain is its domain from before connections read several sites.

func ParseConfig

func ParseConfig(raw []byte) Config

ParseConfig reads the site settings from a connection's config.

type ConversionResult

type ConversionResult struct {
	Days int `json:"days"`
	Rows int `json:"rows"`
}

ConversionResult is what one site's conversions sync stored.

type Result

type Result struct {
	Rows  int `json:"rows"`
	Calls int `json:"calls"`
}

Result is what one run stored.

type SearchResult

type SearchResult struct {
	Days      int `json:"days"`
	Rows      int `json:"rows"`
	Inspected int `json:"inspected"`
	Sitemaps  int `json:"sitemaps"`
}

SearchResult is what one property's page by page run did.

type SiteConfig

type SiteConfig struct {
	ID               string   `json:"id"`
	Domain           string   `json:"domain,omitempty"`
	Off              bool     `json:"off,omitempty"`
	ConversionEvents []string `json:"conversion_events,omitempty"`
	// FilterTestAccounts leaves out the tool's own internal and test users
	// (PostHog's project filters).
	FilterTestAccounts bool `json:"filter_test_accounts,omitempty"`
}

SiteConfig is one site's settings on a connection: the domain that lines it up with other tools when the tool shows none, whether the sync leaves it out, and the events that count as conversions.

type SitePlan

type SitePlan struct {
	connectors.AnalyticsSite
	// Host is the domain the site lines up under; "" when unknown.
	Host string `json:"host"`
	// Domain is the domain someone set, overriding the tool's own.
	Domain string `json:"domain"`
	Sync   bool   `json:"sync"`
	// ConversionEvents are the events that count as a conversion when
	// visits are counted by landing page.
	ConversionEvents []string `json:"conversion_events"`
	// FilterTestAccounts: reports leave out the tool's internal and test
	// users.
	FilterTestAccounts bool `json:"filter_test_accounts"`
}

SitePlan is one site a connection can read, the domain it counts under, and whether the sync stores it.

func Plan

func Plan(cfg Config, sites []connectors.AnalyticsSite) []SitePlan

Plan lines up the sites a tool lists with the connection's settings. Every site syncs unless switched off, up to MaxSites; sites past that sync only when switched on.

type Syncer

type Syncer struct {
	Pool  *pgxpool.Pool
	Store *connectors.Store
	Log   *slog.Logger
	// Now is the clock; tests set it.
	Now func() time.Time
}

Syncer runs analytics syncs as jobs.

func (*Syncer) QueueDue added in v0.25.0

func (s *Syncer) QueueDue(ctx context.Context) error

QueueDue queues a sync of each connection that is due, or whose last run failed and is due a retry (retry.After).

func (*Syncer) Register

func (s *Syncer) Register(q *jobs.Queue)

Register adds the job handler.

func (*Syncer) Run

func (s *Syncer) Run(ctx context.Context)

Run schedules syncs until ctx ends.

func (*Syncer) SyncOnce

func (s *Syncer) SyncOnce(ctx context.Context, ws, integrationID string) (Result, error)

SyncOnce syncs every site of one connection that is switched on.

Jump to

Keyboard shortcuts

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