- Add Analytics container: models, tasks, jobs, controllers, routes, services - Add analytics config with section groups (structure, institute, content, services) - Add database tables: analytics_sessions, analytics_hits, analytics_daily_stats - Add frontend: Analytics page, section detail, chart, overview, navigation components - Add consent banner and JS tracking service (sendBeacon + Inertia listener) - Add permission sync for view_analytics, view_any_analytic - Fix trailing slash URL matching in GetNavigationSectionsTask - Fix event propagation on expand arrows in navigation sections - Fix RecordVisitJob to not depend on dropped analytics_geo_cache table - Replace plain text views/visitors with badge-style icons - Group navigation sections by category with separate blocks - Add config-driven non-CMS sections (faculties, divisions, DPO, journals) - Remove Events, TV, Sveden from analytics sections
51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Containers\Analytics\Tasks;
|
|
|
|
use App\Containers\Analytics\Models\AnalyticsDailyStat;
|
|
use App\Containers\Analytics\Models\AnalyticsHit;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GetDailyStatsTask
|
|
{
|
|
public function run(int $days = 30): array
|
|
{
|
|
$from = Carbon::now()->subDays($days)->startOfDay();
|
|
|
|
$stats = AnalyticsDailyStat::where('date', '>=', $from)
|
|
->selectRaw('
|
|
date,
|
|
SUM(views_count) as total_views,
|
|
SUM(unique_visitors) as total_unique
|
|
')
|
|
->groupBy('date')
|
|
->orderBy('date')
|
|
->get();
|
|
|
|
if ($stats->isEmpty()) {
|
|
return AnalyticsHit::where('created_at', '>=', $from)
|
|
->selectRaw('
|
|
DATE(created_at) as hit_date,
|
|
COUNT(*) as total_views,
|
|
COUNT(DISTINCT session_id) as total_unique
|
|
')
|
|
->groupBy(DB::raw('DATE(created_at)'))
|
|
->orderBy('hit_date')
|
|
->get()
|
|
->map(fn($row) => [
|
|
'date' => (string) $row->hit_date,
|
|
'views' => (int) $row->total_views,
|
|
'visitors' => (int) $row->total_unique,
|
|
])
|
|
->toArray();
|
|
}
|
|
|
|
return $stats->map(fn($row) => [
|
|
'date' => $row->date instanceof Carbon ? $row->date->format('Y-m-d') : (string) $row->date,
|
|
'views' => (int) $row->total_views,
|
|
'visitors' => (int) $row->total_unique,
|
|
])->toArray();
|
|
}
|
|
}
|