# Ads manager (Prompt 6, module `ads`)

## Data

| Table | Notes |
| --- | --- |
| `ad_zones` | `AdZonesSeeder`: new design zones (home top / after Top Stories / sidebar / native in Latest / before footer, story top / in-article / sidebar / end, category, mobile sticky) + old-site banner positions (`is_legacy`: header, right, overlay, footer) |
| `ad_clients` | advertisers |
| `ad_campaigns` | dates, budget, pricing cpm / cpc / flat + rate, priority 1–10, status draft → pending → approved / rejected, paused; `is_political`; `daily_cap`, `impression_goal`, pacing even / asap, frequency cap {count, hours}; targeting {pages, categories, provinces, districts, devices, languages, dayparts [{days, from, to}]}; zones |
| `ad_creatives` | image, gif, html5 (sandboxed iframe `srcdoc`), video, native (title/body/cta); file in Spatie collection **`creative`** (public disk, virus-scanned) |
| `ad_daily_stats` | impressions / clicks per day × campaign × creative × zone |

## Serving

`GET /api/v1/public/ads?zone={key}&context[page]=&context[category]=&context[province]=&context[district]=&context[device]=&context[language]=`
(`private, no-store`; `throttle:ads`). The server takes approved campaigns running now in the zone,
drops political ones while **election silence** is on, matches targeting (Nepal-time dayparts), applies
the **frequency cap** (`ads:freq:{campaign}:{visitor}`), the **daily cap** and **even pacing**
(`ads:day:{date}:{campaign}`), then picks by priority with weighted creatives. Visitor = `X-Visitor-Id`
header or IP + user agent.

Every ad has `sponsored: true` and `label: {ne: प्रायोजित, en: Sponsored}` — always render it.
Beacons: `POST /public/ads/impression {token}` when in view; links go to `click_url`
(`GET /public/ads/click/{token}` counts, then 302). Tokens are HMAC-signed, valid 24 h. Counters live
in Redis DB 5 (`ads:imp:*`, `ads:clk:*`) and `FlushAdStatsJob` (every 5 min, **analytics** queue)
writes them to `ad_daily_stats` (the analytics pipeline of Prompt 7 can read them from there).

## CMS (`/api/v1/cms/ads`)

| Endpoints | Permission |
| --- | --- |
| `GET zones`, `GET clients`, `GET campaigns?status=`, `GET election-silence` | `ads.view` |
| `PATCH zones/{zone}`, `POST/PATCH clients`, `POST/PATCH campaigns`, `POST campaigns/{c}/submit`, `POST/DELETE campaigns/{c}/creatives[/{creative}]` | `ads.campaigns.manage` |
| `POST campaigns/{c}/approve|reject|pause|resume`, `PUT election-silence` | `ads.approve` |
| `GET campaigns/{c}/report?from=&to=`, `GET campaigns/{c}/report.pdf` | `ads.reports.view` |

Editing an approved campaign or its creatives sends it back to **pending**. Submit needs an active
creative; reject needs a reason. Audit: `ad_campaign_submitted|approved|rejected|paused`,
`ad_election_silence_changed` (+ `site_content_*` for clients, campaigns, creatives, zones).

Reports: impressions, clicks, CTR and spend (CPM = impressions / 1000 × rate, CPC = clicks × rate,
flat = budget prorated per day). PDF via **barryvdh/laravel-dompdf** with the bundled Lohit Devanagari
font (`resources/fonts`, SIL OFL); dompdf caches font metrics in `storage/fonts`. dompdf does not shape
complex scripts, so some Devanagari conjuncts may render imperfectly — numbers and Latin text are exact.
