Giter VIP home page Giter VIP logo

bruth / django-tracking2 Goto Github PK

View Code? Open in Web Editor NEW
205.0 205.0 67.0 180 KB

django-tracking2 tracks the length of time visitors and registered users spend on your site. Although this will work for websites, this is more applicable to web _applications_ with registered users. This does not replace (nor intend) to replace client-side analytics which is great for understanding aggregate flow of page views.

License: BSD 2-Clause "Simplified" License

Python 92.12% HTML 7.88%
django tracking visitor

django-tracking2's People

Contributors

bentleycook avatar brandonkane avatar bruth avatar chrisfranklin avatar dacox avatar djedi avatar eastdark avatar fpruitt avatar frankvdp avatar hwkns avatar mjschultz avatar mrg29 avatar tpeaton avatar v-alexeev avatar vganshin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-tracking2's Issues

tracking_visitor.ip_address may not be NULL

On Executing / page i get error

tracking_visitor.ip_address may not be NULL . other details below

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.5
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.sitemaps',
'django.contrib.sites',
'tracking')
Installed Middleware:
('tracking.middleware.VisitorTrackingMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "C:\Python2.7.5\lib\site-packages\django\core\handlers\base.py" in get_response

  1.             response = middleware_method(request, response)
    
    File "C:\Python2.7.5\lib\site-packages\tracking\middleware.py" in process_response
  2.     visitor = self._refresh_visitor(user, request, now)
    
    File "C:\Python2.7.5\lib\site-packages\tracking\middleware.py" in _refresh_visitor
  3.     visitor.save()
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\base.py" in save
  4.                    force_update=force_update, update_fields=update_fields)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\base.py" in save_base
  5.         updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\base.py" in _save_table
  6.         result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\base.py" in _do_insert
  7.                            using=using, raw=raw)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\manager.py" in _insert
  8.     return insert_query(self.model, objs, fields, **kwargs)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\query.py" in insert_query
  9. return query.get_compiler(using=using).execute_sql(return_id)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  10.         cursor.execute(sql, params)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\backends\util.py" in execute
  11.         return super(CursorDebugWrapper, self).execute(sql, params)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\backends\util.py" in execute
  12.             return self.cursor.execute(sql, params)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\utils.py" in exit
  13.             six.reraise(dj_exc_type, dj_exc_value, traceback)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\backends\util.py" in execute
  14.             return self.cursor.execute(sql, params)
    
    File "C:\Python2.7.5\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  15.     return Database.Cursor.execute(self, query, params)
    

Exception Type: IntegrityError at /
Exception Value: tracking_visitor.ip_address may not be NULL

Update url.py to avoid using deprecated string view argument

Currently, in Django 1.9, tracking causes a "RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead." warning. This can easily be fixed, changing the urls.py.

Reduce the number of visitors/sessions created

We're seeing an interesting clash between django, django-tracking2, and our API provider (tastypie).

Essentially what happens is we query our tastypie API a lot. Normally this is fine, but what it does is django will create an ephemeral Session object for the request, it never gets used by tastypie normally so it doesn't get saved to the database. However, in order for django-tracking2 to track visitors it requires a session key (i.e. saves the Session to the database).

But since our use of tastypie isn't Session based, it ends up creating a unique visitor and session object every time. This results in thousands of new objects per day (in two tables!).

I can think of a few ways to handle this:

  1. add option to not track superusers and/or staff users (it works in our case for some API users)
  2. change how TRACK_IGNORE_URLS works. Right now it only ignores urls from populating the Pageview table. It still creates a Visitor object even if that visitor only hits one of the ignored urls. The change would simply make tracking2 oblivious to these pages in the middleware.
  3. allow apps to create a middleware that attaches a boolean (say .tracking_ignore) that, when set to False would have the middleware ignore the request.

I feel like option 2 is the best but it does change the behavior of an existing setting.

base.html not found

I get template error. It can't seem to find the base.html and looks like its missing in the repo

How to correct configure the ignore tracking for some urls

I want to avoid tracking URLs that start with the following:
'/en/jsi18n', '/media...', 'Cache'...
How do I go about configuring this?
I did something like the one below but didn't seem to work:

