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'));
});
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.
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();
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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With