Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor how we calculate section and link counts #1103

Merged
merged 1 commit into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for this specific order that might not be obvious to the next person to touch this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Before, the order was important because of document collections. They are both navigation pages and content pages, so the return statement needed a specific order. But with this switch statement, we look for navigation pages first (and they are findable in a deterministic way now since alphagov/whitehall#3358), and default to a content page at the bottom. That means we don't rely on ordering anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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