From f254d1afd7aef8d89355522fd16070b7980c31d8 Mon Sep 17 00:00:00 2001 From: John Martin Date: Tue, 23 Oct 2012 10:21:41 +0100 Subject: [PATCH 1/2] Tweaks to google analytics CKAN 2.0 JS module 1. Specified `resource_prefix` and `resource_url` context more tightly (could be global context) 2. Changed `this.href` to `jQuery(this).prop('href')` as it will never throw a syntax error when upon the condition of a `a.resource-url-analytics` not having a `href` attribute 3. Also added check so that if no `href` is present it doesn't send a blank link to google analytics --- .../googleanalytics_event_tracking.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ckanext/googleanalytics/fanstatic_library/googleanalytics_event_tracking.js b/ckanext/googleanalytics/fanstatic_library/googleanalytics_event_tracking.js index a6c9f35..322463c 100644 --- a/ckanext/googleanalytics/fanstatic_library/googleanalytics_event_tracking.js +++ b/ckanext/googleanalytics/fanstatic_library/googleanalytics_event_tracking.js @@ -5,10 +5,12 @@ this.ckan.module('google-analytics', function(jQuery, _) { googleanalytics_resource_prefix: '' }, initialize: function() { - resource_prefix = this.options.googleanalytics_resource_prefix; - jQuery('a.resource-url-analytics').on('click', function(){ - resource_url = resource_prefix + encodeURIComponent(this.href); - _gaq.push(['_trackPageview', resource_url]); + jQuery('a.resource-url-analytics').on('click', function() { + var resource_prefix = this.options.googleanalytics_resource_prefix; + var resource_url = resource_prefix + encodeURIComponent(jQuery(this).prop('href')); + if (resource_url) { + _gaq.push(['_trackPageview', resource_url]); + } }); } } From 13a57cd455ebfc5c6bf09297aaeafb557c152424 Mon Sep 17 00:00:00 2001 From: Ross Jones Date: Tue, 23 Oct 2012 17:42:04 +0100 Subject: [PATCH 2/2] Fixed issue where add_resource was used regardless of ckan version --- ckanext/googleanalytics/plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ckanext/googleanalytics/plugin.py b/ckanext/googleanalytics/plugin.py index 910a8a1..11bb893 100644 --- a/ckanext/googleanalytics/plugin.py +++ b/ckanext/googleanalytics/plugin.py @@ -52,7 +52,8 @@ class GoogleAnalyticsPlugin(p.SingletonPlugin): self.track_events = converters.asbool( config.get('googleanalytics.track_events', False)) - p.toolkit.add_resource('fanstatic_library', 'ckanext-googleanalytics') + if not converters.asbool(config.get('ckan.legacy_templates', 'false')): + p.toolkit.add_resource('fanstatic_library', 'ckanext-googleanalytics') def update_config(self, config): '''Change the CKAN (Pylons) environment configuration.