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

87 lines
3.1 KiB
Python
Raw Normal View History

2011-04-04 12:50:18 +02:00
import logging
import urllib
import os
from genshi.filters import Transformer
from genshi import HTML
2011-04-04 18:44:39 +02:00
from genshi.core import START, TEXT
from genshi.filters.transform import INSIDE
from pylons import config
2011-04-04 12:50:18 +02:00
from ckan.plugins import implements, SingletonPlugin
from ckan.plugins import IGenshiStreamFilter, IConfigurable, IRoutes
from ckan.plugins import IConfigurer
from gasnippet import gacode
2011-04-04 18:44:39 +02:00
from commands import DEFAULT_RESOURCE_URL_TAG
import dbutil
log = logging.getLogger('ckanext.googleanalytics')
2011-04-04 12:50:18 +02:00
class GoogleAnalyticsException(Exception):
pass
class GoogleAnalyticsPlugin(SingletonPlugin):
implements(IConfigurable, inherit=True)
implements(IGenshiStreamFilter, inherit=True)
implements(IRoutes, inherit=True)
implements(IConfigurer, inherit=True)
def configure(self, config):
self.config = config
log.info("Loading Google Analytics plugin...")
if (not 'googleanalytics.id' in config):
msg = "Missing googleanalytics.id in config"
raise GoogleAnalyticsException(msg)
def filter(self, stream):
log.info("Inserting GA code into template")
ga_id = self.config['googleanalytics.id']
code = HTML(gacode % ga_id)
stream = stream | Transformer('head').append(code)
2011-04-04 18:44:39 +02:00
resource_url = config.get('googleanalytics.resource_prefix',
DEFAULT_RESOURCE_URL_TAG)
2011-04-04 12:50:18 +02:00
# add download tracking link
def js_attr(name, event):
attrs = event[1][1]
2011-04-04 18:44:39 +02:00
link = '%s%s' % (resource_url,
urllib.quote(attrs.get('href')))
2011-04-04 12:50:18 +02:00
js = "javascript: _gaq.push(['_trackPageview', '%s']);" % link
return js
2011-04-04 18:44:39 +02:00
# add some stats
def download_adder(stream):
download_html = ' <span="downloads-count">(%s downloads)</span>'
count = None
for mark, (kind, data, pos) in stream:
if mark and kind == START:
href = data[1].get('href')
count = dbutil.get_resource_visits_for_url(href)
if count and kind == TEXT and mark == INSIDE:
yield mark, (kind,
data + download_html % count,
pos)
else:
yield mark, (kind, data, pos)
# perform the stream transform
2011-04-04 12:50:18 +02:00
stream = stream | Transformer(
'//div[@id="package"]//td/a')\
2011-04-04 18:44:39 +02:00
.apply(download_adder).attr('onclick', js_attr)
2011-04-04 12:50:18 +02:00
return stream
def after_map(self, map):
map.connect('analytics', '/analytics/package/top',
controller='ckanext.googleanalytics.controller:GAController',
action='view')
return map
def update_config(self, config):
here = os.path.dirname(__file__)
rootdir = os.path.dirname(os.path.dirname(here))
template_dir = os.path.join(rootdir, 'ckanext',
'googleanalytics', 'templates')
config['extra_template_paths'] = ','.join([template_dir,
config.get('extra_template_paths', '')])