A/B testing is a great tool for having your users test your site. It is used by some of the largest sites on the internet to optimize their pages to get people to do exactly what they want. Usually what they want is to drive higher sales or improve click-through rates. The key is in showing users two versions of a page on your site and then monitoring the outcome of that page view. With enough traffic you should be able to derive which page is doing better. It seems somewhat intimidating to run two versions of your website and monitor what people are doing, but it isnâ€™t. First it isnâ€™t your entire site which is participating in a single giant A/B test, rather it is a small portion of it â€“ really the smallest portion possible. In ASP.net MVC terms it is just a single view.
This blog is the first in a series about how to implement A/B testing easily in ASP.net MVC by using Redis
- Having 2 pages
- Choosing your groups
- Storing user clicks
- Seeing your data
The first thing we need is a website on which to try this out. Letâ€™s just use the default ASP.net MVC website. Next we will need two versions of a page. Iâ€™m going to pick the about page which looks like
Next we need to decide what weâ€™re trying to get people to do on the about page. I think Iâ€™m most interested in getting them to go look at the source code for the website. After all we like open source and maybe if we get enough people on our github site theyâ€™ll start to contribute back and grow the community. So letâ€™s build an A version of the page.
The first contains the markup from the first page and the second the markup for the second page. Â Now we need a way to select which one of these pages we show. Iâ€™d like to make this as transparent as possible for developers on the team. The controller code for the view currently looks like
This is, in fact, the stock page code for ASP.net MVC 5. Iâ€™m going to have this page, instead, extend a new base class called BaseController which, in turn, extends Controller. Pretty much every one of my ASP.net MVC applications follows this pattern of having my controllers extend a project specific base class. Iâ€™m generally not a huge fan of inheritance but in this case it is the easiest solution. Altering the base class from Controller to BaseController is all that is needed to activate A/B testing at the controller level.
In the base class I have overridden one of the View() signatures.
In the override I intercept the view name and, if the original view name doesnâ€™t exist I test for A and B versions of the view.
Really the only trick here is to note how we find the viewName if it doesnâ€™t exist: we take it from the action name on the route. Checking the source for ASP.net MVC(helpfully open sourced, thanks Microsoft!) we find that this is exactly approach they take so behaviour should not change. Youâ€™ll also notice that we call out to an ABSelector to chose which view to show. Iâ€™ll cover some approaches to building the ABSelector in part II of this series. For now the code is available on github atÂ https://github.com/stimms/RedisAB.
I hope to have part II out in the next week.