/** * Statistics of a published form: views, starts, sent forms and conversion compared with the * period before, a chart per day, where visitors leave, and how each answer of a choice leads on. * The builder can also show the figures per step on the canvas. */ import React, { createContext, useEffect, useMemo, useState } from 'react'; import type { FormsApi, FormStatsReport } from '../api'; import { analyticsUrl, answerRows, change, percent, periodDays, shownDay, stepFigures, stepRows, worstStep, type StatsPeriod, type StepFigures } from '../stats'; import { deviceName, duration } from './Entries'; import { __, sprintf } from './i18n'; import { Badge, Button, Dialog, Empty, icons, Notice, ProBadge, ProLink, Spinner } from './ui'; /** Figures per step shown on the canvas; null while they are hidden. */ export const FiguresContext = createContext( null ); function number( n: number, locale?: string ): string { try { return new Intl.NumberFormat( locale ).format( n ); } catch { return String( n ); } } /** Change against the period before; with lowerIsBetter a fall shows as good (times). */ function Change( { now, before, locale, lowerIsBetter = false }: { now: number | null; before: number | null; locale?: string; lowerIsBetter?: boolean } ) { const c = change( now, before ); if ( null === c ) { return { __( 'No figures before', 'funnelforms-pro' ) }; } const text = ( c > 0 ? '+' : '' ) + percent( c, locale ); const good = lowerIsBetter ? c < 0 : c > 0; return ( { sprintf( __( '%s vs. period before', 'funnelforms-pro' ), text ) } ); } function Chart( { report, locale }: { report: FormStatsReport; locale?: string } ) { const days = report.daily; const max = Math.max( 1, ...days.map( ( d ) => Math.max( d.views, d.completions ) ) ); const width = 640; const height = 140; const gap = days.length > 60 ? 1 : 2; const bar = width / Math.max( 1, days.length ); const label = sprintf( __( '%1$s views and %2$s sent forms from %3$s to %4$s', 'funnelforms-pro' ), number( report.totals.views, locale ), number( report.totals.completions, locale ), shownDay( report.from, locale ), shownDay( report.to, locale ) ); return (
{ days.map( ( d, i ) => { const h = ( d.views / max ) * ( height - 4 ); const c = ( d.completions / max ) * ( height - 4 ); return ( { sprintf( __( '%1$s: %2$d views, %3$d sent', 'funnelforms-pro' ), shownDay( d.date, locale ), d.views, d.completions ) } ); } ) }
{ __( 'Views', 'funnelforms-pro' ) } { __( 'Sent', 'funnelforms-pro' ) } { shownDay( report.from, locale ) } – { shownDay( report.to, locale ) }
); } /** Sent forms per device kind, as share of those with a known device. */ function Devices( { devices, locale }: { devices: NonNullable; locale?: string } ) { const total = devices.desktop + devices.tablet + devices.mobile; if ( 0 === total ) { return null; } return (

{ __( 'Devices', 'funnelforms-pro' ) }

{ __( 'On which devices the form was sent.', 'funnelforms-pro' ) }

{ ( [ 'mobile', 'desktop', 'tablet' ] as const ).map( ( kind ) => ( ) ) }
{ deviceName( kind ) } { `${ number( devices[ kind ], locale ) } (${ percent( devices[ kind ] / total, locale ) })` }
); } export function StatsView( { api, formId, locale }: { api: FormsApi; formId: number; locale?: string } ) { const [ period, setPeriod ] = useState( 30 ); const [ data, setData ] = useState<{ now: FormStatsReport; before: FormStatsReport } | null>( null ); const [ error, setError ] = useState( null ); useEffect( () => { let live = true; const p = periodDays( period ); setData( null ); setError( null ); Promise.all( [ api.formStats( formId, p.from, p.to ), api.formStats( formId, p.previousFrom, p.previousTo ) ] ) .then( ( [ now, before ] ) => live && setData( { now, before } ) ) .catch( ( e: unknown ) => live && setError( e instanceof Error ? e.message : String( e ) ) ); return () => { live = false; }; }, [ api, formId, period ] ); const steps = useMemo( () => ( data ? stepRows( data.now ) : [] ), [ data ] ); const worst = worstStep( steps ); return (
{ ( [ 7, 30, 90 ] as StatsPeriod[] ).map( ( p ) => ( ) ) }
{ /* The analytics of one form are Funnelforms Pro; Free names them with a link to the page about Pro. */ } { 'free' === process.env.FUNNELFORMS_EDITION ? ( }> { __( 'Where visitors come from, times per step, numbers and ratings, campaigns and when entries come in.', 'funnelforms-pro' ) } ) : ( { __( 'Open analytics', 'funnelforms-pro' ) } } > { __( 'Where visitors come from, times per step, numbers and ratings, campaigns and when entries come in.', 'funnelforms-pro' ) } ) } { error ? { error } : null } { ! data && ! error ? : null } { data ? ( <>
{ __( 'Views', 'funnelforms-pro' ) } { number( data.now.totals.views, locale ) }
{ __( 'Started', 'funnelforms-pro' ) } { number( data.now.totals.starts, locale ) }
{ __( 'Sent', 'funnelforms-pro' ) } { number( data.now.totals.completions, locale ) }
{ __( 'Conversion rate', 'funnelforms-pro' ) } { percent( data.now.totals.conversionRate, locale ) }
{ __( 'Average time to fill in', 'funnelforms-pro' ) } { null == data.now.totals.averageSeconds ? '–' : duration( data.now.totals.averageSeconds ) } { null == data.now.totals.medianSeconds ? ( ) : ( { sprintf( __( 'Median %s', 'funnelforms-pro' ), duration( data.now.totals.medianSeconds ) ) } ) }
{ data.now.abandonment ? (
{ __( 'Recovered by reminders', 'funnelforms-pro' ) } { number( data.now.abandonment.recovered, locale ) } { /* translators: 1: share of reminded visitors who came back, 2: number of reminded visitors, 3: number of visitors who left after typing their address. */ } { sprintf( __( '%1$s of %2$s reminded (%3$s left)', 'funnelforms-pro' ), percent( data.now.abandonment.recoveryRate, locale ), number( data.now.abandonment.reminded, locale ), number( data.now.abandonment.abandoned, locale ) ) }
) : null }

{ __( 'Where visitors leave', 'funnelforms-pro' ) }

{ worst ?

{ sprintf( __( 'Most visitors leave on “%1$s” (%2$s of those who reach it).', 'funnelforms-pro' ), worst.title || __( 'Without a title', 'funnelforms-pro' ), percent( worst.dropRate, locale ) ) }

: null } { steps.map( ( s ) => ( ) ) }
{ __( 'Step', 'funnelforms-pro' ) } { __( 'Reached', 'funnelforms-pro' ) } { __( 'Left here', 'funnelforms-pro' ) }
{ s.title || __( 'Without a title', 'funnelforms-pro' ) } { `${ number( s.reached, locale ) } (${ percent( s.reachRate, locale ) })` } { s.left ? `${ number( s.left, locale ) } (${ percent( s.dropRate, locale ) })` : '0' }
{ data.now.devices ? : null } { data.now.answers?.length ? (

{ __( 'Answers', 'funnelforms-pro' ) }

{ __( 'How often each answer was given, and how many of those visitors sent the form.', 'funnelforms-pro' ) }

{ data.now.answers.map( ( q ) => ( { answerRows( q.options ).map( ( o ) => ( ) ) }
{ q.title || __( 'Without a title', 'funnelforms-pro' ) }
{ __( 'Answer', 'funnelforms-pro' ) } { __( 'Given', 'funnelforms-pro' ) } { __( 'Sent the form', 'funnelforms-pro' ) }
{ o.label || o.id } { `${ number( o.chosen, locale ) } (${ percent( o.share, locale ) })` } { percent( o.sentRate, locale ) }
) ) }
) : null }

{ __( 'Counted per page view, without cookies, for the published version.', 'funnelforms-pro' ) }

) : null }
); } /** The figures per step of the last 30 days, for the canvas, with the period as its label. */ export async function canvasFigures( api: FormsApi, formId: number ): Promise<{ figures: StepFigures; label: string }> { const p = periodDays( 30 ); const report = await api.formStats( formId, p.from, p.to ); return { figures: stepFigures( report ), label: sprintf( __( '%1$s – %2$s', 'funnelforms-pro' ), shownDay( report.from ), shownDay( report.to ) ) }; } /** * The figures of one form on the statistics page of Funnelforms Free (screen form-stats): the * same figures as in the dialog of the form list, with the way to see them on the steps. */ export function FormStatsScreen( { api, formId, published, canvasUrl, locale }: { api: FormsApi; formId: number; published: boolean; canvasUrl?: string; locale?: string } ) { return (

{ __( 'Statistics', 'funnelforms-pro' ) }

{ published && canvasUrl ? ( ) : null }
{ published ? : { __( 'Statistics start when the form is published.', 'funnelforms-pro' ) } }
); } export function StatsDialog( props: { api: FormsApi; formId: number; title: string; locale?: string; onClose: () => void; onShowOnCanvas?: ( figures: StepFigures, label: string ) => void } ) { const { api, formId } = props; const [ busy, setBusy ] = useState( false ); const showOnCanvas = async (): Promise => { if ( ! props.onShowOnCanvas ) { return; } setBusy( true ); try { const shown = await canvasFigures( api, formId ); props.onShowOnCanvas( shown.figures, shown.label ); props.onClose(); } finally { setBusy( false ); } }; return ( { props.onShowOnCanvas ? ( ) : null } } > ); } /** The figures of a step on the canvas. */ export function NodeFigures( { figures }: { figures: { reached: number; left: number } } ) { const rate = figures.reached > 0 ? figures.left / figures.reached : null; return (
{ sprintf( __( '%d reached', 'funnelforms-pro' ), figures.reached ) } { figures.left ? = 0.3 ? 'danger' : 'warning' }>{ sprintf( __( '%1$d left (%2$s)', 'funnelforms-pro' ), figures.left, percent( rate ) ) } : null }
); }