/** * FunnelAnalytics - in-depth automation performance, step-by-step drop-off, * and contact execution queue tracking. */ import { useState, useEffect, useCallback } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button } from '@aime/wp-components'; import { ResponsiveContainer, AreaChart, Area, XAxis, YAxis, Tooltip, CartesianGrid, } from 'recharts'; import useApi from '../../../hooks/useApi'; import Card from '../../common/Card'; import Loader from '../../common/Loader'; import Notice from '../../common/Notice'; import EmptyState from '../../common/EmptyState'; import { formatDateTime, formatDate } from '../../../utils/datetime'; const ICON_MAP = { send_email: '\u2709\uFE0F', add_tag: '\uD83C\uDFF7\uFE0F', remove_tag: '\uD83C\uDFF7\uFE0F', add_to_list: '\uD83D\uDCCB', remove_from_list: '\uD83D\uDCCB', update_contact: '\uD83D\uDC64', webhook: '\uD83C\uDF10', condition: '\u2753', wait: '\u23F3', }; const STATUS_BADGES = { active: 'info', waiting: 'warning', completed: 'success', cancelled: 'danger', failed: 'danger', }; const FunnelAnalytics = ( { funnelId, onNavigate } ) => { const { get, loading, error, clearError } = useApi(); const [ data, setData ] = useState( null ); const fetchReport = useCallback( async () => { if ( ! funnelId ) return; try { const res = await get( `/email/analytics/automations/${ funnelId }` ); setData( res ); } catch ( e ) { /* Handled by useApi */ } }, [ get, funnelId ] ); useEffect( () => { fetchReport(); }, [ fetchReport ] ); if ( loading && ! data ) { return ; } if ( ! data ) { return ( { error && } ); } const { funnel, sequences = [], total_subscribers = 0, active = 0, waiting = 0, completed = 0, cancelled = 0, completion_rate = 0, recent_subscribers = [], trend = [], } = data; return (
{ error && } { /* Top KPI Metrics Grid */ }
👥
{ total_subscribers }
{ __( 'Total Enrolled', 'ai-marketing-expert' ) }
⚡
{ active }
{ __( 'In Progress', 'ai-marketing-expert' ) }
⏳
{ waiting }
{ __( 'Scheduled / Waiting', 'ai-marketing-expert' ) }
🎯
{ completed } ({ completion_rate }%)
{ __( 'Completed Funnels', 'ai-marketing-expert' ) }
🛑
{ cancelled }
{ __( 'Dropped Out / Failed', 'ai-marketing-expert' ) }
{ /* Step-by-Step Drop-Off Analysis */ } { sequences.length === 0 ? (

{ __( 'This automation has no steps configured.', 'ai-marketing-expert' ) }

) : (
{ sequences.map( ( seq, idx ) => { const icon = ICON_MAP[ seq.action_name ] || '\u26A1'; const retentionPct = Math.min( 100, Math.max( 0, seq.retention_rate || 0 ) ); const dropoffPct = Math.min( 100, Math.max( 0, seq.step_dropoff_pct || 0 ) ); return ( ); } ) }
# { __( 'Step & Action', 'ai-marketing-expert' ) } { __( 'Delay', 'ai-marketing-expert' ) } { __( 'Completed', 'ai-marketing-expert' ) } { __( 'Open Rate', 'ai-marketing-expert' ) } { __( 'Click Rate', 'ai-marketing-expert' ) } { __( 'Retention Rate', 'ai-marketing-expert' ) } { __( 'Step Drop-off', 'ai-marketing-expert' ) }
{ idx + 1 }
{ icon }
{ seq.title || seq.action_name } { seq.action_name }
{ seq.delay_value > 0 ? `${ seq.delay_value } ${ seq.delay_unit || 'minutes' }` : __( 'Immediate', 'ai-marketing-expert' ) } { seq.completed || 0 } { seq.open_rate !== null ? (
{ seq.open_rate }% ({ seq.opens || 0 })
) : ( { '\u2014' } ) }
{ seq.click_rate !== null ? (
{ seq.click_rate }% ({ seq.clicks || 0 })
) : ( { '\u2014' } ) }
50 ? '#10b981' : retentionPct > 20 ? '#f59e0b' : '#ef4444', borderRadius: '4px', transition: 'width 0.3s ease', } } />
{ retentionPct }%
{ idx === 0 ? ( { __( 'Entry point', 'ai-marketing-expert' ) } ) : dropoffPct > 0 ? ( 30 ? '#fee2e2' : '#fef3c7', color: dropoffPct > 30 ? '#dc2626' : '#d97706', border: 'none', fontWeight: 600, } } > -{ dropoffPct }% ) : ( 0% ) }
) }
{ /* Trend Chart (14-Day Activity) */ } { trend && trend.length > 0 && (
[ val, __( 'Completed Contacts', 'ai-marketing-expert' ) ] } labelFormatter={ ( label ) => `${ __( 'Date:', 'ai-marketing-expert' ) } ${ label }` } />
) } { /* Recent Enrolled Subscribers Activity Log */ } { __( 'Refresh', 'ai-marketing-expert' ) } } > { recent_subscribers.length === 0 ? (

{ __( 'No contacts have enrolled into this automation yet.', 'ai-marketing-expert' ) }

) : ( { recent_subscribers.map( ( sub ) => { const contactName = [ sub.first_name, sub.last_name ].filter( Boolean ).join( ' ' ); const badgeClass = STATUS_BADGES[ sub.status ] || 'default'; return ( ); } ) }
{ __( 'Contact', 'ai-marketing-expert' ) } { __( 'Status', 'ai-marketing-expert' ) } { __( 'Last Step', 'ai-marketing-expert' ) } { __( 'Next Step', 'ai-marketing-expert' ) } { __( 'Next Run / Status Time', 'ai-marketing-expert' ) } { __( 'Enrolled', 'ai-marketing-expert' ) }
{ onNavigate ? ( ) : (
{ contactName || sub.email } { contactName && ( { sub.email } ) }
)}
{ sub.status } { sub.last_step_title ? (
{ sub.last_step_title } { sub.last_sequence_status && ( ({ sub.last_sequence_status }) ) }
) : ( { '\u2014' } ) }
{ sub.next_step_title ? ( { sub.next_step_title } ) : sub.status === 'completed' ? ( { __( 'Finished', 'ai-marketing-expert' ) } ) : ( { '\u2014' } ) } { sub.status === 'waiting' && sub.next_execution_time ? ( { formatDateTime( sub.next_execution_time, '\u2014' ) } ) : sub.last_executed_time ? ( { formatDateTime( sub.last_executed_time, '\u2014' ) } ) : ( { '\u2014' } ) } { formatDate( sub.created_at, '\u2014' ) }
) }
); }; export default FunnelAnalytics;