Skip to Content
Database

Pricing

Pricing experiments: read out which offering earns more, then roll out the winner.

Usage

import {
  analyticsEvents,
  experimentTables,
  monetisationTables,
} from "@dapplelight/signet-db/columns/sqlite"
import { createExperiments } from "@dapplelight/signet-db/experiments"
import { createOfferings } from "@dapplelight/signet-db/offerings"
import { createPricing } from "@dapplelight/signet-db/pricing"
import { db } from "./db"
import { plans } from "./plans"
import { audience, staffId, subjectId } from "./session"

const { experimentExposures } = experimentTables()
const tables = monetisationTables()
const { customerEvents, customerIdentities } = tables
const events = analyticsEvents()

const ex = createExperiments(db, { tables: experimentTables(), events })
const offerings = createOfferings(db, { tables, plans, experiments: ex })
const actor = { type: "user", id: staffId } as const

await ex.create(
  "annual-first",
  {
    variants: [
      { key: "control", weight: 50 },
      { key: "annual", weight: 50, value: { offering: "annual-first" } },
    ],
    goal: "subscribed",
  },
  { actor },
)
await offerings.setPlacement("onboarding", { offering: "default", experiment: "annual-first" }, { actor })
await ex.start("annual-first", { actor })
await offerings.current("onboarding", audience, subjectId) // sticky variant, exposure recorded

const pricing = createPricing(db, {
  offerings,
  experiments: ex,
  tables: { experimentExposures, customerEvents, customerIdentities },
})
const result = await pricing.readout("annual-first")
await pricing.rollout("annual-first", "annual", { actor })

Attribution needs no extra writes: a subject's purchases are their customer timeline events at or after exposure. Pass customerIdentities to follow anonymous ids through merges. The first variant is the control.

API

ExportWhat it does
createPricing(db, options){ readout, rollout }.
pricingReadout(variants, exposures, events, opts?)The readout as a pure function.
PricingOptions, PricingTables, Pricing, PricingReadout, PricingVariantReadout, PricingExposure, PricingEvent, PerCurrencyTypes.

createPricing(db, options)

OptionTypeDefault
tables.experimentExposurestablerequiredFrom experimentTables().
tables.customerEventstablerequiredFrom monetisationTables().
tables.customerIdentitiestablenoneFollow anonymous exposures to the merged account.
offeringscreateOfferings(...)requiredNeeds placements and setPlacement.
experimentscreateExperiments(...)requiredNeeds get and rollout.

readout(key, options)

Returns { experiment, status, winner, placements, alpha, control, variants }. alpha defaults to 0.05; maxPayments caps the LTV estimate (24).

Per variantMeaning
exposed, converted, rateSubjects who started a trial or bought after exposure.
lift, pValue, significantAgainst the control, by a two-proportion z-test. Null for the control.
trials, trialConverted, trialConversionTrial starts and how many paid after.
revenue, revenuePerExposedPer currency, in minor units. Charges minus refunds.
approximateSome revenue is App Store list price, not settled money.
ltvAn estimate (estimate: true): average charge times expected payments. Null with no charges.

rollout(key, variant, options)

The experiment gives everyone the variant and stops. Every placement running it serves that variant's offering directly, with the experiment unlinked. Safe to retry after a partial failure. Throws if no placement runs the experiment.

Examples

Decide on a result

import { pricing } from "./pricing"
import { actor } from "./session"

const { variants } = await pricing.readout("annual-first")
const best = variants.find((v) => v.significant && (v.lift ?? 0) > 0)
if (best) await pricing.rollout("annual-first", best.key, { actor })