SXOEHXZDG-103 / custom dimensions

This commit is contained in:
Sergey Motornyuk 2016-11-16 12:09:34 +02:00
parent 3be1e138df
commit 45d97159a7
4 changed files with 63 additions and 3 deletions

View File

@ -186,6 +186,18 @@ giving permission in the browser::
$ paster getauthtoken --config=../ckan/development.ini
Custom Dimensions
-----------------
Custom GA dimensions can be configured as below (value is a name of Package object property)::
googleanalytics.custom_dimension.{dimension} = {value}
Examples::
; set value of package_id
googleanalytics.custom_dimension.dimension1 = id
; set value of Package's description
googleanalytics.custom_dimension.dimension4 = notes
Testing
-------

View File

@ -121,8 +121,10 @@ class GAApiController(ApiController):
class GAResourceController(PackageController):
# intercept API calls to record via google analytics
def _post_analytics(
self, user, request_obj_type, request_function, request_id):
self, user, request_obj_type, request_function, request_id,
package_id=None):
if config.get('googleanalytics.id'):
data_dict = {
"v": 1,
"tid": config.get('googleanalytics.id'),
@ -136,9 +138,15 @@ class GAResourceController(PackageController):
"ea": request_obj_type+request_function,
"el": request_id,
}
if package_id:
get_dimensions = config['pylons.h'].get_ga_custom_dimensions
dimensions = get_dimensions(package_id)
for (key, value) in dimensions:
data_dict[key.replace('dimension', 'cd')] = value
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)
self._post_analytics(c.user, "Resource", "Download", resource_id, id)
return PackageController.resource_download(self, id, resource_id,
filename)

View File

@ -9,6 +9,9 @@ import ckan.lib.helpers as h
import ckan.plugins as p
import gasnippet
from routes.mapper import SubMapper, Mapper as _Mapper
from operator import attrgetter
from ckan.common import c
import ckan.model as model
import urllib2
@ -105,6 +108,17 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin):
else:
p.toolkit.add_template_directory(config, 'templates')
custom_dimension_prefix = 'googleanalytics.custom_dimension'
self.ga_dimensions = map(
lambda i: (i[0][len(custom_dimension_prefix) + 1:], i[1]),
filter(
lambda x: x[0].startswith(custom_dimension_prefix),
config.items()
)
)
def before_map(self, map):
'''Add new routes that this extension's controllers handle.
@ -259,7 +273,29 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin):
See ITemplateHelpers.
'''
return {'googleanalytics_header': self.googleanalytics_header}
return {
'googleanalytics_header': self.googleanalytics_header,
'get_ga_custom_dimensions': self.get_ga_custom_dimensions,
}
def get_ga_custom_dimensions(self, pkg_id):
dimensions = []
if len(self.ga_dimensions):
getter = attrgetter(*map(lambda d: d[1], self.ga_dimensions))
package = model.Package.get(pkg_id)
if package is None:
return dimensions
try:
data = getter(package)
except Exception as e:
log.debug('Cannot obtain ga dimensions from package: {0}'.format(e))
return dimensions
if not isinstance(data, tuple):
data = (data, )
dimensions = zip(map(lambda d: d[0], self.ga_dimensions), data)
return dimensions
def googleanalytics_header(self):
'''Render the googleanalytics_header snippet for CKAN 2.0 templates.

View File

@ -6,5 +6,9 @@
ga('create', '{{googleanalytics_id}}', '{{googleanalytics_domain}}');
ga('set', 'anonymizeIp', true);
{% for dimension in h.get_ga_custom_dimensions((c.pkg or c.pkg_dict).id or '') -%}
ga('set', '{{ dimension[0] }}', '{{ dimension[1] }}');
{% endfor -%}
ga('send', 'pageview');
</script>