CHAPTER 12
Ad Rotator Component
The Internet, though begun in an attempt to efficiently share information, is
quickly evolving into a powerful avenue for business. One result of this evolution
is the rapidly growing tendency for web sites to incorporate advertising into their
content pages.
Unfortunately, these ads must be changed often to maintain efficacy, since clients
quickly bore of advertising. The manner by which a webmaster changed the
advertisements on her site used to involve a time-consuming three-step process.
This process involved modifying the content, uploading the file to the server, and
changing the links, if necessary, every time an old ad was to be saved and a new
one displayed. Though several CGI applications became available to make this
process simpler, none of them were less clunky than the original method.
With the advent of the Ad Rotator component, the process by which ads are
displayed has become much simpler. This component allows content providers to
rapidly change ads without relying on webmasters to change links repeatedly or
maintain obtuse naming conventions of ad files for storage until the next time the
same ad is used.
The Ad Rotator component allows you to change the advertisements on your web
site in an automated fashion using a schedule file that you create. This schedule
file contains a list of advertisements, their details (URL, text, etc.), and a weight
factor that instructs the web server how often to display that particular ad. Each
time a page containing a call to the Ad Rotator component’s GetAdvertisement
method is called, the schedule file is referenced by the web server to determine
which ad to display. The ad itself is made up of a text description (for clients who
have graphics turned off), a URI of the graphic for the ad, if one is available, and
the percentage of time that the ad should be displayed relative to the other ads
listed in the schedule file.
The Ad Rotator component also allows you to easily maintain a record of the
number of times users have selected a given advertisement.
Accessory Files/Required DLL Files 237
Ad Rotator
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Accessory Files/Required DLL Files
Adrot.dll
The dynamic link library for the Ad Rotator component. It must be registered on
the web server before it can be instantiated in your web applications. To register
the Ad Rotator component on your web server, perform the following steps:
1. Click on the Start button on the taskbar.
2. Select Run from the Start menu.
3. Type in the following line (assuming your WinNT or Windows directory is on
your C drive):
Windows NT: C:\WinNT\System32\winnt32\inetserv\Regsvr32.exe Adrot.dll
Windows 95/98: C:\Windows\System\Regsvr32.exe Adrot.dll
Redirection File
The Ad Rotator redirection file is an optional accessory file that allows you to trap
clicks on an ad included on a page. It is an Active Server Page that you create to
act as a middle script between the script containing the ad and the ad’s URL. Each
time a user clicks on an ad, the ad’s URL is sent to this redirection file. Within this
redirection file, you could easily add the name of the ad and other details such as
the user’s IP address to the web server log or a database or record it some other
way.
However, the true power of this redirection file lies in your ability to add a script
to this file to save more useful information than simply the number of times the ad
was selected. To name just a few obvious examples, you could determine the
contents of previously created session variables to get more details on the user:
what scripts does he look at, what IP address is he coming from, and what software
is he using. Frequently overlooked, this redirection file gives you the
opportunity to track the details of your users and, thus, customize your site to its
users.
Ad Rotator Summary
Properties
Border
Clickable
TargetFrame
Collections
None
Methods
GetAdvertisement
Events
None
238 Chapter 12 – Ad Rotator Component
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
The following is an example of some code from a redirection file:
<%
' Dimension local variables
Dim strUserName
Dim strRemoteAddress
Dim strURL
Dim strBrowserType
strUserName = Session("UserName")
strRemoteAddress = Request.ServerVariables("REMOTE_ADDR")
strURL = Request.QueryString("url")
strBrowserType = Session("UserBrowser")
[YOU COULD WRITE THE INFORMATION TO A TEXT FILE OR DATABASE HERE]
Response.Redirect strURL
%>
Rotator Schedule File
The rotator schedule file is a custom text file that you create. You can call it
anything you wish. In it, you specify the details for the advertisements to be
displayed on your site. You can specify the sizes of the advertisements, the URLs
of images to be used for your ads, and the percentage of time each ad should be
selected and displayed when the Ad Rotator object’s GetAdvertisement method is
called.
There are two sections in the rotator schedule file. The two sections are separated
by a single line containing only an asterisk (*). The first section contains the
following information that applies to all the advertisements listed in the file:
• The redirection file to use when an ad is clicked. This file’s code will be executed
before the user’s browser is sent to the ad’s URL. As described earlier,
the redirection file allows for details of the user to be recorded before sending
his browser to the ad URL. One good reason to use a redirection file is so
that you can include a default URL that will take the user to a default page if
no ad URL is included in the rotator schedule file. For example, your site may
have a single HTML file that contains a brief description of all its advertisers.
You could use the URL of this page as a default URL in the redirection file.
• The size of the border line for each advertisement.
• The width of the advertisement in pixels.
• The height of the advertisement in pixels.
Each of these elements is optional. If you do not have any of them, the first line
will contain an asterisk, there will be no redirection script called, there will be no
border, and the advertisement graphics will be the size specified in their individual
graphics files.
The second section contains information specific to each ad. This section contains
the following information for each advertisement, with each item on its own line:
Accessory Files/Required DLL Files 239
Ad Rotator
Accessory Files/Required DLL Files
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
• The pathname and filename of the graphics image to use for the advertisement.
• The URL of the advertiser’s home page. This is designed to allow the user to
navigate to the advertiser’s home page by clicking on the ad. If the URL is not
present and the user clicks on the ad, an error results, unless you use a redirection
file that contains a default URL.
• The text for the advertisement.
• The relative weight of the advertisement. For example, suppose a schedule
file detailed four ads with weights of 3, 4, 1, and 2. Upon a call to the Ad
Rotator’s GetAdvertisement method, the web server would retrieve the first ad
30% of the time, the second ad 40% of the time, the third ad 10% of the time,
and the last ad 20% of the time.
All of these elements are optional. If you omit one, however, you must insert a
hyphen (-) on the line where you would put a value. See the following example:
[REDIRECT /Apps/MyRedirectScript.ASP]
[WIDTH 300]
[HEIGHT 50]
[BORDER 3]
*
http://www.ora.com/images/ora.gif
http://www.ora.com
Check out the excellent books at O'Reilly!
20
http://www.BikeCityAthens.com/Graphics/BikeOfTheWeek.gif
http://www.BikeCityAthens.com
-
60
http://www.WidgetWare.com/Images/TodaysWidget.gif
-
-
20
In this example, we can ascertain the following:
• The first section sets the redirection URL, the size to 50´300, and the border
to three pixels.
• There are three advertisements detailed in the file. These will be displayed
20%, 60%, and 20% of the time, respectively.
• The second ad has no text associated with it. If the client has graphics turned
off, she will see nothing.
• The third ad has no home URL. If the user clicks on this ad, an error will be
raised if the redirection file has no default URL.
• Finally, like the second ad, the third ad has no text associated with it and has
no home URL.
240 Chapter 12 – Ad Rotator Component
Instantiating the Ad Rotator
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Instantiating the Ad Rotator
To create an object variable containing an instance of the Ad Rotator, use the
Server.CreateObject method. The syntax for the CreateObject method is as follows:
Set objMyObject = Server.CreateObject(strProgId)
where:
• The objMyObject parameter represents the name of a variable that will contain
a reference to of the object you are instantiating.
• The strProgId parameter is the programmatic identifier (ProgId) of the Ad
Rotator:
MSWC.AdRotator
Example
<%
' The following code uses the Server object's
' CreateObject method to instantiate an Ad Rotator
' object on the server.
Dim objAdRotator
Set objAdRotator = Server.CreateObject("MSWC.AdRotator")
%>
For more details on the use of the CreateObject method, see its entry in Chapter 8,
Server Object.
Comments/Troubleshooting
The Ad Rotator component is very straightforward and can be a real time saver.
Aside from making sure your rotator schedule file is set up correctly, there’s little
to using the Ad Rotator.
If you don’t want the user to be able to click on the ad (for instance, if it is an
informational ad only, not meant to lead to an URL), set the Clickable property of
the component to False, rather than handling it with the URL or in the redirection
file. This property’s value is True by default.
The only problems I’ve experienced with the use of this component stemmed
from incorrect syntax in the schedule file or from the Ad Rotator DLL (adrot.dll)
not having been registered on the web server. The component is, however, automatically
registered when you install IIS, so you have to explicitly remove it for it
not to work.
Finally, it can be beneficial to instantiate an AdRotator object at the session level.
You also can create an ad object at the application level, but doing so gives you
less flexibility on a person-by-person basis.
Figure 12-1 illustrates how the Ad Rotator works.
Border 241
Ad Rotator
Border
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Properties Reference
Border
objAdRot.Border = intSize
Border sets the thickness (in pixels) of the line around your advertisement graphic.
Parameters
intSize
The thickness of the ad graphic’s border in absolute number of pixels
Example
<%
' The following code creates an Ad Rotator object and sets
' border thickness to two pixels. When displayed, the ad
' graphic will be surrounded by a two-pixel border, regardless
' of the setting for the border width in the schedule file.
Figure 12-1: The Ad Rotator component, rotator schedule file, and redirection file in
action
Sample.ASP RotatorSched.txt
Sample.ASP
Ad1
http://www.adspace.com
Ad2
http://www.adspace.com
Ad3
http://www.adspace.com
Redirect.asp
Ad 1
Ad information is retrieved
from RotatorSched.txt and
placed into Sample.ASP
through the use of an Ad
Rotator component
Client browser
requests page
containing Ad
Rotator
Ad 1
User browser directed
to redirection file where
request and user info
is recorded
Client clicks
on Ad1
Ad4
http://www.adspace.com
The client is redirected
to URL for Ad1 if it exsists
242 Chapter 12 – Ad Rotator Component
Clickable
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
Dim objAdRot
objAdRot = Server.CreateObject("MSWC.AdRotator")
objAdRot.Border = 2
%>
Notes
The default border thickness is whatever is set by the [BORDER] value in the Ad
Rotator schedule file. If you set a value for the Border property, it will override
that set in the schedule file.
In addition to the preceding example, see the full example at the end of this
chapter.
Clickable
objAdRot.Clickable = blnClickable
Sets or returns whether the ad graphic represents a clickable image that will redirect
the client to a URL for the ad’s home page.
Parameters
blnClickable
A Boolean value that determines whether the ad graphic, when clicked, will
transport the user to the homepage of the ad. The default value is True.
Example
<%
' The following code creates an Ad Rotator object and
' sets its Clickable property to False. This makes the
' ad a standalone image that is not clickable by the
' client.
Dim objAdRot
Set objAdRot = Server.CreateObject("MSWC.AdRotator")
objAdRot.Clickable = False
%>
Notes
If the Clickable property is set to True, you must have a URL set for this ad in the
rotator schedule file or you must use a redirection file with a default URL. If the
Clickable property is set to False for the component, the ad’s URL in the rotator
schedule file will be ignored.
In addition to this example, see the full example at the end of this chapter.
GetAdvertisement 243
Ad Rotator
GetAdvertisement
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
TargetFrame
objAdRot.TargetFrame = strFrameName
Specifies the name of the frame into which the link represented by a clickable ad
graphic will be loaded. It is functionally equivalent to setting the Target property
of an anchor tag in HTML.
Parameters
strFrameName
A string value that represents the name of the frame into which you want the
linked page loaded. You can set this parameter to _BLANK, _CHILD, _NEW,
_PARENT, _SELF, or _TOP. These settings have exactly the same effect as
setting the TARGET property of an anchor tag.
Example
<%
' The following code demonstrates the creation of an
' Ad Rotator object and the subsequent setting of its
' TargetFrame property to _TOP. Assuming the ad graphic
' resides in a frame, this setting will cause the link
' to be loaded into the top frame.
Dim objAdRot
objAdRot = Server.CreateObject("MSWC.AdRotator")
objAdRot.TargetFrame = _TOP
%>
Notes
Just as when you set the TARGET property of an anchor tag, if you set the value of
the TargetFrame property to a nonexistent frame, the ad link will be loaded into a
new window, as if you’d set the TARGET property to _self.
In addition to this example, see the full example at the end of this chapter.
Methods Reference
GetAdvertisement
objAdRot.GetAdvertisement(strAdScheduleFile)
Retrieves the pertinent information for the next advertisement from the Ad Rotator
schedule file.
From this file, GetAdvertisement retrieves general information about the ad (size,
default border size, etc.). The call to the GetAdvertisement method also retrieves
information about the specific ad that is selected (according to weights) from the
schedule file.
244 Chapter 12 – Ad Rotator Component
Ad Rotator Example
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
For more information about the Ad Rotator schedule file and about the relative
weights for the various ads in it, see the discussion on the Ad Rotator schedule file
earlier in this chapter.
Once the GetAdvertisement method has retrieved this information, the Ad Rotator
object creates the HTML for the ad that is sent to the client.
Parameters
strAdScheduleFile
A string value that represents the full virtual path or the path relative to the
current virtual directory for the ad schedule file. For example, suppose the
current virtual path is /search and this can be resolved to the physical path
c:\inetpub\apps\search. If you specify the strAdScheduleFile parameter as
/search/AdSched.txt, the Ad Rotator object will look for c:\inetpub\apps\
search\AdSched.txt.
Example
<%
' The following code instantiates the Ad Rotator
'object, then retrieves and displays an ad from
'the AdRotSched.txt file.
Dim objAdRot
Set objAdRot = Server.CreateObject("MSWC.AdRotator")
' Display the ad in the HTML sent to the client. Note that
' the following line of code inserts the value returned
' by the call to GetAdvertisement into the HTML stream.
%>
<%= objAdRot.GetAdvertisement("/sched/AdRotSched.txt")%>
Notes
Note that you must use a full virtual path or a filename by itself; in the latter case,
the Ad Rotator object will attempt to find the file in the current virtual directory.
In addition to the previous example, see the following full example.
Ad Rotator Example
The following code demonstrates a complete Ad Rotator example to illustrate the
overall mechanism of the Ad Rotator and its accessory files.
The first file, SampleHome.ASP, is the originally requested page containing the ad
component. After the ad component retrieves it from the rotator schedule, this
page also contains the ad.
<%
' +------------------------------------+
' | SAMPLEHOME.ASP |
' +------------------------------------+
%>
Ad Rotator Example 245
Ad Rotator
Ad Rotator Example
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
<HTML>
<HEAD><TITLE>Ad Rotator Sample</TITLE></HEAD>
<BODY BGCOLOR = #ffffcc>
<%
' Dimension local variables.
Dim adrotSample
Dim strAdRotSchedFile
Dim strAdString
Set adrotSample = Server.CreateObject("MSWC.AdRotator")
' Set the ad to have no border.
adrotSample.Border = 0
' Set the ad so that its corresponding URL is loaded
' into a second, blank browser window.
adrotSample.TargetFrame = "_blank"
' No need to set the Clickable property to True. It is
' the default. If we wanted to temporarily change this
' page's ad so that it was informational only, we
' could uncomment the next line.
'adrotSample.Clickable = False
' Retrieve the ad graphic html code (in this case it
' will be "/ads/graphics/FootTown.gif" with a URL of
' "http://www.foottownusa.com/info/introshoes.html."
' (See the sample rotator schedule file for more
' details on this ad.)
strAdRotSchedFile = "/ads/rotshed.txt"
strAdString = adrotSample.GetAdvertisement(strAdRotSchedFile)
%>
<HR>
<%= strAdString%>
<HR>
Welcome to the shoes outlet page. Please visit our sponsors
above!
</BODY>
</HTML>
When called by the client browser, the previous code will retrieve its current ad
information from the following rotator schedule file (rotshed.txt). Note that the
BORDER entry will be overridden by the Border property setting in the previous
code (adrotSample.Border = 0):
REDIRECT /Ads/AdRecord.asp
WIDTH 300
HEIGHT 40
BORDER 1
*
/ads/graphics/FootTown.gif
http://www.foottownusa.com/info/introshoes.html
Visit Shoe Town, your one stop shop for your footwear needs!
90
/ads/graphics/RunShoe.gif
246 Chapter 12 – Ad Rotator Component
Ad Rotator Example
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
http://www.runshoerun.com/running.html
Click here to see the best running shoe company around!
5
/ads/graphics/WalkingShoe.gif
http://www.walkingshoesUSA.com
The Walking Shoes company provides for your every walking need.
5
These ads have relative weights of 90% 5%, and 5%, respectively. When retrieving
ad information, the script will have a 90% chance of retrieving the first entry and a
5% chance for each of the others. In our example, we’ll assume the first sample is
retrieved.
The following code (SampleHome.html) shows the actual HTML code that is sent
to the client:
<HTML>
<HEAD><TITLE>Ad Rotator Sample</TITLE></HEAD>
<BODY BGCOLOR = #ffffcc>
<HR>
<A HREF = "/Ads/AdRecord.asp?url=http://www.foottownusa.com/
info/introshoes.html&image=/ads/graphics/FootTown.gif" TARGET =
"_blank">
<IMG SRC = "/ads/graphics/FootTown.gif" ALT = "Visit Shoe Town,
your one stop shop for all your footwear needs!" WIDTH = 300
HEIGHT = 40 BORDER = 0>
</A>
<HR>
Welcome to the shoes outlet page. Please visit our sponsors
above!
</BODY>
</HTML>
Note in the ad hyperlink that the following items were all retrieved from the
rotator schedule file:
• The HREF of the redirection file
• The URL of the ad
• The URL of the ad graphic
• The alternate text of the hyperlink
• The width of the ad graphic
• The height of the ad graphic
However, the Border property was set in code:
adrotSample.Border = 0
The following redirection file records assorted information about the client who
clicked on the ad and redirects the client’s browser to the ad URL:
<%
' +------------------------------------+
' | AdRecord.asp |
' +------------------------------------+
Ad Rotator Example 247
Ad Rotator
Ad Rotator Example
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.
' Dimension local variables.
Dim strAdURL
Dim strAdImg
Dim strUserName
Dim strUserIP
' Initialize variables.
strAdURL = Request.QueryString("url")
strAdImg = Request.QueryString("image")
strUserName = Request.ServerVariables("logon_user")
strUserIP = Request.ServerVariables("REMOTE_ADDR")
' Record the user information in the web server log file.
Response.AppendToLog "Ad Hit URL: " & strAdURL
Response.AppendToLog "Ad Hit Img: " & strAdImg
Response.AppendToLog "Ad Hit Usr: " & strUserName
Response.AppendToLog "Ad Hit IP: " & strUserIP
' Redirect to the ad URL if there is one.
' If there is not, redirect to a general advertisers
' description page.
If strAdURL <> "" Then
Response.Redirect strAdURL
Else
Response.Redirect "/ads/AdvertDesc.asp"
End If
%>
This code retrieves information about the ad on which the user clicked and then
about the user herself (logon name and IP address). This information is then
written to the web server log file for later analysis. Finally, the user is redirected to
either the URL of the selected ad, if it exists, or to a default ad description page.
248
ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition
Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved.