Laravel Stats Tracker

Storing user tracking information, on indexed and normalized database tables, wastes less disk space and ease the extract of valuable information about your application and business.

Tracker gathers a lot of information from your requests to identify and store:

  • Sessions
  • Page Views (hits on routes)
  • Users (logged users)
  • Devices (computer, smartphone, tablet...)
  • Languages (preference, language range)
  • User Devices (by, yeah, storing a cookie on each device)
  • Browsers (Chrome, Mozilla Firefox, Safari, Internet Explorer...)
  • Operating Systems (iOS, Mac OS, Linux, Windows...)
  • Geo Location Data (Latitute, Longitude, Country and City)
  • Routes and all its parameters
  • Events
  • Referers (url, medium, source, search term...)
  • Exceptions/Errors
  • Sql queries and all its bindings
  • Url queries and all its arguments
  • Database connections





  • Laravel 5+
  • PHP 5.3.7+
  • Package "geoip/geoip":"~1.14" or "geoip2/geoip2":"~2.0" (If you are planning to store Geo IP information)


Require the tracker package by executing the following command in your command line:

composer require pragmarx/tracker


Add the service provider to your app/config/app.php:



Add the alias to the facade on your app/config/app.php:

'Tracker' => 'PragmaRX\Tracker\Vendor\Laravel\Facade',


Publish tracker configuration:

php artisan vendor:publish --provider="PragmaRX\Tracker\Vendor\Laravel\ServiceProvider" 


Open the newly published config file found at app/config/tracker.php and enable use_middleware:

'use_middleware' => true,


Open the file app/Http/Kernel.php and add the following to your web middlewares:



Create a database connection for it on your config/database.php

'connections' => [
    'mysql' => [
    'tracker' => [
    	'driver'   => '...',
    	'host'     => '...',
    	'database' => ...,
        'strict' => false,    // to avoid problems on some MySQL installs


after that run:

php artisan migrate



Sessions (visits)

$sessions = Tracker::sessions(60 * 24); // get sessions (visits) from the past day
foreach ($sessions as $session)
    var_dump( $session->user->email );

    var_dump( $session->device->kind . ' - ' . $session->device->platform );

    var_dump( $session->agent->browser . ' - ' . $session->agent->browser_version );

    var_dump( $session->geoIp->country_name );

    foreach ($session->session->log as $log)
    	var_dump( $log->path );

PageViews By Country summary

$pageViews = Tracker::pageViewsByCountry(60 * 24); 


Routes By Name

Having a route of

Route::get('user/{id}', ['as' => 'user.profile', 'use' => 'UsersController@profile']);


You can use this method to select all hits on that particular route and count them using Laravel:

return Tracker::logByRouteName('user.profile')
                ->where('parameter', 'id')
                ->where('value', 1);

Tags :