The Ad Rotator Component

The Ad Rotator component displays advertisements on your Web page. You configure a data file called a Rotator Schedule file with information about the different ads you want to display. The Ad Rotator displays a graphic image with your ad and creates a hyperlink to a Web site associated with your ad. Each time the Web page is downloaded or refreshed, the Ad Rotator determines which ad in the data file should appear and then inserts the appropriate HTML. You do not have to change the ads yourself; simply update the Rotator Schedule file. The Ad Rotator also has a mechanism for recording the number of times a visitor jumps to a site that your ad points to.

You can implement an Active Server component in either an EXE or DLL; the Ad Rotator is implemented in the DLL named adrot.dll. If you are running IIS, the file is in the C:\WINNT\SYSTEM32\INETSRV folder.

In general, components that are implemented as DLLs perform better. When a component is implemented as an EXE, it must run as its own process. When the component and IIS pass data between them, the interprocess communication that must occur is relatively expensive. The same component written as a DLL is loaded as part of IIS's processes—there is no interprocess communication, so performance is better. All the components that come with Visual InterDev are implemented as DLLs.

The Rotator Schedule File

The Rotator Schedule file holds the information about the ads you display. The default filename is adrot.txt, and it is in the C:\InetPub\iissamples\sdk\asp\components folder if you are using IIS.

The file holds four pieces of information about each ad:

  • adURL The path to the image file
  • adHomePageURL The URL of the advertiser's home page
  • Text Text to be displayed if the browser does not support graphics
  • Impressions A priority for the ad

The priority for the ad is a number that can range from 0 through 4,294,967,295. This number determines what percentage of time the ad is shown compared to the other ads in the Rotator Schedule file. To figure the percentage, total all the priorities for all the ads in the Rotator Schedule file and compare the ad's priority to that total. For instance, if you have ads with priorities set at 100, 300, 400, and 200, they are shown 10, 30, 40, and 20 percent of the time, respectively.

The Rotator Schedule file also contains header information that you specify only once for the entire file:

  • Redirect The URL for a redirector page. This page counts the number of hits that each ad gets.
  • Width The width of the ad image, in pixels. The default is 440.
  • Height The height of the ad image, in pixels. The default is 60. You'll notice that 440 x 60 is a common size for ads on Web pages.
  • Border The thickness of the border around the image, in pixels. The default is 1. This parameter can be set to 0 for no border.

Below is a sample Rotator Schedule file layout that contains three ads. An asterisk marks the beginning of the actual ad settings; the four items in each setting contain no keywords.

 — ADROT.TXT — REDIRECT AdCounter.asp WIDTH 440 HEIGHT 60 BORDER 1 * images/ads/vi-bank/vi-bank.jpg  http://www.vi-bank.com/ Welcome to the premier Internet Bank! 10 images/ads/cau/logo.jpg _ Consultants Are Us 20  images/ads/cc/freecomponents.gif http://www.coolcomponents.com/ Free Server-Side Components 70 

This schedule file contains three ads. The header section specifies that AdCounter.asp is to be called each time a user clicks on one of the ads on this page. The height, width, and border settings are the same as the default settings, so you can omit these lines. Lines beginning with a hyphen in the header section are ignored; they simply describe what's happening in the header. An asterisk indicates that the ad information is beginning. You cannot put any comments in this section—this is why it's important to put all your descriptive text in the header. The position of the lines in this section determines their meaning, so you have to be careful. For instance, the first line is the path to the image, and the second is the URL for the advertiser's Web site. If the advertiser doesn't have a Web site, you must hold the position with a hyphen. The Consultants Are Us ad, for instance, doesn't include a Web site address for Consultants Are Us.

Using the Ad Rotator in an ASP Web Page

To use the Ad Rotator in an ASP Web page, simply drag the component onto your page from the Server Objects tab on the Toolbox. You can also use the following syntax:

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

In this case, the Server object's CreateObject method instantiates the AdRotator object. This means that it loads the DLL into memory and creates an instance of the object. The Set keyword assigns the object to a variable. Here, a variable named oAdRotator refers to the new AdRotator object you created. Table 17-2 describes the properties available in the AdRotator object.

Table 17-2. Properties in the AdRotator object.

Property Description
Border Sets the size of the border around the image. If you set this property in your VBScript code, it overrides the same property set in your Rotator Schedule file.
Clickable When this property is set to False, the user cannot jump to the advertiser's Web site by clicking on the image. This overrides the advertiser's URL setting that might or might not be set in the Rotator Schedule file.
TargetFrame When used on a Web page that has multiple frames, the property specifies the frame in which the ad will be displayed. The HTML frame tag has a name attribute so that you can name your frames. It's good practice to put your ads in a frame so that users can browse your site in one frame without disturbing the ad in another frame.

The GetAdvertisement method, shown below, does the work for the AdRotator object:

 Output = oAdRotator.GetAdvertisement("schedule file") 

When called, the GetAdvertisement method generates the HTML image tag appropriate for the ad, including hyperlink information. You pass a path to a Rotator Schedule file as an argument to this method. You typically pass the result of this method to the browser using the Response.Write method or using the leading equal sign syntax.

Pulling it all together

To use the AdRotator object, you need to create a Rotator Schedule file, a redirector ASP file, the Web page that will display the actual ad, and some images to display. The Components Web project in the Components/VIntDev98 folder on the companion CD-ROM contains all the source code for this exercise.

