govuk_frontend_toolkit
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -18,2 +18,3 @@ (function() { | ||
this._loadOption(options, 'runImmediately', true); | ||
this._loadOption(options, 'defaultWeight', 1); | ||
@@ -88,8 +89,25 @@ if (this.runImmediately) { | ||
MultivariateTest.prototype.cohortNames = function() { | ||
return $.map(this.cohorts, function(v, i) { return i; }); | ||
MultivariateTest.prototype.weightedCohortNames = function() { | ||
var names = [], | ||
defaultWeight = this.defaultWeight; | ||
$.each(this.cohorts, function(key, cohortSettings) { | ||
var numberForCohort, i; | ||
if (typeof cohortSettings.weight === 'undefined'){ | ||
numberForCohort = defaultWeight; | ||
} else { | ||
numberForCohort = cohortSettings.weight; | ||
} | ||
for(i=0; i<numberForCohort; i++){ | ||
names.push(key); | ||
} | ||
}); | ||
return names; | ||
}; | ||
MultivariateTest.prototype.chooseRandomCohort = function() { | ||
var names = this.cohortNames(); | ||
var names = this.weightedCohortNames(); | ||
return names[Math.floor(Math.random() * names.length)]; | ||
@@ -96,0 +114,0 @@ }; |
@@ -706,3 +706,3 @@ # GOV.UK Frontend Toolkit | ||
pay_your_car_tax: {html: "Pay Your Car Tax"}, | ||
give_us_money: {html: "Give Us Money Or We Will Crush Your Car"}, | ||
give_us_money: {html: "Give Us Money Or We Will Crush Your Car"} | ||
} | ||
@@ -721,3 +721,3 @@ }); | ||
pay_your_car_tax: {callback: function() { ... }}, | ||
give_us_money: {callback: function() { ... }}, | ||
give_us_money: {callback: function() { ... }} | ||
} | ||
@@ -727,2 +727,16 @@ }); | ||
If you want one cohort to appear 25% of the time then you can optionally weight | ||
that cohort: | ||
```javascript | ||
var test = new GOVUK.MultivariateTest({ | ||
name: 'car_tax_button_text', | ||
customVarIndex: 555, | ||
cohorts: { | ||
pay_your_car_tax: {weight: 25, callback: function() { ... }}, // 25% | ||
give_us_money: {weight: 75, callback: function() { ... }} // 75% | ||
} | ||
}); | ||
``` | ||
If you have a complex test, it may be worth extending MultivariateTest with | ||
@@ -736,5 +750,7 @@ your own. Callbacks can be strings which will call a method of that name | ||
- `customVarIndex`: 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: Ashraf Chohan <ashraf.chohan@digital.cabinet-office.gov.uk> | ||
- `cohorts`: An object that maps cohort name to an object that defines the cohort. Name must be same format as test name. Object contains keys (both optional): | ||
- `defaultWeight`: Number of times each cohorts should appear in an array the random cohort is picked from, to be used in conjunction with weights on individual cohorts. | ||
- `cohorts`: An object that maps cohort name to an object that defines the cohort. Name must be same format as test name. Object contains keys (all optional): | ||
- `html`: HTML to fill element with when this cohort is picked. | ||
- `callback`: Function to call when this cohort is chosen. If it is a string, that method on the test object is called. | ||
- `weight`: Number of times this cohort should appear in an array the random cohort is picked from, defaults to the `defaultWeight` of the test. | ||
@@ -741,0 +757,0 @@ Full documentation on how to design multivariate tests, use the data in GA and construct hypothesis tests is on its way soon. |
@@ -145,11 +145,40 @@ describe("MultivariateTest", function() { | ||
describe("#cohortNames", function() { | ||
it("should return the names of the cohorts", function() { | ||
describe("#weightedCohortNames", function() { | ||
it("should return the weighted names of the cohorts when no weights are defined", function() { | ||
var test = new GOVUK.MultivariateTest({ | ||
name: 'stuff', | ||
customVarIndex: 1, | ||
cohorts: {foo: {}, bar: {}} | ||
cohorts: {foo: {}, bar: {}, baz: {}} | ||
}); | ||
expect(test.cohortNames()).toEqual(['foo', 'bar']); | ||
expect(test.weightedCohortNames()).toEqual(['foo', 'bar', 'baz']); | ||
}); | ||
it("should return the weighted names of the cohorts when weights are defined", function() { | ||
var test = new GOVUK.MultivariateTest({ | ||
name: 'stuff', | ||
customVarIndex: 1, | ||
cohorts: {foo: { weight: 2 }, bar: { weight: 1 }, baz: { weight: 3 }} | ||
}); | ||
expect(test.weightedCohortNames()).toEqual(['foo', 'foo', 'bar', 'baz', 'baz', 'baz']); | ||
}); | ||
it("should return the weighted names of the cohorts using default weighting", function() { | ||
var test = new GOVUK.MultivariateTest({ | ||
name: 'stuff', | ||
customVarIndex: 1, | ||
defaultWeight: 2, | ||
cohorts: {foo: {}, bar: {}, baz: {}} | ||
}); | ||
expect(test.weightedCohortNames()).toEqual(['foo', 'foo', 'bar', 'bar', 'baz', 'baz']); | ||
}); | ||
it("should return the weighted names of the cohorts using default weighting or defined weighting", function() { | ||
var test = new GOVUK.MultivariateTest({ | ||
name: 'stuff', | ||
customVarIndex: 1, | ||
defaultWeight: 2, | ||
cohorts: {foo: {}, bar: { weight: 1 }, baz: {}} | ||
}); | ||
expect(test.weightedCohortNames()).toEqual(['foo', 'foo', 'bar', 'baz', 'baz']); | ||
}); | ||
}); | ||
@@ -156,0 +185,0 @@ |
{ | ||
"name": "govuk_frontend_toolkit", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "npm for govuk_frontend_toolkit", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
448709
694