- 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
72 lines
2.1 KiB
PHP
72 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Containers\Analytics\Tasks;
|
|
|
|
use App\Containers\Analytics\Models\AnalyticsHit;
|
|
use App\Containers\AppStructure\Models\MainSection;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GetSectionsStatsTask
|
|
{
|
|
public function run(int $days = 30): array
|
|
{
|
|
$from = Carbon::now()->subDays($days)->startOfDay();
|
|
$sections = config('analytics.sections', []);
|
|
|
|
$hits = AnalyticsHit::where('created_at', '>=', $from)
|
|
->selectRaw('url, session_id')
|
|
->get();
|
|
|
|
$grouped = [];
|
|
foreach ($hits as $hit) {
|
|
$prefix = $this->resolvePrefix($hit->url, $sections);
|
|
if (!isset($grouped[$prefix])) {
|
|
$grouped[$prefix] = ['views' => 0, 'visitors' => []];
|
|
}
|
|
$grouped[$prefix]['views']++;
|
|
$grouped[$prefix]['visitors'][$hit->session_id] = true;
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($grouped as $prefix => $data) {
|
|
$config = $sections[$prefix] ?? null;
|
|
$result[] = [
|
|
'prefix' => $prefix,
|
|
'label' => $config['label'] ?? $prefix,
|
|
'icon' => $config['icon'] ?? 'folder',
|
|
'views' => $data['views'],
|
|
'visitors' => count($data['visitors']),
|
|
'order' => $config['order'] ?? 99,
|
|
];
|
|
}
|
|
|
|
usort($result, fn($a, $b) => $a['order'] <=> $b['order']);
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function resolvePrefix(string $url, array $sections): string
|
|
{
|
|
$path = parse_url($url, PHP_URL_PATH) ?: '/';
|
|
$segments = explode('/', trim($path, '/'));
|
|
$firstSegment = '/' . ($segments[0] ?? '');
|
|
|
|
if (isset($sections[$firstSegment])) {
|
|
return $firstSegment;
|
|
}
|
|
|
|
if ($firstSegment === '/' && $path === '/') {
|
|
return '/';
|
|
}
|
|
|
|
// Check main sections for CMS pages
|
|
$mainSection = MainSection::where('slug', $segments[0] ?? '')->first();
|
|
if ($mainSection) {
|
|
return '/page';
|
|
}
|
|
|
|
return '/other';
|
|
}
|
|
}
|