Laravel package to tracking visitors
Install package via Composer
composer require masoudi/laravel-visitors
Publish and migrate migration
php artisan vendor:publish --tag=masoudi-laravel-visitors
php artisan migrate
Prepare model to working with visitors
use Masoudi\Laravel\Visitors\Contracts\Visitable;
use Masoudi\Laravel\Visitors\Traits\InteractsWithVisitors;
class Article extends Model implements Visitable
{
use InteractsWithVisitors;
}
- Interact with all visitors
- Interact with model visitors
- Interact with A specific model visitors
- Visit A model
- Visit a request
- Visit by add middleware
- Retrieve visitors between two time
- Retrieve visitors by user id
- Retrieve visitors by platform name
- Retrieve visitors by browser name
- Retrieve visitors by IP start range
- Retrieve visitors by IP end range
- Retrieve visitors by referrers urls
- Retrieve visitors by visit urls
The function visitors()
returns a query builder of all visitors so you can work with that as below
$totalVisits = visitors()->count();
// Get ip unique count
$totalUniqueVisits = visitors()->uniqueCount();
Every model that has used InteractsWithVisitors
trait has a static method that returns a query builder specific to the
visitors of that model.
$articlesVisitors = Article::visitors()->get();
Every model that has used InteractsWithVisitors
trait has a public method that returns a query builder specific to the
visitors of that model.
$article = Article::find(1);
$visitors = $article->visitors()->get();
$article = Article::find(1);
# visit model
$article->visit();
class PostController extends Controller {
function index(Request $request){
$request->visit();
// Another codes ...
}
}
After add visitor
middleware to a route or group route when that route called , a visit will be created for that
routes
// routes/web.php
Route::middleware("visitor")->get("/support", "PageController@support");
Route::middleware("visitor")->group(function (){
Route::get("posts/{slug}", "PostController@index");
Route::get("articles/{slug}", "ArticleController@index");
});
$visiCount = visitors()->authId(auth()->id())->count();
$startTime = Carbon::now()->startOfMonth()->toDateTime();
$endTime = Carbon::now()->toDateTime();
$visiCount = visitors()->range($startTime, $endTime)->count();
use Masoudi\Laravel\Visitors\Contracts\VisitorPlatform;
$visiCount = visitors()->platform(VisitorPlatform::ANDROID)->count();
use Masoudi\Laravel\Visitors\Contracts\VisitorBrowser;
$visiCount = visitors()->browser(VisitorBrowser::CHROME)->count();
$visiCount = visitors()->ipStarts('127.0')->count();
$visiCount = visitors()->ipEnds('0.1')->count();
$visiCount = visitors()->referrers('https://google.com', 'https://facebook.com')->count();
$visiCount = visitors()->paths('/support', '/articles/any-slug')->count();