Hassan Baig Hassan Baig - 3 years ago 62
Python Question

Tracking unique users hitting certain urls in Django project

In a Django project of mine, I want to log all unique user IDs visiting a certain section of the web application. Currently, the only distinguishing feature of this section is that it's url patterns are written in a separate module.

What would be the best way to track unique users who visit these url patterns? Doing it as costlessly (resources wise) as possible is what I mean by 'best'. An illustrative example would be great.

Answer Source

Something like google analytics would suit this task

However, if you wish to implement something yourself in Django I would suggest something like the following

class TrackUniqueVisitsMiddleware(object):

    def process_request(self, request):
        if not request.user.is_authenticated:
        if request.resolver_match.namespace in settings.NAMESPACES_TO_TRACK:

Where UniqueUserVisit is a model that stores all unique views that a user has visited. UniqueUserVisit can then be queried to generate reports on user activity.

request.resolver_match.namespace contains the namespace of the visited url.

request.resolver_match.view_name contains the full name of the url.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download