Skip to content

Commit 8d5d6bd

Browse files
committed
Added config options
1 parent 5952bfd commit 8d5d6bd

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Currently split.js uses google analytics as the datastore for experiments, this
66

77
## Usage
88

9+
Basic Usage:
10+
11+
Split.setup(alternatives, options)
12+
913
Split.js is useful for running different functions and measuring the results, you define the alternatives that you wish to test:
1014

1115
Split.setup({
@@ -20,6 +24,19 @@ Split.js is useful for running different functions and measuring the results, yo
2024
When a user hits the page they will be randomly assigned one alternative and that function will be executed.
2125
A custom variable will be set in google analytics with the name of that alternative which you can then pivot your analytics data around.
2226

27+
## Options
28+
29+
There are a number of configurable options that can be parsed as an optional second argument:
30+
31+
Split.setup({
32+
// functions
33+
},{
34+
cookieName: 'abTest',
35+
cookieAge: 30,
36+
customVariableName: 'AB Test alternative',
37+
customVariableIndex: 1
38+
});
39+
2340
## Caveats
2441

2542
* `Split.setup` must be loaded before Google analytics otherwise the custom variable will not be tracked,

example.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
document.getElementById('heading').innerHTML = 'Chunky Bacon';
1414
}, false);
1515
}
16+
},{
17+
cookieName: 'split_test1'
1618
});
1719
</script>
1820
</head>

split.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
// released under the MIT license
66

77
Split = (function(){
8+
9+
var config = {},
10+
defaults = {
11+
cookieName: 'abTest',
12+
cookieAge: 30,
13+
customVariableName: 'AB Test alternative',
14+
customVariableIndex: 1
15+
}
16+
817
function createCookie(name,value,days) {
918
if (days) {
1019
var date = new Date();
@@ -42,21 +51,32 @@ Split = (function(){
4251

4352
function init(){
4453
alternatives = arguments[0]
54+
options = arguments[1]
4555
keys = alternatives.keys()
4656

47-
var alternative = readCookie('abTest')
57+
config = {}
58+
59+
if(options != undefined){
60+
for(k in defaults){
61+
config[k] = (options[k] != undefined) ? options[k] : defaults[k];
62+
}
63+
}else{
64+
config = defaults;
65+
}
66+
67+
var alternative = readCookie(config.cookieName)
4868
if (alternative) {
4969
} else {
5070
alternative = keys[Math.floor(Math.random()*keys.length)]
51-
createCookie('abTest', alternative, 30)
71+
createCookie(config.cookieName, alternative, config.cookieAge)
5272
}
5373
var _gaq = _gaq || [];
54-
_gaq.push(['_setCustomVar', 1, 'AB Test alternative', alternative, 1]);
74+
_gaq.push(['_setCustomVar', config.customVariableIndex, config.customVariableName, alternative, 1]);
5575
alternatives[alternative]();
5676
}
5777
return {
58-
setup: function(args){
59-
init(args)
78+
setup: function(args, options){
79+
init(args, options)
6080
}
6181
}
6282
})();

0 commit comments

Comments
 (0)