Skip to content

Commit

Permalink
Merge pull request #1103 from alphagov/refactor-how-we-calculate-link…
Browse files Browse the repository at this point in the history
…s-and-sections

Refactor how we calculate section and link counts
  • Loading branch information
carvil authored Jul 28, 2017
2 parents b5af110 + 15191df commit cb9e1c4
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 69 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//= require govuk/analytics/external-link-tracker
//= require govuk/analytics/download-link-tracker

//= require analytics/page-content
//= require analytics/custom-dimensions
//= require analytics/static-analytics
//= require analytics/ecommerce
Expand Down
70 changes: 2 additions & 68 deletions app/assets/javascripts/analytics/custom-dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,75 +84,9 @@

function customDimensionsFromDom() {
return {
dimension26: totalNumberOfSections(),
dimension27: totalNumberOfSectionLinks()
dimension26: GOVUK.PageContent.getNumberOfSections(),
dimension27: GOVUK.PageContent.getNumberOfLinks()
};

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;
var browsePageSections = $('#subsection ul:visible').length ||
$('#section ul').length;
var topicPageSections = $('.topics-page nav.index-list').length
var documentCollectionSections = $('.document-collection .group-title').length;
var policyAreaSections = $('.topic section h1.label').length;

// Document collections, being a content item, might have related links.
// That means we need to check for sections on it first, before we default
// to the sections on the side bar.
var sectionCount =
documentCollectionSections ||
sidebarSections ||
sidebarTaxons ||
accordionSubsections ||
gridSections ||
browsePageSections ||
topicPageSections ||
policyAreaSections;

return sectionCount;
}

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;
var browsePageLinks = $('#subsection ul a:visible').length ||
$('#section ul a').length;
var subTopicPageLinks = $('.topics-page .index-list ul a').length;
var topicPageLinks = $('.topics-page .topics ul a').length;
var policyAreaLinks =
$('section.document-block a').length +
$('section .collection-list h2 a').length
var whitehallFinderPageLinks =
$('.document-list .document-row h3 a').length;
var documentCollectionLinks =
$('.document-collection .group-document-list li a').length;
var finderLinks = $('.finder-frontend-content li.document a').length;

// Document collections, being a content item, might have related links.
// That means we need to check for links on it first, before we default
// to the sections on the side bar.
var linksCount =
documentCollectionLinks ||
relatedLinks ||
accordionLinks ||
gridLinks ||
leafLinks ||
browsePageLinks ||
subTopicPageLinks ||
topicPageLinks ||
policyAreaLinks ||
whitehallFinderPageLinks ||
finderLinks;

return linksCount;
}
}

function abTestCustomDimensions() {
Expand Down
120 changes: 120 additions & 0 deletions app/assets/javascripts/analytics/page-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
(function () {
"use strict";
window.GOVUK = window.GOVUK || {};
var PageContent = function () { }

PageContent.getNumberOfSections = function () {
switch(true) {
case isNavigationGridPage():
return $('a[data-track-category="navGridLinkClicked"]').length;
case isNavigationAccordionPage():
return $('[data-track-count="accordionSection"]').length;
case isDocumentCollectionPage():
return $('.document-collection .group-title').length;
case isMainstreamBrowsePage():
return $('#subsection ul:visible').length || $('#section ul').length;
case isTopicPage():
return $('.topics-page nav.index-list').length;
case isPolicyAreaPage():
return $('.topic section h1.label').length;
default:
// It's a content page, not a "finding" page
var sidebarSections = $('[data-track-count="sidebarRelatedItemSection"]').length;
var sidebarTaxons = $('[data-track-count="sidebarTaxonSection"]').length;

return sidebarSections || sidebarTaxons;
}
};

PageContent.getNumberOfLinks = function () {
switch(true) {
case isNavigationGridPage():
return $('a[data-track-category="navGridLinkClicked"]').length
+ $('a[data-track-category="navGridLeafLinkClicked"]').length;
case isNavigationAccordionPage():
return $('a[data-track-category="navAccordionLinkClicked"]').length;
case isNavigationLeafPage():
return $('a[data-track-category="navLeafLinkClicked"]').length;
case isDocumentCollectionPage():
return $('.document-collection .group-document-list li a').length;
case isMainstreamBrowsePage():
return $('#subsection ul a:visible').length ||
$('#section ul a').length;
case isTopicPage():
return $('.topics-page .index-list ul a').length ||
$('.topics-page .topics ul a').length;
case isPolicyAreaPage():
return $('section.document-block a').length +
$('section .collection-list h2 a').length;
case isWhitehallFinderPage():
return $('.document-list .document-row h3 a').length;
case isFinderPage():
return $('.finder-frontend-content li.document a').length;
default:
// It's a content page, not a "finding" page, count related links
return $('a[data-track-category="relatedLinkClicked"]').length;
}
};

function getRenderingApplication () {
return $('meta[name="govuk:rendering-application"]').attr('content');
};

function getFormat () {
return $('meta[name="govuk:format"]').attr('content');
};

function getNavigationPageType () {
return $('meta[name="govuk:navigation-page-type"]').attr('content');
};

function isNavigationGridPage () {
return getRenderingApplication() == 'collections' &&
getFormat() == 'taxon' &&
getNavigationPageType() == 'grid';
};

function isNavigationAccordionPage () {
return getRenderingApplication() == 'collections' &&
getFormat() == 'taxon' &&
getNavigationPageType() == 'accordion';
};

function isNavigationLeafPage () {
return getRenderingApplication() == 'collections' &&
getFormat() == 'taxon' &&
getNavigationPageType() == 'leaf';
};

function isMainstreamBrowsePage () {
return getRenderingApplication() == 'collections' &&
getFormat() == 'mainstream_browse_page'
};

function isTopicPage () {
return getRenderingApplication() == 'collections' &&
getFormat() == 'topic'
};

function isPolicyAreaPage () {
return getRenderingApplication() == 'whitehall' &&
getFormat() == 'placeholder_policy_area'
};

function isDocumentCollectionPage () {
return getRenderingApplication() == 'government-frontend' &&
getFormat() == 'document_collection'
};

function isFinderPage () {
return getRenderingApplication() == 'finder-frontend' &&
getFormat() == 'finder'
};

function isWhitehallFinderPage () {
return getRenderingApplication() == 'whitehall' &&
getFormat() == 'finder'
};

GOVUK.PageContent = PageContent;
})();
Loading

0 comments on commit cb9e1c4

Please sign in to comment.