- 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
57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Containers\Analytics\Services;
|
|
|
|
use App\Containers\Analytics\Models\AnalyticsGeoCache;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GeoIpService
|
|
{
|
|
private const CACHE_TTL_DAYS = 7;
|
|
|
|
public function locate(string $ip): ?array
|
|
{
|
|
$ipHash = sha1($ip);
|
|
|
|
$cached = AnalyticsGeoCache::where('ip_hash', $ipHash)
|
|
->where('cached_at', '>', now()->subDays(self::CACHE_TTL_DAYS))
|
|
->first();
|
|
|
|
if ($cached) {
|
|
return [
|
|
'country' => $cached->country,
|
|
'city' => $cached->city,
|
|
];
|
|
}
|
|
|
|
try {
|
|
$response = Http::timeout(3)->get("http://ip-api.com/json/{$ip}", [
|
|
'fields' => 'country,city',
|
|
]);
|
|
|
|
if ($response->successful()) {
|
|
$data = $response->json();
|
|
|
|
AnalyticsGeoCache::updateOrCreate(
|
|
['ip_hash' => $ipHash],
|
|
[
|
|
'country' => $data['country'] ?? null,
|
|
'city' => $data['city'] ?? null,
|
|
'cached_at' => now(),
|
|
]
|
|
);
|
|
|
|
return [
|
|
'country' => $data['country'] ?? null,
|
|
'city' => $data['city'] ?? null,
|
|
];
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::warning('GeoIP lookup failed', ['ip' => $ip, 'error' => $e->getMessage()]);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|