A laravel package that can help you to measure traffic on your website!
First, install Package to your laravel project with Composer using command bellow:
composer require ismail1k/laravel-analytics
then add these follow lines to config/app.php
:
'providers' => [
Ismail1k\LaravelAnalytics\ServiceProvider::class,
],
'aliases' => [
'Tracker' => Ismail1k\LaravelAnalytics\CartServiceProvider::class,
],
Now, open app/Http/kernel.php
and add this line:
protected $routeMiddleware = [
...
'tracker' => \App\Http\Middleware\TrackerMiddleware::class,
];
Next step, publish some files:
php artisan vendor:publish --provider="Ismail1k\LaravelAnalytics\ServiceProvider"
then, run the follow command line:
php artisan migrate
If you want to track all your routes, open app/Providers/RouteServiceProvider.php
add tracker middleware to your web group routes like this:
Route::middleware(['web', 'tracker'])
->group(base_path('routes/web.php'));
Also do this if you want to track your api group routes:
Route::middleware(['api', 'tracker'])
->prefix('api')
->group(base_path('routes/api.php'));
Or you can track requests from Controller like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Tracker;
class HomeController extends Controller
{
public function index(Request $request){
Tracker::log($request);
return view('welcome');
}
}
You can get sessions like using:
$sessions = Tracker::sessions(60*24); //sessions for last 24H
//Note: $sessions contain: IP, user, Location, Device, Browser, Traffic...
Or, If you want to get just the number of sessions:
$traffic = Tracker::traffic(60*24); //traffics for last 24H