Separate Visitor Counter for 2 different pages in same Laravel Project

308 Views Asked by At

I am trying to track/count visitors for mysite.com/user and mysite.com/user/leadpage separately. I've tried using weboAp/Visitor but it only logs the IP and clicks to the db without any way of specifying which page was visited. I am using the latest Laravel v5.4.

1

There are 1 best solutions below

0
EddyTheDove On

You can create a simple one for yourself where you save Ip & page. You create a simple table to save those. Make a middleware that would do that and pass that middleware to the routes you want to track. It will automatically save the user data when one of those pages are called.

Small illustration

public function handle($request, Closure $next)
{
    $ip = $_SERVER['REMOTE_ADDR'];
    $url = $url = $request->url();
    VisitModel::create(['ip' => $ip, 'url' => $url]);


    return $next($request);
}

That was a basic idea. I guess you can improve it as much as you want.