TRACK_IGNORE_URLS =  r'^(favicon\.ico|robots\.txt|media|Cache|/en/jsi18n/)$',

'module' object has no attribute 'inet_pton'

I am using django 1.6.5 for python 2.7 and did following
pip install django-tracking2
tracking to your project's INSTALLED_APPS
Add tracking.middleware.VisitorTrackingMiddleware
TRACK_AJAX_REQUESTS=True
TRACK_ANONYMOUS_USERS=True
TRACK_REFERER=True
TRACK_QUERY_STRING=True

Looks like we get error under this

def is_valid_ip_address(address_family, ip_str):
try:
socket.inet_pton(address_family, ip_str)
except socket.error:
return False
else:
return True

Tracking showing, but no access to dashboard (tracking/dashboard)

I had thought I successfully installed the package, given that I can see the information on the tracking/ page. However, when I navigate to tracking/dashboard, I get a 404 and Django tells me that it doesn't match any of the urls.

I would share code, but I'm unsure what to share which would help you understand further so please advise what you would like me to share to help solve this issue.

I'm using django 2.2.7 and installed django-tracking2 using pipenv.

How to track already-login user?

Thank you for providing this app.But when I try to test it, I figure out a problem—it can’t track my account when I refresh the page while I’m already login with super-user.

Whether it ignores the super-user ?

Change dashboard view to be at the root of urls.py

Even though the README suggests include the urls module under the tracking/ namespace, the only endpoint that is exposed is under dashboard/ which is unnecessary. This should be moved to the root of the include.

Adding Visitors By Site

I propose having another view where we can see user statistics broke out by page/link. In particular, I have links for file downloads, and want to track who clicked on which links, and on what pages they spent their time.

I dont know proper way to do link visiting (outside of bit.ly or something) but it would b nice to accumulate statistics on links somehow as well as page visits by user.

Default start/end dates README suggestion

Was a little confused by the default state of the dashboard. Currently if you do not specify the GET params 'start' and 'end' as yyy-mm-dd formatted dates (ex: https://www.example.com/tracking/?start=2013-11-1&end=2013-11-30) the dashboard will say there are no stats to show. I think this should, at the very least default to something and maybe mention it in the README. As far as I can see the start and end params are not mentioned anywhere, I only found this by reading the source.

Lines 16 and 17 of views.py appear to be the culprit:

    if not date_str:
        return

This causes the model queries to return empty sets of stats.

I decided to use the upper variable to determine what date to automatically calculate since that appears how it is used elsewhere anyway.

In place of the original lines 16 and 17 I am now using:

    if not date_str:
        if not upper:
            return date.today() - timedelta(days=7)
        else:
            return date.today()

The 7 days value was completely arbitrary, but you get the idea.

Integrity Error

Hello, I'm using a custom User model, that is linked to a profile. I got the following error while loggin in.

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 199, in get_response
    response = middleware_method(request, response)

  File "/usr/local/lib/python2.7/dist-packages/tracking/middleware.py", line 76, in process_response
    visitor.save()

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 545, in save
    force_update=force_update, update_fields=update_fields)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 573, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 654, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 687, in _do_insert
    using=using, raw=raw)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 232, in _insert
    return insert_query(self.model, objs, fields, **kwargs)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1514, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 903, in execute_sql
    cursor.execute(sql, params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)

  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)

  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)

  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue

IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`company`.`tracking_visitor`, CONSTRAINT `user_id_refs_id_e9c7bb5d` FOREIGN KEY (`user_id`) REFERENCES `users_userprofile` (`id`))')

Thanks

Remove dependency on base.html

This has been a source of confusion when attempting to integrate the app since it assumes the project has a base.html defined. The new approach will be to render it's own UI since it has a standalone purpose.

Can't subtract offset-naive and offset-aware datetimes

When visit /dashboard/,

TypeError at /dashboard/
can't subtract offset-naive and offset-aware datetimes

