Skip to content

Commit

Permalink
Merge pull request #292 from ikennaokpala/configurable-cookie-expiration
Browse files Browse the repository at this point in the history
Configurable duration (in days) for AB Test cookie.
  • Loading branch information
dsingleton authored Jul 14, 2016
2 parents 54b96ab + d313102 commit 058e05c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ var test = new GOVUK.MultivariateTest({
});
```

If you want to set the cookie expiration then you can optionally set cookieDuration as follows:

```javascript
var test = new GOVUK.MultivariateTest({
name: 'car_tax_button_text',
cookieDuration: 14,
cohorts: {
pay_your_car_tax: {weight: 25, callback: function() { ... }}, // 25%
give_us_money: {weight: 75, callback: function() { ... }} // 75%
}
});
```
Here, it is set to expire in 14 days time. if this option is not set the default cookie expiration (30 days) take effect.

If you have a complex test, it may be worth extending MultivariateTest with
your own. Callbacks can be strings which will call a method of that name
on the current object.
Expand Down
3 changes: 2 additions & 1 deletion javascripts/govuk/multivariate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
this._loadOption(options, 'runImmediately', true);
this._loadOption(options, 'defaultWeight', 1);
this._loadOption(options, 'contentExperimentId', null);
this._loadOption(options, 'cookieDuration', 30);

if (this.runImmediately) {
this.run();
Expand Down Expand Up @@ -69,7 +70,7 @@
var cohort = GOVUK.cookie(this.cookieName());
if (!cohort || !this.cohorts[cohort]) {
cohort = this.chooseRandomCohort();
GOVUK.cookie(this.cookieName(), cohort, {days: 30});
GOVUK.cookie(this.cookieName(), cohort, {days: this.cookieDuration});
}
return cohort;
};
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/multivariate-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ describe("MultivariateTest", function() {
expect(test.fooCallback).toHaveBeenCalled();
});

it("should assign 30 if cookieDuration isn't defined", function() {
GOVUK.cookie.and.returnValue('foo');
var test = new GOVUK.MultivariateTest({
name: 'cookie_duration_test',
cohorts: {
foo: {callback: function(){}}
}
});
expect(test.cookieDuration).toEqual(30);
});

it("should assign the user's cookie duration, when cookieDuration is defined", function() {
GOVUK.cookie.and.returnValue('foo');
var test = new GOVUK.MultivariateTest({
name: 'cookie_duration_test',
cookieDuration: 14,
cohorts: {
foo: {callback: function(){}}
}
});
expect(test.cookieDuration).toEqual(14);
});

it("should assign a new random cohort if the assigned cohort does not exist", function() {
var fooSpy = jasmine.createSpy('fooSpy');
var barSpy = jasmine.createSpy('barSpy');
Expand Down

0 comments on commit 058e05c

Please sign in to comment.