cms/app/Http/Middleware/TrackAnalytics.php
Funky Waddle 37ed997989 feat(cms): initialize Laravel project structure and core CMS files
- Added standard Laravel directory structure and configuration.

- Included Svelte and Tailwind configuration for the admin interface.

- Added core PHPUnit and testing scripts.
2026-04-13 12:48:06 -05:00

51 lines
1.3 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\PageView;
use Jenssegers\Agent\Agent;
class TrackAnalytics
{
/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
// Only track successful GET requests for public pages (outside admin)
if ($request->isMethod('get') &&
$response->getStatusCode() === 200 &&
!$request->is(env('ADMIN_PATH', 'loom') . '*')) {
$agent = new Agent();
$agent->setUserAgent($request->userAgent());
$view = PageView::create([
'path' => $request->getPathInfo(),
'referrer' => $request->headers->get('referer'),
'browser' => $agent->browser() ?: 'Unknown',
'os' => $agent->platform() ?: 'Unknown',
'device_type' => $this->getDeviceType($agent),
'view_date' => now()->toDateString(),
]);
}
return $response;
}
protected function getDeviceType($agent)
{
if ($agent->isTablet()) {
return 'tablet';
}
if ($agent->isMobile()) {
return 'mobile';
}
return 'desktop';
}
}