Files
ladill-link/app/Support/Link/LinkUserAgentParser.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

44 lines
1.6 KiB
PHP

<?php
namespace App\Support\Link;
final class LinkUserAgentParser
{
/**
* @return array{device_type: string, browser: string, os: string}
*/
public static function parse(string $userAgent): array
{
$uaLower = strtolower($userAgent);
$device = str_contains($uaLower, 'mobile') || str_contains($uaLower, 'android') || str_contains($uaLower, 'iphone')
? 'mobile'
: (str_contains($uaLower, 'tablet') || str_contains($uaLower, 'ipad') ? 'tablet' : 'desktop');
$browser = match (true) {
str_contains($uaLower, 'edg/') => 'Edge',
str_contains($uaLower, 'chrome/') && ! str_contains($uaLower, 'edg/') => 'Chrome',
str_contains($uaLower, 'safari/') && ! str_contains($uaLower, 'chrome/') => 'Safari',
str_contains($uaLower, 'firefox/') => 'Firefox',
str_contains($uaLower, 'instagram') => 'Instagram',
str_contains($uaLower, 'whatsapp') => 'WhatsApp',
default => 'Other',
};
$os = match (true) {
str_contains($uaLower, 'iphone') || str_contains($uaLower, 'ipad') => 'iOS',
str_contains($uaLower, 'android') => 'Android',
str_contains($uaLower, 'windows') => 'Windows',
str_contains($uaLower, 'mac os') || str_contains($uaLower, 'macintosh') => 'macOS',
str_contains($uaLower, 'linux') => 'Linux',
default => 'Other',
};
return [
'device_type' => $device,
'browser' => $browser,
'os' => $os,
];
}
}