Create a gist now

Instantly share code, notes, and snippets.

@svdgraaf /abtest.vcl
Last active Dec 26, 2015

AB testing with Varnish (and Django)
# import the abtesting vmod
import abtest;
sub vcl_deliver {
# only continue, if the config file with all the rules can be loaded
if (abtest.load_config("/etc/varnish/abtest-rules.cfg") == 0) {
# if there isn't a named cookie 'ab' in the request, we can add one
if (req.http.Cookie !~ "ab=") {
# if there is a experiment defined in the rules files for this url
if(abtest.get_rand(req.url)) {
# this is where the magic happens, a cookie is set for this url (page), and
# for the lifetime of the cookie, the user will be in the given random testgroup
set resp.http.Set-Cookie = "ab=" + abtest.get_rand(req.url) + "; path=" + req.url + "; expires=" + abtest.get_expire(req.url);
}
}
}
}
if(location.pathname.indexOf("foo") != -1) {
if(readCookie('ab') == '1') {
// set button color to red
}
if(readCookie('ab') == '2') {
// set button color to blue
}
if(readCookie('ab') == '3') {
// set button color to green
}
}
if(location.pathname.indexOf("bar") != -1) {
if(readCookie('ab') == '1') {
// set button text to 'now'
}
if(readCookie('ab') == '2') {
// set button text to 'later'
}
if(readCookie('ab') == '3') {
// set button text to 'ponies!'
}
}
# location : groups : ttl ;
/bar/:A:33;B:33;C:33;300;
/foo/:A:20;B:20;C:20;D:20;E:20;300;
/unicorns/:A:66;B:34;300;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment