holdout

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0 Imports: 23 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 holdout is the holdout group (M63 G-33): while a period is open, a random share of the workspace's contacts gets no campaigns and/or no automation messages, and a nightly measurement compares what they did with everyone else.

Who is held out is decided by one SQL function, rc_holdout_bucket(contact, seed) < basis_points (migration 03540): the sending engine, Send Readiness, automations, text, push and WhatsApp campaigns, the segment field and the measurement all ask it, so they never disagree. Raising the share keeps everyone already held out; "Start over" draws a new seed. Transactional and system mail are never held out.

Index

Constants

View Source
const (
	MinBasisPoints = 10   // 0.1%
	MaxBasisPoints = 2000 // 20%
)

Limits of the share, in basis points (hundredths of a percent).

View Source
const (
	SpanPeriod = "period"
	Span30     = "30d"
	Span90     = "90d"
)

Spans of a measurement.

View Source
const (
	// MinHoldout is how many contacts the holdout group needs before its
	// results mean anything.
	MinHoldout = 1000
	// TooEarly is how long a period must have run.
	TooEarly = 14 * 24 * time.Hour
	// Confidence is what a difference needs to count (two-sided).
	Confidence = 0.95
	// MeasureEvery is how often a person may ask for a recompute.
	MeasureEvery = 10 * time.Minute
)

Thresholds of the verdict.

View Source
const (
	MetricConversion = "conversion_rate"
	MetricRevenue    = "revenue_per_contact"
	MetricOrders     = "orders_per_contact"
	// MetricVisitors is the share of a group seen on a tracked site (M62),
	// only when the workspace tracks one.
	MetricVisitors = "site_visitors"
)

Metric keys, in the order the results show them. A metric is computed from the stored sums when the results are read.

View Source
const (
	VerdictPending      = "pending"       // not measured yet
	VerdictTooFew       = "too_few"       // fewer than MinHoldout in the holdout group
	VerdictTooEarly     = "too_early"     // the open period is younger than TooEarly
	VerdictAdded        = "added"         // marketing brought more, significantly
	VerdictLost         = "lost"          // the holdout group brought more, significantly
	VerdictNoDifference = "no_difference" // not significant
)

Verdicts of a span, on revenue per contact.

View Source
const MeasureHour = 4

MeasureHour is the workspace's local hour of the nightly measurement.

Variables

Spans lists every span.

Functions

func End

func End(p store.HoldoutPeriod, now time.Time) time.Time

End is when a period ends for its measurement: its end, else now.

func Ended

func Ended(p store.HoldoutPeriod) bool

Ended reports whether a period has ended.

func FromAutomations

func FromAutomations(ctx context.Context, q *store.Queries, workspaceID, contactID uuid.UUID) (bool, error)

FromAutomations reports whether the open period holds the contact out of automation messages.

func HeldFromCampaigns

func HeldFromCampaigns(ctx context.Context, q *store.Queries, workspaceID uuid.UUID, contacts []uuid.UUID) (map[uuid.UUID]bool, error)

HeldFromCampaigns lists which of the contacts the open period holds out of campaigns (text, push and WhatsApp campaigns skip them). Nil when nobody is.

func Open

func Open(ctx context.Context, q *store.Queries, workspaceID uuid.UUID) (*store.HoldoutPeriod, error)

Open is the workspace's open period; nil when the holdout group is off.

func Percent

func Percent(bp int32) float64

Percent is a share in basis points as a percentage (500 → 5).

func Register

func Register(h huma.API, s *Service)

Register adds Audience › Holdout's results (reports:view).

Types

type Group

type Group struct {
	Contacts   int64 `json:"contacts"`
	Converters int64 `json:"converters"`
	// Visitors were seen on a tracked site (Measured.Tracking).
	Visitors  int64   `json:"visitors"`
	Orders    float64 `json:"orders"`
	OrdersSq  float64 `json:"orders_sq"`
	Revenue   float64 `json:"revenue"`
	RevenueSq float64 `json:"revenue_sq"`
}

Group is what one group did in a span: sums over its contacts, with the sums of squares that their variances need. Revenue is in minor units of Measured.Currency.

type HoldoutGroupResult

type HoldoutGroupResult struct {
	Contacts   int64 `json:"contacts"`
	Converters int64 `json:"converters"`
	// Visitors were seen on a tracked site (HoldoutResults.Tracking).
	Visitors     int64 `json:"visitors"`
	Orders       int64 `json:"orders"`
	RevenueCents int64 `json:"revenueCents"`
}

HoldoutGroupResult is what one group did: its contacts, how many of them converted, their orders and revenue (minor units of the main currency).

