页面浏览统计之(三) tracking2

转载 http://www.codingsoho.com/zh/blog/component-tracking2/

安装

github主页 https://github.com/bruth/django-tracking2

它和django-tracking文件夹名字一样,只能放到本地,修改一个名字,否则名字冲突

django-tracking2 0.4.1
相关的名字改成tracking2,目前还不知道有什么更好的办法

配置

添加tracking2到INSTALLED_APPS

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        ...
        'tracking2',
        ...
    )

添加middleware,必须在'django.contrib.sessions.middleware.SessionMiddleware'前面

MIDDLEWARE = (
    'tracking2.middleware.VisitorTrackingMiddleware',
    #
    'cms.middleware.utils.ApphookReloadMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.sites.middleware.CurrentSiteMiddleware', 
)

其他一些配置不是必须的,可以了解一下

TRACK_AJAX_REQUESTS - If True, AJAX requests will be tracked. Default is False
TRACK_ANONYMOUS_USERS - If False, anonymous users will not be tracked. Default is True
TRACK_PAGEVIEWS - If True, individual pageviews will be tracked.
TRACK_IGNORE_URLS - A list of regular expressions that will be matched against the request.path_info (request.path is stored, but not matched against). If they are matched, the visitor (and pageview) record will not be saved. Default includes 'favicon.ico' and 'robots.txt'. Note, static and media are not included since they should be served up statically Django's static serve view or via a lightweight server in production. Read more here
TRACK_IGNORE_STATUS_CODES - A list of HttpResponse status codes that will be ignored. If the HttpResponse object has a status_code in this blacklist, the pageview record will not be saved. For example,
TRACK_IGNORE_STATUS_CODES = [400, 404, 403, 405, 410, 500]
TRACK_REFERER - If True, referring site for all pageviews will be tracked. Default is False
TRACK_QUERY_STRING - If True, query string for all pageviews will be tracked. Default is False

视图

添加urls, 将tracking2.urls加入到urls表

urlpatterns += i18n_patterns(
    url(r'^tracking2/', include('tracking2.urls')), 
    url(r'^admin/', include(admin.site.urls)), # NOQA
    url(r'^', include('cms.urls')),
    url(r'^djangocms_comments/', include('djangocms_comments.urls')),
    # url(r'^tracking/', include('tracking.urls')), 
)

这样访问 trackings2/就可以查看效果了

效果图

有两个相关的template

  • tracking/dashboard.html – 仪表盘综合显示页
  • tracking/snippets/stats.html – 独立显示统计内容的页面

posted on 2018-10-01 22:17  汪汪家园  阅读(344)  评论(0编辑  收藏  举报