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 30aa7563fd..cc0dcf49d9 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js +++ b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/google-analytics.js @@ -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
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 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(); @@ -50,6 +142,4 @@ function setUpGalleryTracking() { var dTitle = $(".itemTitle").text().trim(); ga('send', 'event', 'gallery', 'click', rTitle + " (" + dTitle +")"); }); -} - -setUpResourcesTracking(); \ No newline at end of file +} \ No newline at end of file diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/indicator.js b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/indicator.js index a55a4d10aa..c24c01c054 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/indicator.js +++ b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/indicator.js @@ -21,7 +21,6 @@ $(document).ready(function() { $("#location-dd").find("span").text($(this).text()); }); - //setUpResourcesTracking(); // directly called now from google-analytics.js setUpShareTracking(); setUpGalleryTracking(); }); diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html index a759cc43e2..dadc5efa2e 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/base.html @@ -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() { @@ -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, diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/header.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/header.html index b0258892d1..838a172ec3 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/header.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/header.html @@ -141,6 +141,7 @@

+ {% 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' %} @@ -216,6 +217,7 @@

+

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 0d9826fec5..8d45b12ad0 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 @@ -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 %}