@if (session('status') || $errors->any()) @push('scripts') @endpush @endif @php $statusKey = strtolower((string) $campaign->status); $statusBadge = match ($statusKey) { 'completed' => 'bg-ink-900 text-paper-0', 'running' => 'bg-wa-green/15 text-wa-deep border border-wa-green/30', 'scheduled' => 'bg-accent-amber/15 text-ink-800 border border-accent-amber/30', 'failed' => 'bg-accent-coral/15 text-accent-coral border border-accent-coral/30', 'cancelled' => 'bg-paper-100 text-ink-600 border border-paper-200', 'paused' => 'bg-paper-100 text-ink-700 border border-paper-200', default => 'bg-paper-50 text-ink-700 border border-paper-200', }; @endphp
WA Campaigns / Analytics / #{{ $campaign->id }}
{{ $campaign->campaign_name }}
{{ ucfirst($statusKey ?: 'draft') }} @if (in_array($statusKey, ['scheduled', 'paused', 'draft'], true))
@csrf
@endif @if ($statusKey === 'running')
@csrf
@endif @if (in_array($statusKey, ['paused', 'cancelled'], true))
@csrf
@endif {{-- Edit ⇄ Resend, status-gated + mutually exclusive. Edit only for draft / scheduled / paused; Resend only for completed / failed / cancelled. Running shows neither — it's in flight. --}} @if (in_array($statusKey, ['draft', 'scheduled', 'paused'], true)) Edit @elseif (in_array($statusKey, ['completed', 'failed', 'cancelled'], true)) {{-- Resend used to be one button that always re-sent to EVERY recipient — which re-messages people who already got it and burns quota. Split into scoped actions; "failed only" is first because it is almost always the intent and a wrong resend can't be undone. --}}
@endif
@csrf @method('DELETE')
@php // "Sent but not delivered" = WhatsApp accepted the message but hasn't // confirmed delivery (or reported a failure). Surface the EXACT Meta // reason here so the operator never has to dig through logs. $sentN = (int) ($campaign->sent_count ?? 0); $deliveredN = (int) ($campaign->delivered_count ?? 0); $undeliveredN = max(0, $sentN - $deliveredN); // WABA-ONLY banner. The wording ("WhatsApp has accepted these // messages…") describes Meta Cloud's async accept→webhook delivery // model. Unofficial (Baileys) sends directly and Twilio reports via // its own StatusCallback, so a "not confirmed by WhatsApp" notice is // misleading there — show it only for WABA campaigns. $isWabaCampaign = strtolower((string) ($campaign->provider ?? '')) === 'waba'; @endphp @if ($isWabaCampaign && ($undeliveredN > 0 || !empty($deliveryIssueReason)))
{{ $undeliveredN }} {{ __('sent, delivery not confirmed by WhatsApp') }}
{{-- Show ONLY the exact message WhatsApp/Meta returned — never our own explanation. Empty until Meta reports a reason for a recipient. --}} @if (!empty($deliveryIssueReason))
{{ __("WhatsApp's response") }}: {{ $deliveryIssueReason }}
@endif
@if ((int) $failedCount > 0) {{ __('Exact per-recipient responses from WhatsApp appear under the Failures tab.') }} @else {{ __('WhatsApp has accepted these messages but has not yet returned a delivery or failure update for them.') }} @endif
@endif
{{ ucfirst((string) $campaign->campaign_type) }} {{ __('campaign') }} {{ wa_local($campaign->created_at, $campaign->timezone)?->format('M j, Y H:i') ?? '—' }}

{{ __('Campaign analytics') }}

{{ __('Full delivery, engagement, recipient, reply, and error performance for this WhatsApp broadcast.') }}

{{-- Header tiles. ROI/Audience/Cost/CPC/Quality are derived in the controller from campaign counters + the recipient log's contact-group makeup. The "Device" tile is read directly off the campaign->device relation (TODO: replace with real cost-tracking + revenue tables when those land). --}}
{{ __('Delivered') }}
{{ $header['delivery_rate'] }}%
{{ __('Read') }}
{{ $header['read_rate'] }}%
{{ __('Replied') }}
{{ $header['reply_rate'] }}%
{{ __('Clicked') }}
{{ $header['click_rate'] }}%
{{ __('Audience') }}
{{-- Wrap to 2 lines instead of a hard truncate — a group name + "N manual" never fits one line in this tile. Full value stays available as the tooltip. --}}
{{ $header['audience'] }}
{{ __('Device') }}
{{-- Name / number, not the raw row id. Resolved in the controller (device_id is polymorphic per engine). --}}
{{ $header['device_label'] ?? '—' }}
@if (!empty($followupStats)) @endif
@php $deliveredPct = $campaign->total_recipients > 0 ? ($campaign->delivered_count / max($campaign->total_recipients, 1)) * 100 : 0; $readPct = $campaign->delivered_count > 0 ? ($campaign->read_count / max($campaign->delivered_count, 1)) * 100 : 0; $replyPct = $campaign->total_recipients > 0 ? ($campaign->responded_count / max($campaign->total_recipients, 1)) * 100 : 0; $clickPct = $campaign->total_recipients > 0 ? ($campaign->clicked_count / max($campaign->total_recipients, 1)) * 100 : 0; $failedPct = $campaign->total_recipients > 0 ? ($campaign->failed_count / max($campaign->total_recipients, 1)) * 100 : 0; @endphp {{-- KPI tiles — every counter + percent span carries a data-wac-detail-totals hook so user-wa-campaigns-detail.js can repaint live as the Node bridge fires delivered / read / failed callbacks. data-campaign-id on the wrapper lets the JS find the right id even if the URL is rewritten. --}} {{-- Live failure reasons — filled by the 15s poll (paintFailures). Shows the actual blocker (template not approved, channel disabled, …) without a page reload; hidden while nothing has failed. Server-rendered on first paint so it's correct even before the first poll. --}} @php $liveFailSummary = \App\Models\WpCampaignContact::where('campaign_id', $campaign->id) ->where('status', 'failed')->whereNotNull('error_message') ->selectRaw('error_message, COUNT(*) as c') ->groupBy('error_message')->orderByDesc('c')->limit(5)->get(); @endphp
@if ($liveFailSummary->isNotEmpty())
{{ __('Why sends are failing') }}
    @foreach ($liveFailSummary as $f)
  • {{ $f->c }} {{ $f->error_message }}
  • @endforeach
@endif
{{ __('Total recipients') }}
{{ number_format($campaign->total_recipients) }}
{{ __('Audience size after duplicate cleanup') }}
{{ __('Delivered') }}
{{ number_format($campaign->delivered_count) }}
{{ number_format($deliveredPct, 1) }}% delivery rate
{{ __('Read') }}
{{ number_format($campaign->read_count) }}
{{ number_format($readPct, 1) }}% of delivered
{{ __('Replies') }}
{{ number_format($campaign->responded_count) }}
{{ number_format($replyPct, 1) }}% conversation rate
{{ __('Button taps') }}
{{ number_format($campaign->clicked_count) }}
{{ number_format($clickPct, 1) }}% of total
{{ __('Failed') }}
{{ number_format($campaign->failed_count) }}
{{ number_format($failedPct, 1) }}% failure rate
{{ __('Delivery curve') }}

{{ __('Sent, delivered, read') }}

Sent Delivered Read
{{ __('Outcome mix') }}

{{ __('Final status') }}

{{ __('Conversion funnel') }}

{{ __('From send to reply') }}

{{ __('Recipients') }}{{ number_format($funnel['recipients']) }}
{{ __('Delivered') }}{{ number_format($funnel['delivered']) }} / {{ $funnel['delivered_pct'] }}%
{{ __('Read') }}{{ number_format($funnel['read']) }} / {{ $funnel['read_pct'] }}%
{{ __('Clicked') }}{{ number_format($funnel['clicked']) }} / {{ $funnel['clicked_pct'] }}%
{{ __('Replied') }}{{ number_format($funnel['replied']) }} / {{ $funnel['replied_pct'] }}%
{{ __('Active hours') }}

{{ __('Read heatmap') }}

{{ __('Best segments') }}

{{ __('Top performers') }}

@if (empty($segments))
{{ __('No segment-level data yet. Once recipients reply or read, the breakdown by contact group will appear here.') }}
@else
@foreach ($segments as $i => $seg)
{{ $seg['name'] }} {{ $seg['read_pct'] }}% read
{{ number_format($seg['recipients']) }} recipient{{ $seg['recipients'] === 1 ? '' : 's' }}, {{ number_format($seg['replies']) }} repl{{ $seg['replies'] === 1 ? 'y' : 'ies' }}, {{ $seg['recipients'] > 0 ? round(($seg['opt_outs'] / $seg['recipients']) * 100, 1) : 0 }}% opt-out.
@endforeach
@endif
{{-- Opt-outs — WHO left, not just how many. The page previously carried only a bare opt-out count, so there was no way to follow up, clean a list, or evidence that an opt-out was honoured. --}} @if (!empty($followupStats)) @endif
@push('scripts') {{-- Server-rendered chart payload consumed by resources/js/charts/user-wa-campaigns-detail.js. --}} @endpush