diff --git a/docs/javascript.md b/docs/javascript.md index 50c831d3..e651c399 100644 --- a/docs/javascript.md +++ b/docs/javascript.md @@ -264,6 +264,24 @@ var test = new GOVUK.MultivariateTest({ `customDimensionIndex` is the index of the custom variable in Google Analytics. GA only gives 50 integer slots to each account, and it is important that a unique integer is assigned to each test. Current contact for assigning a custom var slot for GOV.UK is: Tim Leighton-Boyce +For certain types of tests where the custom dimensions in Google Analytics can only have one of three scopes (hit, session, or user). Measuring performance of A/B tests, and want to compare test results for session-scoped custom dimensions (eg: index 222) against hit-scoped ones (eg: index 223) may become important. + +Instead of supplying an integer (like the above example), `customDimensionIndex` can also accept an array of dimension indexes ([222, 223]), see below for more. + +Make sure to check GA debugger that these values are being sent before deploying. + +```js +var test = new GOVUK.MultivariateTest({ + name: 'car_tax_button_text', + customDimensionIndex: [222, 223], + cohorts: { + pay_your_car_tax: {weight: 25}, + give_us_money: {weight: 50} + } +}); +``` + + ## Primary Links `GOVUK.PrimaryList` hides elements in a list which don't have a supplied diff --git a/javascripts/govuk/multivariate-test.js b/javascripts/govuk/multivariate-test.js index 57e18661..398688f2 100644 --- a/javascripts/govuk/multivariate-test.js +++ b/javascripts/govuk/multivariate-test.js @@ -76,14 +76,23 @@ }; MultivariateTest.prototype.setCustomVar = function(cohort) { - if (this.customDimensionIndex) { - GOVUK.analytics.setDimension( - this.customDimensionIndex, - this.cookieName() + "__" + cohort - ); + if (this.customDimensionIndex && + this.customDimensionIndex.constructor === Array) { + for (var index = 0; index < this.customDimensionIndex.length; index++) { + this.setDimension(cohort, this.customDimensionIndex[index]) + } + } else if (this.customDimensionIndex) { + this.setDimension(cohort, this.customDimensionIndex) } }; + MultivariateTest.prototype.setDimension = function(cohort, dimension) { + GOVUK.analytics.setDimension( + dimension, + this.cookieName() + "__" + cohort + ); + }; + MultivariateTest.prototype.setUpContentExperiment = function(cohort) { var contentExperimentId = this.contentExperimentId; var cohortVariantId = this.cohorts[cohort]['variantId']; diff --git a/spec/unit/multivariate-test.spec.js b/spec/unit/multivariate-test.spec.js index 4b024ddf..d75ea0e0 100644 --- a/spec/unit/multivariate-test.spec.js +++ b/spec/unit/multivariate-test.spec.js @@ -63,6 +63,26 @@ describe("MultivariateTest", function() { ); }); + it("should be able to set multiple custom vars with the name and cohort if one is defined as an array", function() { + GOVUK.cookie.and.returnValue('foo'); + var test = new GOVUK.MultivariateTest({ + name: 'stuff', + cohorts: { + foo: {}, + bar: {} + }, + customDimensionIndex: [2,3] + }); + expect(GOVUK.analytics.setDimension).toHaveBeenCalledWith( + 2, + 'multivariatetest_cohort_stuff__foo' + ); + expect(GOVUK.analytics.setDimension).toHaveBeenCalledWith( + 3, + 'multivariatetest_cohort_stuff__foo' + ); + }); + it("should trigger an event to track that the test has been run", function() { GOVUK.cookie.and.returnValue('foo'); var test = new GOVUK.MultivariateTest({