diff --git a/app/assets/javascripts/analytics/static-analytics.js b/app/assets/javascripts/analytics/static-analytics.js
index 3150ffae9..c46512c0d 100644
--- a/app/assets/javascripts/analytics/static-analytics.js
+++ b/app/assets/javascripts/analytics/static-analytics.js
@@ -1,24 +1,18 @@
-(function() {
+(function () {
"use strict";
window.GOVUK = window.GOVUK || {};
- var StaticAnalytics = function(config) {
+ var StaticAnalytics = function (config) {
// Create universal tracker
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/docs/analytics.md
// https://github.com/alphagov/govuk_frontend_toolkit/blob/master/javascripts/govuk/analytics/analytics.js
- var analytics = new GOVUK.Analytics(config);
- this.analytics = analytics;
-
- setPixelDensityDimension();
- setHTTPStatusCodeDimension();
- setTLSVersionDimension();
- this.setDimensionsFromMetaTags();
- this.setAbTestDimensionsFromMetaTags();
+ this.analytics = new GOVUK.Analytics(config);
+
this.callMethodRequestedByPreviousPage();
// Track initial pageview
- analytics.trackPageview();
+ this.trackPageview();
// Begin error and print tracking
GOVUK.analyticsPlugins.error({filenameMustMatch: /gov\.uk/});
@@ -28,25 +22,9 @@
GOVUK.analyticsPlugins.downloadLinkTracker({
selector: 'a[href*="/government/uploads"], a[href*="assets.publishing.service.gov.uk"]'
});
-
- function setPixelDensityDimension() {
- if (window.devicePixelRatio) {
- analytics.setDimension(11, window.devicePixelRatio);
- }
- }
-
- function setTLSVersionDimension() {
- var tls_version = GOVUK.cookie('TLSversion') || 'unknown';
- analytics.setDimension(16, tls_version);
- }
-
- function setHTTPStatusCodeDimension() {
- analytics.setDimension(15, window.httpStatusCode || 200);
- }
-
};
- StaticAnalytics.prototype.callOnNextPage = function(method, params) {
+ StaticAnalytics.prototype.callOnNextPage = function (method, params) {
params = params || [];
if (!$.isArray(params)) {
@@ -59,14 +37,15 @@
}
};
- StaticAnalytics.prototype.callMethodRequestedByPreviousPage = function() {
+ StaticAnalytics.prototype.callMethodRequestedByPreviousPage = function () {
if (GOVUK.cookie && GOVUK.cookie('analytics_next_page_call') !== null) {
var params, method;
try {
params = JSON.parse(GOVUK.cookie('analytics_next_page_call'));
method = params.shift();
- } catch(e) {}
+ } catch (e) {
+ }
if (method && typeof this[method] === "function") {
this[method].apply(this, params);
@@ -77,80 +56,38 @@
}
};
- StaticAnalytics.load = function() {
+ StaticAnalytics.load = function () {
GOVUK.Analytics.load();
};
- StaticAnalytics.prototype.setDimensionsFromMetaTags = function() {
- var $metas = $('meta[name^="govuk:"]'),
- dimensions = {};
-
- $metas.each(function() {
- var $meta = $(this),
- key = $meta.attr('name').split('govuk:')[1],
- value = $meta.attr('content');
-
- dimensions[key] = value;
- });
-
- // Set defaults first, so that we get a stable ordering in tests
- this.setDimensionsThatHaveDefaultValues(dimensions);
- this.setDimensionsThatDoNotHaveDefaultValues(dimensions);
+ // TODO: Remove this, and its corresponding call in collections
+ StaticAnalytics.prototype.setSectionDimension = function () {
};
- StaticAnalytics.prototype.setDimensionsThatHaveDefaultValues = function(dimensions) {
- this.setThemesDimension(dimensions['themes']);
- this.setNavigationPageTypeDimension(dimensions['navigation-page-type']);
- this.setUserJourneyStage(dimensions['user-journey-stage']);
- this.setNavigationDocumentTypeDimension(dimensions['navigation-document-type']);
- this.setContentIdDimension(dimensions['content-id']);
- this.setTaxonSlugDimension(dimensions['taxon-slug']);
- this.setTaxonIdDimension(dimensions['taxon-id']);
- this.setTaxonSlugsDimension(dimensions['taxon-slugs']);
- this.setTaxonIdsDimension(dimensions['taxon-ids']);
- this.setNavigationSectionsDimension(dimensions['navigation-sections']);
- this.setNavigationLinksDimension(dimensions['navigation-links']);
+ // TODO: We're setting this at a session level, because it's called in frontend's live-search.js to update
+ // the search count. We should make this consistent with the other dimensions and pass the dimension
+ // directly into the pageview arguments.
+ StaticAnalytics.prototype.setResultCountDimension = function (count) {
+ this.setDimension(5, count);
};
- StaticAnalytics.prototype.setDimensionsThatDoNotHaveDefaultValues = function(dimensions) {
- this.setSectionDimension(dimensions['section']);
- this.setFormatDimension(dimensions['format']);
- this.setResultCountDimension(dimensions['search-result-count']);
- this.setPublishingGovernmentDimension(dimensions['publishing-government']);
- this.setPoliticalStatusDimension(dimensions['political-status']);
- this.setOrganisationsDimension(dimensions['analytics:organisations']);
- this.setWorldLocationsDimension(dimensions['analytics:world-locations']);
- this.setRenderingApplicationDimension(dimensions['rendering-application']);
- this.setSchemaNameDimension(dimensions['schema-name']);
+ // TODO: We're setting this at a session level, because it's used by search through callOnNextPage. We should
+ // make this consistent with the other dimensions and pass the dimension directly into the pageview arguments.
+ StaticAnalytics.prototype.setSearchPositionDimension = function (position) {
+ this.setDimension(21, position);
};
- StaticAnalytics.prototype.setAbTestDimensionsFromMetaTags = function() {
- var $abMetas = $('meta[name^="govuk:ab-test"]'),
- staticAnalytics = this,
- // This is the block of dimensions assigned to A/B tests
- minAbTestDimension = 40,
- maxAbTestDimension = 49;
-
- $abMetas.each(function() {
- var $meta = $(this),
- dimension = parseInt($meta.data('analytics-dimension')),
- testNameAndBucket = $meta.attr('content');
-
- if (dimension >= minAbTestDimension && dimension <= maxAbTestDimension) {
- staticAnalytics.setDimension(dimension, testNameAndBucket);
- }
- });
- }
-
- StaticAnalytics.prototype.trackPageview = function(path, title, options) {
- this.analytics.trackPageview(path, title, options);
+ StaticAnalytics.prototype.trackPageview = function (path, title, options) {
+ var trackingOptions = this.customDimensions();
+ $.extend(trackingOptions, options);
+ this.analytics.trackPageview(path, title, trackingOptions);
};
- StaticAnalytics.prototype.trackEvent = function(category, action, options) {
+ StaticAnalytics.prototype.trackEvent = function (category, action, options) {
this.analytics.trackEvent(category, action, options);
};
- StaticAnalytics.prototype.setDimension = function(index, value, name, scope) {
+ StaticAnalytics.prototype.setDimension = function (index, value, name, scope) {
if (typeof value === "undefined") {
return;
}
@@ -158,100 +95,131 @@
this.analytics.setDimension(index, value, name, scope);
};
- StaticAnalytics.prototype.trackShare = function(network) {
+ StaticAnalytics.prototype.trackShare = function (network) {
this.analytics.trackShare(network);
};
- StaticAnalytics.prototype.addLinkedTrackerDomain = function(trackerId, name, domain) {
+ StaticAnalytics.prototype.addLinkedTrackerDomain = function (trackerId, name, domain) {
this.analytics.addLinkedTrackerDomain(trackerId, name, domain);
};
- StaticAnalytics.prototype.setSectionDimension = function(section) {
- this.setDimension(1, section);
- };
-
- StaticAnalytics.prototype.setFormatDimension = function(format) {
- this.setDimension(2, format);
- };
-
- StaticAnalytics.prototype.setThemesDimension = function(themes) {
- this.setDimension(3, themes || 'other');
- }
+ StaticAnalytics.prototype.customDimensions = function () {
+ var dimensions = $.extend(
+ {},
+ customDimensionsFromBrowser(),
+ customDimensionsFromMetaTags(),
+ customDimensionsFromDom(),
+ abTestCustomDimensions()
+ );
- StaticAnalytics.prototype.setContentIdDimension = function(contentId) {
- this.setDimension(4, contentId || '00000000-0000-0000-0000-000000000000');
- };
-
- StaticAnalytics.prototype.setResultCountDimension = function(count) {
- this.setDimension(5, count);
- };
-
- StaticAnalytics.prototype.setPublishingGovernmentDimension = function(government) {
- this.setDimension(6, government);
- };
-
- StaticAnalytics.prototype.setPoliticalStatusDimension = function(status) {
- this.setDimension(7, status);
- };
-
- StaticAnalytics.prototype.setOrganisationsDimension = function(orgs) {
- this.setDimension(9, orgs);
- };
-
- StaticAnalytics.prototype.setWorldLocationsDimension = function(locations) {
- this.setDimension(10, locations);
+ return $.each(dimensions, function (key, value) {
+ dimensions[key] = String(value);
+ });
};
- StaticAnalytics.prototype.setSchemaNameDimension = function(position) {
- this.setDimension(17, position);
- };
+ function customDimensionsFromBrowser() {
+ var customDimensions = {
+ dimension15: window.httpStatusCode || 200,
+ dimension16: GOVUK.cookie('TLSversion') || 'unknown'
+ };
- StaticAnalytics.prototype.setRenderingApplicationDimension = function(app) {
- this.setDimension(20, app);
- };
+ if (window.devicePixelRatio) {
+ customDimensions.dimension11 = window.devicePixelRatio;
+ }
- StaticAnalytics.prototype.setSearchPositionDimension = function(position) {
- this.setDimension(21, position);
- };
+ return customDimensions;
+ }
- StaticAnalytics.prototype.setNavigationSectionsDimension = function(sections) {
- this.setDimension(26, sections || '0');
- };
+ function customDimensionsFromMetaTags() {
+ var dimensionMappings = {
+ 'section': {dimension: 1},
+ 'format': {dimension: 2},
+ 'themes': {dimension: 3, defaultValue: 'other'},
+ 'content-id': {dimension: 4, defaultValue: '00000000-0000-0000-0000-000000000000'},
+ 'search-result-count': {dimension: 5},
+ 'publishing-government': {dimension: 6},
+ 'political-status': {dimension: 7},
+ 'analytics:organisations': {dimension: 9},
+ 'analytics:world-locations': {dimension: 10},
+ 'schema-name': {dimension: 17},
+ 'rendering-application': {dimension: 20},
+ 'navigation-page-type': {dimension: 32, defaultValue: 'none'},
+ 'user-journey-stage': {dimension: 33, defaultValue: 'thing'},
+ 'navigation-document-type': {dimension: 34, defaultValue: 'other'},
+ 'taxon-slug': {dimension: 56, defaultValue: 'other'},
+ 'taxon-id': {dimension: 57, defaultValue: 'other'},
+ 'taxon-slugs': {dimension: 58, defaultValue: 'other'},
+ 'taxon-ids': {dimension: 59, defaultValue: 'other'}
+ };
+
+ var $metas = $('meta[name^="govuk:"]');
+ var customDimensions = {};
+ var tags = {};
+
+ $metas.each(function () {
+ var $meta = $(this);
+ var key = $meta.attr('name').split('govuk:')[1];
+
+ var dimension = dimensionMappings[key];
+ if (dimension) {
+ tags[key] = $meta.attr('content');
+ }
+ });
- StaticAnalytics.prototype.setNavigationLinksDimension = function(links) {
- this.setDimension(27, links || '0');
- };
+ $.each(dimensionMappings, function (key, dimension) {
+ var value = tags[key] || dimension.defaultValue;
+ if (typeof value !== 'undefined') {
+ customDimensions['dimension' + dimension.dimension] = value;
+ }
+ });
- StaticAnalytics.prototype.setNavigationPageTypeDimension = function(pageType) {
- this.setDimension(32, pageType || 'none');
- };
+ return customDimensions;
+ }
- StaticAnalytics.prototype.setUserJourneyStage = function(journeyStage) {
- // Track the stage of a user's journey through GOV.UK. If the page does not
- // set a page type in a meta tag, default to "thing", which identifes a page
- // as containing content rather than some kind of navigation.
- this.setDimension(33, journeyStage || "thing");
- };
+ function customDimensionsFromDom() {
+ return {
+ dimension26: totalNumberOfSections(),
+ dimension27: totalNumberOfSectionLinks()
+ };
+
+ function totalNumberOfSections() {
+ var sidebarSections = $('[data-track-count="sidebarRelatedItemSection"]').length;
+ var sidebarTaxons = $('[data-track-count="sidebarTaxonSection"]').length;
+ var accordionSubsections = $('[data-track-count="accordionSection"]').length;
+ var gridSections = $('a[data-track-category="navGridLinkClicked"]').length;
+ return sidebarSections || sidebarTaxons || accordionSubsections || gridSections;
+ }
- StaticAnalytics.prototype.setNavigationDocumentTypeDimension = function(documentType) {
- this.setDimension(34, documentType || "other");
- };
+ function totalNumberOfSectionLinks() {
+ var relatedLinks = $('a[data-track-category="relatedLinkClicked"]').length;
+ var accordionLinks = $('a[data-track-category="navAccordionLinkClicked"]').length;
+ // Grid links are counted both as "sections" (see dimension 26), and as part of the total link count
+ var gridLinks = $('a[data-track-category="navGridLinkClicked"]').length
+ + $('a[data-track-category="navGridLeafLinkClicked"]').length;
+ var leafLinks = $('a[data-track-category="navLeafLinkClicked"]').length;
+ return relatedLinks || accordionLinks || gridLinks || leafLinks;
+ }
+ }
- StaticAnalytics.prototype.setTaxonSlugDimension = function(taxonSlug) {
- this.setDimension(56, taxonSlug || "other");
- };
+ function abTestCustomDimensions() {
+ // This is the block of dimensions assigned to A/B tests
+ var minAbTestDimension = 40;
+ var maxAbTestDimension = 49;
+ var $abMetas = $('meta[name^="govuk:ab-test"]');
+ var customDimensions = {};
- StaticAnalytics.prototype.setTaxonIdDimension = function(taxonId) {
- this.setDimension(57, taxonId || "other");
- };
+ $abMetas.each(function () {
+ var $meta = $(this);
+ var dimension = parseInt($meta.data('analytics-dimension'));
+ var testNameAndBucket = $meta.attr('content');
- StaticAnalytics.prototype.setTaxonSlugsDimension = function(taxonSlugs) {
- this.setDimension(58, taxonSlugs || "other");
- };
+ if (dimension >= minAbTestDimension && dimension <= maxAbTestDimension) {
+ customDimensions['dimension' + dimension] = testNameAndBucket;
+ }
+ });
- StaticAnalytics.prototype.setTaxonIdsDimension = function (taxonIds) {
- this.setDimension(59, taxonIds || "other");
- };
+ return customDimensions;
+ }
GOVUK.StaticAnalytics = StaticAnalytics;
})();
diff --git a/app/views/govuk_component/analytics_meta_tags.raw.html.erb b/app/views/govuk_component/analytics_meta_tags.raw.html.erb
index 784c726b5..07eb51490 100644
--- a/app/views/govuk_component/analytics_meta_tags.raw.html.erb
+++ b/app/views/govuk_component/analytics_meta_tags.raw.html.erb
@@ -81,5 +81,3 @@
<% meta_tags.each do |name, content| %>
<% end %>
-
-<%= yield :analytics_meta_tags %>
diff --git a/app/views/govuk_component/analytics_meta_tags_navigation.raw.html.erb b/app/views/govuk_component/analytics_meta_tags_navigation.raw.html.erb
deleted file mode 100644
index 0579a8031..000000000
--- a/app/views/govuk_component/analytics_meta_tags_navigation.raw.html.erb
+++ /dev/null
@@ -1,9 +0,0 @@
-<%
- number_of_sections ||= 0
- number_of_links ||= 0
-%>
-
-<% content_for :analytics_meta_tags do %>
-
-
-<% end %>
diff --git a/app/views/govuk_component/docs/analytics_meta_tags_navigation.yml b/app/views/govuk_component/docs/analytics_meta_tags_navigation.yml
deleted file mode 100644
index b4f884335..000000000
--- a/app/views/govuk_component/docs/analytics_meta_tags_navigation.yml
+++ /dev/null
@@ -1,10 +0,0 @@
-name: Analytics Meta Tags
-description: Meta tags to provide analytics information about the current page
-body: |
- A component that appends counts for navigation elements to the analytics_meta_tags component.
-
- The code which reads the meta tags can be found in static-analytics.js.
-fixtures:
- default:
- number_of_sections: 2
- number_of_links: 6
diff --git a/app/views/govuk_component/related_items.raw.html.erb b/app/views/govuk_component/related_items.raw.html.erb
index 80bf8bef3..9112f81c1 100644
--- a/app/views/govuk_component/related_items.raw.html.erb
+++ b/app/views/govuk_component/related_items.raw.html.erb
@@ -1,14 +1,7 @@
<% if local_assigns[:sections] && !sections.blank? %>
-
- <%
- number_of_sections = 0
- number_of_links = 0
- %>
-
-
- <%= render file: 'govuk_component/analytics_meta_tags_navigation.raw',
- locals: {
- number_of_sections: number_of_sections,
- number_of_links: number_of_links,
- } %>
-
<% end %>
diff --git a/app/views/govuk_component/taxonomy_sidebar.raw.html.erb b/app/views/govuk_component/taxonomy_sidebar.raw.html.erb
index 0c08cbce1..e39cd1ff4 100644
--- a/app/views/govuk_component/taxonomy_sidebar.raw.html.erb
+++ b/app/views/govuk_component/taxonomy_sidebar.raw.html.erb
@@ -1,14 +1,8 @@
<% if local_assigns[:items] && !items.blank? %>
- <%
- number_of_sections = 0
- number_of_links = 0
- %>
-