shetabit/visitor
Track and log Laravel visitors: browser, platform, device, IP, languages, user agent, and request data. Use visitor() helper or $request->visitor(). Record visits for any model via Visitable trait, count views/unique visitors, and detect online users.
Visitable and Visitor traits enable model-agnostic visit tracking, reducing boilerplate for domain-specific logic (e.g., tracking visits to Post, Product, or User models). This is ideal for domain-driven design (DDD) where entities need lightweight observability.LogVisits middleware provides a declarative approach to visit tracking, leveraging Laravel’s middleware pipeline. This is a clean fit for HTTP-driven applications where visits are tied to routes/models (e.g., Route::model('post', Post::class)).onlineVisitors() and isOnline() methods offer a lightweight alternative to WebSockets for real-time presence, suitable for collaborative tools or admin dashboards where live user counts are needed.visits table, which is a standard Laravel pattern (e.g., php artisan vendor:publish --provider="Shetabit\Visitor\Provider\VisitorServiceProvider"). Teams familiar with Laravel migrations will find this straightforward.LogVisits middleware automatically logs visits for all routes with model binding. This could lead to unintended logging (e.g., API endpoints, admin routes) if not configured carefully. Mitigation: Exclude sensitive routes via middleware configuration (e.g., except in LogVisits).visit() writes to a queue instead of DB directly).purgeLogs() method to comply with data retention policies./admin, /api) from logging?referrer, campaign_id)?Visitor facade).Post, Product, User)./admin, /api/webhooks).composer require shetabit/visitor
php artisan vendor:publish --provider="Shetabit\Visitor\Provider\VisitorServiceProvider"
php artisan migrate
app/Http/Kernel.php:
protected $middleware = [
\Shetabit\Visitor\Middlewares\LogVisits::class,
];
public function handle($request, Closure $next)
{
if ($request->is('admin/*') || $request->is('api/*')) {
return $next($request);
}
// ... rest of logic
}
Visitable trait to models for visit tracking:
use Shetabit\Visitor\Traits\Visitable;
class Post extends Model
{
use Visitable;
}
Visitor trait to User model for online detection:
use Shetabit\Visitor\Traits\Visitor;
class User extends Authenticatable
{
use Visitor;
}
$request->visitor()->browser()).visitor()->visit($post)).User::online()->count()).v4.5.4 if using Laravel 10.visits table.Visitable trait to 2–3 critical models (e.g., Post, Product).LogVisits middleware with route exclusions.How can I help you explore Laravel packages today?