panchodp / laravel-page-monitor
Monitor page visits in your Laravel application. Each visit is stored in the database with information about the user (authenticated or guest), device type, IP address, and timestamp.
Requires
- php: ^8.4
Requires (Dev)
- larastan/larastan: ^3.9.6
- laravel/pint: ^1.29.1
- orchestra/testbench: ^11.1
- pestphp/pest: ^4.7.0
- pestphp/pest-plugin-drift: ^4.1
- rector/rector: ^2.4.2
This package is not auto-updated.
Last update: 2026-05-04 10:02:36 UTC
README
Laravel Page Monitor
Monitor page visits in your Laravel application. Each visit is stored in the database with information about the user (authenticated or guest), device type, IP address, and timestamp.
Compatibility
| Laravel | PHP | Package |
|---|---|---|
| 13.x | 8.4+ | ^1.x |
| 12.x | 8.4+ | ^1.x |
| 11.x | 8.4+ | ^1.x |
Installation
composer require panchodp/laravel-page-monitor
Publish everything at once
php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider"
Or publish each piece individually:
# Migration php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider" --tag="laravel-page-monitor-migrations" # Configuration php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider" --tag="laravel-page-monitor-config" # Views php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider" --tag="laravel-page-monitor-views" # Assets (CSS/JS) php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider" --tag="laravel-page-monitor-assets"
Then run the migration:
php artisan migrate
This creates the page_visits table with the following columns:
| Column | Description |
|---|---|
id |
Auto-increment primary key |
page |
Route name or full URL |
user_id |
Authenticated user ID, null for guests |
session_id |
Session identifier |
ip_address |
Visitor IP (supports IPv4 and IPv6) |
device_type |
mobile, tablet, or desktop |
user_agent |
Raw User-Agent string |
visited_at |
Visit timestamp |
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="Panchodp\LaravelPageMonitor\LaravelPageMonitorServiceProvider" --tag="laravel-page-monitor-config"
This creates config/laravel_page_monitor.php:
return [ 'enabled' => env('MONITOR_ENABLED', true), 'user_model' => env('PAGE_MONITOR_USER_MODEL', 'App\Models\User'), 'middleware' => ['web', 'auth'], 'track_all' => env('MONITOR_TRACK_ALL', false), ];
enabled: Enable or disable the monitor globally. Defaults totrue.user_model: The Eloquent model used to associate authenticated visits. Change this if your application uses a custom User model.middleware: Middleware applied to the/page-monitordashboard route. Defaults to['web', 'auth'].track_all: Whentrue, all routes in thewebmiddleware group are tracked automatically — no need to addvisits-countto each route manually. The/page-monitordashboard is always excluded. Defaults tofalse.
// Use a custom user model 'user_model' => 'App\Models\Admin', // or via .env PAGE_MONITOR_USER_MODEL=App\Models\Admin
Usage
1. Track pages
Option A — Track all pages automatically
Set track_all to true in the config (or via .env) and every route in the web group is tracked with zero additional setup:
MONITOR_TRACK_ALL=true
Option B — Track specific routes manually
Add the visits-count middleware only to the routes you want to monitor:
// Single route Route::get('/home', HomeController::class)->middleware('visits-count'); // Route group Route::middleware('visits-count')->group(function () { Route::get('/home', HomeController::class); Route::get('/about', AboutController::class); });
2. View the dashboard
Navigate to /page-monitor to see all recorded visits with page, user, device type, IP address, and timestamp.
The dashboard requires authentication by default (auth middleware). To restrict access further, define the view-page-monitor gate in your AppServiceProvider:
use Illuminate\Support\Facades\Gate; Gate::define('view-page-monitor', fn ($user) => $user->isAdmin());
You can also replace the middleware entirely via the config:
// config/laravel_page_monitor.php 'middleware' => ['web', 'auth', 'role:admin'],
Facade
You can query visit data anywhere in your application using the Facade:
use Panchodp\LaravelPageMonitor\Facades\LaravelPageMonitor; // Pages grouped and sorted by most visited first $ranking = LaravelPageMonitor::ranking(); // Pages grouped and sorted alphabetically A → Z $byName = LaravelPageMonitor::byNameOrder(desc: false); // Pages grouped and sorted alphabetically Z → A $byNameDesc = LaravelPageMonitor::byNameOrder(desc: true);
Each method returns an Illuminate\Database\Eloquent\Collection where each item contains page, visits (total count), and last_visit.
You can also query the PageVisit model directly for full control:
use Panchodp\LaravelPageMonitor\Models\PageVisit; // All visits for a specific page PageVisit::where('page', 'home')->get(); // Visits by authenticated users only PageVisit::whereNotNull('user_id')->with('user')->get(); // Visits from mobile devices today PageVisit::where('device_type', 'mobile') ->whereDate('visited_at', today()) ->get();
Artisan Commands
Prune old records
php artisan pagemonitor:prune
Deletes records based on the pruning config. Add it to your schedule for automatic cleanup:
// routes/console.php Schedule::command('pagemonitor:prune')->daily();
You can tune the limits in the config (or disable them with null):
'pruning' => [ 'retention_days' => 30, // delete visits older than 30 days 'max_records' => 10000, // keep at most 10,000 records ],
Reset all visit records
php artisan pagemonitor:reboot-count
Deletes all recorded visits from the database.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
