S
S
Sergey Nizhny Novgorod2017-10-03 13:14:45
Django
Sergey Nizhny Novgorod, 2017-10-03 13:14:45

How to make a view counting system?

Hello everyone
Task: the site has a lot (several thousand products), you need to first develop a system for recording views - i.e. which product is viewed more, which less, etc. This will then be used for various recommendation systems.
What is the best way to count views?

  • +1 every time the page is rendered
  • +1 every time after full page load via ajax request

How to protect yourself from bots, spiders and other trash that can wind up the counter for nothing?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Cheremisin, 2017-10-03
@leahch

Make a url counter that is inserted into the page as empty <script src="/mycounter">
. Firstly, bots usually don't load scripts, and secondly, it's very simple. And thirdly, most likely the browser has already loaded the page.
Don't forget to give a non-cached header, otherwise the browser will load your empty counter once and won't load it again.

N
NotProblem, 2017-10-06
@alexbog80

Good afternoon, we have implemented this field in the product model: In the product view class (we have DetailView), the get_object method is overridden:

def get_object(self, queryset=None):
        self.object = super(DetailViewPageView, self).get_object()
        self.object.show_count += 1
        self.object.save()
        return self.object

can also be applied if the function is simply used when calling the object +1 and save()

T
Tash1moto, 2017-10-03
@Tash1moto

1. downloaded https://freegeoip.net/json/
on the client 2. if there is none in the database, add its ip to the database and make +1 view.
3. if there is in the database, then we are glad to see you again on our website :)

U
un1t, 2017-10-03
@un1t

Why reinvent the bicycle? Yandex Metrica and Google Analytics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question