1. 程式人生 > >頁面瀏覽統計之(三) tracking2

頁面瀏覽統計之(三) tracking2

ocm csr html github eight 文件夾 uri icon tracking

轉載 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 – 獨立顯示統計內容的頁面

技術分享圖片

頁面瀏覽統計之(三) tracking2