add resource downloads

This commit is contained in:
joetsoi 2015-11-16 00:09:55 +00:00
parent f9d0bba7df
commit 0652beb2d0
2 changed files with 33 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from webob.multidict import UnicodeMultiDict
from paste.util.multidict import MultiDict
from ckan.controllers.api import ApiController
from ckan.controolers.package import PackageController
log = logging.getLogger('ckanext.googleanalytics')
@ -114,3 +115,29 @@ class GAApiController(ApiController):
log.debug(str(e))
pass
self._post_analytics(c.user, register, "search", id)
class GAResourceController(PackageController):
# intercept API calls to record via google analytics
def _post_analytics(
self, user, request_obj_type, request_function, request_id):
if config.get('googleanalytics.id'):
data_dict = {
"v": 1,
"tid": config.get('googleanalytics.id'),
"cid": hashlib.md5(user).hexdigest(),
# customer id should be obfuscated
"t": "event",
"dh": c.environ['HTTP_HOST'],
"dp": c.environ['PATH_INFO'],
"dr": c.environ.get('HTTP_REFERER', ''),
"ec": "CKAN Resource Download Request",
"ea": request_obj_type+request_function,
"el": request_id,
}
plugin.GoogleAnalyticsPlugin.analytics_queue.put(data_dict)
def resource_download(self, id, resource_id, filename=None):
self._post_analytics(c.user, "Resource", "Download", resource_id)
return PackageController.resource_download(self, id, resource_id,
filename)

View File

@ -155,6 +155,12 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin):
m.connect('/rest/{register}/{id}', action='update', conditions=POST)
m.connect('/rest/{register}/{id}', action='delete', conditions=DELETE)
with SubMapper(map, controller='ckanext.googleanalytics.controller:GAResourceController') as m:
m.connect('/dataset/{id}/resource/{resource_id}/download',
action='resource_download')
m.connect('/dataset/{id}/resource/{resource_id}/download/{filename}',
action='resource_download')
return map
def after_map(self, map):