Skip to content

rezamasoudi/laravel-visitors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Version on Packagist Total Downloads License: MIT

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

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();

CHANGELOGLICENSE