/** * The analytics mode of the canvas (Pro): the figures of the published form as a layer over the * steps. Paths are drawn by how many took them, nodes carry a band by how many left them, the * strongest way from the start stands out, and the inspector shows the fields of the selected node. * Editing stays possible; the mode only adds the figures. */ import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import { useStore } from 'zustand'; import type { FormsApi, FormStatsReport } from '../api'; import { canvasAnalytics, nodeFields, shownDay, percent, periodDays, SMALL_SAMPLE, type CanvasAnalytics, type DropBand, type EdgeAnalytics, type NodeAnalytics, type StatsPeriod } from '../stats'; import { nodeName } from '../document'; import type { BuilderStore } from '../store'; import { useBuilder } from './context'; import { duration } from './Entries'; import { __, sprintf } from './i18n'; import { Spinner } from './ui'; export interface Analytics { on: boolean; setOn: ( on: boolean ) => void; period: StatsPeriod; setPeriod: ( period: StatsPeriod ) => void; /** Only a published form has figures. */ published: boolean; report: FormStatsReport | null; error: string | null; /** The figures per node and path while the mode is on and they are loaded. */ figures: CanvasAnalytics | null; } /** The analytics mode; null in Funnelforms Free and outside the editor. */ export const AnalyticsContext = createContext( null ); export function useAnalytics(): Analytics | null { return useContext( AnalyticsContext ); } /** State of the analytics mode of one form: on or off, the period, and its figures. */ export function useAnalyticsState( api: FormsApi, formId: number, store: BuilderStore ): Analytics { const [ on, setOn ] = useState( false ); const [ period, setPeriod ] = useState( 30 ); const [ loaded, setLoaded ] = useState<{ report: FormStatsReport | null; error: string | null }>( { report: null, error: null } ); const published = useStore( store, ( s ) => null !== ( s.form?.currentRevision ?? null ) ); const doc = useStore( store, ( s ) => s.doc ); // Whether the plugin was told that the mode was opened (view=canvas); once per opening, not per period. const counted = useRef( false ); useEffect( () => { if ( ! on ) { counted.current = false; } if ( ! on || ! published ) { return undefined; } let live = true; const p = periodDays( period ); const view = counted.current ? undefined : 'canvas'; counted.current = true; setLoaded( { report: null, error: null } ); api.formStats( formId, p.from, p.to, 'full', view ) .then( ( report ) => live && setLoaded( { report, error: null } ) ) .catch( ( e: unknown ) => live && setLoaded( { report: null, error: e instanceof Error ? e.message : String( e ) } ) ); return () => { live = false; }; }, [ api, formId, on, period, published ] ); const figures = useMemo( () => ( on && loaded.report && doc ? canvasAnalytics( doc, loaded.report ) : null ), [ on, loaded.report, doc ] ); return useMemo( () => ( { on, setOn, period, setPeriod, published, report: loaded.report, error: loaded.error, figures } ), [ on, period, published, loaded, figures ] ); } function number( n: number ): string { try { return new Intl.NumberFormat().format( n ); } catch { return String( n ); } } function bandName( band: DropBand ): string { return { low: __( 'Few leave', 'funnelforms-pro' ), medium: __( 'Some leave', 'funnelforms-pro' ), high: __( 'Many leave', 'funnelforms-pro' ) }[ band ]; } /** Switch, period and state of the mode, in a panel at the top right of the canvas. */ export function AnalyticsPanel() { const analytics = useAnalytics(); const toggle = useCallback( () => analytics?.setOn( ! analytics.on ), [ analytics ] ); if ( ! analytics ) { return null; } const { on, period, setPeriod, published, report, error } = analytics; return (
{ on ? (
{ published ? (
{ ( [ 7, 30, 90 ] as StatsPeriod[] ).map( ( p ) => ( ) ) }
) : null }
{ ! published ? __( 'Statistics start when the form is published.', 'funnelforms-pro' ) : null } { published && error ? sprintf( __( 'The figures could not be loaded: %s', 'funnelforms-pro' ), error ) : null } { published && ! error && ! report ? : null } { report ? sprintf( __( '%1$s views from %2$s to %3$s, of the published version.', 'funnelforms-pro' ), number( report.totals.views ), shownDay( report.from ), shownDay( report.to ) ) : null } { report && report.totals.views < SMALL_SAMPLE ?

{ sprintf( __( 'Fewer than %d views: the shares can change a lot with each visitor.', 'funnelforms-pro' ), SMALL_SAMPLE ) }

: null }
) : null }
); } /** What the lines, bands and marks of the mode mean, at the bottom of the canvas. */ export function AnalyticsLegend( { dots = false }: { dots?: boolean } ) { return (
{ dots ? ( <> ) : null } { __( 'Strongest path', 'funnelforms-pro' ) } { ( [ 'low', 'medium', 'high' ] as DropBand[] ).map( ( band ) => ( ) ) }
); } /** The figures of a node on the canvas; forecast: of the prediction (virtual visitors). */ export function NodeAnalyticsView( { type, figures: all, forecast = false, played = null }: { type: 'step' | 'contact' | 'end'; figures: NodeAnalytics; forecast?: boolean; played?: { reached: number; left: number } | null } ) { // While the visitors of a prediction play, the counts so far. const figures = played ? { ...all, reached: Math.round( all.reached * played.reached ), left: Math.round( all.left * played.left ), sent: Math.round( all.sent * played.reached ) } : all; if ( 0 === all.reached ) { return
{ forecast ? __( 'No virtual visitor reaches this.', 'funnelforms-pro' ) : __( 'Nobody reached this in the period.', 'funnelforms-pro' ) }
; } if ( 'end' === type ) { return (
{ figures.sent ? sprintf( __( '%d sent forms ended here', 'funnelforms-pro' ), figures.sent ) : sprintf( __( '%d reached', 'funnelforms-pro' ), figures.reached ) }
); } return (
{ figures.band ? ( { bandName( figures.band ) } ) : null } { sprintf( __( '%1$d reached · %2$d left (%3$s)', 'funnelforms-pro' ), figures.reached, figures.left, percent( figures.dropRate ) ) } { 'contact' === type ? { sprintf( __( '%1$d sent (%2$s)', 'funnelforms-pro' ), figures.sent, percent( figures.sentRate ) ) } : null } { null !== figures.seconds ? { sprintf( __( 'Median time here: %s', 'funnelforms-pro' ), duration( figures.seconds ) ) } : null }
); } /** The label of a path: views that took it and their share of the step before. */ export function edgeFigures( figures: EdgeAnalytics ): { label: string; title: string } { return { label: sprintf( __( '%1$s · %2$s', 'funnelforms-pro' ), number( figures.sessions ), percent( figures.share ?? 0 ) ), title: sprintf( __( '%1$s views took this path, %2$s of those who reached the step before.', 'funnelforms-pro' ), number( figures.sessions ), percent( figures.share ?? 0 ) ), }; } /** The analytics of the selected node in the inspector: its fields with checks, inputs and answers. */ export function NodeAnalyticsSection( { nodeId }: { nodeId: string } ) { const analytics = useAnalytics(); const node = useBuilder( ( s ) => s.doc?.nodes.find( ( n ) => n.id === nodeId ) ); const report = analytics?.report; const figures = analytics?.figures?.nodes.get( nodeId ); if ( ! report || ! figures || ! node ) { return null; } const fields = nodeFields( report, nodeId ); const extended = report.extended ?? 0; return (

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

{ fields.length ? ( { fields.map( ( f ) => ( ) ) }
{ __( 'Fields of the step', 'funnelforms-pro' ) }
{ __( 'Field', 'funnelforms-pro' ) } { __( 'Check failed', 'funnelforms-pro' ) } { __( 'Filled in', 'funnelforms-pro' ) } { __( 'Last before leaving', 'funnelforms-pro' ) }
{ f.title || __( 'Without a title', 'funnelforms-pro' ) } { f.invalid ? `${ number( f.invalid ) } (${ percent( f.invalidRate ) })` : '0' } { extended ? number( f.touched ) : '–' } { extended ? number( f.lastTouched ) : '–' }
) : null } { fields.length ? (

{ extended ? sprintf( __( 'Filled in and last before leaving: from %1$s of %2$s views with consent to statistics.', 'funnelforms-pro' ), number( extended ), number( report.totals.views ) ) : __( 'Filled in and last before leaving are counted only for views with consent to statistics; there were none in the period.', 'funnelforms-pro' ) }

) : null } { fields .filter( ( f ) => f.answers.length ) .map( ( f ) => ( { f.answers.map( ( o ) => ( ) ) }
{ f.title || __( 'Without a title', 'funnelforms-pro' ) }
{ __( 'Answer', 'funnelforms-pro' ) } { __( 'Given', 'funnelforms-pro' ) } { __( 'Sent the form', 'funnelforms-pro' ) }
{ o.label || o.id } { `${ number( o.chosen ) } (${ percent( o.share ) })` } { percent( o.sentRate ) }
) ) }
); } /** The paths with their figures as a table, in the inspector while nothing is selected. */ export function PathsSection() { const analytics = useAnalytics(); const doc = useBuilder( ( s ) => s.doc ); const figures = analytics?.figures; if ( ! figures || ! doc ) { return null; } const name = ( id: string ): string => { const node = doc.nodes.find( ( n ) => n.id === id ); return ( node && nodeName( node, doc.locales.default ) ) || __( 'Without a title', 'funnelforms-pro' ); }; return (

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

{ __( 'Select a step to see its fields and answers.', 'funnelforms-pro' ) }

{ doc.edges.map( ( edge ) => { const f = figures.edges.get( edge.id ); return f ? ( ) : null; } ) }
{ __( 'Path', 'funnelforms-pro' ) } { __( 'Views', 'funnelforms-pro' ) } { __( 'Share', 'funnelforms-pro' ) }
{ sprintf( __( '%1$s → %2$s', 'funnelforms-pro' ), name( edge.from ), name( edge.to ) ) } { f.strongest ? ( <> { ' (' + __( 'Strongest path', 'funnelforms-pro' ) + ')' } ) : null } { number( f.sessions ) } { percent( f.share ) }
); }