Request Method: GET
Request URL: http://mysite.com/dashboard/
Django Version: 1.4
Exception Type: TypeError
Exception Value:
can't subtract offset-naive and offset-aware datetimes
Exception Location: PROJECT_DIR/tracking/middleware.py in process_response, line 64
Python Executable: /usr/local/bin/python
Python Version: 2.7.3
Python Path:
['',
'/usr/local/sae/python/lib/python27.zip',
'/usr/local/sae/python/lib/python2.7',
'/usr/local/sae/python/lib/python2.7/plat-linux2',
'/usr/local/sae/python/lib/python2.7/lib-tk',
'/usr/local/sae/python/lib/python2.7/lib-old',
'/usr/local/sae/python/lib/python2.7/lib-dynload',
'/usr/local/sae/python/3rd/django1.4',
'/usr/local/sae/python/lib/python2.7/site-packages']

Usage of Total Visitors on index page

I understand that as per the library we can only see the total visitors and all other stats when we have access to admin using ip/tracking/ template however i am looking to use the total visitors on my page s footer

Understand you use Total {{ visitor_stats.registered.total }} as template variable in dashboard.html , is it possible in someway that i can include this in my site base.html and wll users visiting the site could see total number of visitors and other such stats ?

where is base.html

templates/tracking/dashboard.html extends "base.html", but wher is it?

Stat views wishlist

summary/dashboard - /

  • supports start and end dates with GET params
  • stats - visitor count, % returning, avg. time on site, avg. pages/visit
  • registered user list - # visits, avg. time on site, avg. pages/visit
  • pageviews - total, unique

visitor centric - /visitors/:id

  • overview - /
    • stats - # visits, avg. time on site, avg. pages/visit
    • paginated list of visits with click-through to visit detail
  • visit detail - /visits/:id - info about a particular visit
    • stats - start/end time, duration, # pageviews, views/page, avg. time/page, ip, platform/browser
    • paginated list of pageviews with click-through to pageview detail
  • page detail - /pages/:key - info about a particular page (across all visits)
    • stats - total views, avg. duration, avg. views/visit
    • paginated list of all visits for this pa
  • pageview detail - /pageviews/:id - info about a page for a given visit
    • display url, request method (as of v0.1.9)
    • stats - view time, duration (diff from next page view)

page/URL centric - /pages

  • overview - /
    • stats - total page views, total unique pages
    • paginated list of pages with click-through to page detail
  • page detail - /:key (key could be encoded version of URL?)
    • stats - total views, avg. duration, # unique visitors
    • paginated list of pageviews with click-through to /visitors/:id/pages/:key

Race Condition in _refresh_visitor

Issue

Even with the best efforts to get around the possible race condition in _refresh_visitor, there still seems to be a chance for an error on Visitor.save(). In the following traceback, you can see that after the IntegrityError is caught, when Visitor.objects.get(pk=session_key) is called, the Visitor with that key no longer exists. This means either a) the IntegrityError is being raised for another reason other than the assumed clashing session keys, which I can't see how that could happen, or b) There's some weird race condition in which .save() raises an IntegrityError but when we try to get the Visitor it doesn't exist yet.

Stacktrace

DoesNotExist: Visitor matching query does not exist.
  File "django/core/handlers/base.py", line 223, in get_response
    response = middleware_method(request, response)
  File "newrelic/hooks/framework_django.py", line 331, in wrapper
    return wrapped(*args, **kwargs)
  File "tracking/middleware.py", line 141, in process_response
    visitor = self._refresh_visitor(user, request, now)
  File "tracking/middleware.py", line 98, in _refresh_visitor
    visitor = Visitor.objects.get(pk=session_key)
  File "django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "django/db/models/query.py", line 334, in get
    self.model._meta.object_name

Example Context at time of error

  • ip_address:
    None
  • request:
    WSGIRequest at 0x139752192184104
  • self:
    tracking.middleware.VisitorTrackingMiddleware object at 0x7f1a9e789cc0
  • session_key:
    '3ylrw1m7oad6fxs71mcenhxfsosnyx8o'
  • time_on_site:
    -0.004183
  • user:
    None
  • user_agent:
    'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'
  • visit_time:
    datetime.datetime(2015, 11, 30, 23, 13, 22, 232516, tzinfo=)
  • visitor:
    Visitor: Visitor object

