# Phase 6 — Analytics & Reporting
## Weeks 11–12

---

## STEP 6.1 — Analytics Data Sync

### Command: `analytics:sync` (daily at 02:00)

For each active SocialAccount:
- Fetch follower count, impressions, engagements via platform API
- Store/update `analytics_snapshots` record for today

For each published Post:
- Fetch per-post metrics (likes, comments, shares, clicks)
- Store in `post_analytics` table (new migration):
```php
Schema::create('post_analytics', function (Blueprint $table) {
    $table->id();
    $table->foreignId('post_id')->constrained()->cascadeOnDelete();
    $table->integer('impressions')->default(0);
    $table->integer('reach')->default(0);
    $table->integer('likes')->default(0);
    $table->integer('comments')->default(0);
    $table->integer('shares')->default(0);
    $table->integer('saves')->default(0);
    $table->integer('clicks')->default(0);
    $table->integer('video_views')->default(0);
    $table->integer('video_watch_time')->default(0);  // seconds
    $table->decimal('engagement_rate', 5, 2)->default(0);
    $table->date('fetched_for');
    $table->timestamps();
});
```

---

## STEP 6.2 — Analytics Dashboard

**Route:** `GET /analytics` → Livewire: `Analytics\AnalyticsDashboard`

Matches Hootsuite Analytics UI:

### KPI Summary Cards (top row):
- Posts Sent | Total Engagement | Total Clicks | Website Visits
- Each card: big number + % change vs previous period + mini sparkline (Chart.js)

### Metrics Summary Section:
- Date range picker (custom range or presets: 7d, 30d, 90d)
- Platform filter dropdown
- Tab switcher: Engagement | Post Engagement Rate | Clicks | Website Visits
- Line chart (Chart.js) showing daily metric over selected range
- Tooltip on hover: breakdown (Likes, Comments, Shares, Clicks)

### Top Posts table (right side):
- Post thumbnail + caption snippet
- Engagement count + % change badge
- Clicks count
- Date
- Post type badge (carousel, video, image, text)
- "View post performance" link

### Bottom section — full Top Posts table:
- Sortable columns
- Pagination (25 per page)

---

## STEP 6.3 — Per-Platform Report Pages

**Route:** `GET /analytics/{platform}` → Livewire: `Analytics\PlatformReport`

- Follower growth line chart
- Best day of week bar chart
- Best time of day heatmap (hour × engagement)
- Content type performance (pie/donut chart: image vs video vs carousel)
- Hashtag performance ranking table

---

## STEP 6.4 — PDF + CSV Export

### `app/Services/Analytics\ReportExportService.php`
```php
public function exportPDF(User $user, array $params): string  // returns file path
// Uses barryvdh/laravel-dompdf
// Includes brand logo at top, date range, all charts as images (Chart.js → canvas → base64)

public function exportCSV(User $user, array $params): StreamedResponse
// Uses maatwebsite/excel
// Columns: Date, Platform, Post Caption, Impressions, Reach, Likes, Comments, Shares, Clicks, Engagement Rate
```

### Scheduled Report Email:
- `ReportEmailNotification` sent weekly/monthly based on user preference
- Setting in `GET /settings/notifications`

---

## COMPLETION CHECKLIST
- [ ] analytics:sync command fetching real data from all platforms
- [ ] post_analytics migration + model
- [ ] KPI summary cards with sparklines
- [ ] Date range picker with presets
- [ ] Engagement line chart (Chart.js) with hover tooltip
- [ ] Top Posts table (sortable, paginated)
- [ ] Per-platform report pages
- [ ] Best time heatmap (hour × day grid)
- [ ] Content type donut chart
- [ ] Hashtag performance table
- [ ] PDF export with brand logo
- [ ] CSV export
- [ ] Scheduled report email (weekly/monthly)

When done: update to `7`, commit `feat(phase6): complete analytics and reporting`, stop.
