Click here to Skip to main content
Click here to Skip to main content

Implement Split A/B pages in your ASP NET project

, 20 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Quickly set up the ability to serve up several pages and record which was served in SQL Server

Introduction

Having built a website, I pointed a Google Ads campaign at it only to get no sales. I tend to learn things the difficult way. So, after getting some great advice about serving up a random selection of pages when the visitors come in, I set about putting together a crude solution for my own ends. Okay, it only took half a day, but somebody out there might take it and turn it into something a little more useful and re-share

Background

All I needed was that I could point my advertising campaign at a page on my website (say, www.whatever.com/sales/Default.aspx) and have the page issue a redirect to one of a selection of landing pages. I wanted the page which was served up to be logged in a database (SQL Server), and a session variable set so that future pages could be aware of how the visitor came in (ie, via which page). Also, for future use, I thought I'd create a field in the database that could store an email address enabling me to build a list later on

Using the code

To set up and use this code, we have to do several things, an assumption is made that you are using ASP NET with SQL Server as a database.

Set up the database

First thing to do is to create the database, I have called mine 'Marketing'. The scripts in the file SetupDB.txt accomplish this. There is one table called Events and another lookup table called EventTypes. Basically the Events table stores the events as people hit your web pages. I have created three event types - 1= to record the arrival of a visitor, 2 = to record at any subsequent page during the visitors time on your website, 3 = to take the customers email address if required, might be useful for list building

Unpack and check out the Tester

There is a simple tester app provided so you can make sure things are working before placing in your ASP Net project. Unpack and run the solution, the screen will look like this

Setting the DB connection

Apologies for the poor presentation, and entering bad values into fields will cause errors as I haven't invested any time in trapping errors - this app is simply for testing the dll in the project SplitTesting. Use the connection panel to connect to the database you created with the scripts, then click 'Left' to close the left panel. This will leave the main white text box in view, I have placed some test data in there to save time typing. Screen will now look like this

Main Screen of the testing app

The three lines of text will be parsed into 'Page' objects. A Page object simply holds the address of a page and the split (out of a hundred) that it will be served up. So, two pages should be 50/50, or 60/40 etc. You can see here that the three are 30, 30 and 40. testpage3, then, will be served up 40% of the time

At this point, it might be good to have SQL Management Studio open so that you can see the results of running the test. Click 'Run Once' in the tester, then run Select * from Events in SQL. You should see an event showing which of the three pages was randomly selected. Clicking 'Run Many' will simply do the same x number of times, where x is the number in the text box to the right of the button. You will see x records in the database after hitting this button

The other two buttons are 'interim' and 'email'. Interim simply records an event saying that a second page in the website was reached, you'll see how to implement this in the ASP example later - the idea being that the original landing page that the user came in on can be tied to a page later on in the session thanks to a session variable

Getting going in ASP NET

To get up and running in ASP NET we need to do the following

1. Add a connection string into your web config file

<connectionStrings>
  <add name="Marketing" connectionString="Data Source=MyDB;Initial Catalog=Marketing;Integrated Security=true;" providerName="System.Data.SqlClient" />
</connectionStrings> 

2. Add the dll SplitTester into the bin directory of your website. You need the dll called SplitTesting.dll found in the bin/debug directory under SplitTesting

3. Add a module in the App_Code section which will dish out an instance of 'Tester' to any web pages which need it. Doing it this way allows you to lazily call up the Tester object from anywhere within the page lifetime and know that you are sharing one instance

Imports Microsoft.VisualBasic
Imports SplitTesting
Imports System.Web.Configuration

Public Class SplitTesterContext
  Public Shared Function GetTester() As Tester
    If Not HttpContext.Current.Items.Contains("Tester") Then
      Dim conn As String = WebConfigurationManager.ConnectionStrings("Marketing").ConnectionString
      Dim ts As New Tester(conn)
      HttpContext.Current.Items.Add("Tester", ts)
    End If
    Return HttpContext.Current.Items("Tester")
  End Function
End Class

Usage in web pages

Okay, if you've got the three things in place above this section, we should be able to start serving up some random landing pages. Let's say that you default page in a given directory is where your customers will be arriving from an advertisement. This page will never be served up, we just put the following code into the load event

Imports SplitTesting

Partial Class Sales_Default Inherits System.Web.UI.Page

'in the load event, SplitTester will redirect to our landing page

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Session.Clear()
    If Not Page.IsPostBack Then
      Dim tester As Tester = SplitTesterContext.GetTester
      tester.Scheme = "JustAName"
      tester.RegisterPage("LandingPageOne.aspx", 50)
      tester.RegisterPage("LandingPageTwo.aspx", 50)
      Dim page As Page = tester.GetTarget
      Session("LandingPage") = page.Address
      Response.Redirect(page.Address)
    End If
  End Sub
End Class

In the load event, a 'tester' class is retrieved and the pages to which the customer could be directed are given to the tester class. When 'GetTarget' is called, one of the pages is returned at random and an event is written into the database to say which one was served up.

Assuming that your lander pages then usher your customers to your home page, you could have the following code in the load event of your home page to record their arrival there

    Dim tester As Tester = SplitTesterContext.GetTester
    tester.Scheme = "YourICE"
    tester.InterimPage(Session("LandingPage"), Request.RawUrl)

An event will be written showing which page the customer came from.

Points of Interest

Of course, this solution is simple in the extreme, but it solves a real world problem for me. It most certainly could be developed further, and if anybody does I would be pleased to see it in a more advanced state.

If anybody can't figure out quite how to develope it further but might need a tip or two on how, please drop a comment, I'm pretty proactive at replying

History

Original version uploaded 19th August 2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TheCardProject.co.uk
Software Developer (Senior) RichardGreenSoftware Ltd
United Kingdom United Kingdom
I am technical Director of TheCardProject.co.uk in Nottingham, a website giving information, software, services and expertise in the area of ID cards, smart cards.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 20 Aug 2014
Article Copyright 2014 by TheCardProject.co.uk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid