From 5a1150f4c72b6c4c52c06e4c5debca989daba7dd Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Tue, 5 Apr 2011 15:28:27 +0100 Subject: [PATCH] add show_downloads config option --- README.txt | 24 ++++++++++++++++-------- ckanext/googleanalytics/plugin.py | 10 ++++++++-- tests/test_general.py | 2 ++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/README.txt b/README.txt index dd688be..f3a94ec 100644 --- a/README.txt +++ b/README.txt @@ -21,17 +21,25 @@ Installation googleanalytics.id = UA-1010101-1 googleanalytics.username = googleaccount@gmail.com googleanalytics.password = googlepassword + googleanalytics.show_downloads = true # the following *must* match profile name in GA dashboard googleanalytics.profile_name = mydomain.com -That last comment is worth emphasising. Due to the strange -relationship between tracking IDs and profiles, you need to get that -right. It's the relevant value in the "Name" column for the list of -"Website Profiles" that you see when you click on an Analytics Account -link in the Google Analytics homepage. E.g. you'll need two clicks -from the analytics home page to see the profile name. Sometimes your -profile name might have a trailing slash; you need to include that, -too, if so. + That last comment is worth emphasising. Due to the strange + relationship between tracking IDs and profiles, you need to get that + right. It's the relevant value in the "Name" column for the list of + "Website Profiles" that you see when you click on an Analytics Account + link in the Google Analytics homepage. E.g. you'll need two clicks + from the analytics home page to see the profile name. Sometimes your + profile name might have a trailing slash; you need to include that, + too, if so. + + Note also that your password will probably be readable by other + people; so you may want to set up a new gmail account specifically + for accessing your gmail profile. + + If ``show_downloads`` is set, a download count for resources will be + displayed on individual package pages. 3. Wait a day or so for some stats to be recorded in Google diff --git a/ckanext/googleanalytics/plugin.py b/ckanext/googleanalytics/plugin.py index 1327309..a2748e2 100644 --- a/ckanext/googleanalytics/plugin.py +++ b/ckanext/googleanalytics/plugin.py @@ -1,6 +1,7 @@ import logging import urllib import os +from paste.deploy.converters import asbool from genshi.filters import Transformer from genshi import HTML from genshi.core import START, TEXT, END @@ -40,6 +41,8 @@ class GoogleAnalyticsPlugin(SingletonPlugin): stream = stream | Transformer('head').append(code) resource_url = config.get('googleanalytics.resource_prefix', DEFAULT_RESOURCE_URL_TAG) + show_downloads = asbool(config.get('googleanalytics.show_downloads', + False)) # add download tracking link def js_attr(name, event): @@ -69,8 +72,11 @@ class GoogleAnalyticsPlugin(SingletonPlugin): # perform the stream transform stream = stream | Transformer( '//div[@id="package"]//td/a')\ - .apply(download_adder).attr('onclick', js_attr) - + .attr('onclick', js_attr) + if show_downloads: + stream = stream | Transformer( + '//div[@id="package"]//td/a')\ + .apply(download_adder) return stream def after_map(self, map): diff --git a/tests/test_general.py b/tests/test_general.py index 621dfad..55011f4 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -51,6 +51,8 @@ class TestLoadCommand: = 'borf' config.local_conf['googleanalytics.profile_name'] \ = 'borf' + config.local_conf['googleanalytics.show_downloads'] \ + = 'true' cls.config = config.local_conf wsgiapp = make_app(config.global_conf, **config.local_conf) env = {'HTTP_ACCEPT': ('text/html;q=0.9,text/plain;'