-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmultivariate-test.js
134 lines (117 loc) · 3.85 KB
/
multivariate-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(function() {
"use strict";
window.GOVUK = window.GOVUK || {};
var $ = window.$;
// A multivariate test framework
//
// Based loosely on https://github.com/jamesyu/cohorts
//
// Full documentation is in README.md.
//
function MultivariateTest(options) {
this.$el = $(options.el);
this._loadOption(options, 'name');
this._loadOption(options, 'customDimensionIndex', null);
this._loadOption(options, 'cohorts');
this._loadOption(options, 'runImmediately', true);
this._loadOption(options, 'defaultWeight', 1);
this._loadOption(options, 'contentExperimentId', null);
if (this.runImmediately) {
this.run();
}
}
MultivariateTest.prototype._loadOption = function(options, key, defaultValue) {
if (options[key] !== undefined) {
this[key] = options[key];
}
if (this[key] === undefined) {
if (defaultValue === undefined) {
throw new Error(key+" option is required for a multivariate test");
}
else {
this[key] = defaultValue;
}
}
};
MultivariateTest.prototype.run = function() {
var cohort = this.getCohort();
if (cohort) {
this.setUpContentExperiment(cohort);
this.setCustomVar(cohort);
this.executeCohort(cohort);
this.createDummyEvent(cohort);
}
};
MultivariateTest.prototype.executeCohort = function(cohort) {
var cohortObj = this.cohorts[cohort];
if (cohortObj.callback) {
if (typeof cohortObj.callback === "string") {
this[cohortObj.callback]();
}
else {
cohortObj.callback();
}
}
if (cohortObj.html) {
this.$el.html(cohortObj.html);
this.$el.show();
}
};
// Get the current cohort or assign one if it has not been already
MultivariateTest.prototype.getCohort = function() {
var cohort = GOVUK.cookie(this.cookieName());
if (!cohort || !this.cohorts[cohort]) {
cohort = this.chooseRandomCohort();
GOVUK.cookie(this.cookieName(), cohort, {days: 30});
}
return cohort;
};
MultivariateTest.prototype.setCustomVar = function(cohort) {
if (this.customDimensionIndex) {
GOVUK.analytics.setDimension(
this.customDimensionIndex,
this.cookieName() + "__" + cohort
);
}
};
MultivariateTest.prototype.setUpContentExperiment = function(cohort) {
var contentExperimentId = this.contentExperimentId;
var cohortVariantId = this.cohorts[cohort]['variantId'];
if(typeof contentExperimentId !== 'undefined' &&
typeof cohortVariantId !== 'undefined' &&
typeof window.ga === "function"){
window.ga('set', 'expId', contentExperimentId);
window.ga('set', 'expVar', cohortVariantId);
};
};
MultivariateTest.prototype.createDummyEvent = function(cohort) {
// Fire off a dummy event to set the custom var and the content experiment on the page.
// Ideally we'd be able to call setCustomVar before trackPageview,
// but would need reordering the existing GA code.
GOVUK.analytics.trackEvent(this.cookieName(), 'run', {nonInteraction:true});
};
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.weightedCohortNames();
return names[Math.floor(Math.random() * names.length)];
};
MultivariateTest.prototype.cookieName = function() {
return "multivariatetest_cohort_" + this.name;
};
window.GOVUK.MultivariateTest = MultivariateTest;
}());