Page

11.2.0- The Ad Rotator Component

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 11. Active Server Pages Components.

Not tagged.
<< 11.1.1- Creating an Instance of a Component Chapter11 11.3.0- The Content Linking Component >>

The Ad Rotator Component

To date, one of the most common sources of revenue on the World Wide Web has been the sale of advertising space. Although the Ad Rotator is not limited to Banner ads, in this book we will concentrate on this standard format of a .GIF file: 440 pixels wide by 60 pixels high. Since viewers will pay little attention to the same ad again and again, site designers want to rotate a collection of ads in the hope of giving each visit a fresh and inviting look. Furthermore, many sites charge advertisers by the number of hits or impressions (both mean the number of times an ad is shown to viewers) or the number of click-throughs that you track for invoicing. ASP includes a component, the Ad Rotator, which performs and manages the task of presenting ads in rotation as pages are requested.

 

The Ad Rotator component utilizes the following four files. The first two you make specifically to implement the Ad Rotator. The third is an existing page in your site, upon which you want to display ads. The fourth, the Targets, are provided by and hosted at the site of the advertiser.

 

File

Purpose

Type

File Name in the Try-It-Out

Scheduler

Hold information on the ads

DOS

ASCII text

AdRotatorSchedule.txt

Redirector

React to user clicking on an ad, including statistics and redirection to advertiser's site

.ASP

AdRotatorRedirector.asp

 

File

Purpose

Type

File Name in the Try-It-Out

Display Page

Web page that will display ads along with the rest of the information on the page

.ASP

AdRotatorHomePage.asp

Targets

Pages on advertisers' site where users are sent when an ad is clicked

Any URL

Wrox Press homepage
Wrox Conferences homepage
ActivePath homepage

 

When a visitor requests the display page, the ad rotator will calculate which ad to show next, gather the information for that ad from the scheduler.txt and pass it to the display page. The ad will appear on the display page, along with the rest of the text and graphics of the page. If the visitor clicks on the ad then the redirector page is opened, with the URL for the ad's target page passed along in the querystring. Usually the redirector page contains no viewable HTML; rather it has code that updates click-through statistics and then immediately redirects the user to the advertiser's site.

 

The click-through statistics can be sent to a datastore or held in application or session level variables. Our exercise will use application variables created in global.asa and we will create a small page called AdRotatorViewHits.asp to check on the number of times visitors have clicked on each ad. So, we will also work with the following two files.

 

File

Purpose

Type

File Name in the
Try-It-Out

global.asa

Holds application variables which hold count of clicks on ads

global.asa

global.asa

Hits Viewer

Used by webmaster (not visitors) to view number of click-throughs

.asp

AdRotatorViewHits.asp

 

Let's take a closer look at the structure of the scheduler file before we try out the ad rotator. The schedule keeps track of which ads should be displayed, how frequently to display them, the advertiser's URL where the user will be sent, and an alternate text to display. Create your scheduler file as a simple ASCII text file with two sections. The first section sets the general parameters for displaying all ads and ends with an asterisk. The second section sets specific parameters for each ad in the rotation.

 

In the first section, there are four general parameters that can be set to apply to all ads in the file. If you leave any of these parameters out, then ASP will provide the default shown in parenthesis.

 

  • The name of the file that is used to redirect the user when he/she clicks on the ads, as listed in row one of the table below (No default)
  • The width of the ad as it will appear on the page (460)
  • The height of the ad as it will appear on the page (65)
  • The width of the borders of the ad as it will appear on the page (1)

 

Remember that this section must end with a single line containing an asterisk, and nothing else on that line. In the following example schedule file, we will depart from the normal ad size to illustrate changes from the defaults.

 

Redirect AdRotatorRedirector.asp

Width 460

Height 65

Border 1

*

wroxconferences.gif

http://www.WroxConferences.com

WroxConferences - Held in America, Europe and Asia

20

activepath.gif

-

Activepath provides textbooks and teaching materials for software training

10

wroxpress.gif

http://www.wrox.com

Wrox Press, Programmer to Programmer

50

 

In the second section of the above file, we write four lines for each ad in the rotation. Since there is no marker dividing the entries, the parser counts off every four lines as a group. Therefore, use no more and no less then four lines per ad and do not revise the order of information. Note that we do not type double quotes around any of the parameters, in either of the two sections of the scheduler file. The four lines used for each ad represent the following:

 

  • The name of the file that holds the ad graphic (for example, WroxConferences.gif).
  • The URL of the hyperlink to be sent to the Redirector file if the user clicks on the ad. If no hyperlink is required – that is, the user's click causes no action – then we use a single hyphen (with no spaces) in place of an URL.
  • Text description of the ad graphic for text-only browsers.
  • The frequency with which the ad should be displayed.

 

Calculate the frequency of display for an ad by dividing its frequency parameter by the total of all the frequency parameters. So, in the sample below, the total of the frequencies is
20+10+50 = 80. The WroxConferences ad will run 20/80 or 25% of the time. (Remember that a number is not a percentage unless the total comes to 100.)

 Common Errors in the Scheduler file include:

  • Putting quotes around parameters in the scheduler file (as we mentioned earlier, this isn't necessary).
  • Leaving out the asterisk after the section containing the four general parameters.
  • More or less than four lines of information for each ad – you must have exactly four lines.
  • Wrong order of information for an ad – the information must follow the order described above: Filename, URL, Description, and Frequency.
Try It Out – The Ad Rotator Component

We'll create a page that gives a tip for programming students. Each time the page is viewed, it will also display one of three ads.

 

1.    Begin by downloading three sample ad files. In the real world, your advertisers would send these to you, but for this exercise, we will download WroxConferences.gif, ActivePath.gif and WroxPress.gif from the Wrox Press web site at http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764543636,descCd-download_code.html, and save them into your BegASPFiles directory.

At the same time, you can also download all of the files that we will be creating in this chapter from the website .

2.    Next, we set up the scheduler file. Either download AdRotatorSchedule.txt from the Wrox site or open up your editor of choice and type the following lines. Save this as AdRotatorSchedule.txt in your BegASPFiles directory.

Redirect AdRotatorRedirector.asp

Width 460

Height 65

Border 1

*

wroxconferences.gif

http://www.WroxConferences.com

WroxConferences - Held in America, Europe and Asia

20

activepath.gif

-

Activepath provides textbooks and teaching materials for software training

10

wroxpress.gif

http://www.wrox.com

Wrox Press, Programmer to Programmer

50

 

3.    Now, we need a place to hold the number of clicks on each ad. In this exercise, we will do that in an application variable. Add the following shaded lines to your global.asa. More sophisticated editors will create a global.asa for you and you will just have to add the shaded lines below. If you do not already have an .asa, then type all of the lines below into a simple text file and name it global.asa in your BegASPFiles directory.

Sub Application_OnStart

  Application("iWroxPress") = 0

  Application("iWroxConf") = 0

End Sub

</SCRIPT>

 

4.    Next, we create the file to handle a user's click on an ad. Open your editor, type the following code, and save as AdRotatorRedirector.asp in your BegASPFiles directory.

<%

  ' This page handles logging of clicks, then redirects

 

  strURL = Request.Querystring("url")

 

  Select Case lcase(strURL)

    Case "http://www.wrox.com"

  Application.Lock

  Application("iWroxPress") = Application("iWroxPress") + 1

  Application.Unlock

    Case "http://www.wroxconferences.com"

  Application.Lock

  Application("iWroxConf") = Application("iWroxConf") + 1

  Application.Unlock

  End Select

 

  Response.Redirect strURL

%>

 

5.    Lastly, we can create a page that will display ads using the Ad Rotator Component and the above files. Clear your editor, type the following code, and save as AdRotatorHomePage.asp, again in the BegASPFiles directory.

<HTML>

<HEAD>

<TITLE>Ad_Rotator_Home_Page</TITLE>

</HEAD>

<BODY>

 

<H3>Ad rotator home page</H3>

Programmer's tip:<BR>

When writing a loop always double check which commands <BR>

should be inside the loop and which should be outside the loop.<BR><BR>

<%

  Dim objAR

  Set objAR = Server.Createobject("MSWC.AdRotator")

  Response.Write (objAR.GetAdvertisement("AdRotatorSchedule.txt"))

%>

</BODY>

</HTML>

 

6.    That takes care of the pages for the visitor using the ad rotator. However, as webmasters, we would like to know how many clicks our ads have accumulated. We will now make a final page that displays the statistics. Clear your editor and type the following code and save this code as AdRotatorViewHits.asp, again in the BegASPFiles directory.

<HTML>

<HEAD>

<TITLE>Ad_Rotator_Hit_Viewer</TITLE>

</HEAD>

<BODY>

This page gives you feedback on number of click-throughs<BR>

 

<TABLE Border = 1>

  <TR>

    <TD>WroxPress</TD>

    <TD><%= Application("iWroxPress")%></TD>

  </TR>

  <TR>

    <TD>WroxConferences</TD>

    <TD><%= Application("iWroxConf")%></TD>

  </TR>

</TABLE>

 

</BODY>

</HTML>

 

7.    We have now finished entering our code. However, before testing, close your browser and then stop your application to reset the application variables back to zero. Open up the MMC and select the Default Web Site. Click on the Action Menu, and then select Stop to stop your web server. Wait a few seconds and click Start, then close the MMC.

8.    Test your Ad Rotator by using your browser to look at http://my_server_name/ AdRotatorHomePage.asp. Try viewing the page a number of times by pressing the Refresh/Reload button. You should get the text information (Programmer's tip) every time, but a different ad appearing. Clicking on the ad should take you to the appropriate advertiser.

 

Chapter11_image002

 

After several clicks on ads, use the browser to open AdRotatorViewHits.asp to see the click-through statistics.

 

Chapter11_image003

How It Works

We set up an ad rotation scheme featuring the ads of three companies in our AdRotatorSchedule.txt. First, we specify the name of the file to which is sent the advertiser's URL after a click. Then we slightly change the ad size and border width from the default. Then we end the first section with a line containing nothing more than an asterisk:

 

Redirect AdRotatorRedirector.asp

Width 460

Height 65

Border 2

*

 

Next, we code the details of the three ads, ensuring that this section contains exactly four rows for each ad. These rows contain the name of the ad image file, the hyperlink URL, a comment that the browser associates with the graphic, and the relative frequency with which the ad should be shown. Note that in this example, Active Path has asked us to show their ad but do not offer click-throughs, therefore their URL is replaced with a hyphen. The frequencies have been set based on selling eighths of the total volume of ad appearances. While Wrox Conferences bought two eighths (20 out of a total of 80), Active Path bought one eighth only:

 

wroxconferences.gif

http://www.wroxconferences.com

WroxConferences - Held in America, Europe and Asia

20

activepath.gif

-

Activepath provides textbooks and teaching materials for software training

10

wroxpress.gif

http://www.wrox.com

Wrox Press, Programmer to Programmer

50

 

In this exercise, we store the statistics on click-throughs in an application variable in the global.asa. Application and Session variables are covered in depth in Chapter 8 . For now, we just need to create a variable for each ad within the Application_Open event. In this case, we set them equal to zero since there will be no click-throughs when the application is started:

 

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

Sub Application_OnStart

  Application("iWroxPress") = 0

  Application("iWroxConf") = 0

End Sub

</SCRIPT>

 

The file to handle a user's click is, like Gaul, divided into three parts. Note that this page will perform some processing and then send the user on his way to one of the advertiser's sites. Therefore we do not need <HTML>, <HEADER> or any other tags for setting up a "real" page that the user would view. Firstly, we grab the data named "url" from the querystring and store it in a variable:

 

<%

  ' this page handles logging of clicks, then redirects

 

  strURL = Request.Querystring("url")

Then we figure out which URL we have got using a Select Case. Depending on the match, we update one or the other of the application variables. Just prior to changing the application variable we lock the application and just after the change we unlock it. Since there are no click-throughs from ActivePath (remember the hyphen in the Schedule.txt?) we do not need to include ActivePath in the Select Case:

 

  Select Case lcase(strURL)

    Case "http://www.wrox.com"

  Application.lock

  Application("iWroxPress") = Application("iWroxPress") + 1

  Application.unlock

    Case "http://www.wroxconferences.com"

  Application.lock

  Application("iWroxConf") = Application("iWroxConf") + 1

  Application.unlock

  End Select

 

Our last step is to send the visitor to the advertiser's site:

 

  Response.Redirect strURL

%>

</BODY>

</HTML>

 

The AdRotatorHomePage.asp starts with the usual headings, and then displays the Programmer's tip:

 

<HTML>

<HEAD>

<TITLE>Ad_Rotator_Home_Page</TITLE>

</HEAD>

<BODY>

 

<H3>Ad rotator home page</H3>

Programmer's tip:<BR>

When writing a loop always double check which commands <BR>

should be inside the loop and which should be outside the loop.<BR><BR>

 

The code that actually shows the ad is very concise. We Dim and Set an object of the AdRotator class. Then we run its GetAdvertisement method and put that result onto the page with a Response.Write:

 

<%

  Dim objAR

  Set objAR=Server.CreateObject("MSWC.AdRotator")

  Response.Write (objAR.Getadvertisement("AdRotatorSchedule.txt"))

%>

</BODY>

</HTML>

 

Viewing the statistics with the AdRotatorViewHits.asp again relies on the techniques of Application variables as covered in Chapter 8 . We build a simple table and write the current value of the application variables:

 

<HTML>

<HEAD>

<TITLE>Ad_Rotator_Hit_Viewer</TITLE>

</HEAD>

<BODY>

This page gives you feedback on number of click-throughs<BR>

<TABLE Border = 1>

<TR>

  <TD>WroxPress</TD>

  <TD><%= Application("iWroxPress")%></TD>

</TR>

<TR>

  <TD>WroxConferences</TD>

  <TD><%= Application("iWroxConf")%></TD>

</TR>

</TABLE>

</BODY>

Common Errors

If you had trouble with this example, here's a checklist of things that you might have missed:

 

  • Testing before all elements are in place; scheduler, ads and redirector
  • Redirector file does not do a Server.Execute or Server.Transfer to actually get the user to the advertiser's site
  • Files are scattered across different folders without paths
  • Mismatches between variable names in global.asa, AdRotatorRedirector.asp and AdRotatorViewHits.asp
  • Mismatch in file names between Schedule.txt and Redirector.asp

 

After reading the chapter on data access you may want to try making another AdRotatorRedirector.asp, this time keeping the click counts in a datastore rather then an application variable. At the simplest level you would set up a table named Ads with each record being an advertisement, and having fields called ClickCounter and AdURL. Then instead of the Select Case section you would use:

 

///// Store to database an increment

SQLtext = "UPDATE Ads"

SQLtext=SQLtext& " SET Ads.ClickCounter = Ads.ClickCounter+1

SQLtext=SQLtext& " WHERE Ads.AdURL='" & strURL & "');"

 

We'll be talking more about using datastores with ASP in Chapter 12 .

<< 11.1.1- Creating an Instance of a Component Chapter11 11.3.0- The Content Linking Component >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |