Tutorials » Setting Up Website Advertising Using Microsoft AdServer - Enabling Ad Display

Enabling Ad Display - The global.asa File
For the purposes of demonstration, we shall assume that AdServer is installed in the default directory (C:\Inetpub\wwwroot) resulting in the creation of two sub-directories (AdManager and AdSamples), and that the Application that will display ads is a sub-web called GuruWeb on the server 'guruserver'. You now need to enable ads on your site by creating an instance of the AdServer object in the global.asa file and storing it in an Application variable so that it is available to every ASP page on your site. (If your application does not already include a global.asa file, you'll need to create one in the Application root.) Add the following two lines of code to the 'Application_OnStart' sub:

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
      End Sub
      %>

In our examples we shall call the instance of the AdServer object 'MyAd', but of course you can name it anything you like. Next you need to set the ConnectionString property of the AdServer object to the connection string for the advertising database. The simplest way of doing so is with one line of code which details the DSN, user ID and password (if applicable) as follows:

       MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"

For security reasons this option may be undesirable because it makes the DSN information visible to anyone who can access the global.asa file. The alternative is to extract this information from the site.csc file in the AdManager/config directory where it was placed during setup. To create the connection string in this way, replace the above single line of code with the following six lines:

      pathConfig = Server.MapPath("/AdManager/config/site.csc")
      streamConfig = "ASConfig"
      Set dictConfig = Server.CreateObject("Commerce.Dictionary")
      Set fileDoc = Server.CreateObject("Commerce.FileDocument")
      Call fileDoc.ReadDictionaryFromFile(pathConfig, streamConfig, dictConfig)
      MyAd.ConnectionString = dictConfig.MASConnectionString

For the sake of clarity, we shall use the single line of code to assign DSN information to the ConnectionString property, so the Application_OnStart sub should now look something like this:

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
      End Sub
      %>

In order to keep a record of the number of times an ad is clicked, Ad Server uses an ASP file called 'adredir.asp'. This is installed in the AdSamples folder and needs to be copied to the root directory of your application. Once you have done this, you need to uncomment the code by removing the word 'REM' from those lines where it occurs. Then you need to add a line of code to your global.asa file which sets the RedirectURL property of the AdServer object to the path of the redirect file:

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
      End Sub
      %>

Finally you should include a line in the global.asa file which sets the Application property of the AdServer object to the application's virtual root directory. This allows you to refresh the advertising schedule for your site in Ad Manager, and it also helps in debugging by associating errors with the application that produced them.

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
             MyAd.Application = "http://guruserver/GuruWeb"
      End Sub
      %>

These are all the required settings needed to enable advertising on your site. Next we shall take a look at some of the optional ones.

The global.asa File - Optional Settings
Beside the above compulsory settings in the global.asa file, there are also a number of optional ones worth considering. Every 15 minutes (900 seconds) AdManager updates the database with its advertising information. So, for example, if a new campaign item has been included, this is when the information is added to the database, making that ad available to the AdServer component to serve out in response to an ad request. At the same time, Ad Manager updates its information with the latest figures concerning the number of ad requests and ad clicks. The time period between these refreshes is represented by the RefreshInterval property, which can be altered to any number of seconds from 60 to 86400 (1 day). In our example, we shall set this value to 300 (5 minutes). (A value between 300 and 900 seconds is recommended for the RefreshInterval as a compromise between optimum Server performance and currency of data.)

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
             MyAd.Application = "http://guruserver/GuruWeb"
             MyAd.RefreshInterval = 300
      End Sub
      %>

If Admanager is unable to connect to the database for whatever reason, it will try again after 60 seconds. By setting the RetryInterval property, you can alter the amount of time between connection attempts to anything from 0 to 3600 seconds. For example purposes we shall set this to 30 seconds:

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
             MyAd.Application = "http://guruserver/GuruWeb"
             MyAd.RefreshInterval = 300
             MyAd.RetryInterval = 30
      End Sub
      %>

During the setting up of advertising on your site you may find it useful to have debugging information relating to ad delivery errors displayed on your web page. You can do this by enabling design mode, and setting the DesignMode property to True. (The default is False, so you only need to include this line during the design and testing stage.)

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
             MyAd.Application = "http://guruserver/GuruWeb"
             MyAd.RefreshInterval = 300
             MyAd.RetryInterval = 30
             MyAd.DesignMode = True
      End Sub
      %>

Finally, you may want to include a default ad in case other ads cannot be displayed. This could occur, for instance, when there is no current advertising campaign on your site. The DesignMode property must first be set to False (as this is the default setting it is left out of the code below), then simply assign the HTML which creates the default ad as a string to the DefaultAd property:

      <%
      Sub Application_OnStart
             Set Ad = Server.CreateObject("Commerce.AdServer")
             Set Application("MyAd") = Ad
             MyAd.ConnectionString = "DSN=guruDB;UID=admin;PWD=admin911"
             MyAd.RedirectURL = "adredir.asp"
             MyAd.Application = "http://guruserver/GuruWeb"
             MyAd.RefreshInterval = 300
             MyAd.RetryInterval = 30
             MyAd.DefaultAd = "<img src=""http://guruserver/ads/guruAd.gif"" " & _
                   "width=468 height=60 border=1>"
      End Sub
      %>

If there are no ads (including a default ad) eligible to be displayed, and DesignMode is turned off, then the page where a request for an ad is made is unaffected; No HTML code is inserted into it by Ad Server, and nothing appears in the Browser. It's worth being aware of this, as there may be cases where this is just the behavior you want. Now that you have a web page set up ready for your first ad, we shall look at how to manage advertising on your site using Ad Manager.

 
  1 2 3 4 5 6 7