/** * "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 { __( 'Running', 'funnelforms-pro' ) }; } if ( waitsForDecision( experiment ) ) { return { __( 'Waits for you', 'funnelforms-pro' ) }; } return 'paused' === experiment.status ? { __( 'Paused', 'funnelforms-pro' ) } : { __( 'Stopped', 'funnelforms-pro' ) }; } /** Opens the A/B tests of the Settings workspace, with one test unfolded. */ const OPEN_EVENT = 'ffxb:open-experiment'; function useOpenTest(): ( id: string ) => void { const view = useContext( ViewContext ); return ( id: string ): void => { view( 'settings' ); window.setTimeout( () => { window.dispatchEvent( new CustomEvent( OPEN_EVENT, { detail: id } ) ); document.getElementById( 'ffxb-part-experiments' )?.scrollIntoView?.( { behavior: 'smooth', block: 'start' } ); }, 50 ); }; } /** * The top of a step's properties: "Add a variation" when the step is in no test; tabs of the * variants (A, B, …) when it is, each selecting its step, with "+" for one more variant. */ export function StepVariants( { doc, nodeId }: { doc: FormDocument; nodeId: string } ) { const { store, readOnly, ctx } = useEnv(); const change = useChange(); const openTest = useOpenTest(); const test = testOf( doc, nodeId ); if ( ! test ) { const refusal = stepTestRefusal( doc, nodeId ); if ( 'not_a_step' === refusal ) { return null; } const node = doc.nodes.find( ( n ) => n.id === nodeId ); const add = (): void => { let copy = ''; change( ( d ) => { copy = addStepTest( d, nodeId, { name: sprintf( __( 'Test of “%s”', 'funnelforms-pro' ), node ? nodeName( node, ctx.locale ) : nodeId ), a: __( 'Original', 'funnelforms-pro' ), b: __( 'Variant B', 'funnelforms-pro' ), } )?.copy ?? ''; } ); if ( copy ) { store.getState().select( { node: copy } ); } }; 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' ) }

); } const { experiment, variant, steps } = test; const refusal = variantRefusal( doc, experiment.id ); const more = (): void => { let added: string | null = null; change( ( d ) => { added = addVariant( d, experiment.id, sprintf( __( 'Variant %s', 'funnelforms-pro' ), LETTER( experiment ) ) ); } ); const next = added ? testOf( store.getState().doc ?? doc, nodeId )?.steps.find( ( s ) => s.variant === added ) : undefined; if ( next ) { store.getState().select( { node: next.node } ); } }; return (
{ __( 'A/B test', 'funnelforms-pro' ) }
{ steps.map( ( s, i ) => ( ) ) }

{ 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 ) ) }

); } export function ExperimentsSection( { doc }: { doc: FormDocument } ) { const { api, rights, readOnly, ctx } = useEnv(); const change = useChange(); const formId = useBuilder( ( s ) => s.form?.id ?? null ); const published = useBuilder( ( s ) => null !== s.form?.currentRevision ); const [ open, setOpen ] = useState( null ); const [ stats, setStats ] = useState( [] ); useEffect( () => { const unfold = ( e: Event ): void => setOpen( String( ( e as CustomEvent ).detail ) ); window.addEventListener( OPEN_EVENT, unfold ); return () => window.removeEventListener( OPEN_EVENT, unfold ); }, [] ); const experiments = doc.experiments ?? []; const steps = doc.nodes.filter( ( n ) => 'step' === n.type ); useEffect( () => { if ( null === formId || ! rights.entries || ! published || 0 === experiments.length ) { return undefined; } let live = true; api.experimentStats( formId ).then( ( list ) => live && setStats( list ), () => undefined ); return () => { live = false; }; }, [ api, formId, rights.entries, published, experiments.length ] ); const add = ( nodeId: string ): void => { let id = ''; change( ( d ) => { id = addStepTest( d, nodeId, { name: sprintf( __( 'Test of “%s”', 'funnelforms-pro' ), nodeName( doc.nodes.find( ( n ) => n.id === nodeId ) ?? { id: nodeId, type: 'step', fields: [] }, ctx.locale ) ), a: __( 'Original', 'funnelforms-pro' ), b: __( 'Variant B', 'funnelforms-pro' ), } )?.experiment ?? ''; } ); setOpen( id ); }; const addDesign = (): void => { let id = ''; change( ( d ) => { id = addDesignTest( d, { name: __( 'Test of the design', 'funnelforms-pro' ), a: __( 'Current design', 'funnelforms-pro' ), b: __( 'Variant B', 'funnelforms-pro' ) } ) ?? ''; } ); setOpen( id ); }; return (

{ __( 'A/B tests', 'funnelforms-pro' ) }

{ 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' ) }

    { experiments.map( ( x ) => { const expanded = open === x.id; const suggestion = 'running' === x.status ? suggestWinner( stats.find( ( s ) => s.id === x.id ) ) : null; return (
  • { expanded ? (
    s.id === x.id ) } readOnly={ readOnly } />
    ) : null }
  • ); } ) }
{ steps.length ? (
) : ( <>

{ 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 ? ( ) : ( ) }

{ 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 ( <>

{ __( 'Run time', 'funnelforms-pro' ) }

set( ( x ) => void ( x.onEnd = v ) ) } />

{ '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 ? ( setToken( token, e.target.value ) } /> ) : 'shadow' === token ? ( ) : ( setToken( token, e.target.value ) } /> ) } { changed && token.startsWith( 'color.' ) ? (