Skip to content

Commit

Permalink
Merge pull request #890 from alphagov/multiple-ab-tests
Browse files Browse the repository at this point in the history
Add block of Google Analytics dimensions for A/B tests
  • Loading branch information
tijmenb authored Feb 10, 2017
2 parents f87ba63 + dfce693 commit 1575113
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
24 changes: 19 additions & 5 deletions app/assets/javascripts/analytics/static-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
setPixelDensityDimension();
setHTTPStatusCodeDimension();
this.setDimensionsFromMetaTags();
this.setAbTestDimensionsFromMetaTags();
this.callMethodRequestedByPreviousPage();

// Track initial pageview
Expand Down Expand Up @@ -94,9 +95,26 @@
this.setOrganisationsDimension(dimensions['analytics:organisations']);
this.setWorldLocationsDimension(dimensions['analytics:world-locations']);
this.setRenderingApplicationDimension(dimensions['rendering-application']);
this.setAbTestDimension(dimensions['ab-test']);
};

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);
};
Expand Down Expand Up @@ -157,9 +175,5 @@
this.setDimension(21, position);
};

StaticAnalytics.prototype.setAbTestDimension = function(testNameAndBucket) {
this.setDimension(40, testNameAndBucket);
};

GOVUK.StaticAnalytics = StaticAnalytics;
})();
49 changes: 46 additions & 3 deletions spec/javascripts/analytics/static-analytics-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe("GOVUK.StaticAnalytics", function() {
<meta name="govuk:political-status" content="historic">\
<meta name="govuk:analytics:organisations" content="<D10>">\
<meta name="govuk:analytics:world-locations" content="<W1>">\
<meta name="govuk:ab-test" content="name-of-test:name-of-ab-bucket">\
');

analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});
Expand All @@ -75,7 +74,6 @@ describe("GOVUK.StaticAnalytics", function() {
expect(universalSetupArguments[8]).toEqual(['set', 'dimension7', 'historic']);
expect(universalSetupArguments[9]).toEqual(['set', 'dimension9', '<D10>']);
expect(universalSetupArguments[10]).toEqual(['set', 'dimension10', '<W1>']);
expect(universalSetupArguments[11]).toEqual(['set', 'dimension40', 'name-of-test:name-of-ab-bucket']);
});

it('ignores meta tags not set', function() {
Expand All @@ -86,8 +84,53 @@ describe("GOVUK.StaticAnalytics", function() {
universalSetupArguments = window.ga.calls.allArgs();

expect(universalSetupArguments[4]).toEqual(['set', 'dimension1', 'section']);
expect(universalSetupArguments[5]).toEqual(['send', 'pageview']);
assertClosingArgumentIsAtIndex(universalSetupArguments, 5);
});

it('sets A/B meta tags as dimensions', function() {
$('head').append('\
<meta name="govuk:ab-test" content="name-of-test:name-of-ab-bucket" data-analytics-dimension="42">\
<meta name="govuk:ab-test" content="name-of-other-test:name-of-other-ab-bucket" data-analytics-dimension="48">\
');

analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});
universalSetupArguments = window.ga.calls.allArgs();

expect(universalSetupArguments[4]).toEqual(['set', 'dimension42', 'name-of-test:name-of-ab-bucket']);
expect(universalSetupArguments[5]).toEqual(['set', 'dimension48', 'name-of-other-test:name-of-other-ab-bucket']);
});

it('ignores dimensions outside of the A/B test range', function () {
$('head').append('\
<meta name="govuk:ab-test" content="name-of-test-dimension-too-low:some-bucket" data-analytics-dimension="39">\
<meta name="govuk:ab-test" content="name-of-valid-test:some-bucket" data-analytics-dimension="40">\
<meta name="govuk:ab-test" content="name-of-other-valid-test:some-bucket" data-analytics-dimension="49">\
<meta name="govuk:ab-test" content="name-of-test-dimension-too-high:some-bucket" data-analytics-dimension="50">\
');

analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});
universalSetupArguments = window.ga.calls.allArgs();

expect(universalSetupArguments[4]).toEqual(['set', 'dimension40', 'name-of-valid-test:some-bucket']);
expect(universalSetupArguments[5]).toEqual(['set', 'dimension49', 'name-of-other-valid-test:some-bucket']);
assertClosingArgumentIsAtIndex(universalSetupArguments, 6);
});

it('ignores A/B meta tags with invalid dimensions', function () {
$('head').append('\
<meta name="govuk:ab-test" content="name-of-test:some-bucket">\
<meta name="govuk:ab-test" content="name-of-test:some-bucket" data-analytics-dimension="not a number">\
');

analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});
universalSetupArguments = window.ga.calls.allArgs();

assertClosingArgumentIsAtIndex(universalSetupArguments, 4);
});

function assertClosingArgumentIsAtIndex(setupArgs, argIndex) {
expect(setupArgs[argIndex]).toEqual(['send', 'pageview']);
}
});
});

Expand Down

0 comments on commit 1575113

Please sign in to comment.