Posts Learn Components Snippets Categories Tags Tools About
/

How to implement visitor views / visitor counter in Laravel application

Learn how to anonymously count your visitor visit on your page and display the stats to the public

2 years ago

9 mins read

16522 views

In this post, you'll be learning how to "implement a visitor counter" for any of your modal (e.g page or an article) that you have on your Laravel application. We'll be using the Eloquent Viewable package and the steps are very simple to follow, so get ready, and let's start implementing.

Installation


Require the package to your Laravel application using composer.
composer require cyrildewit/eloquent-viewable
This package by default will require us to "publish" the migration manually so let's publish it and run the migration.
php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="migrations"
php artisan migrate
If you prefer to change the table name, do change the table name on the migration file and then run the migration command. Other than that, the configuration can also be published and you can change some of the predefined configs from the file if necessary.
php artisan vendor:publish --provider="CyrildeWit\EloquentViewable\EloquentViewableServiceProvider" --tag="config"

Implementation


To allow the package to record the total user visit, your model class will have to implement the "Viewable Interface". Aside from that, make use of the "InteractsWithViews" trait to provide the functionality of recording and retrieving the views count.
use Illuminate\Database\Eloquent\Model;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use CyrildeWit\EloquentViewable\Contracts\Viewable;

class Article extends Model implements Viewable
{
    use InteractsWithViews;

    // others code ommited
}

Recording Views


In order for you to record the views, you can call the "views" helper method and pass in the model that you have defined to be viewable and chain with the "record" method to increment the count value by one.
views($article)->record();

This code is typically written inside the controller method and it will look something like below. Do note that you can record multiple views by defining the code multiple times.
// ArticleController.php
public function show(Article $article)
{
    views($article)->record();

    return view('articles.show', compact('article'));
}
One of my recommendations is to also add a cooldown for each anonymous user visiting the page by calling the "cooldown" method and passing the total time in minutes. This is very important to prevent duplicate count when user kept on refreshing the page. 
views($article)
    ->cooldown($minutes = 3)
    ->record();

Retrieving Views


Finally, to retrieve the total number of views for the model, just call the helper method and chain it with the "count" method as follows. This will retrieve the total article count and it's accessible with the variable you have defined.
$totalViews = views($article)->count();
If you want to limit by 2 date range, do specify the period range like below and the result will be limited to the particular range.
use CyrildeWit\EloquentViewable\Support\Period;

// Example: get views count from 2020 upto 2021
views($article)
    ->period(Period::create('2020', '2021'))
    ->count();
That's not all though, you can refer to the package documentation for more details on what are the available methods you can make use of.

I hope this tutorial is helpful for you and if there's any question, do comment out below and let's start the discussion. Also, don't forget to share it with your friend as well and I would appreciate it a lot. Cheers and have a good try.

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)