Files
ladill-link/app/Support/Link/LinkGeoResolver.php
T
isaaccladandCursor 0e5e0f6ca5
Deploy Ladill Link / deploy (push) Successful in 32s
Add device, platform, and country analytics to link show page.
Record parsed user-agent and geo data on clicks, then surface breakdowns and richer recent-click context on per-link and account analytics views.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-27 19:32:40 +00:00

41 lines
986 B
PHP

<?php
namespace App\Support\Link;
use Illuminate\Http\Request;
final class LinkGeoResolver
{
public static function countryCode(Request $request): ?string
{
foreach (['CF-IPCountry', 'X-Country-Code', 'CloudFront-Viewer-Country'] as $header) {
$value = $request->header($header);
if (! is_string($value) || strlen($value) !== 2) {
continue;
}
$code = strtoupper($value);
if (! in_array($code, ['XX', 'T1'], true)) {
return $code;
}
}
return null;
}
public static function countryLabel(?string $code): string
{
if ($code === null || $code === '') {
return 'Unknown';
}
if (function_exists('locale_get_display_region')) {
$name = locale_get_display_region('_'.$code, 'en');
return is_string($name) && $name !== '' ? $name : $code;
}
return $code;
}
}