type HoldoutMetric

type HoldoutMetric struct {
	Key         string  `json:"key" enum:"conversion_rate,revenue_per_contact,orders_per_contact,site_visitors"`
	Holdout     float64 `json:"holdout"`
	Marketing   float64 `json:"marketing"`
	Lift        float64 `json:"lift"`
	Confidence  float64 `json:"confidence"`
	Tested      bool    `json:"tested"`
	Significant bool    `json:"significant"`
}

HoldoutMetric compares the groups on one measure. Values are in the metric's unit: conversion_rate and site_visitors in percent, revenue_per_contact in minor units of the main currency, orders_per_contact in orders. site_visitors is there only when the workspace tracks a site (Website tracking). Lift is the marketing group's over the holdout group's, in percent; Confidence is 1 − p (two-sided); Tested is false when there was nothing to test.

type HoldoutOtherCurrency

type HoldoutOtherCurrency struct {
	Currency       string `json:"currency"`
	HoldoutCents   int64  `json:"holdoutCents"`
	MarketingCents int64  `json:"marketingCents"`
}

HoldoutOtherCurrency is what was recorded in another currency: named apart, never converted.

type HoldoutPeriodItem

type HoldoutPeriodItem struct {
	ID          string     `json:"id"`
	StartedAt   time.Time  `json:"startedAt"`
	EndedAt     *time.Time `json:"endedAt,omitempty"`
	BasisPoints int        `json:"basisPoints"`
	Percent     float64    `json:"percent"`
	Campaigns   bool       `json:"campaigns"`
	Automations bool       `json:"automations"`
	Open        bool       `json:"open"`
}

HoldoutPeriodItem is one period of the holdout group: its share, scope and when it ran.

type HoldoutResults

type HoldoutResults struct {
	// Periods are the open period and the last closed one, newest first;
	// empty when the workspace never had a holdout group.
	Periods []HoldoutPeriodItem `json:"periods"`
	Period  *HoldoutPeriodItem  `json:"period,omitempty"`
	Window  string              `json:"window" enum:"period,30d,90d"`
	// From and To are the span's dates (omitted until measured).
	From *time.Time `json:"from,omitempty"`
	To   *time.Time `json:"to,omitempty"`
	// Verdict: none (no holdout group ever), pending (not measured yet),
	// too_few, too_early, added, lost or no_difference.
	Verdict    string     `json:"verdict" enum:"none,pending,too_few,too_early,added,lost,no_difference"`
	ComputedAt *time.Time `json:"computedAt,omitempty"`
	// Computing is true while a requested recompute hasn't finished;
	// NextMeasureAt is when Recompute is allowed again.
	Computing       bool                   `json:"computing"`
	NextMeasureAt   *time.Time             `json:"nextMeasureAt,omitempty"`
	Holdout         HoldoutGroupResult     `json:"holdout"`
	Marketing       HoldoutGroupResult     `json:"marketing"`
	Metrics         []HoldoutMetric        `json:"metrics"`
	Currency        string                 `json:"currency"`
	OtherCurrencies []HoldoutOtherCurrency `json:"otherCurrencies"`
	// CampaignsIncluded counts the email campaigns of the span that went
	// to the holdout group too.
	CampaignsIncluded int64 `json:"campaignsIncluded"`
	MinHoldout        int64 `json:"minHoldout"`
	// Tracking: the workspace tracked a site when this span was measured,
	// so the groups count site visitors and the metrics include them.
	Tracking bool `json:"tracking"`
}

HoldoutResults is Audience › Holdout: the period and span shown, its verdict on revenue per contact, the groups, the metrics, and whether a recompute is running or allowed.

type Inserter

type Inserter interface {
	InsertTx(ctx context.Context, tx pgx.Tx, args river.JobArgs, opts *river.InsertOpts) (*rivertype.JobInsertResult, error)
}

Inserter queues jobs in a transaction (the River client).

type MeasureArgs

type MeasureArgs struct {
	WorkspaceID uuid.UUID `json:"workspace_id"`
}

MeasureArgs measures one workspace's holdout group (holdout.measure).

func (MeasureArgs) InsertOpts

func (MeasureArgs) InsertOpts() river.InsertOpts

InsertOpts implements river.JobArgsWithInsertOpts: one per workspace in any 10 minutes.

func (MeasureArgs) Kind

func (MeasureArgs) Kind() string

Kind implements river.JobArgs.

type MeasureWorker

type MeasureWorker struct {
	river.WorkerDefaults[MeasureArgs]
	S *Service
}

MeasureWorker runs MeasureArgs.

func (*MeasureWorker) Timeout

