• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Project 10. Creating a Rotating Ad Syste... > Writing the ADO.NET Code to Track Cl...

Writing the ADO.NET Code to Track Click-Throughs

With the database connection set up, you can proceed to the task at hand: writing the code that will track click-through traffic. The script will simply take a QueryString parameter indicating which URL to redirect the user to.

Create a new ASP.NET C# file, and save it in the root directory with the name of go.aspx.

Erase the default code that Dreamweaver inserts into the page and insert the top-level @ parameters:

Listing 10.15.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

These declare the language being used and import the data namespaces.

Begin the server script block and declare the Page_Load event using this code:

Listing 10.16.

<script runat="server">
protected void Page_Load(Object Src, EventArgs E)

Truthfully, you could use any one of the page’s events (Page_Init, Page_Load, Page_PreRender). There’s no real performance boost based on which one you pick, so it doesn’t matter.

Declare and load your variables with these lines:

Listing 10.17.

//Declare Variables
string connstr,referer,url,sql;
OleDbConnection connection;
OleDbCommand command;

connstr = ConfigurationSettings.AppSettings["MM_CONNECTION_STRING_advoodoo"];
referer = Request.ServerVariables["HTTP_REFERER"];
url = Request.QueryString["url"].ToString();
sql = "INSERT INTO ClickThroughs (referer,url)";
sql += " VALUES ('"+ referer +"','"+ url +"')";
connection = new OleDbConnection(connstr);


There are four variables being instantiated here: connstr, referer, url, and sql. Notice that you are inserting the referer and url into the database in the sql string. Basically, you’re logging where the visitor comes from and where they are going.

Add this code to connect to the database and execute your SQL insert statement:

Listing 10.18.

//Connect to the database
command = connection.CreateCommand();
command.CommandText = sql;

Redirect the user and close out the server script block with this code:

Listing 10.19.

     //Redirect the user

Point your banner ads (the <NavigateUrl> tag in the ads.xml file) to something like go.aspx?url=http://www.communitymx.com. This data can be used in a number of ways, not the least of which to show potential advertisers what kind of traffic they can expect to get by advertising on your site.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint