Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Get website unique visitor count

Tags:

php

laravel

I want to collect the unique amount of visitors on my website and store them in a database. Even when someone without an account accesses the website the visitor count goes up. How can I accomplish this?

I know I have to fetch the users IP address or something along those lines but I don't know how to fetch the IP address of a user without an account when the page loads

Currently I have this DB table

Visitors
 - ip
 - date_visited

Route

Route::get('/', function () {
    $ip = Request::ip();
    return view('welcome', compact('ip'));
});
like image 433
Rainier laan Avatar asked Jan 24 '20 12:01

Rainier laan


People also ask

How do you count unique visitors on a website?

How do marketers measure unique visitors? A marketer can determine who's a unique visitor by looking at the IP addresses that visitors use to access a website. No matter how many times an internet user visits a webpage with their IP address, it only counts once for the relevant time period.

How to count the number of visitors on my website using Laravel?

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

How to install visitor in Laravel?

The recommended way to install Visitor is through composer. Add if your laravel version is < 5.5 to the list of service providers in app/config/app.php the config.php will be copied to /config at the same time or where ever you want just adjust the package config to reflect the new location, it's used to geo locate visitors

Does Laravel start a session for each client?

Alternatively if you're using web middleware (not api ), Laravel starts a session for each client. You can change your session driver and use database instead of default file. As you can see, there is ip_address, user_agent and last_activity.

How to prevent Laravel users from getting halted by tracking code?

You should also consider moving any logic in the middleware inside a " try catch "-statement, it minimized the risk that your user gets halted by any errors caused be the "tracking"-code. Show activity on this post. Alternatively if you're using web middleware (not api ), Laravel starts a session for each client.

How to record the total user visit in Salesforce?

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.


1 Answers

Try to use Request::ip() to get the ip;

$ip = Request::ip();

For Laravel 5.4 +:

$ip = $request->ip();
// or
$ip = request()->ip();

And I think you can use middleware and redis to calculate this count, this will reduce the db's pressure.

like image 128
TsaiKoga Avatar answered Sep 21 '22 00:09

TsaiKoga