From a08efc630925377e5eaf36efb60c210eb6ed4050 Mon Sep 17 00:00:00 2001 From: alexandru-m-g Date: Thu, 9 Jun 2016 01:01:00 +0300 Subject: [PATCH] #4228 First draft of tracking download and page view events in mixpanel + refactoring for google analytics (cherry picked from commit b629a21) --- .../controllers/dataset_controller.py | 47 +++++++------- .../hdx_theme/fanstatic/google-analytics.js | 17 ++++- .../ckanext/hdx_theme/templates/base.html | 62 ++++++++++++++----- .../hdx_theme/templates/indicator/read.html | 11 +++- .../templates/package/resource_read.html | 13 ++-- .../package/snippets/resource_item.html | 2 +- .../templates/snippets/mixpanel.html | 44 +++++++++++++ common-config-ini.txt | 3 + 8 files changed, 149 insertions(+), 50 deletions(-) create mode 100644 ckanext-hdx_theme/ckanext/hdx_theme/templates/snippets/mixpanel.html diff --git a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py index 6440df67bd..b1378920dd 100644 --- a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py +++ b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py @@ -704,8 +704,10 @@ def read(self, id, format='html'): template = template[:template.index('.') + 1] + format # set dataset type for google analytics - modified by HDX - c.ga_dataset_type = self._google_analytics_dataset_type(c.pkg_dict) - c.ga_location = self._google_analytics_location(c.pkg_dict) + # c.ga_dataset_type = self._google_analytics_dataset_type(c.pkg_dict) + c.analytics_is_cod = self._analytics_is_cod(c.pkg_dict) + c.analytics_is_indicator = self._analytics_is_indicator(c.pkg_dict) + c.analytics_group_names, c.analytics_group_ids = self._analytics_location(c.pkg_dict) # changes done for indicator act_data_dict = {'id': c.pkg_dict['id'], 'limit': 7} @@ -770,31 +772,26 @@ def read(self, id, format='html'): assert False, "We should never get here" - def _google_analytics_dataset_type(self, pkg_dict): - type = 'standard' - tags = [tag.get('name', '') for tag in pkg_dict.get('tags', [])] - + def _analytics_is_indicator(self, pkg_dict): if int(pkg_dict.get('indicator', 0)) == 1: - type = 'indicator' - if 'cod' in tags: - type = 'cod~indicator' if type == 'indicator' else 'cod' + return 'true' + return 'false' - return type + def _analytics_is_cod(self, pkg_dict): + tags = [tag.get('name', '') for tag in pkg_dict.get('tags', [])] + if 'cod' in tags: + return 'true' + return 'false' - def _google_analytics_location(self, pkg_dict): - limit = 15 + def _analytics_location(self, pkg_dict): locations = pkg_dict.get('groups', []) - if len(locations) >= limit: - result = 'many' - else: - locations = [item.get('name', '') for item in locations] - locations.sort() - result = "~".join(locations) + location_names = [] + location_ids = [] + for l in sorted(locations, key=lambda item: item.get('name', '')): + location_names.append(l.get('name', '')) + location_ids.append(l.get('id', '')) - if not result: - result = 'none' - - return result + return json.dumps(location_names), json.dumps(location_ids) def _get_org_extras(self, org_id): """ @@ -1101,8 +1098,10 @@ def resource_read(self, id, resource_id): c.resource['has_views'] = len(resource_views) > 0 # set dataset type for google analytics - modified by HDX - c.ga_dataset_type = self._google_analytics_dataset_type(c.package) - c.ga_location = self._google_analytics_location(c.package) + # c.ga_dataset_type = self._google_analytics_dataset_type(c.package) + c.analytics_is_cod = self._analytics_is_cod(c.package) + c.analytics_is_indicator = self._analytics_is_indicator(c.package) + c.analytics_group_names, c.analytics_group_ids = self._analytics_location(c.package) current_resource_view = None view_id = request.GET.get('view_id') diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js index c5bc0875d6..30aa7563fd 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js +++ b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js @@ -2,12 +2,23 @@ function setUpResourcesTracking(){ $('.ga-download').on('click', function(){ - //var rTitle = $(this).parents(".resource-item").find(".heading").attr("title"); - //var dTitle = $(".itemTitle").text().trim(); var rTitle = $(this).find(".ga-download-resource-title").text().trim(); - var dTitle = $(this).find(".ga-download-dataset-title").text().trim(); + // var dTitle = $(this).find(".ga-download-dataset-title").text().trim(); + var dTitle = analyticsInfo.datasetName; ga('send', 'event', 'resource', 'download', rTitle + " (" + dTitle +")"); ga('send', 'event', 'dataset', 'resource-download', dTitle); + + mixpanel.track("resource download", { + "resource name": rTitle, + "dataset name": dTitle, + "dataset id": analyticsInfo.datasetId, + "org name": analyticsInfo.organizationName, + "org id": analyticsInfo.organizationId, + "group names": analyticsInfo.groupNames, + "group ids": analyticsInfo.groupIds, + "is cod": analyticsInfo.isCod, + "is indicator": analyticsInfo.isIndicator + }); }); $('.ga-share').on('click', function(){ diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html index b78468cf07..a759cc43e2 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html @@ -10,6 +10,10 @@ {%- endblock -%} +{% block mixpanel_init %} + {{ h.snippet('snippets/mixpanel.html') }} +{% endblock %} + {# Allows custom attributes to be added to the tag #} @@ -27,22 +31,50 @@ //ga('create', 'UA-48221887-3', { // 'cookieDomain': 'none' //}); - var dimension1Org = '{% block ga_dimension1 %}{% endblock %}'; - if ( dimension1Org ) { - ga('set', 'dimension1', dimension1Org); - } - - var dimension2Type = '{% block ga_dimension2 %}{% endblock %}'; - if ( dimension2Type ) { - ga('set', 'dimension2', dimension2Type); - } - - var dimension3Location = '{% block ga_dimension3 %}{% endblock %}'; - if ( dimension3Location ) { - ga('set', 'dimension3', dimension3Location); - } - ga('send', 'pageview'); + /** + * A map holding all the information to be sent to the analytics servers + * @type {object} + */ + var analyticsInfo = { + 'organizationName': '{% block analytics_org_name %}None{% endblock %}', + 'organizationId': '{% block analytics_org_id %}None{% endblock %}', + 'groupNames': {% block analytics_group_names %}[]{% endblock %}, + 'groupIds': {% block analytics_group_ids %}[]{% endblock %}, + 'isCod': {% block analytics_is_cod %}false{% endblock %}, + 'isIndicator': {% block analytics_is_indicator %}false{% endblock %}, + 'datasetName': '{% block analytics_dataset_name %}None{% endblock %}', + 'datasetId': '{% block analytics_dataset_id %}None{% endblock %}' + }; + + (function() { + if (analyticsInfo.organizationName != 'None') { + ga('set', 'dimension1', analyticsInfo.organizationName); + } + + var dimension2Type = analyticsInfo.isCod ? 'cod' : ''; + dimension2Type += analyticsInfo.isCod && analyticsInfo.isIndicator ? '~' : ''; + dimension2Type += analyticsInfo.isIndicator ? 'indicator' : ''; + + if (dimension2Type) { + ga('set', 'dimension2', dimension2Type); + } + + var dimension3Location = analyticsInfo.groupNames.length < 15 ? analyticsInfo.groupNames.join('~') : 'many'; + if (dimension3Location) { + ga('set', 'dimension3', dimension3Location); + } + ga('send', 'pageview'); + + mixpanel.track("page view", { + "org name": analyticsInfo.organizationName, + "org id": analyticsInfo.organizationId, + "group names": analyticsInfo.groupNames, + "group ids": analyticsInfo.groupIds, + "is cod": analyticsInfo.isCod, + "is indicator": analyticsInfo.isIndicator + }); + }()); diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/indicator/read.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/indicator/read.html index ea0831d973..3bb88eb083 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/indicator/read.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/indicator/read.html @@ -5,9 +5,14 @@ {% block subtitle %}{{ pkg.title or pkg.name }}{% endblock %} {# The line below is for google analytics #} -{% block ga_dimension1 %}{{ pkg.organization.title }}{% endblock %} -{% block ga_dimension2 %}{{ c.ga_dataset_type }}{% endblock %} -{% block ga_dimension3 %}{{ c.ga_location }}{% endblock %} +{% block analytics_org_name %}{{ pkg.organization.name }}{% endblock %} +{% block analytics_org_id %}{{ pkg.organization.id }}{% endblock %} +{% block analytics_is_cod %}{{ c.analytics_is_cod }}{% endblock %} +{% block analytics_is_indicator %}{{ c.analytics_is_indicator }}{% endblock %} +{% block analytics_group_names %}{{ c.analytics_group_names | safe }}{% endblock %} +{% block analytics_group_ids %}{{ c.analytics_group_ids | safe }}{% endblock %} +{% block analytics_dataset_name %}{{ pkg.name }}{% endblock %} +{% block analytics_dataset_id %}{{ pkg.id }}{% endblock %} {% block breadcrumb_content %} {% block breadcrum_parent_item %} diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html index 9dd2214433..0d9826fec5 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/resource_read.html @@ -4,9 +4,14 @@ {% set resource_dwd_url = res.perma_link if res.perma_link else res.url %} {# The line below is for google analytics #} -{% block ga_dimension1 %}{{ c.package.organization.title }}{% endblock %} -{% block ga_dimension2 %}{{ c.ga_dataset_type }}{% endblock %} -{% block ga_dimension3 %}{{ c.ga_location }}{% endblock %} +{% block analytics_org_name %}{{ c.package.organization.name }}{% endblock %} +{% block analytics_org_id %}{{ c.package.organization.id }}{% endblock %} +{% block analytics_is_cod %}{{ c.analytics_is_cod }}{% endblock %} +{% block analytics_is_indicator %}{{ c.analytics_is_indicator }}{% endblock %} +{% block analytics_group_names %}{{ c.analytics_group_names | safe }}{% endblock %} +{% block analytics_group_ids %}{{ c.analytics_group_ids | safe }}{% endblock %} +{% block analytics_dataset_name %}{{ c.package.name }}{% endblock %} +{% block analytics_dataset_id %}{{ c.package.id }}{% endblock %} {% block head_extras -%} {{ super() }} @@ -58,7 +63,7 @@ #}  {{ _('Download') }} - + {##} {% endif %} diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/snippets/resource_item.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/snippets/resource_item.html index 6fea3a7593..52934f01b8 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/snippets/resource_item.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/package/snippets/resource_item.html @@ -73,7 +73,7 @@  {{ _('Download') }} - + {##} {% set button_id = 'social-btn-' + res.id %} diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/snippets/mixpanel.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/snippets/mixpanel.html new file mode 100644 index 0000000000..375c946511 --- /dev/null +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/snippets/mixpanel.html @@ -0,0 +1,44 @@ + + + \ No newline at end of file diff --git a/common-config-ini.txt b/common-config-ini.txt index 2bdbbe1095..d492f0b5ed 100644 --- a/common-config-ini.txt +++ b/common-config-ini.txt @@ -214,3 +214,6 @@ hdx.checks.config_path = /srv/ckan/ckanext-hdx_service_checker/ckanext/hdx_servi hdx.explorer.url = /mpx/#/ hdx.explorer.iframe.width = 100% hdx.explorer.iframe.height = 750px + +# Mixpanel +hdx.mixpanel.token = 875bfe50f9cb981f4e2817832c83c165