{{-- window.t() map for the strings this page's JS paints (table cells, status badges, confirm copy). English locales get no map and fall back to the English key, so nothing here is ever an untranslated placeholder. --}} @include('partials.js-i18n') @php /* Everything below reads from $stats — the SAME payload /flows/analytics/data returns (FlowsController::buildAnalytics), so the first paint carries real numbers before any fetch resolves. This mirrors $chartData on the campaign detail page. Nothing is estimated: a metric the controller could not derive comes back null and its tile is hidden rather than shown as a zero. */ $totals = $stats['totals'] ?? []; $trend = $stats['series'] ?? [ 'granularity' => 'day', 'categories' => [], 'enrolled' => [], 'completed' => [], 'failed' => [], ]; $board = $stats['flows'] ?? []; $topReasons = $stats['failure_reasons'] ?? []; $maxBatch = (int) ($stats['retry_max_batch'] ?? 50); $cooldown = (int) ($stats['retry_cooldown_seconds'] ?? 30); $flowsTotal = (int) ($totals['flows_total'] ?? 0); $runsTotal = (int) ($totals['runs'] ?? 0); $completionRate = $totals['completion_rate'] ?? null; $avgSeconds = $totals['avg_complete_seconds'] ?? null; $rangeChips = ['7' => __('7 days'), '30' => __('30 days'), '90' => __('90 days'), 'all' => __('All time')]; $currentRange = in_array((string) $range, array_keys($rangeChips), true) ? (string) $range : '30'; /* Flow lifecycle pill — same treatment as the flow cards on /flows so a flow reads identically wherever it appears. */ $stateStyle = [ 'live' => 'bg-wa-green/10 text-wa-deep border border-wa-green/30', 'paused' => 'bg-[#EFE5F5] text-[#5B3D8A] border border-[#D9C7E8]', 'draft' => 'bg-paper-50 text-ink-500 border border-paper-200', ]; $stateLabel = ['live' => __('Live'), 'paused' => __('Paused'), 'draft' => __('Draft')]; /* Seconds → "2 h 14 m" / "45 s". Returns null for null, so a missing average renders no tile instead of a fabricated zero. */ $fmtDuration = function ($seconds) { if ($seconds === null) { return null; } $s = max(0, (int) $seconds); if ($s < 60) { return __(':n s', ['n' => $s]); } if ($s < 3600) { return __(':n m', ['n' => (int) floor($s / 60)]); } if ($s < 86400) { $h = (int) floor($s / 3600); $m = (int) floor(($s % 3600) / 60); return $m > 0 ? __(':h h :m m', ['h' => $h, 'm' => $m]) : __(':n h', ['n' => $h]); } $d = (int) floor($s / 86400); $h = (int) floor(($s % 86400) / 3600); return $h > 0 ? __(':d d :h h', ['d' => $d, 'h' => $h]) : __(':n d', ['n' => $d]); }; $flowName = fn($name, $id) => $name ?: __('Untitled flow') . ' #' . $id; @endphp {{-- ========== HEADER ========== --}}
{{ __('Flows') }} / {{ __('Analytics') }}@if ($flow) / #{{ $flow->id }} @endif
{{ $flow ? $flowName($flow->flow_name, $flow->id) : __('All flows') }}
@if ($flow) @php $flState = $flow->is_published ? ($flow->is_active ? 'live' : 'paused') : 'draft'; @endphp {{ $stateLabel[$flState] }} @endif
{{-- Real generation timestamp from the payload, localised in JS. --}} @if ($flow) {{ __('All flows') }} {{ __('Open in builder') }} @endif
@if ($flowsTotal < 1) {{-- Nothing to measure: no flow exists in this workspace yet. One honest empty state — no charts, no zeroed tiles. --}} @include('user.partials.empty-state', [ 'title' => __('No flows to analyse yet'), 'message' => __( 'Flow analytics fills in as soon as a flow runs — every enrolment, completion, failure and retry is recorded here.'), 'actionHref' => route('user.flows.index'), 'actionLabel' => __('Go to flows'), ]) @else {{-- ========== FILTER BAR ========== --}}
{{ __('Period') }} @foreach ($rangeChips as $key => $label) @endforeach
{{-- Window boundaries straight from the payload (from/to), so the operator always knows exactly what the numbers cover. --}}
{{-- ========== KPI ROW ========== --}} {{-- Every tile maps to a field the controller actually returns. The two that can be undefined — completion rate needs at least one run, average duration needs at least one completed run — carry data-fa-tile and are hidden when null, never shown as a fake 0. --}}
{{ __('Total runs') }}
{{ number_format($runsTotal) }}
{{ __('Enrolments in this period') }}
{{ __('Completed') }}
{{ number_format((int) ($totals['completed'] ?? 0)) }}
{{ __('Reached the end of the flow') }}
{{ __('Failed') }}
{{ number_format((int) ($totals['failed'] ?? 0)) }}
{{ __('Stopped with an error') }}
{{ __('Active') }}
{{ number_format((int) ($totals['active'] ?? 0)) }}
{{ __('Still running now') }}
{{ __('Completion rate') }}
{{ $completionRate === null ? '' : number_format((float) $completionRate, 1) }}%
{{ __('Of all runs in this period') }}
{{ __('Avg time to complete') }}
{{ $fmtDuration($avgSeconds) }}
{{ __('Enrolment to completion') }}
{{-- Secondary counters — plain integers from the same payload. --}}
@foreach ([['contacts_reached', __('Contacts reached')], ['paused', __('Paused runs')], ['retried_runs', __('Runs retried')], ['retry_attempts', __('Retry attempts')], ['flows_live', __('Live flows')], ['flows_with_runs', __('Flows with runs')], ['flows_total', __('Flows total')]] as [$k, $lbl]) {{ $lbl }} {{ number_format((int) ($totals[$k] ?? 0)) }} @endforeach
{{-- ========== TABS ========== --}}
{{-- ========== OVERVIEW ========== --}}
{{ __('Run trend') }}

{{ __('Enrolments, completions, failures') }}

{{ __('Enrolled') }} {{ __('Completed') }} {{ __('Failed') }}
{{-- Chart node stays hidden until the payload actually has buckets — an empty window shows the app's empty state, never a flat invented line. --}}
@include('user.partials.empty-state', [ 'title' => __('No runs in this period'), 'message' => __('Widen the period, or pick a different flow, to see activity here.'), 'class' => 'border-0 shadow-none', ])
{{ __('Outcome mix') }}

{{ __('Run status') }}

@include('user.partials.empty-state', [ 'title' => __('Nothing to split yet'), 'message' => __('The status mix appears once at least one run exists in this period.'), 'class' => 'border-0 shadow-none', ])
{{-- ---------- PER-FLOW LEADERBOARD ---------- --}}
{{ __('Per-flow performance') }}

{{ __('Flow leaderboard') }}

{{ __('Select a row to scope this whole page to that flow.') }}

@foreach ($board as $row) @endforeach
{{ __('Flow') }} {{ __('Runs') }} {{ __('Completed') }} {{ __('Failed') }} {{ __('Completion rate') }}
{{ $flowName($row['name'], $row['flow_id']) }}
{{ $stateLabel[$row['state']] ?? $row['state'] }}
{{ number_format((int) $row['runs']) }} {{ number_format((int) $row['completed']) }} {{ number_format((int) $row['failed']) }} {{ $row['completion_rate'] === null ? '—' : number_format((float) $row['completion_rate'], 1) . '%' }}
@include('user.partials.empty-state', [ 'title' => __('No flow has run in this period'), 'message' => __('Once a contact enters a flow, its per-flow totals appear here.'), 'class' => 'border-0 shadow-none', ])
{{-- ---------- TOP FAILURE REASONS ---------- --}}
{{ __('Why runs fail') }}

{{ __('Top failure reasons') }}

@include('user.partials.empty-state', [ 'title' => __('No failures recorded'), 'message' => __('Nothing in this period stopped with an error.'), 'class' => 'border-0 shadow-none', ])
{{-- ===== KEYWORD TRIGGERS — which flow ran on which keyword ===== --}}
{{ __('Attribution') }}

{{ __('Keyword triggers') }}

{{ __('Which flow each keyword started, and how many times it fired in this period.') }}

@php $kt = $stats['keyword_triggers'] ?? []; @endphp @if (count($kt))
@foreach ($kt as $row) @endforeach
{{ __('Trigger keyword(s)') }} {{ __('Flow started') }} {{ __('Fires') }} {{ __('Last fired') }}
@foreach (array_slice(array_filter(array_map('trim', explode(',', (string) $row['keyword']))), 0, 8) as $kw) {{ $kw }} @endforeach {{ $row['flow_name'] }} {{ number_format($row['fires']) }} {{ $row['last_fired'] ? \Illuminate\Support\Carbon::parse($row['last_fired'])->diffForHumans() : '—' }}
@else
{{ __('No keyword-triggered flow runs in this period. When a customer types a keyword that starts a flow, it appears here with the flow it launched.') }}
@endif
{{-- ===== DROP-OFF FUNNEL — where active runs are parked / abandoned ===== --}} @php $do = $stats['drop_off'] ?? []; $doScoped = !empty($stats['flow_id']); @endphp
{{ __('Drop-off') }}

{{ __('Where customers stop replying') }}

{{ __('Active runs waiting at each question right now. A high count — especially “stalled” (no reply for 24h+) — is where the flow is losing people.') }}

@if (count($do))
@unless ($doScoped) @endunless @foreach ($do as $row) @unless ($doScoped) @endunless @endforeach
{{ __('Waiting at') }}{{ __('Flow') }}{{ __('Waiting') }} {{ __('Stalled 24h+') }} {{ __('Last activity') }}
{{ $row['label'] ?: ('#' . $row['node_id']) }}{{ $row['flow_name'] }}{{ number_format($row['waiting']) }} {{ number_format($row['stalled']) }} {{ $row['last_at'] ? \Illuminate\Support\Carbon::parse($row['last_at'])->diffForHumans() : '—' }}
@else
{{ __('No customers are waiting mid-flow right now. When a run pauses at a question and the customer stops replying, it shows here so you can see exactly where the flow drops off.') }}
@endif
{{-- ========== EXECUTION HISTORY ========== --}} {{-- ========== ERROR LOGS ========== --}} {{-- ========== RETRY RECORDS ========== --}} @endif
@if ($flowsTotal > 0) @push('scripts') @endpush @endif