Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track the number of "page views" or "hits" of an object?

Tags:

django

view

I am sure that someone has a pluggable app (or tutorial) out there that approximates this, but I have having trouble finding it: I want to be able to track the number of "views" a particular object has (just like a question here on stackoverflow has a "view count").

If the user isn't logged in, I wouldn't mind attempting to place a cookie (or log an IP) so they can't inadvertently run up the view count by refreshing the page; and if a user is logged in, only allow them one "view" across sessions/browsers/IP addresses. I don't think I need it any fancier than that.

I figure the best way to do this is with Middleware that is decoupled from the various models I want to track and using an F expression (of sorts) -- other questions on StackOverflow have alluded to this (1), (2), (3).

But I wonder if this code exists out in the wild already -- because I am not the savviest coder and I'm sure someone could do it better. Smile.

Have you seen it?

like image 600
thornomad Avatar asked Oct 21 '09 20:10

thornomad


2 Answers

I am not sure if it's in the best taste to answer my own question but, after a bit of work, I put together an app that solves the problems in earnest: django-hitcount.

You can read about how to use it at the documentation page.

The ideas for django-hitcount came came from both of my two original answers (Teebes -and- vikingosegundo), which really got me started thinking about the whole thing.

This is my first attempt at sharing a pluggable app with the community and hope someone else finds it useful. Thanks!

like image 92
thornomad Avatar answered Sep 28 '22 18:09

thornomad


You should use the django built-in session framework, it already does a lot of this for you. I implemented this in the following way with a Q&A app where I wanted to track views:

in models.py:

class QuestionView(models.Model):     question = models.ForeignKey(Question, related_name='questionviews')     ip = models.CharField(max_length=40)     session = models.CharField(max_length=40)     created = models.DateTimeField(default=datetime.datetime.now()) 

in views.py:

def record_view(request, question_id):      question = get_object_or_404(Question, pk=question_id)      if not QuestionView.objects.filter(                     question=question,                     session=request.session.session_key):         view = QuestionView(question=question,                             ip=request.META['REMOTE_ADDR'],                             created=datetime.datetime.now(),                             session=request.session.session_key)         view.save()      return HttpResponse(u"%s" % QuestionView.objects.filter(question=question).count()) 

Vikingosegundo is probably right though that using content-type is probably the more reusable solution but definitely don't reinvent the wheel in terms of tracking sessions, Django already does that!

Last thing, you should probably have the view that records the hit be either called via Ajax or a css link so that search engines don't rev up your counts.

Hope that helps!

like image 28
Teebes Avatar answered Sep 28 '22 18:09

Teebes