experiments

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: 20 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 experiments measures whether a change to a website's pages helped. A before and after experiment compares the pages with themselves. A holdout experiment splits the pages into matched groups: the change goes only to the treatment group, and the control group shows what would have happened anyway, so a Google update or a seasonal dip isn't mistaken for the change. The measurement starts when the change goes live, from a production deploy of the site (package deploys) or a person saying when, and outcomes come from the workspace's own Search Console data (search_daily).

Index

Constants

View Source
const (
	BeforeAfter = "before_after"
	Holdout     = "holdout"

	Treatment = "treatment"
	Control   = "control"

	Draft     = "draft"
	Waiting   = "waiting"
	Measuring = "measuring"
	Done      = "done"
	Stopped   = "stopped"

	// MaxPages is the most pages an experiment takes, the busiest first.
	MaxPages = 1000
)

Designs, metrics, groups and statuses.

View Source
const Cols = `` /* 452-byte string literal not displayed */

Cols and Scan read an experiment; From is the table with its page counts.

Variables

View Source
var ErrNoSearchData = errors.New("no search data")

ErrNoSearchData means no Search Console property covers the site, or it has no pages that match.

View Source
var Metrics = []string{"clicks", "impressions", "ctr", "position"}

Metrics are the outcomes an experiment can measure.

Functions

func Conclude

func Conclude(ctx context.Context, tx pgx.Tx, ws string, e Experiment, r *Result) (bool, error)

Conclude keeps an experiment's final result, in tx. ok is false when it had ended already. The caller records experiment.concluded with ConcludedData.

func ConcludedData

func ConcludedData(e Experiment, r *Result) map[string]any

ConcludedData is experiment.concluded's data.

func Newest

func Newest(ctx context.Context, q Querier, ws, siteHost string) (time.Time, error)

Newest is the newest day Search Console has sent for a site, or zero.

func Pattern

func Pattern(p string) (*regexp.Regexp, error)

Pattern checks a page pattern: empty, or a path starting with / where * stands for anything (/blog/*).

func Window

func Window(ctx context.Context, q Querier, ws, siteHost, metric string, from, to time.Time, only []string) (map[string]*PageValue, error)

Window reads each page's metric from Search Console for [from, to), for the pages of a site's hosts (or only those listed).

Types

type Assignment

type Assignment struct {
	URL      string
	Group    string
	Baseline float64
}

Assignment is one page and its group.

func Assign

func Assign(ctx context.Context, q Querier, ws, siteHost string, e Experiment, seed string) ([]Assignment, error)

Assign picks a site's pages that match the pattern over the baseline days that end on Search Console's newest day, the busiest MaxPages first, and splits them. A holdout ranks them by baseline and keeps every page whose turn falls on the control share, starting at an offset the seed picks, so the groups are matched.

type Experiment

type Experiment struct {
	ID             string     `json:"id"`
	SiteID         string     `json:"site_id"`
	BrandID        *string    `json:"brand_id"`
	Name           string     `json:"name"`
	Hypothesis     string     `json:"hypothesis"`
	Design         string     `json:"design"`
	Pattern        string     `json:"pattern"`
	Metric         string     `json:"metric"`
	HoldoutPercent int        `json:"holdout_percent"`
	BaselineDays   int        `json:"baseline_days"`
	MeasureDays    int        `json:"measure_days"`
	StartOn        string     `json:"start_on"`
	Status         string     `json:"status"`
	ChangedAt      *time.Time `json:"changed_at"`
	DeployID       *string    `json:"deploy_id"`
	ConcludedAt    *time.Time `json:"concluded_at"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
	// The pages in each group.
	TreatmentPages int `json:"treatment_pages"`
	ControlPages   int `json:"control_pages"`
	// The result so far while measuring, or the final one.
	Result *Result `json:"result"`
}

Experiment is one experiment as kept.

func Get

func Get(ctx context.Context, q Querier, ws, id string) (Experiment, error)

Get loads one experiment of a workspace.

func Scan

func Scan(row pgx.Row) (Experiment, error)

type Group

type Group struct {
	Pages int `json:"pages"`
	// The metric per page (daily for clicks and impressions) before and
	// after the change, and the change as a share (0.12 is 12% more).
	// Position falls as it gets better.
	Before float64  `json:"before"`
	After  float64  `json:"after"`
	Change *float64 `json:"change"`
}

Group is one group's outcome.

type PageValue

type PageValue struct {
	URL                 string
	Clicks, Impressions float64

	Value float64
	// contains filtered or unexported fields
}

PageValue is one page's metric over a window.

type Querier

type Querier interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

Querier is a pool or a transaction.

type Result

type Result struct {
	// The days compared: the baseline before the change and the days
	// measured after it, so far.
	BaselineFrom string `json:"baseline_from"`
	MeasureFrom  string `json:"measure_from"`
	MeasureTo    string `json:"measure_to"`
	DaysMeasured int    `json:"days_measured"`
	Treatment    Group  `json:"treatment"`
	// Null for a before and after experiment.
	Control *Group `json:"control"`
	// For a holdout, how much better the treatment did than the control
	// (0.08 is 8% better); for before and after, the treatment's change.
	// For position, a positive lift means it ranks higher.
	Lift *float64 `json:"lift"`
	// The chance the difference is noise, from the pages' changes; null
	// when there aren't enough pages to tell.
	PValue *float64 `json:"p_value"`
	// better, worse, no_clear_change, or too_early while it measures.
	Verdict string `json:"verdict"`
	Final   bool   `json:"final"`
}

Result is what an experiment found, so far or in the end.

func Measure

func Measure(ctx context.Context, q Querier, ws, siteHost string, e Experiment) (*Result, error)

Measure works out an experiment's result from Search Console, or nil until it has a change time.

type Service

type Service struct {
	Pool *pgxpool.Pool
	Log  *slog.Logger
	// Now is the clock; nil is time.Now.
	Now func() time.Time
}

Service starts experiments on deploys and ends them when their days are measured.

func (*Service) OnDeploy

func (s *Service) OnDeploy(ctx context.Context, tx pgx.Tx, d deploys.Deploy) error

OnDeploy starts the waiting experiments on a deploy's site, in the transaction that records it going live. Register it as a deploys hook.

func (*Service) Run

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

Run ends experiments until ctx ends, checking every hour.

func (*Service) Tick

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

Tick ends every measuring experiment whose days Search Console has now sent: the result is kept and experiment.concluded recorded.

Jump to

Keyboard shortcuts

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