codemastersolucoes/laravel-visitor-tracker

Visitor tracker and statistics for Laravel 5. Package forked from voerro/laravel-visitor-tracker.

v1.2.7 2019-01-01 20:17 UTC

README

This package has been forked from voerro/laravel-visitor-tracker and modified by codemastersolucoes/laravel-visitor-tracker

Track your authenticated and unauthenticated visitors, login attempts, ajax requests, and more. Includes a controller and a bunch of routes and views to display the statistics, as well as a helper class to fetch the statistics easily (in case you want to display the statistics yourself).

Live Demo

Installation - Basic

  1. Install the package using composer:
composer require CodeMaster/laravel-visitor-tracker
  1. Run the migration to install the package's table to record visits to by executing:
php artisan migrate
  1. Add the middleware to app/Http/Kernel.php:
protected $middlewareGroups = [
    ...
    'web' => [
        ...
        \CodeMaster\Laravel\VisitorTracker\Middleware\RecordVisits::class,
    ],
    ...
];
  1. Laravel 5.5 has package auto-discovery. If you have an older version, register the service provider in config/app.php:
...
'providers' => [
    ...
    CodeMaster\Laravel\VisitorTracker\VisitorTrackerServiceProvider::class,
    ...
],
...

If you want to fetch and display the visitor statistics yourself register the facade in the same file:

...
'aliases' => [
    ...
    'VisitStats' => CodeMaster\Laravel\VisitorTracker\Facades\VisitStats::class,
    ...
],
...
  1. Publish the config file, assets, and views by running:
php artisan vendor:publish

Choose CodeMaster\Laravel\VisitorTracker\VisitorTrackerServiceProvider in the provided list.

  1. Create a connection, named visits_tracker for Laravel Visitor Tracker on config/databases.php, connection name can not be changed Examples: #MySql
'visits_tracker' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

#MySql

'visits_tracker' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
],

#Sql Server

'visits_tracker' => [
    'driver' => 'sqlsrv',
    'host' => env('DB_HOST', 'localhost'),
    'port' => env('DB_PORT', '1433'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'collation' => 'SQL_Latin1_General_CP1_CI_AS',
    'prefix' => '',
],

Installation - Geoapi

The tracker uses external API to fetch the geolocation data. To turn geoapi off set the geoip_on setting in the config file to false. To change a provider change the geoip_driver field. The supported drivers are listed in the configuration file. You might need to fill out additional API keys depending on the driver you choose.

Since fetching data from an external API takes time, the operation is queued an performed asynchronously. This is done using Laravel Jobs and probably won't work on a shared hosting. There are multiple drivers supported. We'll describe how to set up the database driver.

First, in your .env file you need to set:

QUEUE_DRIVER=database

Then run these commands one after another:

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

Finally, you need to start the worker that will take care of the queue. Run the following command and keep it running:

php artisan queue:work

Read more on Queues and Jobs in the Laravel documentation. This section describes how to restart the queue worker automatically in case the process fails.

P.S. You need to restart the worker every time you've made changes to the package's config file.

Configuration

Check out the config/visitortracker.php file. It is well commented and additional explanations are not required. You can exclude certain user groups, individual users, and certain requests from being tracked there among other things.

Testing

The external API calls to retrieve geolocation information are disabled in the testing environment. Otherwise your tests would run really slow, since the tracker tracks all the requests.

Displaying Statistics

The package comes with a controller and a bunch of routes and views to display statistics. You can fetch and display the stats yourself using the VisitStats class, but we'll talk about it later. The provided views are uncomplicated and styled with the standard Bootstrap classes.

To install the in-built routes add this line to your routes.php file:

VisitStats::routes();

You can put this line inside a group to restrict the access with middlewares and/or to add a prefix to the routes. For example, like this:

Route::middleware('auth')->prefix('admin')->group(function () {
    VisitStats::routes();
});

You can integrate the views into your existing layouts. Take a look at the Views section of the config file. All the routes are named so you could easily add and style links to all the pages. Below is the complete list of routes.

What Information is Being Collected

This is the data that is being collected by the tracker.

The package uses piwik/device-detector to parse the user agent.

Manually Fetching and Displaying Statistics

In case you are not content with the provided views, you can use the CodeMaster\Laravel\VisitorTracker\Facades\VisitStats class to fetch the statistics data and then make your own controller and views to display this data.

Take a look at the controller located at src/Controllers/StatisticsController.php to understand how to work with the class, it's pretty simple. The original class is located at src/VisitStats.php and all the methods inside are documented, in case you need more insights.

License

This is open-sourced software licensed under the MIT license.