PEP8 fixes for Google Analytics Event Tracking for CKAN API

This commit is contained in:
Alex Sadleir 2014-11-05 12:43:24 +11:00
parent f83664c98e
commit d9f811cea7
1 changed files with 78 additions and 50 deletions

View File

@ -1,9 +1,9 @@
import logging
from ckan.lib.base import BaseController, c, render
from ckan.lib.base import BaseController, c, render, request
import dbutil
import urllib
from pprint import pprint
import logging
import ckan.logic as logic
import hashlib
@ -17,6 +17,7 @@ from ckan.controllers.api import ApiController
log = logging.getLogger('ckanext.googleanalytics')
class GAController(BaseController):
def view(self):
# get package objects corresponding to popular GA content
@ -24,61 +25,88 @@ class GAController(BaseController):
c.top_resources = dbutil.get_top_resources(limit=10)
return render('summary.html')
class GAApiController(ApiController):
# 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') != None):
data = urllib.urlencode({
"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 API Request",
"ea":request_obj_type+request_function,
"el":request_id,
})
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 API Request",
"ea": request_obj_type+request_function,
"el": request_id,
}
data = urllib.urlencode(data_dict)
log.debug("Sending API event to Google Analytics: "+data)
# send analytics asynchronously
threading.Thread(target=urllib.urlopen,args=("http://www.google-analytics.com/collect", data)).start()
threading.Thread(target=urllib.urlopen,
args=(
"http://www.google-analytics.com/collect",
data)).start()
def action(self, logic_function, ver=None):
try:
function = logic.get_action(logic_function)
except Exception,e:
log.debug(e)
pass
try:
side_effect_free = getattr(function, 'side_effect_free', False)
request_data = self._get_request_data(try_url_params=side_effect_free)
if isinstance(request_data, dict):
id = request_data.get('id','')
if 'q' in request_data.keys():
id = request_data['q']
if 'query' in request_data.keys():
id = request_data['query']
self._post_analytics(c.user,logic_function,'', id)
except Exception,e:
print log.debug(e)
pass
function = logic.get_action(logic_function)
side_effect_free = getattr(function, 'side_effect_free', False)
request_data = self._get_request_data(
try_url_params=side_effect_free)
if isinstance(request_data, dict):
id = request_data.get('id', '')
if 'q' in request_data:
id = request_data['q']
if 'query' in request_data:
id = request_data['query']
self._post_analytics(c.user, logic_function, '', id)
except Exception, e:
log.debug(e)
pass
return ApiController.action(self,logic_function, ver)
return ApiController.action(self, logic_function, ver)
def list(self, ver=None, register=None,
subregister=None, id=None):
self._post_analytics(c.user,
register +
("_"+str(subregister) if subregister else ""),
"list",
id)
return ApiController.list(self, ver, register, subregister, id)
def show(self, ver=None, register=None,
subregister=None, id=None, id2=None):
self._post_analytics(c.user,
register +
("_"+str(subregister) if subregister else ""),
"show",
id)
return ApiController.show(self, ver, register, subregister, id, id2)
def update(self, ver=None, register=None,
subregister=None, id=None, id2=None):
self._post_analytics(c.user,
register +
("_"+str(subregister) if subregister else ""),
"update",
id)
return ApiController.update(self, ver, register, subregister, id, id2)
def delete(self, ver=None, register=None,
subregister=None, id=None, id2=None):
self._post_analytics(c.user,
register +
("_"+str(subregister) if subregister else ""),
"delete",
id)
return ApiController.delete(self, ver, register, subregister, id, id2)
def list(self, ver=None, register=None, subregister=None, id=None):
self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"list",id)
return ApiController.list(self,ver, register, subregister, id)
def show(self, ver=None, register=None, subregister=None, id=None, id2=None):
self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"show",id)
return ApiController.show(self,ver, register, subregister, id,id2)
def update(self, ver=None, register=None, subregister=None, id=None, id2=None):
self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"update",id)
return ApiController.update(self,ver, register, subregister, id,id2)
def delete(self, ver=None, register=None, subregister=None, id=None, id2=None):
self._post_analytics(c.user,register+("_"+str(subregister) if subregister else ""),"delete",id)
return ApiController.delete(self,ver, register, subregister, id,id2)
def search(self, ver=None, register=None):
id = None
try:
@ -88,6 +116,6 @@ class GAApiController(ApiController):
if 'query' in params.keys():
id = params['query']
except ValueError, e:
print str(e)
log.debug(str(e))
pass
self._post_analytics(c.user,register,"search",id)
self._post_analytics(c.user, register, "search", id)