The Commerce Starter Kit ( http://www.commercestarterkit.org/ ) provides a simple and effective solution to sell items on the Internet. The online store supports PayPal, ASP.NET 2.0, and the new Provider Model in the .NET 2.0 Framework.
Previously I created an InventoryProvider for the Commerce Starter Kit which you can read about in the following post:
Now I would like to talk about a ReferralProvider for the Commerce Starter Kit. You can read all my posts on the Commerce Starter Kit in the following category:
ReferralProvider for Tracking Online Store Referrals
One of the features not included in the original design is a simple ReferralProvider that tracks referrals to your online store. As an online retailer you often want to know how people are finding your online store. Is someone linking to your store or are they finding you via the search engines? By tracking your online referrals you are able to answer these questions as well as make better decisions on how to market your online store.

Create an HttpModule - ReferralModule
One of the ways you can track referrals to your website is by creating an HttpModule that subscribes to the Global BeginRequest Event. This event is fired upon each HTTP Request to your website. When you find a referral from another website, you can send the referring URL and the target URL to the ReferralManager which will delegate the responsibility of inserting it to the proper provider ( in this case SqlReferralProvider ).

/// <summary>
/// Summary description for ReferralModule
/// </summary>
public class ReferralModule : IHttpModule
{
public ReferralModule()
{
}
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest +=
new EventHandler(context_BeginRequest);
}
#endregion
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Context == null)
return;
HttpContext context = app.Context;
if (context.Request.UrlReferrer == null ||
string.IsNullOrEmpty(context.Request.
UrlReferrer.ToString()))
return;
if (context.Request.UrlReferrer.Host.
Equals(context.Request.Url.Host))
return;
ReferralManager.InsertReferral(context.Request.
UrlReferrer.ToString(), context.Request.Url.
ToString());
}
}
ReferralManager - Business Layer Manager Handling UI Requests
The HttpModule passes the request to the ReferralManager that does nothing but hand off the referral information to the ReferralProvider registered in the Web.Config file.
/// <summary>
/// Summary description for ReferralManager
/// </summary>
public class ReferralManager
{
/// <summary>
/// Gets all the referrals in the database
/// </summary>
/// <returns>IDataReader</returns>
public static IDataReader GetReferrals()
{
return Commerce.Providers.ReferralProvider.
Instance.GetReferrals();
}
/// <summary>
/// Insert a referral in the database
/// </summary>
/// <param name="referringUrl">From Url</param>
/// <param name="url">To Url</param>
public static void InsertReferral(string referringUrl,
string url)
{
Commerce.Providers.ReferralProvider.Instance.
InsertReferral(referringUrl, url);
}
/// <summary>
/// Delete all referrals in the database
/// </summary>
public static void DeleteAll()
{
Commerce.Providers.ReferralProvider.Instance.
DeleteAllReferrals();
}
/// <summary>
/// Delete all referrals in the database before a date.
/// </summary>
/// <param name="referralDate">Delete referrals
before this date.</param>
public static void DeleteBefore(DateTime referralDate)
{
Commerce.Providers.ReferralProvider.Instance.
DeleteReferralsBefore(referralDate);
}
}
SqlReferralProvider - Concrete ReferralProvider Tracking Online Store Referrals
The SqlReferralProvider stores and manages the referral information in a table, called CMRC_Referrals, via a number of stored procedures.


SqlReferralProvider provides a Count field that shows a rolling count of the number of referrals from a specific URL so that you can get some sort of idea as to the amount of traffic from a specific URL. This allows you to identify your most active referrers. This rolling count is handled in the Insert Stored Procedure to keep database calls to a minimum. Below is a snippet of SqlReferralProvider responsible for inserting the referral.
As mentioned before, the Commerce Starter Kit uses the Data Access Application Block ( DAAB ) 2.0 to help with data access layer functionality. Here I am calling the stored procedure, CMRC_REFERRALS_Insert, with the referringURL and target URL.
public override void InsertReferral
(string referringUrl, string url)
{
SqlParameter[] parameters =
new SqlParameter[2];
SqlParameter referringUrlParameter =
new SqlParameter("@ReferringUrl",
SqlDbType.VarChar, 255);
referringUrlParameter.Value = referringUrl;
parameters[0] = referringUrlParameter;
SqlParameter urlParameter =
new SqlParameter("@Url", SqlDbType.VarChar,
255);
urlParameter.Value = url;
parameters[1] = urlParameter;
SqlHelper.ExecuteNonQuery(_connectionString,
CommandType.StoredProcedure,
"CMRC_REFERRALS_Insert", parameters);
}
Conclusion
The ReferralProvider extension to the ASP.NET 2.0 and PayPal Commerce Starter Kit provides a simple and effective solution for tracking referrals to your online store. You can modify the existing code or create a new provider for your own online store. Extremely busy stores will want to provide a more scalable solution that queues each request for insertion at a later time.
You can download the source code here.
Written by David Hayden ( ASP.NET Developer, Blog )