Add 'track_events' config option, tidy up config

reading.
This commit is contained in:
John Glover 2012-07-11 14:34:18 +01:00
parent 1fa09bae92
commit 03b4023676
4 changed files with 64 additions and 34 deletions

View File

@ -60,12 +60,10 @@ Installation
Finally, there are some optional configuration settings (shown here
with their default settings)::
googleanalytics.show_downloads = true
googleanalytics.resource_prefix = /downloads/
googleanalytics.domain = auto
If ``show_downloads`` is set, a download count for resources will
be displayed on individual package pages.
googleanalytics.show_downloads = true
googleanalytics.track_events = false
``resource_prefix`` is an arbitrary identifier so that we can query
for downloads in Google Analytics. It can theoretically be any
@ -76,8 +74,16 @@ Installation
``domain`` allows you to specify a domain against which Analytics
will track users. You will usually want to leave this as ``auto``;
if you are tracking users from multiple subdomains, you might want
to specify something like ``.mydomain.com``. See `Google's
documentation <http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setDomainName>`_ for more info.
to specify something like ``.mydomain.com``.
See `Google's documentation
<http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setDomainName>`_
for more info.
If ``show_downloads`` is set, a download count for resources will
be displayed on individual package pages.
If ``track_events`` is set, Google Analytics event tracking will be
enabled.
5. Restart CKAN (e.g. by restarting Apache)

View File

@ -1,4 +1,4 @@
gacode = """
header_code = """
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']);
@ -11,3 +11,15 @@ gacode = """
})();
</script>
"""
footer_code = """
<script type="text/javascript" src="%s"></script>
"""
download_style = """
<style type="text/css">
span.downloads-count {
font-size: 0.9em;
}
</style>
"""

View File

@ -6,6 +6,7 @@ from genshi import HTML
from genshi.core import START, TEXT
from genshi.filters.transform import INSIDE, EXIT
from pylons import config, request
import ckan.lib.helpers as h
import ckan.plugins as p
import gasnippet
import commands
@ -25,48 +26,62 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
def configure(self, config):
self.config = config
if (not 'googleanalytics.id' in config):
msg = "Missing googleanalytics.id in config"
raise GoogleAnalyticsException(msg)
ga_id = config['googleanalytics.id']
ga_domain = config.get('googleanalytics.domain', 'auto')
js_url = h.url_for_static('/scripts/ckanext-googleanalytics.js')
self.resource_url = config.get('googleanalytics.resource_prefix',
commands.DEFAULT_RESOURCE_URL_TAG)
self.show_downloads = asbool(
config.get('googleanalytics.show_downloads', True)
)
self.track_events = asbool(
config.get('googleanalytics.track_events', False)
)
self.header_code = HTML(gasnippet.header_code % (ga_id, ga_domain))
self.footer_code = HTML(gasnippet.footer_code % js_url)
def update_config(self, config):
p.toolkit.add_template_directory(config, 'templates')
p.toolkit.add_public_directory(config, 'public')
def after_map(self, map):
map.redirect("/analytics/package/top", "/analytics/dataset/top")
map.connect('analytics', '/analytics/dataset/top',
controller='ckanext.googleanalytics.controller:GAController',
action='view')
map.connect(
'analytics', '/analytics/dataset/top',
controller='ckanext.googleanalytics.controller:GAController',
action='view'
)
return map
def filter(self, stream):
log.info("Inserting GA code into template")
ga_id = self.config['googleanalytics.id']
ga_domain = self.config.get('googleanalytics.domain', 'auto')
code = HTML(gasnippet.gacode % (ga_id, ga_domain))
stream = stream | Transformer('head').append(code)
resource_url = config.get('googleanalytics.resource_prefix',
commands.DEFAULT_RESOURCE_URL_TAG)
log.info("Inserting Google Analytics code into template")
stream = stream | Transformer('head').append(self.header_code)
if self.track_events:
stream = stream | Transformer('body/div[@id="scripts"]')\
.append(self.footer_code)
routes = request.environ.get('pylons.routes_dict')
action = routes.get('action')
controller = routes.get('controller')
if (controller == 'package' and \
action in ['search', 'read', 'resource_read']) or \
(controller == 'group' and action == 'read'):
log.info("Tracking of resource downloads")
show_downloads = (
asbool(config.get('googleanalytics.show_downloads', True)) and
action == 'read' and controller == 'package'
)
# add download tracking link
def js_attr(name, event):
attrs = event[1][1]
href = attrs.get('href').encode('utf-8')
link = '%s%s' % (resource_url, urllib.quote(href))
link = '%s%s' % (self.resource_url, urllib.quote(href))
js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link
return js
@ -85,22 +100,15 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin):
yield INSIDE, (TEXT, HTML(download_html % count), pos)
yield mark, (kind, data, pos)
# and some styling
download_style = '''
<style type="text/css">
span.downloads-count {
font-size: 0.9em;
}
</style>
'''
# perform the stream transform
stream = stream | Transformer('//a[contains(@class, "resource-url-analytics")]')\
.attr('onclick', js_attr)
if show_downloads:
if (self.show_downloads and action == 'read' and
controller == 'package'):
stream = stream | Transformer('//a[contains(@class, "resource-url-analytics")]')\
.apply(download_adder)
stream = stream | Transformer('//head').append(HTML(download_style))
stream = stream | Transformer('//head')\
.append(HTML(gasnippet.download_style))
return stream

View File

@ -0,0 +1,4 @@
(function ($) {
$(document).ready(function () {
});
}(jQuery));