/**
* Chatbot Analytics - main landing page with KPIs, SVG charts, bots overview, and recent conversations.
* Uses the same chart style as the SEO Analyzer module.
*/
import { useState, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Button, SelectControl } from '@aime/wp-components';
import useApi from '../../../../hooks/useApi';
import Card from '../../../common/Card';
import Loader from '../../../common/Loader';
import Notice from '../../../common/Notice';
import { DonutChart, HBarChart, StackedBar } from '../../Seo/views/SeoCharts';
const DAILY_PAGE_SIZE = 10;
/* Inline SVG area chart */
const AreaChart = ( { data, dataKey, color = '#1565c0', height = 180 } ) => {
if ( ! data.length ) return null;
const values = data.map( ( d ) => d[ dataKey ] || 0 );
const max = Math.max( ...values, 1 );
const W = 600;
const H = height;
const padTop = 20;
const padBottom = 30;
const padLeft = 40;
const padRight = 10;
const plotW = W - padLeft - padRight;
const plotH = H - padTop - padBottom;
const step = data.length > 1 ? plotW / ( data.length - 1 ) : plotW;
const points = values.map( ( v, i ) => {
const x = padLeft + ( data.length > 1 ? i * step : plotW / 2 );
const y = padTop + plotH - ( v / max ) * plotH;
return { x, y, v };
} );
const linePath = points.map( ( p, i ) => `${ i === 0 ? 'M' : 'L' } ${ p.x } ${ p.y }` ).join( ' ' );
const areaPath = `${ linePath } L ${ points[ points.length - 1 ].x } ${ padTop + plotH } L ${ points[ 0 ].x } ${ padTop + plotH } Z`;
// Y-axis gridlines (5 lines)
const gridLines = [ 0, 0.25, 0.5, 0.75, 1 ].map( ( pct ) => {
const y = padTop + plotH - pct * plotH;
const val = Math.round( pct * max );
return { y, val };
} );
// X-axis labels - pick up to 7 evenly spaced
const maxLabels = Math.min( 7, data.length );
const labelStep = data.length > 1 ? Math.ceil( data.length / maxLabels ) : 1;
const xLabels = data.filter( ( _, i ) => i % labelStep === 0 || i === data.length - 1 );
return (
);
};
const CONVERSATION_STATUS_COLORS = {
active: '#4caf50',
closed: '#9e9e9e',
human_takeover: '#ff9800',
};
const CONVERSATION_STATUS_LABELS = {
active: 'Active',
closed: 'Closed',
human_takeover: 'Human Takeover',
};
const Analytics = ( { onNavigate } ) => {
const { get, loading, error, clearError } = useApi();
const [ stats, setStats ] = useState( null );
const [ trends, setTrends ] = useState( [] );
const [ days, setDays ] = useState( '30' );
const [ botFilter, setBotFilter ] = useState( '' );
const [ bots, setBots ] = useState( [] );
const [ dailyPage, setDailyPage ] = useState( 1 );
useEffect( () => {
const loadBots = async () => {
try {
const res = await get( '/chatbot/bots' );
const arr = Array.isArray( res ) ? res : res.items || [];
setBots( arr.map( ( b ) => ( { label: b.name, value: String( b.id ) } ) ) );
} catch ( e ) {
// silent
}
};
loadBots();
}, [ get ] );
const fetchData = useCallback( async () => {
try {
const params = {};
if ( botFilter ) params.bot_id = botFilter;
const [ overview, trendData ] = await Promise.all( [
get( '/chatbot/analytics/overview', { ...params, days: parseInt( days ) } ),
get( '/chatbot/analytics/conversations', { ...params, days: parseInt( days ) } ),
] );
setStats( overview );
setTrends( Array.isArray( trendData ) ? trendData : [] );
} catch ( e ) {
// silent
}
}, [ get, botFilter, days ] );
useEffect( () => {
fetchData();
}, [ fetchData ] );
if ( loading && ! stats ) {
return
| { __( 'Date', 'ai-marketing-expert' ) } | { __( 'Conversations', 'ai-marketing-expert' ) } | { __( 'Messages', 'ai-marketing-expert' ) } | { __( 'Leads', 'ai-marketing-expert' ) } | { __( 'Takeovers', 'ai-marketing-expert' ) } | { __( 'Unique Visitors', 'ai-marketing-expert' ) } |
|---|---|---|---|---|---|
| { row.date } | { row.total_conversations || 0 } | { row.total_messages || 0 } | { row.leads_captured || 0 } | { row.human_takeovers || 0 } | { row.unique_visitors || 0 } |
| { __( 'Visitor', 'ai-marketing-expert' ) } | { __( 'Bot', 'ai-marketing-expert' ) } | { __( 'Status', 'ai-marketing-expert' ) } | { __( 'Messages', 'ai-marketing-expert' ) } | { __( 'Started', 'ai-marketing-expert' ) } |
|---|---|---|---|---|
|
{ c.visitor_name || __( 'Anonymous', 'ai-marketing-expert' ) }
{ c.visitor_email && { c.visitor_email } }
|
{ c.bot_name || '\u2014' } | { CONVERSATION_STATUS_LABELS[ c.status ] || c.status } | { c.message_count || 0 } | { c.created_at?.split( ' ' )[ 0 ] } |
{ __( 'No conversations yet. Create a chatbot to get started!', 'ai-marketing-expert' ) }
) }