/** * 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 ( { /* Grid lines */ } { gridLines.map( ( g, i ) => ( { g.val } ) ) } { /* Gradient fill */ } { /* Data dots */ } { points.map( ( p, i ) => ( { `${ data[ i ].date }: ${ p.v }` } ) ) } { /* X-axis labels */ } { xLabels.map( ( d, i ) => { const idx = data.indexOf( d ); const x = padLeft + ( data.length > 1 ? idx * step : plotW / 2 ); return ( { d.date?.slice( 5 ) } ); } ) } ); }; 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 ; } const kpis = [ { label: __( 'Total Conversations', 'ai-marketing-expert' ), value: stats?.total_conversations || 0, icon: '\uD83D\uDCAC' }, { label: __( 'Total Messages', 'ai-marketing-expert' ), value: stats?.total_messages || 0, icon: '\u2709\uFE0F' }, { label: __( 'Unique Visitors', 'ai-marketing-expert' ), value: stats?.unique_visitors || 0, icon: '\uD83D\uDC65' }, { label: __( 'Active Now', 'ai-marketing-expert' ), value: stats?.active_conversations || 0, icon: '\uD83D\uDFE2' }, { label: __( 'Leads Captured', 'ai-marketing-expert' ), value: stats?.leads_captured || 0, icon: '\uD83C\uDFAF' }, { label: __( 'Human Takeovers', 'ai-marketing-expert' ), value: stats?.human_takeovers || 0, icon: '\uD83D\uDE4B' }, ]; const botOptions = [ { label: __( 'All Bots', 'ai-marketing-expert' ), value: '' }, ...bots, ]; const dayOptions = [ { label: '7 days', value: '7' }, { label: '14 days', value: '14' }, { label: '30 days', value: '30' }, { label: '90 days', value: '90' }, ]; // Conversation status donut data const statusDonut = [ { label: __( 'Active', 'ai-marketing-expert' ), value: stats?.active_status_conversations || 0, color: '#4caf50' }, { label: __( 'Closed', 'ai-marketing-expert' ), value: stats?.closed_conversations || 0, color: '#9e9e9e' }, { label: __( 'Human Takeover', 'ai-marketing-expert' ), value: stats?.human_takeover_active || 0, color: '#ff9800' }, ].filter( ( d ) => d.value > 0 ); // Engagement stacked bar const engagementSegments = [ { label: __( 'Conversations', 'ai-marketing-expert' ), value: stats?.total_conversations || 0, color: '#1565c0' }, { label: __( 'Messages', 'ai-marketing-expert' ), value: stats?.total_messages || 0, color: '#7C3AED' }, { label: __( 'Leads', 'ai-marketing-expert' ), value: stats?.leads_captured || 0, color: '#ff9800' }, ]; // Bot performance bar chart data const botBarData = ( stats?.bots || [] ).map( ( bot ) => ( { label: bot.name, value: bot.conversation_count || bot.conversations || 0, color: bot.status === 'active' ? '#1565c0' : '#9e9e9e', } ) ); return (
{ error && }

{ __( 'AI Chatbot', 'ai-marketing-expert' ) }

{ bots.length > 1 && ( ) }
{ /* KPI cards */ }
{ kpis.map( ( k ) => (
{ k.value } { k.label }
) ) }
{ /* SVG area charts - conversations and messages over time */ }
{ trends.length > 0 && ( ) } { trends.length > 0 && ( ) }
{ /* Donut + Engagement + Bot Performance row */ }
{ statusDonut.length > 0 && ( ) }
{ botBarData.length > 0 && ( ) }
{ /* Bot status breakdown */ } { stats?.bots?.length > 0 && ( onNavigate( 'bots' ) }> { __( 'Manage', 'ai-marketing-expert' ) } } >
{ stats.bots.map( ( bot ) => (
{ bot.name } { bot.status === 'active' ? __( 'Active', 'ai-marketing-expert' ) : __( 'Inactive', 'ai-marketing-expert' ) }
{ bot.conversation_count > 0 && ( { bot.conversation_count } { __( 'conversations', 'ai-marketing-expert' ) } ) }
) ) }
) } { /* Daily Breakdown table with pagination */ } { trends.length > 0 && ( () => { const reversed = trends.slice().reverse(); const totalPages = Math.ceil( reversed.length / DAILY_PAGE_SIZE ); const paged = reversed.slice( ( dailyPage - 1 ) * DAILY_PAGE_SIZE, dailyPage * DAILY_PAGE_SIZE ); return ( { paged.map( ( row, i ) => ( ) ) }
{ __( '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 }
{ totalPages > 1 && (
{ __( 'Page', 'ai-marketing-expert' ) } { dailyPage } / { totalPages }
) }
); } )() } { /* Recent Conversations */ } onNavigate( 'conversations' ) }> { __( 'View All', 'ai-marketing-expert' ) } } > { stats?.recent_conversations?.length > 0 ? ( { stats.recent_conversations.map( ( c ) => ( onNavigate( 'conversation', { id: c.id } ) } > ) ) }
{ __( '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' ) }

) }
); }; export default Analytics;