/**
* "A/B tests": test a step or the design in two or more versions, set the shares and how long a
* test runs, see how the variants convert with a suggestion when one clearly wins, and pause, stop
* or end a test. The plugin ends a test after its days (ExperimentSchedule). Shown in the
* Settings workspace as the overview of all tests; a step is tested right where it is edited,
* with "Add a variation" and the variant tabs in its properties (StepVariants).
*/
import React, { useContext, useEffect, useState } from 'react';
import type { Experiment, FormDocument } from '@funnelforms/form-schema';
import type { ExperimentStats } from '../api';
import { nodeName } from '../document';
import { addDesignTest, addStepTest, addVariant, DESIGN_TOKENS, endTest, inRound, nextPlanned, nextRound, planLater, runAgain, runTime, setVariantStyle, shareOf, splitEvenly, stepTestRefusal, suggestWinner, testKind, testOf, testSteps, today, variantRefusal, waitsForDecision, type DesignToken } from '../experiments';
import type { Recipe } from '../store';
import { effectiveTokens } from '../themes';
import { useBuilder, useEnv, ViewContext } from './context';
import { __, sprintf } from './i18n';
import { Badge, Button, icons, Notice, NumberInput, Select, TextInput, Toggle } from './ui';
function useChange(): ( recipe: Recipe, key?: string ) => void {
return useEnv().store.getState().change;
}
function percent( value: number | null ): string {
return null === value ? '–' : `${ ( value * 100 ).toFixed( 1 ).replace( /\.0$/, '' ) } %`;
}
function variantName( experiment: Experiment, id: string ): string {
const v = experiment.variants.find( ( x ) => x.id === id );
return v?.label || id.toUpperCase();
}
const LETTER = ( experiment: Experiment ): string => String.fromCharCode( 65 + experiment.variants.length );
const dayName = ( day: string ): string => new Date( day + 'T12:00:00' ).toLocaleDateString( undefined, { day: 'numeric', month: 'long' } );
/** Running, paused, waiting for a decision after its days, or stopped. */
function TestStatus( { experiment }: { experiment: Experiment } ) {
if ( 'running' === experiment.status ) {
return
{ 'too_many' === refusal ? __( 'This form has the most tests it can have.', 'funnelforms-pro' ) : 'start_taken' === refusal ? __( 'Another test already tries a different first step.', 'funnelforms-pro' ) : __( 'A/B test: variant B starts as a copy of this step. Visitors see A or B, and the figures show which one brings more entries.', 'funnelforms-pro' ) }
{ steps[ 0 ]?.node === nodeId ? __( 'You are editing the original. Paths, answers to the questions and calculations apply to every variant.', 'funnelforms-pro' ) : sprintf( __( 'You are editing %s. Change its texts, answers or images; paths and calculations stay those of the original.', 'funnelforms-pro' ), variantName( experiment, variant ) ) }
{ 0 === experiments.length ? __( 'Show visitors different versions of a step or of the design and see which one brings more entries. To test a step, select it on the canvas and click “Add a variation”.', 'funnelforms-pro' ) : __( 'All tests of this form. A step’s variants are also edited right at the step, with the tabs A, B and +.', 'funnelforms-pro' ) }
{ __( 'Lost an earlier round and gets no views.', 'funnelforms-pro' ) }
: null } { i > 0 && plannable && ! v.retired ? ({ 0 === i ? __( 'Shows the form’s design.', 'funnelforms-pro' ) : null }
) : (
{ __( 'Shows:', 'funnelforms-pro' ) }{ ' ' }
{ nodes.map( ( id, n ) => {
const node = doc.nodes.find( ( x ) => x.id === id );
return (
{ sprintf( __( 'Test plan: when this round ends, the variant kept meets %s, one round each.', 'funnelforms-pro' ), planned.map( ( v ) => variantName( experiment, v.id ) ).join( ', ' ) ) }
) : null } { 'running' === experiment.status && 0 === total ?| { __( 'Variant', 'funnelforms-pro' ) } | { __( 'Views', 'funnelforms-pro' ) } | { __( 'Entries', 'funnelforms-pro' ) } | { __( 'Conversion', 'funnelforms-pro' ) } | { __( 'Uplift', 'funnelforms-pro' ) } | { __( 'Chance to win', 'funnelforms-pro' ) } |
|---|---|---|---|---|---|
| { variantName( experiment, v.id ) } | { v.views } | { v.completions } | { percent( v.conversionRate ) } | { null === v.uplift ? '–' : `${ v.uplift > 0 ? '+' : '' }${ percent( v.uplift ) }` } | { percent( v.chanceToBeatControl ) } |
{ rights.entries ? __( 'Figures appear here once the published form has views.', 'funnelforms-pro' ) : __( 'The figures need the right to see entries.', 'funnelforms-pro' ) }
) }{ __( 'Changes to the test take effect when you publish. Figures are of the last 30 days; the first variant is the comparison.', 'funnelforms-pro' ) }
{ 'stopped' !== experiment.status ? ({ sprintf( __( 'Stopped: everyone sees %s.', 'funnelforms-pro' ), variantName( experiment, experiment.winner ?? experiment.variants[ 0 ]?.id ?? '' ) ) } { experiment.outcome && experiment.endedAt ? ' ' + sprintf( __( 'The test ended on its own on %1$s: %2$s', 'funnelforms-pro' ), dayName( experiment.endedAt ), outcomeText( experiment ) ) : null }
{ design ? __( 'Ending makes the kept design the form’s design.', 'funnelforms-pro' ) : __( 'Ending removes the paths to the other variants; their steps stay on the canvas until you delete them.', 'funnelforms-pro' ) }
> ) } > ); } type SetTest = ( recipe: ( x: Experiment ) => void, key?: string ) => void; const DURATIONS = [ 7, 10, 14, 30 ]; /** What the figures said when the test ended on its own. */ function outcomeText( experiment: Experiment ): string { if ( 'winner' === experiment.outcome && experiment.suggested ) { return sprintf( __( '%s brought the most entries.', 'funnelforms-pro' ), variantName( experiment, experiment.suggested ) ); } return 'tie' === experiment.outcome ? __( 'No variant was clearly better.', 'funnelforms-pro' ) : __( 'There were too few views for a clear result, also after the test ran longer.', 'funnelforms-pro' ); } /** How long a test runs, what happens at its end, where it stands, and pausing it. */ function RunTimeControls( { doc, experiment, readOnly, set }: { doc: FormDocument; experiment: Experiment; readOnly: boolean; set: SetTest } ) { const time = runTime( experiment, today() ); const days = [ ...new Set( [ ...DURATIONS, ...( experiment.days ? [ experiment.days ] : [] ) ] ) ].sort( ( a, b ) => a - b ); const first = variantName( experiment, experiment.variants[ 0 ]?.id ?? 'a' ); let where = ''; if ( 'paused' === experiment.status ) { where = waitsForDecision( experiment ) ? '' : sprintf( __( 'Paused: everyone sees %s. The days paused do not count.', 'funnelforms-pro' ), first ); } else if ( 'running' === experiment.status ) { if ( ! experiment.days ) { where = __( 'Runs until you stop it.', 'funnelforms-pro' ); } else if ( ! time ) { where = sprintf( __( 'Runs %d days from the day you publish it.', 'funnelforms-pro' ), experiment.days ); } else { where = sprintf( __( 'Day %1$d of %2$d, the last day is %3$s.', 'funnelforms-pro' ), time.day, time.of, dayName( time.last ) ); } } return ( <>{ 'ask' === experiment.onEnd ? sprintf( __( 'At the end the test pauses, everyone sees %s, and you get a mail to decide.', 'funnelforms-pro' ), first ) : nextPlanned( experiment ) ? __( 'At the end the variant that brought the most entries, or the original when none was clearly better, meets the next variant of the test plan, and the days start anew. You get a mail.', 'funnelforms-pro' ) : __( 'At the end everyone sees the variant that brought the most entries, or the original when none was clearly better. You get a mail. With too few views the test runs half its days longer, once.', 'funnelforms-pro' ) }
{ doc.settings.valueCalculation || 'value' === experiment.goal ? ({ where }
: null } { 'running' === experiment.status || ( 'paused' === experiment.status && ! waitsForDecision( experiment ) ) ? ({ outcomeText( experiment ) } { sprintf( __( 'Until you decide, everyone sees %s.', 'funnelforms-pro' ), variantName( experiment, first ) ) }
{ __( 'Takes effect when you publish.', 'funnelforms-pro' ) }
{ sprintf( __( 'Too early for a winner: every variant needs about %d more views.', 'funnelforms-pro' ), suggestion.views ) }
; } const name = variantName( experiment, suggestion.variant ); const text = 'winner' === suggestion.kind ? null === suggestion.uplift ? sprintf( __( '%1$s brings more entries than the comparison, with %2$s certainty.', 'funnelforms-pro' ), name, percent( suggestion.chance ) ) : sprintf( __( '%1$s brings %2$s more entries than the comparison, with %3$s certainty.', 'funnelforms-pro' ), name, percent( suggestion.uplift ), percent( suggestion.chance ) ) : sprintf( __( 'No variant beats %1$s; it stays ahead with %2$s certainty.', 'funnelforms-pro' ), name, percent( suggestion.chance ) ); return (