Commit 7f488541 authored by David Read's avatar David Read
Browse files

[1516] Universal Analytics event syntax. Also ready to insert the publisher name in.

parent 2988a804
......@@ -490,20 +490,28 @@ def scraper_icon(res, alt=None):
"Powered by scraperwiki.com.".format(url=res.get('scraper_source'), date=res.get('scraped').format("%d/%m/%Y"))
return icon('scraperwiki_small', alt=alt)
def ga_download_tracking(resource, action='download'):
'''Google Analytics event tracking for downloading a resource.
def get_organization_from_resource(res_dict):
from ckan import model
res_id = res_dict.get('id')
res = model.Resource.get(res_id)
if not res:
return None
return res.resource_group.package.get_organization()
def ga_download_tracking(resource, publisher_name, action='download'):
'''Google Analytics event tracking for downloading a resource. (Universal
Analytics syntax)
Values for action: download, download-cache
c.f. Google example:
<a href="#" onClick="_gaq.push(['_trackEvent', 'Videos', 'Play', 'Baby\'s First Birthday']);">Play</a>
e.g. ga('send', 'event', 'button', 'click', 'nav buttons', 4);
The call here is wrapped in a timeout to give the push call time to complete as some browsers
will complete the new http call without allowing _gaq time to complete. This *could* be resolved
will complete the new http call without allowing ga() time to complete. This *could* be resolved
by setting a target of _blank but this forces the download (many of them remote urls) into a new
tab/window.
'''
return "var that=this;_gaq.push(['_trackEvent','resource','%s','%s',0,true]);"\
return "var that=this;ga('send','event','resource','%s','%s');"\
"setTimeout(function(){location.href=that.href;},200);return false;" \
% (action, resource.get('url'))
......
......@@ -71,11 +71,12 @@
<!--! id=dataset for ckanext-spatial -->
</div>
<py:with vars="publisher_name = c.pkg.get_organization().name">
<div class="dataset-resources" py:if="not h.is_unpublished_item(c.pkg_dict)">
<!-- Resources -->
<py:if test="h.individual_resources()">
<h2>Data Resources (${len(h.individual_resources())})</h2>
${resource_table(h.individual_resources())}
${resource_table(h.individual_resources(), publisher_name)}
</py:if>
<py:if test="h.timeseries_resources()">
<h2>Data Resources (${len(h.timeseries_resources())} in a time series)</h2>
......@@ -83,7 +84,7 @@
<div class="year">
<h3>${year} <span class="showyear"></span><span class="hideyear"></span></h3>
<div class="year_items">
${resource_table(items)}
${resource_table(items, publisher_name)}
</div>
</div>
</py:for>
......@@ -92,13 +93,14 @@
<div class="dataset-resources-additional" py:if="not h.is_unpublished_item(c.pkg_dict)">
<py:if test="h.additional_resources() or h.gemini_resources()">
<h2>Additional Links (${len(h.additional_resources()) + len(h.gemini_resources())})</h2>
${resource_table(h.additional_resources() + h.gemini_resources())}
${resource_table(h.additional_resources() + h.gemini_resources(), publisher_name)}
</py:if>
<py:if test="not (h.dataset_type(c.pkg_extras) == 'uklp' or h.individual_resources() or h.timeseries_resources() or h.additional_resources())">
<em>(none)</em>
</py:if>
</div>
</py:with>
<div class="dataset-additional-information" py:if="not h.is_unpublished_item(c.pkg_dict)">
<!-- Dataset Information Section -->
......@@ -186,7 +188,7 @@
</py:def>
<div py:def="resource_table(resources)" class="row">
<div py:def="resource_table(resources, publisher_name)" class="row">
<div class="col-sm-6" py:for="res in resources">
<div class="dataset-resource">
<div class="dataset-resource-format">
......@@ -223,7 +225,7 @@
</a>
<ul class="dropdown-menu" role="menu" style="pointer-events:auto;" py:with="gemini = res.get('gemini',False)">
<li py:if="gemini">
<a href="${res.get('url', '')}" rel="dcat: distribution" target="_blank" onclick="${h.ga_download_tracking(res)}">
<a href="${res.get('url', '')}" rel="dcat: distribution" target="_blank" onclick="${h.ga_download_tracking(res, publisher_name)}">
<i class="icon-search"></i>&nbsp; ${res['action']}
</a>
</li>
......@@ -233,7 +235,7 @@
</a>
</li>
<li py:if="not gemini">
<a href="${res.get('url', '')}" onclick="${h.ga_download_tracking(res)}">
<a href="${res.get('url', '')}" onclick="${h.ga_download_tracking(res, publisher_name)}">
<i class="icon-download-alt"></i>&nbsp;
<py:if test="h.dataset_type(c.pkg_extras)!='uklp'">Download</py:if>
<py:if test="h.dataset_type(c.pkg_extras)=='uklp'">Direct Link</py:if>
......@@ -241,7 +243,7 @@
</li>
<py:with vars="cache_url, cache_timestamp = h.get_cache(res)">
<li py:if="not gemini and cache_url">
<a href="${cache_url}" class="js-tooltip" data-placement="bottom" title="Cached&nbsp;by&nbsp;data.gov.uk&nbsp;on ${cache_timestamp.strftime('%d/%m/%Y') if cache_timestamp else 'unknown date'}" onclick="${h.ga_download_tracking(res, 'download-cache')}">
<a href="${cache_url}" class="js-tooltip" data-placement="bottom" title="Cached&nbsp;by&nbsp;data.gov.uk&nbsp;on ${cache_timestamp.strftime('%d/%m/%Y') if cache_timestamp else 'unknown date'}" onclick="${h.ga_download_tracking(res, publisher_name, 'download-cache')}">
<i class="icon-save"></i>&nbsp;
Cached
</a>
......
......@@ -38,8 +38,8 @@
</py:with>
</div>
<div class="pull-right">
<a py:if="c.resource.get('url')" class="btn btn-danger resource-url-analytics resource-type-${c.resource.get('resource_type')}" href="${c.resource.get('url')}" onclick="${h.ga_download_tracking(c.resource)}">
<div class="pull-right" py:with="publisher=h.get_organization_from_resource(c.resource)">
<a py:if="c.resource.get('url')" class="btn btn-danger resource-url-analytics resource-type-${c.resource.get('resource_type')}" href="${c.resource.get('url')}" onclick="${h.ga_download_tracking(c.resource, publisher.name)}">
<py:choose test="c.resource.get('resource_type')">
<py:when test="'listing'"><i class="icon-search"></i>&nbsp; View</py:when>
<py:when test="'service'"><i class="icon-search"></i>&nbsp; View</py:when>
......@@ -57,7 +57,7 @@
</py:choose>
</a>
<py:with vars="cache_url, cache_timestamp = h.get_cache(c.resource)">
<a py:if="cache_url and not bool(h.get_resource_wms(c.resource))" class="btn btn-danger resource-url-analytics resource-type-${c.resource.get('resource_type')}" href="${cache_url}" onclick="${h.ga_download_tracking(c.resource, 'download-cache')}">
<a py:if="cache_url and not bool(h.get_resource_wms(c.resource))" class="btn btn-danger resource-url-analytics resource-type-${c.resource.get('resource_type')}" href="${cache_url}" onclick="${h.ga_download_tracking(c.resource, publisher.name, 'download-cache')}">
<div class="download js-tooltip" data-placement="bottom" data-original-title="Cached by data.gov.uk on: ${cache_timestamp.strftime('%d/%m/%Y') if cache_timestamp else 'unknown'}">
<py:if test="not bool(h.get_resource_wms(c.resource))">
<i class="icon-save"></i>&nbsp; Cached
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment