|
Log Banner Impressions Asynchronously
Create an ASP.NET application using XML to track ad impressions on Web sites without losing performance.
by Jonathan Goodyear, MCSD, MCP, CLS
Posted August 9, 2002
If you sell banner ad space on your Web site, you know that your advertisers want to know how many people are clicking on their ads. The most common way to track this information is by logging "banner ad impressions" in a database, such as Microsoft SQL Server. The problem with this approach is that it requires extra calls to the database, which slows your page load times. The .NET Framework offers a better way to handle banner impression logging: Use a combination of the AdRotator control and asynchronous delegate calls to log banner impressions to a SQL Server table.
Start by creating a new SQL Server table called banner_impression_log (see Listing 1). This table consists of a unique identifier, the name of the advertiser, the URL of the banner image being tracked, and the date that each log entry was created. Note by the structure of the table that each banner impression will have its own separate record. Don't be tempted to use only one record for each banner and increment a counter field for each impression. This is a bad design for two reasons. First, if your Web site receives a lot of concurrent page views, you will encounter lock contention because several processes will try to update the same record at the same time. Second, by incrementing a counter in a single record for each banner, you lose flexibility for reporting banner impressions over specific periods of time.
With the SQL Server table now created, write a stored procedure named sp_add_impression so you can log a banner impression (see Listing 2). This stored procedure accepts an advertiser parameter and a banner_url parameter. The unique identifier and created_dt fields are automatically populated upon row insertion, according to the table definition.
Create a new C# ASP.NET Web application called BannerImpressionLogging. In order to isolate your banner impression logging code, create a user control and call it BannerFrame. Drag an AdRotator server control from the Toolbox onto the user control design surface. The AdRotator server control needs a data source, which takes the form of an XML-formatted advertisement file named adFile.xml (see Listing 3).
A standard advertisement file for an AdRotator server control consists of a series of <Ad> elements, which each have five main subelements. The ImageUrl element contains the URL to the banner image file. The NavigateURL element contains the URL to which the browser will be redirected when a user clicks on the banner. The AlternateText element contains the pop-up text that appears before the banner images loads, as well as when the user hovers the mouse over a banner. The Impressions element determines the frequency that a banner will be displayed compared to other banners in the advertisement file. The Keyword element allows you to partition the advertisement file into multiple buckets that can be filtered by using the KeywordFilter property of the AdRotator server control. You can also add custom elements to an advertisement file. The advertisement file for this demo contains one extra element, Advertiser, that contains the name of the company that has purchased the banner space. For now, set the AdvertisementFile property of the AdRotator to adFile.xml.
Open up the code-behind class for the BannerFrame user control. Add a reference to the System.Data.SqlClient namespace to the top of the file. Next, declare a private delegate named ImpressionLogger inside the BannerFrame class definition:
private delegate void ImpressionLogger(
string Advertiser,string ImageURL);
Back to top
|