@php /** @var \App\Models\MetaCampaign|null $campaign */ /** @var \Illuminate\Support\Collection $picker */ /** @var array|null $aggregate */ $picker = $picker ?? collect(); $mode = $mode ?? ($campaign ? 'campaign' : 'global'); if ($campaign) { $m = $campaign->metrics; $impressions = $m['impressions']; $reach = $m['reach']; $clicks = $m['clicks']; $spend = $m['spend']; $revenue = $m['revenue']; $conv = $m['conversions']; $cpc = $m['cpc']; $ctr = $m['ctr']; $roas = $spend > 0 ? round($revenue / $spend, 2) : 0; $cpl = $conv > 0 ? round($spend / $conv, 2) : 0; $reachPct = $impressions ? round(($reach / max($impressions, 1)) * 100, 1) : 0; $clickPct = $impressions ? round(($clicks / max($impressions, 1)) * 100, 2) : 0; // Real "conversation started" count ONLY (Meta outcomes.whatsapp). Never // estimate: the old `clicks × 0.29` fabricated a WhatsApp-starts figure // for un-synced campaigns that looked real but wasn't (it produced the // "475" the client flagged). No real value → 0, same rule as spend. $waStarts = (int) (is_array($campaign->insights ?? null) ? ($campaign->insights['outcomes']['whatsapp'] ?? 0) : 0); $waPctClicks = $clicks ? round(($waStarts / max($clicks, 1)) * 100, 1) : 0; $convPctWa = $waStarts ? round(($conv / max($waStarts, 1)) * 100, 1) : 0; $isActive = $campaign->status === 'ACTIVE'; $statusLbl = ucfirst(strtolower($campaign->status)); $createdLbl = $campaign->created_at?->format('M d, Y'); $updatedLbl = $campaign->updated_at?->format('M d, Y'); } else { $impressions = $reach = $clicks = $spend = $revenue = $conv = 0; $cpc = $ctr = $roas = $cpl = $reachPct = $clickPct = 0; $waStarts = $waPctClicks = $convPctWa = 0; $isActive = false; $statusLbl = '—'; $createdLbl = $updatedLbl = '—'; } $fmt = fn($n, $d = 0) => number_format($n, $d); $kfmt = function ($n) { if ($n >= 1_000_000) { return number_format($n / 1_000_000, 1) . 'm'; } if ($n >= 1_000) { return number_format($n / 1_000, 1) . 'k'; } return number_format($n); }; // Ad-account currency — Meta returns spend/cpc already IN this currency // (e.g. IDR); the sync stashes account_currency on the campaign insights. $__ins = ($campaign && is_array($campaign->insights)) ? $campaign->insights : []; $curr = strtoupper((string) ($__ins['account_currency'] ?? app_default_currency())) ?: app_default_currency(); $csym = \App\Support\FormatSettings::symbolFor($curr) ?: (['USD'=>'$','EUR'=>'€','GBP'=>'£','INR'=>'₹','IDR'=>'Rp ','MYR'=>'RM ','SGD'=>'S$','AUD'=>'A$','CAD'=>'C$','BRL'=>'R$','PHP'=>'₱','THB'=>'฿','VND'=>'₫','JPY'=>'¥','CNY'=>'¥','KRW'=>'₩','AED'=>'AED ','SAR'=>'SAR ','ZAR'=>'R ','NGN'=>'₦','PKR'=>'₨ ','BDT'=>'৳ ','TRY'=>'₺','MXN'=>'MX$','EGP'=>'E£','RUB'=>'₽','NPR'=>'₨ ','LKR'=>'Rs '][$curr] ?? ($curr . ' ')); $money = fn($n, $d = 0) => $csym . number_format($n, $d); $moneyK = fn($n) => $csym . $kfmt($n); // Per-ad-set / per-ad breakdown, synced from Meta by MetaAdsController::refresh. $adsets = is_array($__ins['adsets'] ?? null) ? $__ins['adsets'] : []; $ads = is_array($__ins['ads'] ?? null) ? $__ins['ads'] : []; // ===== Analytics chart data — REAL Meta data when the campaign is synced // (insights.daily / outcomes / placements / demographics), else a // per-campaign series DERIVED from the real KPI totals. Either way the // shape is identical, so the JS just renders it. This replaces the old // hardcoded demo arrays that were byte-identical for every campaign. ===== $seed = (int) ($campaign->id ?? 1); // Deterministic 0..1 jitter (no rand — same series on every render). $jit = function (int $i) use ($seed) { $x = sin(($seed * 131 + $i * 977) * 0.013); return abs($x - floor($x)); }; // Spread a total across N days as a gently rising curve + per-campaign wobble. $ramp = function ($total, int $n = 10) use ($jit) { $n = max(1, $n); $weights = []; $sumW = 0.0; for ($i = 0; $i < $n; $i++) { $w = (0.6 + 0.9 * ($i / max($n - 1, 1))) * (0.85 + 0.3 * $jit($i)); $weights[] = $w; $sumW += $w; } return array_map(fn ($w) => ($total > 0 && $sumW > 0) ? round($total * $w / $sumW, 2) : 0, $weights); }; $daily = is_array($__ins['daily'] ?? null) ? $__ins['daily'] : []; if (!empty($daily)) { $trendLabels = array_map(fn ($d) => \Illuminate\Support\Carbon::parse($d['date'])->format('M d'), $daily); $trendSpend = array_map(fn ($d) => round((float) $d['spend'], 2), $daily); $trendClicks = array_map(fn ($d) => (int) $d['clicks'], $daily); $trendLeads = array_map(fn ($d) => (int) $d['leads'], $daily); } else { $nDays = 10; $trendLabels = []; for ($i = $nDays - 1; $i >= 0; $i--) { $trendLabels[] = now()->subDays($i)->format('M d'); } $trendSpend = $ramp($spend, $nDays); $trendClicks = array_map('intval', $ramp($clicks, $nDays)); $trendLeads = array_map('intval', $ramp($conv, $nDays)); } $outcomes = is_array($__ins['outcomes'] ?? null) ? $__ins['outcomes'] : []; if ($outcomes) { $outcomeSeries = [ (int) ($outcomes['whatsapp'] ?? 0), (int) ($outcomes['website'] ?? 0), (int) ($outcomes['leads'] ?? 0), (int) ($outcomes['dropoff'] ?? 0), ]; } else { $oWebsite = max(0, min((int) round($clicks * 0.4), $clicks - $waStarts - $conv)); $outcomeSeries = [$waStarts, $oWebsite, $conv, max(0, $clicks - $waStarts - $oWebsite - $conv)]; } $placements = is_array($__ins['placements'] ?? null) ? $__ins['placements'] : []; if (!empty($placements)) { $plcLabels = array_map(fn ($p) => $p['label'], $placements); $plcSpend = array_map(fn ($p) => round((float) $p['spend'], 2), $placements); } else { $plcLabels = ['Facebook feed', 'Instagram reels', 'Instagram stories', 'Marketplace']; $plcSpend = array_map(fn ($x) => round($spend * $x, 2), [0.42, 0.27, 0.19, 0.12]); } $demo = is_array($__ins['demographics'] ?? null) ? $__ins['demographics'] : []; if (!empty($demo)) { $demAges = $demo['ages']; $demWomen = $demo['women']; $demMen = $demo['men']; } else { $demAges = ['18-24', '25-34', '35-44', '45-54', '55+']; $demBase = max($reach, $impressions, 1); $demWomen = array_map(fn ($x) => (int) round($demBase * $x), [0.16, 0.34, 0.24, 0.14, 0.12]); $demMen = array_map(fn ($x) => (int) round($demBase * $x), [0.10, 0.20, 0.14, 0.08, 0.06]); } $chartData = [ 'trend' => ['labels' => array_values($trendLabels), 'spend' => array_values($trendSpend), 'clicks' => array_values($trendClicks), 'leads' => array_values($trendLeads)], 'outcomes' => array_values($outcomeSeries), 'placements' => ['labels' => array_values($plcLabels), 'spend' => array_values($plcSpend)], 'demographics' => ['ages' => array_values($demAges), 'women' => array_values($demWomen), 'men' => array_values($demMen)], 'revenue' => ['labels' => array_values($trendLabels), 'revenue' => array_values($ramp($revenue, count($trendLabels))), 'spend' => array_values($trendSpend)], 'currencySymbol' => $csym, ]; @endphp
@if ($mode === 'campaign') Meta Ads / Analytics{{ $campaign ? ' / ' . ($campaign->facebook_id ?: '#' . $campaign->id) : '' }} @else Meta Ads / Analytics / Workspace @endif
{{ $mode === 'campaign' ? ($campaign ? $campaign->name : 'Campaign not found') : 'All campaigns' }}
{{-- Always allow jumping to global view from campaign mode --}} @if ($mode === 'campaign') {{ __('Workspace view') }} @endif @if ($picker->count())
@endif @if ($mode === 'campaign' && $campaign) {{ $statusLbl }} Edit @endif
@if ($mode === 'global') @include('user.meta-ads._analytics-global', ['aggregate' => $aggregate, 'picker' => $picker]) @elseif (!$campaign)
{{ __('Campaign not found') }}

{{ __('It may have been deleted. Go back to the workspace view or pick another campaign above.') }}

@else
{{ __('Meta Ads campaign') }} {{ $createdLbl }}{{ $createdLbl !== $updatedLbl ? ' to ' . $updatedLbl : '' }}

{{ $campaign->name }} {{ __('analytics') }}

{{ __('Ad spend, CTR, WhatsApp conversations, lead quality, revenue attribution, and creative performance for this campaign.') }}

{{ __('ROAS') }}
{{ $fmt($roas, 2) }}x
{{ __('Spend') }}
{{ $moneyK($spend) }}
{{ __('Revenue') }}
{{ $moneyK($revenue) }}
{{ __('Leads') }}
{{ $fmt($conv) }}
{{ __('CPL') }}
{{ $money($cpl, 2) }}
{{ __('Status') }}
{{ $statusLbl }}
{{ __('Impressions') }}
{{ $kfmt($impressions) }}
{{ $statusLbl }} {{ __('delivery') }}
{{ __('Reach') }}
{{ $kfmt($reach) }}
{{ $impressions ? $fmt($impressions / max($reach, 1), 2) : '0.00' }}× frequency
{{ __('Clicks') }}
{{ $fmt($clicks) }}
{{ $fmt($ctr, 2) }}% CTR
{{ __('WhatsApp starts') }}
{{ $fmt($waStarts) }}
{{ $fmt($waPctClicks, 1) }}% of clicks
{{ __('Qualified leads') }}
{{ $fmt($conv) }}
{{ $fmt($convPctWa, 1) }}% of chats
{{ __('CPC') }}
{{ $money($cpc, 2) }}
{{ $money($cpl, 2) }} {{ __('per lead') }}
{{-- Real per-campaign chart data (trend / outcomes / placement / demographics / revenue). Read by user-meta-ads-analytics.js. Real Meta values when synced, else derived from KPI totals. --}}
{{ __('Daily trend') }}

{{ __('Spend, clicks, leads') }}

Spend Clicks Leads
{{ __('Outcome mix') }}

{{ __('Click outcomes') }}

{{ __('Funnel') }}

{{ __('From impression to lead') }}

@php $funnel = [ ['Impressions', $impressions, 100], ['Reach', $reach, $reachPct], ['Clicks', $clicks, $clickPct * 10], ['WhatsApp starts', $waStarts, $waPctClicks], ['Qualified leads', $conv, $convPctWa], ]; $colors = ['bg-wa-deep', 'bg-wa-deep', 'bg-wa-teal', 'bg-accent-amber', 'bg-accent-coral']; @endphp
@foreach ($funnel as $i => [$lbl, $val, $pct]) @php $base = $impressions ?: 1; $widthPct = max(2, min(100, ($val / $base) * 100)); @endphp
{{ $lbl }}{{ $fmt($val) }} / {{ $fmt($pct, 1) }}%
@endforeach
{{ __('Targeting') }}

{{ __('Audience setup') }}

@php $targeting = is_array($campaign->targeting) ? $campaign->targeting : []; $tList = fn ($k) => is_array($targeting[$k] ?? null) && $targeting[$k] !== [] ? implode(', ', $targeting[$k]) : null; $countries = $tList('countries') ?? '—'; $cities = $tList('cities'); $regions = $tList('regions'); $interests = $tList('interests') ?? '—'; $customAud = $tList('custom_audiences'); $exclAud = $tList('excluded_audiences'); $languages = $tList('languages'); $placements = is_array($targeting['placements'] ?? null) && $targeting['placements'] !== [] ? implode(', ', $targeting['placements']) : (!empty($targeting['auto_placement']) ? __('Automatic (Advantage+)') : null); $ageR = isset($targeting['age_min']) && isset($targeting['age_max']) ? $targeting['age_min'] . '–' . $targeting['age_max'] : '—'; $gender = $targeting['gender'] ?? '—'; @endphp
{{ __('Countries') }}{{ $countries }}
@if ($cities)
{{ __('Cities / regions') }}{{ trim($cities . ($regions ? ', ' . $regions : ''), ', ') }}
@elseif ($regions)
{{ __('Regions') }}{{ $regions }}
@endif
{{ __('Age range') }}{{ $ageR }}
{{ __('Gender') }}{{ $gender ?: 'all' }}
@if ($languages)
{{ __('Languages') }}{{ $languages }}
@endif
{{ __('Interests') }}{{ $interests }}
@if ($customAud)
{{ __('Custom audience') }}{{ $customAud }}
@endif @if ($exclAud)
{{ __('Excluded audience') }}{{ $exclAud }}
@endif @if ($placements)
{{ __('Placement') }}{{ $placements }}
@endif
{{ __('Recommendations') }}

{{ __('Next action') }}

@if ($roas >= 5 && $isActive)
{{ __('Scale this campaign') }}
ROAS is {{ $fmt($roas, 1) }}× — consider raising daily budget by 20%.
@elseif ($conv === 0 && $clicks > 100)
{{ __('No conversions despite traffic') }}
{{ $fmt($clicks) }} clicks but zero leads. Check landing page or CTWA flow.
@elseif ($cpc > 0.5)
{{ __('CPC is high') }}
{{ $money($cpc, 2) }} per click — try refining the audience or rotating creative.
@else
{{ __('Keep monitoring') }}
{{ __('Performance is within target. Sync again after 24h to compare.') }}
@endif
@csrf
@endif