Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable duration (in days) for AB Test cookie. #292

Merged
merged 1 commit into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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