How would you build page visits counter, for dynamic pages ?

If I have a table with 2000 ids. And for every id I am generating web page ( number of ids will increase dynamically ). I would like to track how many users visits each of those pages has. How would you do this with yii2 ? Is there any extension for this, or a simple way ? What would be necessary steps and best practices to achieve this ?

More info: lets say that I have actionView($id). This action will display the page based on that id. Can anyone suggest good solution for making page hit counter, for each of those pages ?

Can’t help at your task with concrete code…

But maybe you can achieve what you want with one of this extensions?

http://demos.krajee.com/social-details/google-analytics

Regards

Can multiple visit from one user increase the hit counter multiple times or only one? If only one - are those visitors anonymous or registered users? With registered users it’s easy - just store each visit somewhere (i.e. db). Guests are more difficult - you can track them by IP or save cookie id - this is general idea.

For simple db counter increase check updateCounters method.

Users have to be registered and every time they visit some page, page counter should increase. What about this approach:

  1. I will form database table with the fields: id, id_of_page, visits.

  2. Every time when select query that is displaying certain page ( requested by not guest ) will trigger the page counter mechanism.

  3. Page counter will take the page id from URL and update ‘visits’ field +1 for the id_of_page.

  4. Then I can display visits for each page by its id.

What do you guys think ?

@MetaCrawler:

If I use google analytics, how can I know the page visits for each page ? For example if I have two pages: www.example.com/post/1 and www.example.com/post/2, how can I grab, display and maybe store somewhere how many visits each page has ?

Thanks guys ! :)

The same way I would do it ;)

In case I would like to count page visits by guests, I guess that I can use the same mechanism ? Maybe I would have separate field that will store counted visits for guests, and some other way to count page readings for each member. I mean, if I want to display count of page red by guests and for each member of web site separetely. What would be the best tactic for this ?

Analytics can give you per-page statistics - that’s the whole point of it. You can even acces this information via Analytics API and display it on your site or admin backend (it’s a Ruby example on the linked page but it can be ported to PHP easily).

EDIT: Just make sure that every page has exactly one valid URL. <link rel=“canonical”> can’t help when working with Analytics.