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
- Variables
- func Conclude(ctx context.Context, tx pgx.Tx, ws string, e Experiment, r *Result) (bool, error)
- func ConcludedData(e Experiment, r *Result) map[string]any
- func Newest(ctx context.Context, q Querier, ws, siteHost string) (time.Time, error)
- func Pattern(p string) (*regexp.Regexp, error)
- func Window(ctx context.Context, q Querier, ws, siteHost, metric string, ...) (map[string]*PageValue, error)
- type Assignment
- type Experiment
- type Group
- type PageValue
- type Querier
- type Result
- type Service
Constants ¶
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.
const Cols = `` /* 452-byte string literal not displayed */
Cols and Scan read an experiment; From is the table with its page counts.
Variables ¶
var ErrNoSearchData = errors.New("no search data")
ErrNoSearchData means no Search Console property covers the site, or it has no pages that match.
var Metrics = []string{"clicks", "impressions", "ctr", "position"}
Metrics are the outcomes an experiment can measure.
Functions ¶
func Conclude ¶
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.
Types ¶
type Assignment ¶
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.
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.
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 ¶
OnDeploy starts the waiting experiments on a deploy's site, in the transaction that records it going live. Register it as a deploys hook.