Features
Experiments
Show two or more versions of something to different visitors, and let Beacon tell you which works better. Nothing about who got what is stored: each visitor's variant is worked out from a hash.
Set one up
- In Experiments, choose New experiment. Give it a name and a key (used in your code), and list its variants. The first is the control, usually what your site does today. Weights set how visitors are shared between them.
- Pick one or more goals, the events that count as success: someone signing up (conversion), how many times they do something (count), or the total of a number such as
amounton a purchase (sum). The first goal is the one the verdict is about. - Add the code below to your site, then choose Start experiment. Until then everyone gets
null.
In your code
const variant = await beacon('variant', 'new-pricing');
if (variant === 'yearly-first') {
showYearlyPricesFirst();
} else {
// 'control', or null: not in the experiment, or it's stopped
showMonthlyPricesFirst();
}beacon('variant', key) returns a promise of the variant's key, or null when the visitor isn't in the experiment, it isn't running, or there's nothing to split by. Always treat null as your normal version. The first time it runs on a visit it records a $exposure event, which is how results count who actually saw the experiment: ask for the variant where the change happens, not on every page.
function Pricing() {
const [variant, setVariant] = useState<string | null | undefined>(undefined);
useEffect(() => {
beacon('variant', 'new-pricing').then(setVariant);
}, []);
if (variant === undefined) return <PricingSkeleton />; // a moment, on the first visit
return variant === 'yearly-first' ? <YearlyFirst /> : <MonthlyFirst />;
}A callback works too, if you prefer:
beacon('variant', 'new-pricing', function (variant) {
if (variant === 'yearly-first') showYearlyPricesFirst();
});Who gets which variant
- Each visitor (the default): every browser gets its own variant, and keeps it.
- Each signed-in user: the same variant on every device, for visitors your site has identified. Everyone else gets
null. Use it on pages where people are already signed in, or someone who signs in partway could see the variant change. - A property, such as
account: everyone with the same value, a whole company, say, sees the same thing. Send it with beacon('set', { account: … }) before asking for the variant.
In the experiment sets how many are included: at 20%, four in five visitors get null and aren't counted. Raising it later adds people without moving anyone already in it to another variant.
Results
Each experiment's page leads with a verdict in plain words for each variant against the control, for example: "yearly-first beats control: +8% (likely between +2% and +14%)", or "No clear winner yet… about 1,400 more visitors per variant". Below it, a table per goal. Choose what to show with Show:
- Rate or mean per variant, and the change against the control.
- Likely range: the interval the true change most likely falls in.
- p-value and confidence (1 − p): how surprising the difference would be if the variants were really the same.
- Chance to beat control: the probability the variant is better, given what's been seen.
Statistics sets the method. Safe to check any time (the default) uses a sequential test, so looking at results every day never turns a fluke into a "win". Classic is the usual fixed-sample z-test: it reaches a verdict sooner, but it's only valid if you decided up front how many visitors to wait for and didn't act on earlier looks. You can also pick a 90%, 95% or 99% confidence level, and limit results to a cohort to see who a change helped.
Results count each visitor from the first time they saw the experiment, and leave out anyone who saw more than one variant. They're per visitor (browser) whatever the split.
Stopping
Stop sends everyone back to null within a couple of minutes, and keeps the results. Resume picks up where it left off. Once an experiment has started, its key, variants and split are fixed; to test something else, create a new one. To ship the winner, change your code, or turn it into a feature flag.
On your server
Your server can work out the same variant, so pages render the right version with no flicker. Hash "<salt>:<unit>" with MurmurHash3 into a bucket from 0 to 9,999: under rollout × 100 means in the experiment. Then "<salt>:v:<unit>" picks the variant by weight. The salt is on the experiment's page in the dashboard; the unit is the visitor ID (Beacon's _bk cookie), your user ID, or the property's value.
<?php
// The same answer the Beacon tag gives, on your server (PHP 8.1+).
function beacon_bucket(string $key): int {
return hexdec(hash('murmur3a', $key)) % 10000;
}
function beacon_variant(string $salt, int $rollout, array $variants, string $unit): ?string {
if ($unit === '' || beacon_bucket("$salt:$unit") >= $rollout * 100) {
return null; // not in the experiment
}
$total = array_sum($variants); // ['control' => 50, 'yearly-first' => 50]
$bucket = beacon_bucket("$salt:v:$unit");
$acc = 0;
foreach ($variants as $key => $weight) {
$acc += $weight;
if ($bucket < $acc / $total * 10000) {
return $key;
}
}
return null;
}
// The visitor ID is in Beacon's first-party cookie, _bk.
$variant = beacon_variant('SALT', 100, ['control' => 50, 'yearly-first' => 50], $_COOKIE['_bk'] ?? '');// MurmurHash3 (x86, 32-bit), the hash the Beacon tag uses.
function murmur(str) {
const b = new TextEncoder().encode(str);
let h = 0, k, i = 0;
for (; i + 4 <= b.length; i += 4) {
k = b[i] | (b[i + 1] << 8) | (b[i + 2] << 16) | (b[i + 3] << 24);
k = Math.imul(k, 0xcc9e2d51); k = (k << 15) | (k >>> 17); k = Math.imul(k, 0x1b873593);
h ^= k; h = (h << 13) | (h >>> 19); h = (Math.imul(h, 5) + 0xe6546b64) | 0;
}
k = 0;
switch (b.length & 3) {
case 3: k ^= b[i + 2] << 16;
case 2: k ^= b[i + 1] << 8;
case 1: k ^= b[i];
k = Math.imul(k, 0xcc9e2d51); k = (k << 15) | (k >>> 17); k = Math.imul(k, 0x1b873593); h ^= k;
}
h ^= b.length; h ^= h >>> 16; h = Math.imul(h, 0x85ebca6b); h ^= h >>> 13; h = Math.imul(h, 0xc2b2ae35); h ^= h >>> 16;
return h >>> 0;
}
function beaconVariant(salt, rollout, variants, unit) { // variants: [['control', 50], ['yearly-first', 50]]
if (!unit || murmur(`${salt}:${unit}`) % 10000 >= rollout * 100) return null;
const total = variants.reduce((a, [, w]) => a + w, 0);
const bucket = murmur(`${salt}:v:${unit}`) % 10000;
let acc = 0;
for (const [key, weight] of variants) {
acc += weight;
if (bucket < (acc / total) * 10000) return key;
}
return null;
}When your server picks the variant, still call beacon('variant', key) on the page where it shows, so the exposure is counted.