Timeout implements river.Worker: a large workspace takes a while.

func (*MeasureWorker) Work

func (w *MeasureWorker) Work(ctx context.Context, job *river.Job[MeasureArgs]) error

Work implements river.Worker.

type Measured

type Measured struct {
	From              time.Time       `json:"from"`
	To                time.Time       `json:"to"`
	Currency          string          `json:"currency"`
	Holdout           Group           `json:"holdout"`
	Marketing         Group           `json:"marketing"`
	OtherCurrencies   []OtherCurrency `json:"other_currencies"`
	CampaignsIncluded int64           `json:"campaigns_included"`
	// Tracking: the workspace tracked a site (M62) when this was measured,
	// so the groups count site visitors.
	Tracking bool `json:"tracking,omitempty"`
}

Measured is holdout_results.data: one span of one period.

func (Measured) Metrics

func (m Measured) Metrics() []Metric

Metrics compares the groups of a span: conversion rate (a two-proportion z-test), revenue and orders per contact (Welch's t-test), and, when the workspace tracks a site, the share of site visitors (z-test).

func (Measured) Verdict

func (m Measured) Verdict(p store.HoldoutPeriod) string

Verdict says what a span's results show.

type Metric

type Metric struct {
	Key         string
	Holdout     float64
	Marketing   float64
	Lift        float64
	Confidence  float64
	Tested      bool
	Significant bool
}

Metric compares the two groups on one measure: the holdout group's value and the marketing group's (conversion rate in percent, revenue per contact in minor units, orders per contact), the marketing group's lift over the holdout's in percent, and the test's confidence (1 − p, two-sided). Tested is false when the test can't run or the holdout's value is 0 (no lift to express).

type OtherCurrency

type OtherCurrency struct {
	Currency       string `json:"currency"`
	HoldoutCents   int64  `json:"holdout_cents"`
	MarketingCents int64  `json:"marketing_cents"`
}

OtherCurrency is what a span recorded in a currency other than the main one, per group: named apart, never converted or added.

type ScheduleArgs

type ScheduleArgs struct{}

ScheduleArgs is the hourly tick that queues the nightly measurements (holdout.schedule).

func (ScheduleArgs) InsertOpts

func (ScheduleArgs) InsertOpts() river.InsertOpts

InsertOpts implements river.JobArgsWithInsertOpts.

func (ScheduleArgs) Kind

func (ScheduleArgs) Kind() string

Kind implements river.JobArgs.

type ScheduleWorker

type ScheduleWorker struct {
	river.WorkerDefaults[ScheduleArgs]
	S *Service
}

ScheduleWorker runs ScheduleArgs.

func (*ScheduleWorker) Work

Work implements river.Worker.

type Service

type Service struct {
	Pool *pgxpool.Pool
	Q    *store.Queries
	Jobs Inserter
	Log  *slog.Logger
	Now  func() time.Time
}

Service measures holdout periods and reads the results.

func (*Service) Measure

func (s *Service) Measure(ctx context.Context, workspaceID uuid.UUID) error

Measure computes and stores the results of the workspace's open period and its last closed one, every span.

func (*Service) RequestMeasure

func (s *Service) RequestMeasure(ctx context.Context, ws *store.Workspace) (HoldoutResults, error)

RequestMeasure queues a recompute of the workspace's results, at most once every MeasureEvery.

func (*Service) Results

func (s *Service) Results(ctx context.Context, ws *store.Workspace, periodID *uuid.UUID, span string) (HoldoutResults, error)

Results reads a period's span (the newest period when periodID is nil).

func (*Service) ScheduleMeasures

func (s *Service) ScheduleMeasures(ctx context.Context) (int, error)

ScheduleMeasures queues the measurements that are due; it returns how many.

type Share

type Share struct {
	Seed        int64 `json:"seed"`
	BasisPoints int32 `json:"basis_points"`
}

Share is who an audience query holds out: contacts whose bucket for Seed is under BasisPoints. The zero Share holds out nobody.

func ForCampaigns

func ForCampaigns(ctx context.Context, q *store.Queries, workspaceID uuid.UUID) (Share, error)

ForCampaigns is the share the open period holds out of campaigns; the zero Share when there is none or it holds out automations only.

func (Share) Held

func (s Share) Held(ctx context.Context, q *store.Queries, contacts []uuid.UUID) (map[uuid.UUID]bool, error)

Held lists which of the contacts the share holds out.

func (Share) On

func (s Share) On() bool

On reports whether the share holds anyone out.

func (Share) Percent

func (s Share) Percent() float64

Percent is the share as a percentage.

Jump to

Keyboard shortcuts

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