diff --git a/app/assets/javascripts/analytics/custom-dimensions.js b/app/assets/javascripts/analytics/custom-dimensions.js index d1c16c7da..866bcc5de 100644 --- a/app/assets/javascripts/analytics/custom-dimensions.js +++ b/app/assets/javascripts/analytics/custom-dimensions.js @@ -82,15 +82,14 @@ } }); - customDimensions['dimension23'] = $('main[id="content"]').attr('lang') || 'unknown'; - return customDimensions; } function customDimensionsFromDom() { return { dimension26: GOVUK.PageContent.getNumberOfSections(), - dimension27: GOVUK.PageContent.getNumberOfLinks() + dimension27: GOVUK.PageContent.getNumberOfLinks(), + dimension23: $('main[id="content"]').attr('lang') || 'unknown' }; } diff --git a/spec/javascripts/analytics/static-analytics-spec.js b/spec/javascripts/analytics/static-analytics-spec.js index 49e4fdbcd..ac5bb3739 100644 --- a/spec/javascripts/analytics/static-analytics-spec.js +++ b/spec/javascripts/analytics/static-analytics-spec.js @@ -46,33 +46,6 @@ describe("GOVUK.StaticAnalytics", function() { expect(GOVUK.analyticsPlugins.error).toHaveBeenCalled(); }); - describe('when ecommerce results are present', function() { - beforeEach(function() { - window.ga.calls.reset(); - }); - - afterEach(function() { - $('.test-fixture').remove(); - }); - - it('sends the ecommerce fields', function() { - $('body').append('\ -
\ -
\ -
\ -
\ -
\ - ') - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(window.ga).toHaveBeenCalledWith('send', 'pageview', pageViewObject); - }); - }); - describe('when there are govuk: meta tags', function() { beforeEach(function() { window.ga.calls.reset(); @@ -82,50 +55,6 @@ describe("GOVUK.StaticAnalytics", function() { $('head').find('meta[name^="govuk:"]').remove(); }); - it('sets them as dimensions', function() { - $('body').append('\ -
\ -
\ -
\ - '); - $('head').append('\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - '); - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - - expect(pageViewObject.dimension1).toEqual('section'); - expect(pageViewObject.dimension2).toEqual('format'); - expect(pageViewObject.dimension5).toEqual('1000'); - expect(pageViewObject.dimension6).toEqual('2005-to-2010-labour-government'); - expect(pageViewObject.dimension7).toEqual('historic'); - expect(pageViewObject.dimension9).toEqual(''); - expect(pageViewObject.dimension10).toEqual(''); - expect(pageViewObject.dimension12).toEqual('withdrawn'); - expect(pageViewObject.dimension17).toEqual('schema-name'); - expect(pageViewObject.dimension23).toEqual('fr'); - expect(pageViewObject.dimension30).toEqual('education') - }); - - it('ignores meta tags not set', function() { - $('head').append(''); - - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - - expect(Object.keys(pageViewObject).length).toEqual(1 + numberOfDimensionsWithDefaultValues); - expect(pageViewObject.dimension1).toEqual('section'); - }); - it('sets A/B meta tags as dimensions', function() { $('head').append('\ \ @@ -139,17 +68,26 @@ describe("GOVUK.StaticAnalytics", function() { expect(pageViewObject.dimension48).toEqual('name-of-other-test:name-of-other-ab-bucket'); }); - it('ignores A/B meta tags with invalid dimensions', function () { $('head').append('\ \ - \ + \ '); analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); pageViewObject = getPageViewObject(); - expect(Object.keys(pageViewObject).length).toEqual(numberOfDimensionsWithDefaultValues); + // 1. check the dimensions haven't been created by using the "value" of data-analytics-dimension + expect(pageViewObject.hasOwnProperty('dimensionnot a number')).toEqual(false); + expect(pageViewObject.hasOwnProperty('dimensionundefined')).toEqual(false); + + // 2. check the values haven't been used at all + var values = []; + for (key in pageViewObject) { + values.push(pageViewObject[key]); + } + expect(values).not.toContain('name-of-test:some-bucket'); + expect(values).not.toContain('name-of-test:some-other-bucket'); }); [ @@ -202,12 +140,22 @@ describe("GOVUK.StaticAnalytics", function() { name: 'navigation-legacy', number: 30, defaultValue: 'none' + }, + { + name: 'withdrawn', + number: 12, + defaultValue: 'not withdrawn' + }, + { + name: 'content-has-history', + number: 39, + defaultValue: 'false' } ].forEach(function (dimension) { it('sets the ' + dimension.name + ' dimension from a meta tag if present', function () { $('head').append('\ - \ - '); + \ + '); analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); pageViewObject = getPageViewObject(); @@ -223,270 +171,327 @@ describe("GOVUK.StaticAnalytics", function() { }); }); - describe('when tracking the number of sections and links on a page', function() { - describe('on a page with a normal sidebar', function() { - beforeEach(function() { - $('head').append('\ -
\ - \ - \ -
\ - ') - $('body').append('\ -
\ -
\ + \ +
\ + '); }); - describe('on a page with a taxon sidebar', function() { - beforeEach(function() { - $('head').append('\ -
\ - \ - \ -
\ - ') - $('body').append('\ -
\ -
\ - \ \ - \ + \ \ - '); - }); - - afterEach(function() { - $('.test-meta-tags').remove(); - $('.test-fixture').remove(); - }); - - it('tracks the number of sidebar sections', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension26).toEqual('2'); - }); - - it('tracks the total number of related links, including headers', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension27).toEqual('5'); - }); + \ + '); }); - describe('on a page with an accordion', function() { - beforeEach(function() { - $('head').append('\ -
\ - \ - \ - \ -
\ - ') - $('body').append('\ -
\ -
\ -
\ -
\ -
\ -

Section 1

\ -
\ -
\ -
    \ -
  1. \ - \ - Link 1.1\ - \ -
  2. \ -
  3. \ - \ - Link 1.2\ - \ -
  4. \ -
\ -
\ -
\ -
\ -
\ -

Section 2

\ -
\ -
\ -
    \ -
  1. \ - \ - Link 2.1\ - \ -
  2. \ -
\ -
\ + afterEach(function() { + $('.test-meta-tags').remove(); + $('.test-fixture').remove(); + }); + + it('tracks the number of accordion sections', function() { + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension26).toEqual('2'); + }); + + it('tracks the total number of accordion section links', function() { + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension27).toEqual('3'); + }); + }); + + describe('on a page with a grid', function() { + beforeEach(function() { + $('head').append('\ +
\ + \ + \ + \ +
\ + ') + $('body').append('\ +
\ +
\ + \ +
\ +
\ +
\ +

Grid leaves

\ +
    \ +
  1. \ + \ + Leaf 1\ + \ +

  2. \ + \
  3. \ + \ + Leaf 2\ + \ +

  4. \ +
\
\
\
\ -
\ - '); - }); - - afterEach(function() { - $('.test-meta-tags').remove(); - $('.test-fixture').remove(); - }); - - it('tracks the number of accordion sections', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension26).toEqual('2'); - }); - - it('tracks the total number of accordion section links', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension27).toEqual('3'); - }); + \ +
\ + '); }); - describe('on a page with a grid', function() { - beforeEach(function() { - $('head').append('\ -
\ - \ - \ - \ -
\ - ') - $('body').append('\ -
\ -
\ - \ -
\ -
\ -
\ -

Grid leaves

\ -
    \ -
  1. \ - \ - Leaf 1\ - \ -

  2. \ - \
  3. \ - \ - Leaf 2\ - \ -

  4. \ -
\ -
\ -
\ -
\ -
\ -
\ - '); - }); - - afterEach(function() { - $('.test-meta-tags').remove(); - $('.test-fixture').remove(); - }); - - it('tracks the number of sections', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension26).toEqual('2'); - }); - - it('tracks the total number of grid links and leaf links', function() { - analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); - pageViewObject = getPageViewObject(); - expect(pageViewObject.dimension27).toEqual('5'); - }); + afterEach(function() { + $('.test-meta-tags').remove(); + $('.test-fixture').remove(); + }); + + it('tracks the number of sections', function() { + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension26).toEqual('2'); + }); + + it('tracks the total number of grid links and leaf links', function() { + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension27).toEqual('5'); }); }); @@ -916,6 +921,30 @@ describe("GOVUK.StaticAnalytics", function() { expect(pageViewObject.dimension27).toEqual('3'); }); }); + + it('sets the page language from the main element as a custom dimension', function () { + $('.test-fixture').remove(); + $('body').append('\ +
\ +
\ +
\ + '); + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension23).toEqual('fr'); + }); + + it('sets the page language as "unknown" if the main element has no lang attribute as a custom dimension', function () { + $('.test-fixture').remove(); + $('body').append('\ +
\ +
\ +
\ + '); + analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'}); + pageViewObject = getPageViewObject(); + expect(pageViewObject.dimension23).toEqual('unknown'); + }); }); }); @@ -1038,13 +1067,34 @@ describe("GOVUK.StaticAnalytics", function() { // needs to be called manually. We do this below and reload the arguments sent // to window.ga in order to obtain the trackPageView args as well. + // 1. get all arguments to all calls to the ga() function universalSetupArguments = window.ga.calls.allArgs() + // 2. get the ga(function(tracker) { ...}) call - it's the 4th one: + // 1st is the call to create "universal-id" + // 2nd is the call to set "anonymizeIp" + // 3rd is the call to set "displayFeaturesTask" + // 4th is the call that sets the tracker function callback bound = universalSetupArguments[3][0]; + + // 3. trigger the callback with a canned tracker object that has a stubbed + // get method that always retuns the same client id. This is the only + // method we expect to call on the tracker object - calling anything + // else should cause these specs to fail. bound({get: function () { return '12345.67890' }}); + // 4. get all the calls to ga() again as executing the callback will add a + // 5th call. This time it's the one to to send "pageview" which is the + // initial page view tracking we're interested in. universalSetupArguments = window.ga.calls.allArgs(); lastArgumentSet = universalSetupArguments.pop(); + + // 5. make sure this final set of arguments is the one we're looking for + // and fail the test otherwise. + expect(lastArgumentSet[0]).toEqual('send'); + expect(lastArgumentSet[1]).toEqual('pageview'); + + // 6. extract the arguments to that last call and return them pageViewObject = lastArgumentSet[2]; return pageViewObject;