Skip to content

Commit

Permalink
#4228 First draft of tracking download and page view events in mixpanel
Browse files Browse the repository at this point in the history
+  refactoring for google analytics
(cherry picked from commit b629a21)
  • Loading branch information
alexandru-m-g committed Jun 8, 2016
1 parent eefa505 commit a08efc6
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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')
Expand Down
17 changes: 14 additions & 3 deletions ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
62 changes: 47 additions & 15 deletions ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<!--[if gt IE 8]><!--> <html lang="{{ lang }}"> <!--<![endif]-->
{%- endblock -%}

{% block mixpanel_init %}
{{ h.snippet('snippets/mixpanel.html') }}
{% endblock %}

{# Allows custom attributes to be added to the <head> tag #}
<head{% block headtag %}{% endblock %}>

Expand All @@ -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
});
}());

</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }}
Expand Down Expand Up @@ -58,7 +63,7 @@
#}
<img src="/images/homepage/download.svg" alt=" {{ _('Download') }}" style="width: 14px;" />
<span class="ga-download-resource-title" style="display: none">{{ res.name }}</span>
<span class="ga-download-dataset-title" style="display: none">{{ c.package.title }}</span>
{#<span class="ga-download-dataset-title" style="display: none">{{ c.package.title }}</span>#}
</a>
</li>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<a href="{{ res.url }}" class="btn btn-secondary hdx-btn resource-url-analytics ga-download resource-btn resource-icon-btn" title="{{ _('Download') }}" tracking-type="{{res.url_type}}">
<img src="/images/homepage/download.svg" alt=" {{ _('Download') }}" />
<span class="ga-download-resource-title" style="display: none">{{ res.name }}</span>
<span class="ga-download-dataset-title" style="display: none">{{ pkg.title }}</span>
{#<span class="ga-download-dataset-title" style="display: none">{{ pkg.title }}</span>#}
</a>

{% set button_id = 'social-btn-' + res.id %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!-- start Mixpanel -->
<script type="text/javascript">
(function (e, b) {
if (!b.__SV) {
var a, f, i, g;
window.mixpanel = b;
b._i = [];
b.init = function (a, e, d) {
function f(b, h) {
var a = h.split(".");
2 == a.length && (b = b[a[0]], h = a[1]);
b[h] = function () {
b.push([h].concat(Array.prototype.slice.call(arguments, 0)))
}
}

var c = b;
"undefined" !== typeof d ? c = b[d] = [] : d = "mixpanel";
c.people = c.people || [];
c.toString = function (b) {
var a = "mixpanel";
"mixpanel" !== d && (a += "." + d);
b || (a += " (stub)");
return a
};
c.people.toString = function () {
return c.toString(1) + ".people (stub)"
};
i = "disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" ");
for (g = 0; g < i.length; g++)f(c, i[g]);
b._i.push([a, e, d])
};
b.__SV = 1.2;
a = e.createElement("script");
a.type = "text/javascript";
a.async = !0;
a.src = "undefined" !== typeof MIXPANEL_CUSTOM_LIB_URL ? MIXPANEL_CUSTOM_LIB_URL : "file:" === e.location.protocol && "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//) ? "https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js" : "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";
f = e.getElementsByTagName("script")[0];
f.parentNode.insertBefore(a, f)
}
})(document, window.mixpanel || []);
mixpanel.init("{{ h.hdx_get_ckan_config('hdx.mixpanel.token') }}");
</script>
<!-- end Mixpanel -->
3 changes: 3 additions & 0 deletions common-config-ini.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a08efc6

Please sign in to comment.