melvnberd
melvnberd

Reputation: 3163

How do I effectively record page views on php [ laravel ]

Good day,

I am trying to record page views/visit on my page.. currently I added a helper function on my controller

my helper function:

function record_user_click($type=null,$item_id=null)
    {
        $referer = (array_key_exists('HTTP_REFERER',$_SERVER)) ? $_SERVER['HTTP_REFERER']:'direct_access';
        \DB::table('click_statistics')->insert([
        ['user_id' => Auth::id(),'type'=>$type,'item_id'=>$item_id,'referer'=>$referer]
    ]);
    }

and on my product controller I the helper function:

 public function product_view($id){
   record_user_click('product',$id);
   -- controller codes here
 }

this function records every visit/view but if I refresh the page fast, it also records them, which will add to its view statistics on the table.

my question is, how can I possibly make it more reliable something like stackoverflow's views function.

enter image description here

Upvotes: 0

Views: 100

Answers (1)

Mike Foxtech
Mike Foxtech

Reputation: 1651

If you want to register to browse pages when it is fully loaded, you need to do this using javascript. Wait for fully loaded pages and send ajax with data to your backend portion and then register the user

Upvotes: 1

Related Questions