drsoft/visitor-tracker is a Laravel package.
It currently has 1 GitHub stars and 95 downloads on Packagist (latest version v2.2.4).
Install it with composer require drsoft/visitor-tracker.
Discover more Laravel packages by drsoft
or browse all Laravel packages to compare alternatives.
Last updated
The Laravel Visitor Tracker package allows you to track guest and user activity on your web application. It provides detailed information about visitors, including their IP address, location, visited URLs, and more. This guide will walk you through the installation, configuration, and usage of the package.
Install the package via Composer:
composer require drsoft28/visitortracker
Publish the configuration file and migrations:
php artisan vendor:publish --provider="Drsoft28\VisitorTracker\VisitorTrackerServiceProvider"
Or
php artisan vendor:publish --tag=visitor-tracker-migrations --tag=visitor-tracker-config --tag=visitor-tracker-model
This will create:
visitortracker.php file in the config/ directory.visitor_trackers table in the database/migrations/ directory.Run the migration to create the visitor_trackers table:
php artisan migrate
After publishing the configuration file, you can customize the package behavior by editing the config/visitortracker.php file. Here are the default options:
return [
/*
|--------------------------------------------------------------------------
| Model
|--------------------------------------------------------------------------
|
| This value is the model used for tracking visitors.
| By default, it uses the package's built-in model.
| You can replace it with your own model if needed.
*/
'model' => \Drsoft28\VisitorTracker\Models\VisitorTracker::class,
/*
|--------------------------------------------------------------------------
| Headers to Track
|--------------------------------------------------------------------------
|
| Specify the headers to store in the `request_info` JSON column.
| Add or remove headers as needed.
*/
'headers' => [
'HTTP_USER_AGENT',
'HTTP_HOST',
'SERVER_NAME',
'SERVER_SOFTWARE',
'REMOTE_ADDR',
'HTTPS',
],
];
To start tracking visitors, you need to add the VisitorTrackerMiddleware to your application's middleware stack.
Add the middleware to the web group in app/Http/Kernel.php:
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Drsoft28\VisitorTracker\Middleware\VisitorTrackerMiddleware::class,
],
Add the middleware in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\Drsoft28\VisitorTracker\Middleware\VisitorTrackerMiddleware::class,
]);
})
Once the middleware is enabled, the package will automatically track visitors and store their information in the visitor_trackers table.
By default, the package uses the \Drsoft28\VisitorTracker\Models\VisitorTracker model. You can change this in the visitortracker.php configuration file.
The visitor_trackers table contains the following columns:
user_id: The ID of the authenticated user (if applicable).host_schema: The protocol (e.g., http or https).host: The hostname (e.g., example.com).ip: The visitor's IP address.path: The visited path (e.g., /about).full_url: The full URL visited.url: The relative URL.country_name: The visitor's country name.country_code: The visitor's country code.region_name: The visitor's region name.region_code: The visitor's region code.city_name: The visitor's city name.zip_code: The visitor's ZIP code.iso_code: The ISO code of the visitor's location.latitude: The visitor's latitude.longitude: The visitor's longitude.timezone: The visitor's timezone.referer: The referer URL.route_name: The name of the route visited.route_params: The parameters of the route.request_info: A JSON column containing additional request information (e.g., headers).The VisitorTracker model provides the following methods for querying visitor data:
visitorsWithinSeconds($seconds):
$seconds seconds.visitorsWithinMinutes($minutes):
$minutes minutes.visitorsWithinHours($hours):
$hours hours.Example Usage:
use Drsoft28\VisitorTracker\Models\VisitorTracker;
// Get visitors from the last 5 minutes
$recentVisitors = VisitorTracker::visitorsWithinMinutes(5)->get();
If you want to use a custom model for tracking visitors, follow these steps:
Create a new model in your app/Models directory:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CustomVisitorTracker extends Model
{
protected $table = 'visitor_trackers';
protected $fillable = [
'user_id', 'host_schema', 'host', 'ip', 'path', 'full_url', 'url',
'country_name', 'country_code', 'region_name', 'region_code', 'city_name',
'zip_code', 'iso_code', 'latitude', 'longitude', 'timezone', 'referer',
'route_name', 'route_params', 'request_info',
];
}
Update the visitortracker.php configuration file to use your custom model:
'model' => \App\Models\CustomVisitorTracker::class,
You can customize the headers tracked in the request_info column by updating the headers array in the visitortracker.php configuration file.
Middleware Not Tracking Visitors:
web or global).Migration Errors:
visitor_trackers table does not already exist before running the migration.Configuration Not Applied:
visitortracker.php file:
php artisan config:clear
If you encounter any issues or have questions, please open an issue on the GitHub repository.
Thank you for using Laravel Visitor Tracker! 🚀