The code below assumes that the images can be found in a directory named /images/ads in your Web project. The Rotator Schedule file in the code below lists three ads, which are scheduled at 40, 20, and 40 percent, respectively. It also specifies an ASP Web page named AdCounter.asp to act as your redirector page. The rotator schedule file, adrot.txt, is located in the root directory of the Components Web project.

 — — Advertisement Schedule — — Contains three ads to be shown 40%, 20%, and 40% — of the time, respectively — REDIRECT AdCounter.asp WIDTH 88 HEIGHT 31 BORDER 1 * images/ads/vi-bank.jpg http://www.vi-bank.com VI-Bank 40 images/ads/nts_iis.gif http://www.microsoft.com/iis Microsoft Internet Information Server 20 images/ads/ie.gif _ Microsoft Internet Explorer 40 

The third ad, for Internet Explorer, doesn't have a URL to go to. The hyphen holds the place of the URL.

The sample redirector page, AdCounter.asp, does a couple of things. First, it records when users visit one of your advertisers' Web pages. Second (and most obvious), it helps send users to the advertiser's Web site. The AdRotator object does not do this automatically; when a user clicks on the image of the ad, the AdRotator object calls your redirector page and the advertiser's URL is sent in the query string. Therefore, in your redirector page, you have to check the Request.QueryString object and see which URL to redirect to.

 <%@ Language=VBScript %> <%     TargetURL = Request.QueryString("URL")     HitCount = Session("ADCNT" & TargetURL)     Session("ADCNT" & TargetURL) = HitCount + 1     Response.Redirect TargetURL %> 

The redirector page has Active Server Script code that saves the URL that you need to go to. The URL is passed to this page in a variable named URL in the QueryString object. A counter is then used to determine the number of times this URL has been jumped to during this session. Look for a variable in the Session object named ADCNT with the URL appended. The Session variable name might look like this:

 ADCNThttp://www.vi-bank.com 

If the URL has not been jumped to during the current session, referring to this variable creates a new session variable and initializes its value to 0. The code then saves the previous number of hits plus one in the Session object. This updates the number of times the URL has been jumped to during this session. Moreover, the Redirect method of the Response object is used to jump to the target URL.

This process saves the number of hits to the URL in the Session object. At the end of the session, you must save this information more permanently. The best place to store this information is in a database.

Putting the ad on a Web page

Now that the images are in place, a redirector ASP file is built, and a schedule file is ready to go, all you need to do is put the ad on a page. You create an AdRotator object and then call its GetAdvertisement method. You can do both of these tasks on the same page. However, you probably want to use the same set of ads in more than one page in your site. So it's better programming practice not to create an AdRotator object on each page but rather to create one AdRotator object for the session and use it on each page necessary.

To create the AdRotator object once for the session, create it in the Session_OnStart event procedure in the global.asa file as shown below:

 <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Session_OnStart     ' Create an AdRotator object to be     ' used in this session     Set Session("oAdRotator") = _         Server.CreateObject("MSWC.AdRotator") End Sub ' Session_OnEnd ' Application_OnStart ' Application_OnEnd   </SCRIPT> 

Notice that since the code needs to reside within a <SCRIPT> block, we cannot use the <OBJECT> block to define the Ad Rotator component. We therefore have to use the Server.CreateObject syntax.

Use the Set keyword to create an AdRotator object named oAdRotator. The Server.CreateObject("MSWC.AdRotator") command creates the object for you. The value MSWC.AdRotator is the programmatic ID of the object you want to create. If you are not using the Server Objects tab of the Toolbox, you must know the programmatic ID of any component that you need to use from your ASP Web page.

After the object is created, you can use it from any page in your application without having to create the object each time. The code below, AdRotator.asp, shows how to actually put the ad on a page:

 <@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <H2>Ad Rotator Sample</H2> <HR> <% Output = Session("oAdRotator").GetAdvertisement("adrot.txt") Response.Write Output %> <P> <A HREF="AdRotator.asp">Reload Page</A> <P> <A HREF="AdCounterDisplay.asp">Check number of image clicks</A> </BODY> </HTML> 

You need only one line—the call to the GetAdvertisement method of the AdRotator object. You pass the path to the schedule file you created earlier. The object itself stays in the Session object—you created it in the Session_OnStart procedure in the global.asa file when the session started.

When the Web browser hits this page, it sees the source code shown below:

 <@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <H2>Ad Rotator Sample</H2> <HR> <A HREF="AdCounter.asp?url=http://www.vi-bank.com     &image=images/ads/vi-bank.jpg">     <IMG SRC="images/ads/vi-bank.jpg"      ALT="VI-Bank" WIDTH=88 HEIGHT=31 BORDER=1></A> <P> <A HREF="AdRotator.asp">Reload Page</A> <P> <A HREF="AdCounterDisplay.asp">Check number of image clicks</A> </BODY> </HTML> 

The AdRotator object places a reference (using the HTML <A> tag) to your redirector page and passes it two variables in the query string. The first is the URL for the ad, and the second is the URL for the image file. It also inserts the image file with the appropriate size and border. When users click on the image, the query string is sent to your redirector. In the example on the CD-ROM, you can access the AdCounterDisplay.asp page to query the current values of the session values that track the number of hits to the various images.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net