PHP code example of ghdj / laravel-visitor-tracker
1. Go to this page and download the library: Download ghdj/laravel-visitor-tracker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// In routes/web.php
Route::middleware(['track-visitor'])->group(function () {
Route::get('/', [HomeController::class, 'index']);
// ... more routes
});
// Or globally in bootstrap/app.php (Laravel 11)
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\Ghdj\VisitorTracker\Middleware\TrackVisitor::class,
]);
})
use Ghdj\VisitorTracker\Facades\VisitorTracker;
// Get statistics
$stats = VisitorTracker::stats();
// Total visitors (unique)
$totalVisitors = $stats->totalVisitors();
// Total page views
$totalPageViews = $stats->totalPageViews();
// Currently online visitors
$onlineNow = $stats->onlineVisitors();
// Today's visitors
$todayVisitors = $stats->todayVisitors();
// Visitors in last N days
$weeklyVisitors = $stats->visitorsLastDays(7);
// Most visited pages
$topPages = $stats->mostVisitedPages(10);
// Top referrers
$topReferrers = $stats->topReferrers(10);
// Browser statistics
$browsers = $stats->browserStats();
// Platform/OS statistics
$platforms = $stats->platformStats();
// Device type statistics
$devices = $stats->deviceStats();
// Country statistics (
// Get tracker instance
$tracker = visitor();
// Get statistics
$stats = visitor()->stats()->summary();
$online = visitor_stats()->onlineVisitors();
use Ghdj\VisitorTracker\Models\Visitor;
use Ghdj\VisitorTracker\Models\Visit;
// Get all visitors
$visitors = Visitor::all();
// Get online visitors
$online = Visitor::online()->get();
// Get visitors excluding bots
$humans = Visitor::excludeBots()->get();
// Get authenticated visitors only
$authenticated = Visitor::authenticated()->get();
// Get visitors from date range
$recent = Visitor::between(now()->subWeek(), now())->get();
// Get visits for a specific path
$homeVisits = Visit::path('/')->get();
// Get visits with referrers
$referred = Visit::withReferrer()->get();
use Ghdj\VisitorTracker\Services\UserAgentParser;
use Ghdj\VisitorTracker\Services\BotDetector;
// Parse user agent
$parser = new UserAgentParser();
$result = $parser->parse($request->userAgent());
// Returns: ['browser' => 'Chrome', 'browser_version' => '120.0', 'platform' => 'Windows', ...]
// Detect bots
$detector = new BotDetector();
$isBot = $detector->isBot($request->userAgent());
$botName = $detector->getBotName($request->userAgent());
$category = $detector->getBotCategory($request->userAgent()); // search_engine, social_media, ai_bot, etc.
use Ghdj\VisitorTracker\Events\VisitorTracked;
// In EventServiceProvider or using Event facade
Event::listen(VisitorTracked::class, function (VisitorTracked $event) {
$visitor = $event->visitor;
$visit = $event->visit;
// Custom logic here
logger("New visit from {$visitor->ip} to {$visit->path}");
});
use Illuminate\Support\Facades\Schedule;
Schedule::command('visitor-tracker:prune --force')->daily();
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*'); // Or a specific list of proxy IPs
})