• home
  • forum
  • my
  • kt
  • download
  • A complete free banner ad system in action

    Author: 2007-06-24 19:57:32 From:

    The advertising on my site is served entirely in-house, by code freely available for you to use. This article will step you through the code.

    The banners are all stored in a database table, but I don't access the database to serve the ads. Instead, at the beginning of every day the site automatically loads the ads for that day into Application variables. How the "BrandNewDay" trigger works is discussed in a related article about tracking IP addresses.

    Loading the days ads

    // get current advertising banners
    DBInitConnection  ( );

    DBGetRecords ( 'SELECT BannerID,BannerDescr,BannerURL FROM Banners WHERE ClicksLeft > 0' );

    Firstly, ads "qualify" to be shown that day if there are any purchased clickthroughs still available (for details on how I sell per clickthrough, not impression, read this). That's done with the SQL "WHERE ClicksLeft > 0".

    var sBanners = new Array;
    var nBannerIDs = new Array;
    var nBanners = 0;

    // loop through banners
    while ( !oRecordSet.EOF )
    {
       var nBannerID = oRecordSet ( 0 ) - 0;
       var sBannerDescr = '' + oRecordSet ( 1 );
       var sBannerURL = '' + oRecordSet ( 2 );

       // small image to track impressions first
       var sBanner = '<img src="BannerCounter.asp?ID=' + nBannerID + '" border=0 width=1 height=1>';

    The resulting recordset is looped through, and banner ID, description and URL extracted. To track the ad impressions, a link to a small 1x1 "image" is created, with BannerCounter.asp as the target. More about that file later.

    // add URL (patched through our counter)
    sBanner += '<a href="BannerCounter.asp?ID=' + nBannerID + '&CYAredir=' + Server.URLEncode ( sBannerURL ) + '" target="CYAExternal">'

    Next, the URL that will be called when the banner id clicked is assembled. The advertisers original URL, in the variable sBannerURL, is passed as a parameter into (again) BannerCounter.asp. Because I want to pass the URL into another file I have to first encode it. This replaces any special characters such as /?& into their hexadecimal equivalents.

    // add image
    sBanner += '<img src="images/ads/' + sBannerDescr + '.gif" border=0 width=468 height=60></a>';

    The last of the HTML, the image of the actual banner, is added to the sBanner variable...

       // store banner IDs for later use
       nBannerIDs [ nBanners ] = nBannerID;

       sBanners [ nBanners++ ] = sBanner + '<p>';

       oRecordSet.moveNext ( );
    }

    ...and the data stored into the temporary array. The loop continues with the next record in the recordset.

    // now set data into Application variables
    Application.Lock ( );

    Application ( 'TotalBanners' ) = nBanners;

    for ( var i=0; i<nBanners; i++)
       Application ( 'Banner' + i ) = sBanners [ i ];

    Application.Unlock ( );

    Outside the loop, after all the records have been processed and stored in the array, I finally move the data into Application variables, locking the Application first. The array was used just to make this lock/unlock section as short as possible.

    the ads for the day into Application variables for fast database-free access. Now I'll use those variables to display the ads:

    // ============================================
    // show the next available advertisement
    // ============================================
    function ShowAd ( )
    {
       var nBanners = Application ( 'TotalBanners' );

       if ( !nBanners )
          return;

       // increment banner counter
       Application.Lock ( );

       if ( ++Application ( 'CurrentBanner' ) >= nBanners )
          Application ( 'CurrentBanner' ) = 0;

       Application.Unlock ( );

       // output banner html
       Out ( '<center>' + Application ( 'Banner' + Application ( 'CurrentBanner' ) ) + '</center>' );
    }

    This code is in utils/banners.asp. Calling the ShowAd( ) function outputs the HTML that I stored away in the Application variables earlier. The first step is to make sure I have some ads stored - if not then the function returns, having done nothing.

    Next the Application is locked so I can increment yet another Application variable - "CurrentBanner". This variable is incremented until it is greater than the maximum number of banners, then it is reset to start again from zero.

    I then reference the relevant Application variable to output the banner code. "Banner0", then "Banner1", etc.

    This is a very simple banner rotation system! It currently allows no weighting, no zones and takes no notice of the user watching or the page it is displayed on. All these things can change if demand warrants it of course!

    Now let's go back and investigate BannerCounter.asp - the file that counts impressions and manages the clickthroughs.

    BannerCounter.asp is an asp page, but one that outputs no HTML. Simplified source is shown below (you can download the real thing later).

    var nBannerID = Request.QueryString( 'ID' ) - 0;
    var sRedirect = '' + Request.QueryString( 'CYAredir' );

    // the field name to increment in BannerStats
    // (impression or click) depending on if redir passed
    var sVariable;

    if ( sRedirect == 'undefined' )
       sVariable = 'ImpressionCount';
    else
       sVariable = 'ClickCount';

    // open database connection
    DBInitConnection ( );

    // increment count in BannerStats table
    oConnection.Execute( 'UPDATE BannerStats SET ' + sVariable + '=' + sVariable + '+1 WHERE BannerDate=' + Application ( 'Date' ) + ' AND BannerID=' + nBannerID );

    // close connection
    DBReleaseConnection ( );

    // redirect to URL if clickthrough
    if ( sRedirect != 'undefined' )
       Response.Redirect ( sRedirect ) ;

    This file is called in two different ways. The first way is as the target of an <img> tag, where it simply increments the impression counter in the database. The second way is as the link the banner points to, where it increments the clickthough counter and then redirects the user to the advertisers URL.

    The difference is the number of parameters sent in the QueryString. If no "CYAredir" is passed in, then I'm in impression mode. In this case the code above sets sVariable to "ImpressionCount", the name of the field in the BannerStats table that we want to increment. This field is then incremented in the record for that banner on todays date.

    We'll see how that record gets into the BannerStats table in a minute.

    If there is a "CYAredir" parameter passed in, the "ClickCount" is incremented instead, and then Response.Redirect is used to redirect the user to the advertisers URL.

    Remember the BrandNewDay( ) function in utils/Init.asp that's called once per day or whenever we want to put the days banners into Application variables? That also has the following code in it:

    // now create new entries in BannerStats table for today
    for ( var i=0; i<nBanners; i++ )
    {
       // see if it already exists
       DBGetRecords ( 'SELECT BannerID FROM BannerStats WHERE BannerDate=' + sDate + ' AND BannerID=' + nBannerIDs [ i ] );

       // create it if it doesnt exist
       if ( oRecordSet.EOF )
          oConnection.Execute ( 'INSERT INTO BannerStats (BannerDate,BannerID) VALUES (' + sDate + ',' + nBannerIDs [ i ] + ')' );
    }

    "nBanners" contains the number of banners that will be rotated through the day - for each banner I need to create a new record in the BannerStats table every day.

    First I test if the record already exists for that banner and todays date. If it doesn't then I obviously create one. This code is called at the start of every day, and can also be called manually by me when adding new banners.

    And that's that. From top to bottom, how I added advertising to CoverYourASP! Now you can do it too...

    discuss this topic to forum

    relation tutorial

    No relevant information

    New

    Hot