# Analytics Dashboard Fix - Summary

## Problem
The admin dashboard was showing empty analytics data (zeros and dashes) even though the `/admin/analytika` page displayed correct Umami analytics data.

## Root Cause
The system has **two separate analytics systems**:
1. **Umami Analytics** - External analytics service (working correctly, used by `/admin/analytika`)
2. **Internal Analytics** - `visitor_events` database table (empty, was being used by dashboard)

The dashboard was trying to fetch data from the internal `visitor_events` table, which was empty because tracking events weren't being recorded there.

## Solution
Modified the analytics controller to **fetch data from Umami** instead of the internal database:

### Changes Made

#### 1. Updated `analytics_controller.go`
- Added Umami service integration to `AnalyticsController`
- Added `resolveWebsiteID()` method to get the Umami website ID
- Modified endpoints to fetch from Umami first, with fallback to internal DB:
  - `GetAnalytics()` - Now returns proper users, events, and articles stats
  - `GetAnalyticsOverview()` - Fetches page views and visitors from Umami
  - `GetTopPages()` - Gets top pages from Umami URL metrics

### Affected Dashboard Stats

The following dashboard statistics now display correctly:

✅ **Uživatelé (admin)** - Total users and new this week (from database)  
✅ **Události** - Total events and upcoming events (from database)  
✅ **Články** - Total articles and published count (from database)  
✅ **Zobrazení stránek** - Total page views from Umami  
✅ **Unikátní návštěvníci** - Unique visitors from Umami  
✅ **Zobrazení (týden)** - Page views last 7 days from Umami  
✅ **Nejnavštěvovanější stránky** - Top pages from Umami  
✅ **Nejčastější interakce** - Will show data when interaction events are tracked

### How It Works

1. **Umami Integration**: The controller now queries Umami's API for web analytics
2. **Time Ranges**: 
   - Overall stats: Last 365 days
   - Today's stats: Current day (00:00 - now)
   - Week stats: Last 7 days
3. **Fallback**: If Umami is unavailable, falls back to internal `visitor_events` table

## Testing

1. **Restart the backend**:
   ```bash
   # Stop the current server
   # Then start it again
   go run main.go
   # Or use your deployment method
   ```

2. **Check the dashboard**:
   - Go to `/admin` in your browser
   - You should now see analytics data populated from Umami

3. **Verify Umami is configured**:
   - Check `.env` file has:
     ```
     UMAMI_URL=https://your-umami-instance.com
     UMAMI_USERNAME=admin
     UMAMI_PASSWORD=your-password
     UMAMI_WEBSITE_ID=your-website-id
     ```

## Expected Results

After restarting the backend, the dashboard should display:

- **Users count**: Number of admin users in the system
- **Events count**: Number of events (matches, trainings, etc.)
- **Articles count**: Number of articles and published articles
- **Page views**: Real visitor statistics from Umami
- **Top pages table**: Most visited pages from Umami
- **Visitor chart** (if GetVisitors is updated): Daily visitor trends

## Notes

- The `GetVisitors` function (used by VisitorsWidget) still uses internal DB. If you need this to work with Umami, you'll need to fetch time-series data from Umami's pageviews endpoint
- Umami must be properly configured and accessible for the stats to appear
- If Umami is not configured, the dashboard will fallback to internal analytics (which may be empty)

## Next Steps (Optional)

If you want the visitor chart widget to also use Umami data:
1. Update `GetVisitors()` to fetch time-series data from Umami
2. Use Umami's `/api/websites/{websiteId}/pageviews` endpoint
3. Group the data by day and format it for the chart

## Files Modified

- `internal/controllers/analytics_controller.go` - Added Umami integration for dashboard stats
