Skip to content

Commit

Permalink
#4228 tracking the search event
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-m-g committed Jun 13, 2016
1 parent 503eb12 commit a0429a6
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 40 deletions.
164 changes: 127 additions & 37 deletions ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,134 @@
$(function setUpSearchTracking() {
var formEl = $("#dataset-filter-form");
if (formEl.length > 0) {
var mixpanelMapping = {
'q': {
'name': 'search term',
'isList': false,
'mandatory': true
},
'tags': {
'name': 'tag filters',
'isList': true,
'mandatory': true
},
'res_format': {
'name': 'format filters',
'isList': true,
'mandatory': true
},
'organization': {
'name': 'org filters',
'isList': true,
'mandatory': true
},
'groups': {
'name': 'location filters',
'isList': true,
'mandatory': true
},
'ext_page_size': {
'name': 'items per page',
'isList': false,
'mandatory': false
},
'sort': {
'name': 'sorting',
'isList': false,
'mandatory': false
}
};

var paramList = formEl.serializeArray();
var mixpanelEventMeta = {
"page title": analyticsInfo.pageTitle,
"org name": analyticsInfo.organizationName,
"org id": analyticsInfo.organizationId,
"group names": analyticsInfo.groupNames,
"group ids": analyticsInfo.groupIds
};
var sendTrackingEvent = false;
for (var i = 0; i < paramList.length; i++) {
var param = paramList[i];
var mappingInfo = mixpanelMapping[param.name];
var paramValue = param.value.trim();
if (mappingInfo && paramValue) {
populateMetadata(mixpanelEventMeta, mappingInfo, paramValue);
sendTrackingEvent = sendTrackingEvent || mappingInfo.mandatory;
}
}
if (sendTrackingEvent){
var reResult = /ext_search_source=([^&]+)(&|$)/.exec(location.href);
if (reResult && reResult.length > 1) {
mixpanelEventMeta["search box location"] = reResult[1];
}
console.log(JSON.stringify(mixpanelEventMeta));
mixpanel.track("search", mixpanelEventMeta);
}
else {
console.log("No mandatory properties found. Not sending search event to mixpanel.");
}
}

function setUpResourcesTracking(){
$('.ga-download').on('click', function(){
var rTitle = $(this).find(".ga-download-resource-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
});
});
/**
* Populates the object that is sent to mixpanel for one <form> parameter
* @param mixpanelEventMeta {Object} map of property-values to be sent to mixpanel
* @param mappingInfo {{name:string, isList: boolean, mandatory: boolean}} information about how the param should be formatted
* @param paramValue {string} the value of the <form> parameter
*/
function populateMetadata(mixpanelEventMeta, mappingInfo, paramValue) {
if (mappingInfo.isList) {
mixpanelEventMeta[mappingInfo.name] = mixpanelEventMeta[mappingInfo.name] ?
mixpanelEventMeta[mappingInfo.name] : [];
mixpanelEventMeta[mappingInfo.name].push(paramValue);
}
else {
mixpanelEventMeta[mappingInfo.name] = paramValue;
}
}
});

$('.ga-share').on('click', function(){
var rTitle = $(this).parents(".resource-item").find(".heading").attr("title");
var dTitle = $(".itemTitle").text().trim();
ga('send', 'event', 'resource', 'share', rTitle + " (" + dTitle +")");
ga('send', 'event', 'dataset', 'resource-share', dTitle);
});

$('.ga-preview').on('click', function(){
console.log("sending event");
var rTitle = $(this).parents(".resource-item").find(".heading").attr("title");
var dTitle = $(".itemTitle").text().trim();
ga('send', 'event', 'resource', 'preview', rTitle + " (" + dTitle +")");
ga('send', 'event', 'dataset', 'resource-preview', dTitle);
});
}
(function() {
function setUpResourcesTracking() {
$('.ga-download').on('click', function () {
var rTitle = $(this).find(".ga-download-resource-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,
"page title": analyticsInfo.pageTitle,
"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 () {
var rTitle = $(this).parents(".resource-item").find(".heading").attr("title");
var dTitle = $(".itemTitle").text().trim();
ga('send', 'event', 'resource', 'share', rTitle + " (" + dTitle + ")");
ga('send', 'event', 'dataset', 'resource-share', dTitle);
});

$('.ga-preview').on('click', function () {
console.log("sending event");
var rTitle = $(this).parents(".resource-item").find(".heading").attr("title");
var dTitle = $(".itemTitle").text().trim();
ga('send', 'event', 'resource', 'preview', rTitle + " (" + dTitle + ")");
ga('send', 'event', 'dataset', 'resource-preview', dTitle);
});
}
setUpResourcesTracking();
}());
function setUpShareTracking(){
$(".indicator-actions.followButtonContainer a").on('click', function (){
var dTitle = $(".itemTitle").text().trim();
Expand All @@ -50,6 +142,4 @@ function setUpGalleryTracking() {
var dTitle = $(".itemTitle").text().trim();
ga('send', 'event', 'gallery', 'click', rTitle + " (" + dTitle +")");
});
}

setUpResourcesTracking();
}
1 change: 0 additions & 1 deletion ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ $(document).ready(function() {
$("#location-dd").find("span").text($(this).text());
});

//setUpResourcesTracking(); // directly called now from google-analytics.js
setUpShareTracking();
setUpGalleryTracking();
});
5 changes: 4 additions & 1 deletion ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
'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 %}'
'datasetId': '{% block analytics_dataset_id %}None{% endblock %}',
'pageTitle': '{% if self.subtitle()|trim %}{{ self.subtitle()|trim }}{% else %} {{ 'None' }} {% endif -%}'

};

(function() {
Expand All @@ -67,6 +69,7 @@
ga('send', 'pageview');

mixpanel.track("page view", {
"page title": analyticsInfo.pageTitle,
"org name": analyticsInfo.organizationName,
"org id": analyticsInfo.organizationId,
"group names": analyticsInfo.groupNames,
Expand Down
2 changes: 2 additions & 0 deletions ckanext-hdx_theme/ckanext/hdx_theme/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ <h1>
<div class="input-prepend input-append">
<i class="icon-search"></i>
<input type="text" class="searchInput" id="q" name="q" value="{{ c.q }}" autocomplete="off" placeholder="{{ _('Search Datasets') }}">
<input type="hidden" value="main-nav" name="ext_search_source" />
{% if c.full_facet_info %}
{% set filter_icon_class = 'icon-close_filter_button' if c.full_facet_info.get("filters_selected") or c.full_facet_info.get("query_selected") else 'icon-open_filter_button' %}
<i id="search_filter_btn" class="{{ filter_icon_class }} filter-icon" title="{{ _('Filters') }}"></i>
Expand Down Expand Up @@ -216,6 +217,7 @@ <h1>
<div class="input-prepend input-append">
<i class="icon-search"></i>
<input type="text" class="searchInput" id="q" name="q" value="" autocomplete="off" placeholder="Search Datasets">
<input type="hidden" value="main-nav" name="ext_search_source" />
</div>
<div class="search-ahead"></div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% set res = c.resource %}
{% set resource_dwd_url = res.perma_link if res.perma_link else res.url %}

{# The line below is for google analytics #}
{# The lines below are for analytics #}
{% 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 %}
Expand Down

0 comments on commit a0429a6

Please sign in to comment.