Files
analytics/app/Http/Controllers/DashboardController.php
T

52 lines
1.6 KiB
PHP
Raw Normal View History

2026-03-24 21:12:21 +01:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Concerns\ParsesDateRange;
use App\Models\Website;
use App\Services\AnalyticsQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class DashboardController extends Controller
{
use ParsesDateRange;
public function show(Request $request, Website $website): View
{
$this->authorize('view', $website);
[$from, $to] = $this->parseDateRange($request);
[$prevFrom, $prevTo] = $this->previousPeriod($from, $to);
$service = AnalyticsQueryService::for($website, $from, $to);
$prevService = AnalyticsQueryService::for($website, $prevFrom, $prevTo);
$metrics = $service->summary();
$prevMetrics = $prevService->summary();
$chartData = $service->visitorsOverTime();
$activeVisitors = AnalyticsQueryService::activeVisitors($website);
$goalConversions = $service->conversionsReport();
$goalChartData = $service->goalCompletionsOverTime();
$topPages = $service->topPages(5);
$topSources = $service->trafficSources(5);
return view('dashboard.show', compact(
'website', 'metrics', 'prevMetrics', 'chartData', 'activeVisitors',
'goalConversions', 'goalChartData', 'from', 'to',
'topPages', 'topSources',
));
}
public function realtime(Website $website): JsonResponse
{
$this->authorize('view', $website);
return response()->json([
'active' => AnalyticsQueryService::activeVisitors($website),
]);
}
}