Referral or Affiliate Code tracking Module in ASP.NET

I recently had a need to track referral codes for ad campaigns as they came in through Google ad clicks. This same module can easily be used to handle affiliate codes as they come in. This module will be executed at each request, will check for a query variable, and store the value in a cookie. You can then retrieve the cookie where ever you need it in you app.

The HTTP Module

/// <summary>
/// Adds a cookie to the clients machine to track what ref code they came in through
/// </summary>
public class RefCodeExtraction : IHttpModule
{
    public void Dispose()
    {
    }
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }
    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = (sender as HttpApplication).Context;
       //replace "q" with your variable name
        if (!String.IsNullOrEmpty(context.Request.QueryString["q"]))
            SaveRefCodeToCookie(context.Request.QueryString["q"], context);
        else//ADD A DEFAULT CODE
            SaveRefCodeToCookie("ORGANIC", context);
    }
    private void SaveRefCodeToCookie(string refCode, HttpContext context)
    {
        try
        {
            context.Response.Cookies.Remove("RefCookieName");
            HttpCookie c = new HttpCookie("RefCookieName");
            c.Value = refCode.EndsWith("/") ? refCode.TrimEnd('/'):refCode;
            c.Expires = DateTime.Now.AddDays(30);
            context.Response.Cookies.Add(c);
        }
        catch {}
    }
}

Activate the Module

To activate this module, place the class in your App_Code directory and add the class name to the web.config file under the HttpModules section. For example:

        <httpModules>
            <add name="RefCodeExtraction" type="RefCodeExtraction"/>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>

It will now be executed at the beginning of each request.

Retrieve the Ref Code from the Cookie

To retrieve the code later on, just call:

string refCode = HttpContext.Current.Request.Cookies["RefCookieName"].Value;

Conclusion

How you use the referral code or ad code is up to you. In my case, the ad campaign code is attached to a google ad using the variable "q" followed by the campaign code. This is set in the ad management system and allows for our web site to track and persist which ad codes are generating conversions. This could be adapted to pay out affiliates for referrals or whatever else you might need.

The code is below.

RefCodeExtraction.cs (1.24 kb)

kick it on DotNetKicks.com


Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

October 7. 2008 06:42 AM

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008