/**
* 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
{ __( 'This automation has no steps configured.', 'ai-marketing-expert' ) }
) : (| # | { __( '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% ) } |
{ __( 'No contacts have enrolled into this automation yet.', 'ai-marketing-expert' ) }
) : (| { __( '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' ) } |