Skip to content

Commit

Permalink
Merge pull request #1013 from alphagov/track-custom-dimensions-on-pag…
Browse files Browse the repository at this point in the history
…e-view

Pass custom dimensions directly to page tracking
  • Loading branch information
carvil authored May 2, 2017
2 parents 746e78e + 17265dd commit 907f4ce
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 270 deletions.
308 changes: 132 additions & 176 deletions app/assets/javascripts/analytics/static-analytics.js
Original file line number Diff line number Diff line change
@@ -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/});
Expand All @@ -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)) {
Expand All @@ -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);
Expand All @@ -77,193 +56,170 @@
}
};

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.setTotalNumberOfSections();
this.setTotalNumberOfSectionLinks();
// 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;
}

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.setRenderingApplicationDimension = function(app) {
this.setDimension(20, app);
};
function customDimensionsFromBrowser() {
var customDimensions = {
dimension15: window.httpStatusCode || 200,
dimension16: GOVUK.cookie('TLSversion') || 'unknown'
};

StaticAnalytics.prototype.setSearchPositionDimension = function(position) {
this.setDimension(21, position);
};
if (window.devicePixelRatio) {
customDimensions.dimension11 = window.devicePixelRatio;
}

StaticAnalytics.prototype.setSchemaNameDimension = function(position) {
this.setDimension(17, position);
};
return customDimensions;
}

StaticAnalytics.prototype.setTotalNumberOfSections = function() {
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;
var totalNumberOfSections = sidebarSections || sidebarTaxons || accordionSubsections || gridSections;
this.setDimension(26, totalNumberOfSections);
};
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.setTotalNumberOfSectionLinks = function() {
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;
var totalNumberOfSectionLinks = relatedLinks || accordionLinks || gridLinks || leafLinks;
this.setDimension(27, totalNumberOfSectionLinks);
};
$.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;
})();
Loading

0 comments on commit 907f4ce

Please sign in to comment.