diff --git a/app/assets/javascripts/analytics/static-analytics.js b/app/assets/javascripts/analytics/static-analytics.js
index f596ea832..61f26581a 100644
--- a/app/assets/javascripts/analytics/static-analytics.js
+++ b/app/assets/javascripts/analytics/static-analytics.js
@@ -13,6 +13,7 @@
setPixelDensityDimension();
setHTTPStatusCodeDimension();
this.setDimensionsFromMetaTags();
+ this.setAbTestDimensionsFromMetaTags();
this.callMethodRequestedByPreviousPage();
// Track initial pageview
@@ -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);
};
@@ -157,9 +175,5 @@
this.setDimension(21, position);
};
- StaticAnalytics.prototype.setAbTestDimension = function(testNameAndBucket) {
- this.setDimension(40, testNameAndBucket);
- };
-
GOVUK.StaticAnalytics = StaticAnalytics;
})();
diff --git a/spec/javascripts/analytics/static-analytics-spec.js b/spec/javascripts/analytics/static-analytics-spec.js
index 9510afc9e..3d3218b79 100644
--- a/spec/javascripts/analytics/static-analytics-spec.js
+++ b/spec/javascripts/analytics/static-analytics-spec.js
@@ -62,7 +62,6 @@ describe("GOVUK.StaticAnalytics", function() {
\
\
\
- \
');
analytics = new GOVUK.StaticAnalytics({universalId: 'universal-id'});
@@ -75,7 +74,6 @@ describe("GOVUK.StaticAnalytics", function() {
expect(universalSetupArguments[8]).toEqual(['set', 'dimension7', 'historic']);
expect(universalSetupArguments[9]).toEqual(['set', 'dimension9', '']);
expect(universalSetupArguments[10]).toEqual(['set', 'dimension10', '']);
- expect(universalSetupArguments[11]).toEqual(['set', 'dimension40', 'name-of-test:name-of-ab-bucket']);
});
it('ignores meta tags not set', function() {
@@ -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('\
+ \
+ \
+ ');
+
+ 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('\
+ \
+ \
+ \
+ \
+ ');
+
+ 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('\
+ \
+ \
+ ');
+
+ 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']);
+ }
});
});