⬆️ ⬇️

Methods for calculating statistics of visiting pages on the site "on the fly"

Often there is the task of counting the number of visits to a particular portal page or counting clicks on a link. For example. It may be interesting to count the number of views of an article or advertisement, the number of clicks on the “show phone number” on the advertisement page, etc. etc.



The main thing is to understand exactly what you are planning to use these counters for and what they may be needed in the foreseeable future. Do you need to select / sort / group entities by them or do you just need rare one-time reports (for example, once a day)? Or maybe you want to show these counters in the admin panel or in the user's personal account? Do you want to show these numbers in real time or can you limit yourself to the hour / daily cutoff? Depending on these and possibly some other criteria, the choice of method for counting and storing the counters will depend.



Counting clicks / visits



The task of counting clicks / visits can be solved in different ways:



')

At this stage, it is necessary to decide whether we need to count the visits of search robots. You may not want to read requests made from the internal network. It may also be interesting to consider only requests from authorized users, etc.



It is also important to understand how critical the accuracy of the calculation is, whether it is possible to make a mistake by 1-2-3 units or not. Much can depend on it. If accuracy is important, then performance can be neglected, or vice versa, if performance is important (for example, page speed), then accuracy can be neglected slightly.



Counter Storage



You can store counters in different ways too:





Life example



Let's look at a specific example from our practice. At once I will say that we use MySQL.



Task: counting the number of visits to the page of the announcement of the Purchase and Sale section of the Auto Mail.Ru project, excluding Web bots (Google, Yandex, Mail.Ru Search). You also need to consider the total number of views of all announcements of a car dealership publishing its ads for sale on our portal Auto Mail.Ru.



Important points:





At first they tried to implement "in the forehead." We created the views_count field in the entity table (similarly in the car dealership table) and updated it every time we looked at the advertisement page (did UPDATE

. .

Source: https://habr.com/ru/post/206494/



All Articles