masoudi / laravel-visitors
Laravel package for tracking visitors
Installs: 1 180
Dependents: 0
Suggesters: 0
Security: 0
Stars: 33
Watchers: 1
Forks: 2
Open Issues: 2
Requires
- php: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
README
Laravel Visitors
Laravel package to tracking visitors
Installation
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; }
Documentation
- 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
Interact with all visitors
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();
Interact with model visitors
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();
Interact with A specific model visitors
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();
Visit a model
$article = Article::find(1); # visit model $article->visit();
Visit a request
class PostController extends Controller { function index(Request $request){ $request->visit(); // Another codes ... } }
Visit by add middleware
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"); });
Retrieve visitors by user id
$visiCount = visitors()->authId(auth()->id())->count();
Retrieve visitors between two time
$startTime = Carbon::now()->startOfMonth()->toDateTime(); $endTime = Carbon::now()->toDateTime(); $visiCount = visitors()->range($startTime, $endTime)->count();
Retrieve visitors by platform name
use Masoudi\Laravel\Visitors\Contracts\VisitorPlatform; $visiCount = visitors()->platform(VisitorPlatform::ANDROID)->count();
Retrieve visitors by browser name
use Masoudi\Laravel\Visitors\Contracts\VisitorBrowser; $visiCount = visitors()->browser(VisitorBrowser::CHROME)->count();
Retrieve visitors by IP start range
$visiCount = visitors()->ipStarts('127.0')->count();
Retrieve visitors by IP end range
$visiCount = visitors()->ipEnds('0.1')->count();
Retrieve visitors by referrers urls
$visiCount = visitors()->referrers('https://google.com', 'https://facebook.com')->count();
Retrieve visitors by visit urls
$visiCount = visitors()->paths('/support', '/articles/any-slug')->count();