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