Skip to content

Commit

Permalink
Merge pull request #294 from ikennaokpala/accomodate-multiple-custom-…
Browse files Browse the repository at this point in the history
…dimensions

Allow use of multiple `customDimensionIndex` option
  • Loading branch information
dsingleton authored Jul 19, 2016
2 parents 058e05c + 444bbcb commit 23e342d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
18 changes: 18 additions & 0 deletions docs/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>

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
Expand Down
19 changes: 14 additions & 5 deletions javascripts/govuk/multivariate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
20 changes: 20 additions & 0 deletions spec/unit/multivariate-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 23e342d

Please sign in to comment.