Related Code Blocks

middleware.py, lines 140-141

# update/create the visitor object for this request
        visitor = self._refresh_visitor(user, request, now)

middleware.py, lines 90-98

try:
    visitor.save()
except IntegrityError:
    # there is a small chance a second response has saved this
    # Visitor already and a second save() at the same time (having
    # failed to UPDATE anything) will attempt to INSERT the same
    # session key (pk) again causing an IntegrityError
    # If this happens we'll just grab the "winner" and use that!
    visitor = Visitor.objects.get(pk=session_key)

Note

This only occurs when search bots are indexing our site. I'm pretty sure that the rapid request/response cycle combined with a race condition is causing this error.

Suggested Fix

#57

django-tracking2 is not compatible with the new Django User model

In Django 1.5 a configurable user model was added.

The models in django-tracking2 have a foreign key to auth.User directly. In the new Django this should point to settings.AUTH_USER_MODEL, which will point to whatever class is being used as the user model in a given app.

Log HTTP method

Could you please log the HTTP method (GET, POST, etc) for the request?

That'll give good information how frequently the various ReST operations are used for an API.

Django 1.4 sessions

In order to use with Django 1.4, new sessions now need to be saved to the database first.

Sorry for the lack of pull request to fix, but it's a fairly simple one. Simply add the following in the middleware.py:

# If it's a new session, save it to the database first (required for Django 1.4+)    
if not request.session.session_key:
        request.session.save()

This will be before:

session_key = request.session.session_key

Error: AppRegistryNotReady

I've gone through the setup step by step. However, when attempting migrations (Django 1.7), I get

django.core.exceptions.AppRegistryNotReady: Models for app 'auth' haven't been imported yet.

Any hints as to why this is happening and how to fix it? I can't make the migrations necesary (or run the dev server). Could it be related to this error in Tastypie?

No statistics available in front end despite data displaying in admin

Hey,

I can see both Pageviews and Visitors in the backend but I don't get anything showing up on the frontend. It just displays:

Visitors
No visitor stats available

Registered Users
No registered user stats available

Pageviews
No pageview stats available

Do you know what might be wrong? Do I need to have a certain number of visitors before stats activate? Is it deliberately skipping the superuser account from stats generation?

Sorry to be a pest,

Cheers,

Chris

Stats by pageview?

Would it be possible to have the ability to view pageview counts/stats grouped by url (or partial url), assuming TRACK_PAGEVIEWS = True

For example,

Total  Unique  URL
1400   800     /
600    300     /about-us/
100    17      /about-us/people/

Maybe with a column or link showing the most recent visitors?

I'm willing to add the code to render the view if it's possible to do without major changes.

The `stat.html` part of the front end doesn't appear

Hi!

The /tracking page isn't showing any data to me. I've input end dates and start dates and all i get is

Visitor tracking began on 2016-07-04 05:10:11

No matter the end date I put. I already tried to set an end date tomorrow and 5 days from now and it hasn't helped.

It seems like it's not loading the stat.html part of the page.

I apologize if it's something I'm doing wrong and thanks for the great app :)

Cheers!
Fábio

This is what the page looks like:

screen shot 2016-07-07 at 02 52 33

Timezone support

Problem: Dashboard empty and the following start date: Tracking began on Jul 23, 2014 @ 5:49:22 a.m. But I really started doing tracking at 9:49:22 a.m.

I did a bit of code research and found:

Visitor.objects.user_stats(None, None)

/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py:848: RuntimeWarning: DateTimeField Visitor.start_time received a naive datetime (2014-07-23 00:00:00) while time zone support is active.
RuntimeWarning)

[]

There is a problem with the TZ support in general.

Thanks!

Tracking the time a user spends on the site more accurately

Why should VisitorTrackingMiddleware be before django.contrib.sessions.middleware.SessionMiddleware?

I have custom middleware that logs a user out after 1 hour. I want to run this before VisitorTrackingMiddleware otherwise it greatly overstates the time a user spends on the site.

Or is there another way to be more accurate in the time a user spends on the site?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.