From 59cff2f0b1024a86eb251860346b4ea08d2e3bee Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 25 Jan 2017 16:18:57 +0200 Subject: [PATCH] Revert "Added custom dimensions" --- README.rst | 12 ------ ckanext/googleanalytics/controller.py | 28 ++------------ ckanext/googleanalytics/plugin.py | 38 +------------------ .../snippets/googleanalytics_header.html | 4 -- 4 files changed, 5 insertions(+), 77 deletions(-) diff --git a/README.rst b/README.rst index 7903a80..36d5aec 100644 --- a/README.rst +++ b/README.rst @@ -186,18 +186,6 @@ 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 ------- diff --git a/ckanext/googleanalytics/controller.py b/ckanext/googleanalytics/controller.py index 2f01fb1..a02413c 100644 --- a/ckanext/googleanalytics/controller.py +++ b/ckanext/googleanalytics/controller.py @@ -16,10 +16,6 @@ from paste.util.multidict import MultiDict from ckan.controllers.api import ApiController from ckan.controllers.package import PackageController -from ckan.common import g -if 'cloudstorage' in g.plugins: - is_using_cloudstorage = True - from ckanext.cloudstorage.controller import StorageController log = logging.getLogger('ckanext.googleanalytics') @@ -125,10 +121,8 @@ 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, - package_id=None): + self, user, request_obj_type, request_function, request_id): if config.get('googleanalytics.id'): - data_dict = { "v": 1, "tid": config.get('googleanalytics.id'), @@ -142,23 +136,9 @@ 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, id) - - if is_using_cloudstorage: - use_controller = StorageController - use_class = StorageController() - else: - use_controller = PackageController - use_class = PackageController() - - return use_controller.resource_download(use_class,id, resource_id, - filename) + self._post_analytics(c.user, "Resource", "Download", resource_id) + return PackageController.resource_download(self, id, resource_id, + filename) diff --git a/ckanext/googleanalytics/plugin.py b/ckanext/googleanalytics/plugin.py index f7dd791..5c3d6a1 100644 --- a/ckanext/googleanalytics/plugin.py +++ b/ckanext/googleanalytics/plugin.py @@ -9,9 +9,6 @@ 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 @@ -108,17 +105,6 @@ 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. @@ -273,29 +259,7 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin): See ITemplateHelpers. ''' - 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 + return {'googleanalytics_header': self.googleanalytics_header} def googleanalytics_header(self): '''Render the googleanalytics_header snippet for CKAN 2.0 templates. diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html index 73188fc..1486393 100644 --- a/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html @@ -6,9 +6,5 @@ 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');