ckanext-googleanalytics_v2..../ckanext/googleanalytics/plugin.py

115 lines
4.2 KiB
Python
Raw Normal View History

2011-04-04 12:50:18 +02:00
import logging
import urllib
2011-04-05 16:28:27 +02:00
from paste.deploy.converters import asbool
2011-04-04 12:50:18 +02:00
from genshi.filters import Transformer
from genshi import HTML
from genshi.core import START, TEXT
2011-04-05 11:38:25 +02:00
from genshi.filters.transform import INSIDE, EXIT
import pylons
import ckan.lib.helpers as h
2012-07-11 11:38:47 +02:00
import ckan.plugins as p
import gasnippet
import commands
2011-04-04 18:44:39 +02:00
import dbutil
log = logging.getLogger('ckanext.googleanalytics')
2011-04-04 12:50:18 +02:00
class GoogleAnalyticsException(Exception):
pass
2012-07-11 11:38:47 +02:00
class GoogleAnalyticsPlugin(p.SingletonPlugin):
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IGenshiStreamFilter, inherit=True)
p.implements(p.IRoutes, inherit=True)
p.implements(p.IConfigurer, inherit=True)
2011-04-04 12:50:18 +02:00
def configure(self, 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)
2012-07-11 11:38:47 +02:00
def update_config(self, config):
p.toolkit.add_template_directory(config, 'templates')
p.toolkit.add_public_directory(config, 'public')
2012-07-11 11:38:47 +02:00
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'
)
2012-07-11 11:38:47 +02:00
return map
2011-04-04 12:50:18 +02:00
def filter(self, stream):
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)
2011-04-04 12:50:18 +02:00
routes = pylons.request.environ.get('pylons.routes_dict')
2012-07-11 11:38:47 +02:00
action = routes.get('action')
2012-04-05 19:12:25 +02:00
controller = routes.get('controller')
2012-04-05 19:12:25 +02:00
if (controller == 'package' and \
action in ['search', 'read', 'resource_read']) or \
(controller == 'group' and action == 'read'):
log.info("Tracking of resource downloads")
2012-07-11 11:38:47 +02:00
# add download tracking link
def js_attr(name, event):
attrs = event[1][1]
href = attrs.get('href').encode('utf-8')
link = '%s%s' % (self.resource_url, urllib.quote(href))
js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link
return js
# add some stats
def download_adder(stream):
download_html = '''<span class="downloads-count">
[downloaded %s times]</span>'''
count = None
for mark, (kind, data, pos) in stream:
if mark and kind == START:
href = data[1].get('href')
if href:
count = dbutil.get_resource_visits_for_url(href)
if count and mark is EXIT:
# emit count
yield INSIDE, (TEXT, HTML(download_html % count), pos)
yield mark, (kind, data, pos)
# perform the stream transform
stream = stream | Transformer('//a[contains(@class, "resource-url-analytics")]')\
.attr('onclick', js_attr)
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(gasnippet.download_style))
2011-04-04 12:50:18 +02:00
return stream