Home > Articles > Web Development

  • Print
  • + Share This

Simple Web Page Commercial, Version 1

The simplest web page commercial is a single ad that pops up at a regular frequency. For example, you can set up your web page commercial to pop up every 10 times a user views a certain page. Now, before you can create this simple web page commercial it almost goes without saying that you need an advertisement, so start by signing up with an advertiser and getting the advertiser's ad code (see my Week 9 article).

Suppose you have the following ad code (see Listing 1):

Listing 1-Original Ad Code

<a href="http://www.qksrv.net/click-814820-1932310" target="_top" >
[ccc]<img src="http://www.qksrv.net/image-814820-1932310"
[ccc] width="468" height="60" alt="Last Minute
[ccc] Travel Deals" border="0"></a>

The next major step is to put this code into an Active Server Pages script. Follow these steps:

  1. Open your favorite text editor.

  2. Paste the ad code into the editor. (Note that the code shown in Listing 1 wouldn't actually include the line breaks you see here; they're used in this example to keep the code listing from running off your screen. A bit later, I'll show you how to prevent this problem in your own code.)

  3. Save the file as an ASP script; for example, called adcheck.asp.

To use this ad code, we need to put it in a variable. In preparation for putting the ad code into a variable, make sure that the ad code is all contained in one line:

  1. Remove any carriage return/linefeed characters.

  2. To save the ad code into a variable, replace all double quotes (") in your ad with two double quotes (""). Then place the entire ad code in a string and assign this string to a variable, such as v_adcode (see Listing 2). Again, we've had to include line breaks here, but the next step shows how you can avoid this problem.

  3. Listing 2 - adcheck.asp: Ad Code in a Variable

    v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"
    [ccc]" target=""_top"" >
    [ccc]<img src=""http://www.qksrv.net/image-814820-1932310"
    [ccc]" width=""468"" height=""60"" alt=""Last Minute
    [ccc] Travel Deals"" border=""0""></a>"
  4. If you really don't want the ad code all in one line, you can space it out using the Visual Basic concatenation and continuation operators, an ampersand (&) and underscore (_), respectively (see Listing 3):

  5. Listing 3 - adcheck.asp: Ad Code Spaced Out

    v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"" " & _
         "target=""_top"" > "                   & _
         "<img src=""http://www.qksrv.net/image-814820-1932310"" " & _
         "width=""468"" height=""60"" "              & _
         "alt=""Last Minute Travel Deals"" border=""0""></a>"
  6. Now enclose the ad variable within begin/end percent symbols (see Listing 4):

    Listing 4 - adcheck.asp: Begin/End Percent Symbols Added

    <%
    v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"" " & _
         "target=""_top"" > "                   & _
         "<img src=""http://www.qksrv.net/image-814820-1932310"" " & _
         "width=""468"" height=""60"" "              & _
         "alt=""Last Minute Travel Deals"" border=""0""></a>"
    %>

We now have the advertisement code stored in a variable. To make this code useful, we need to somehow keep track of how many times a page is displayed, and then, after a certain number of times, display the ad. The next section examines how to do this.

Tracking How Many Times a Web Page Is Displayed

The easiest way to keep track of how many times a user has viewed a page on your site is via a cookie. We first need to see whether the cookie already exists; if it doesn't, we create the cookie, initializing its value to zero. The general code in Listing 5 does this:

Listing 5 - General Code for Initializing a Cookie

if request.cookies("cookie_name")="" then
 response.cookies("cookie_name")=0
end if

where cookie_name is the name of the cookie we use to keep track of how many times a page was displayed. Suppose we named this cookie pagecount. Listing 6 shows the new version of the code:

Listing 6 - Initializing a Cookie Named pagecount

if request.cookies("pagecount")="" then
 response.cookies("pagecount")=0
end if

Ready to go? Follow these steps:

  1. Add the code in Listing 6 right above the variable containing the ad code (see Listing 7):

  2. Listing 7 - adcheck.asp: Creating a Cookie and Initializing Its Value to Zero

    <%
    '
    ' Initialize the cookie
    '
    if request.cookies("pagecount")="" then
     response.cookies("pagecount")=0
    end if
    
    v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"" " & _
         "target=""_top"" > "                   & _
         "<img src=""http://www.qksrv.net/image-814820-1932310"" " & _
         "width=""468"" height=""60"" "              & _
         "alt=""Last Minute Travel Deals"" border=""0""></a>"
    %>
  3. Now that we know the cookie exists, we need to read it and update its value. To do so, read the cookie and store its value in a variable. Increment this variable by 1 and then write this new value out to the cookie (see Listing 8):

    Listing 8-General Code for Updating a Cookie by 1

    v_cookie_name=CINT(request.cookies("cookie_name"))
    v_cookie_name=v_cookie_name+1
    response.cookies("cookie_name")=v_cookie_name

    So, if our cookie is named pagecount, the specific code for updating this cookie by 1 would be as shown in Listing 9:

    Listing 9-Updating a Cookie Named pagecount by 1

    v_pagecount=CINT(request.cookies("pagecount"))
    v_pagecount=v_pagecount+1
    response.cookies("pagecount")=v_pagecount

    Notice that the variable to store the pagecount cookie is named v_pagecount. This is merely an arbitrary convention we've adopted for naming variables used to store cookie values. It's similar to the naming convention we use for variables that store HTML control values.

  4. Add the code in Listing 9 immediately after the code that initializes the cookie (see Listing 10):

    Listing 10-Initializing and Updating the pagecount Cookie

    <%
    '
    ' Initialize the cookie
    '
    if request.cookies("pagecount")="" then
     response.cookies("pagecount")=0
    end if
    '
    ' Increment the page count by 1
    '
    v_pagecount=CINT(request.cookies("pagecount"))
    v_pagecount=v_pagecount+1
    response.cookies("pagecount")=v_pagecount
    
    v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"" " & _
         "target=""_top"" > "                   & _
         "<img src=""http://www.qksrv.net/image-814820-1932310"" " & _
         "width=""468"" height=""60"" "              & _
         "alt=""Last Minute Travel Deals"" border=""0""></a>"
    %>

Next, we want to display the ad only if the page has been viewed a certain number of times.

Displaying an Advertisement at Some Interval

Suppose we have a variable named v_frequency whose value we set to the number of page views (N) that we want the user to have prior to showing the commercial. Assuming that v_pagecount is as defined earlier, the general code for displaying the ad after N page views is shown in Listing 11:

Listing 11-General Code for Displaying an Advertisement After N Page Views

v_frequency=N
IF (v_pagecount MOD v_frequency)=0 THEN
... Display commercial with ...
END IF

The key to making this technique work is the Visual Basic MOD operator. The MOD operator takes two values: a total and an interval. When the total equals the interval, MOD returns zero; otherwise MOD returns the total. Suppose we want to show an ad after a user views a page 10 times. The specific code would be as shown in Listing 12:

Listing 12-Displaying an Advertisement After 10 Page Views

v_frequency=10
IF (v_pagecount MOD v_frequency)=0 THEN
... Display commercial with ...
END IF

The simplest commercial is one in which we just display the banner without any accompanying text (see Listing 13):

Listing 13-Displaying Only the Banner

v_frequency=10
IF (v_pagecount MOD v_frequency)=0 THEN
 response.write(v_adcode)
END IF

We're almost finished with our first version of the commercial code. Add the advertisement display code above to adcheck.asp, immediately after the variable that contains the ad (v_adcode). Your code should look like Listing 14:

Listing 14-adcheck.asp: Code to Display an Ad After 10 Page Views

<%
'
' Initialize the cookie
'
if request.cookies("pagecount")="" then
 response.cookies("pagecount")=0
end if
'
' Increment the page count by 1
'
v_pagecount=CINT(request.cookies("pagecount"))
v_pagecount=v_pagecount+1
response.cookies("pagecount")=v_pagecount

v_adcode="<a href=""http://www.qksrv.net/click-814820-1932310"" " & _
     "target=""_top"" > "                   & _
     "<img src=""http://www.qksrv.net/image-814820-1932310"" " & _
     "width=""468"" height=""60"" "              & _
     "alt=""Last Minute Travel Deals"" border=""0""></a>"
'
' Display advertisment after 10 page views
'
v_frequency=10
IF (v_pagecount MOD v_ frequency)=0 THEN
 response.write(v_adcode)
END IF
%>

Adding the Commercial Code (Version 1)

Adding the code described above to display a commercial is a simple matter of including that file (adcheck.asp) at the top of the source file for the web page where you want the commercial to appear. For example, suppose we had the following file named commercialpage.asp (see Figure 4):

Figure 4 Sample source file (commercialpage.asp) for the web page in which we want the commercial to appear.

You simply include adcheck.asp to the top of this file (see Figure 5).

Figure 5 commercialpage.asp, instrumented for displaying a commercial.

In short, to add a commercial to any of your web pages, include adcheck.asp to the top of the source file for that web page.

Note that this only works for ASP files. However, any HTML file can be easily transformed into an ASP file by saving it with the .ASP extension. If you do this, remember to update any links pointing to the original HTML file, to point to the new file with the .ASP extension.

NOTE

If you have other included files in your source file, such as an include file to open the database, place adcheck.asp after the other include files.

Testing the Commercial

If you don't already have a web page that links to the page you've instrumented to display a commercial, create a link page. We've created one named linkpage.asp, shown in Figure 6. Figure 7 shows this page displayed in a browser.

Figure 6 linkpage.asp: A test page that links to a page with a commercial.

Figure 7 linkpage.asp displayed in a browser.

If you click the Click here link, the first nine times you see the page without a commercial (see Figure 8). Every tenth time, however, the advertisement is displayed (see Figure 9).

Figure 8 commercialpage.asp: No commercial the first nine times the page is viewed.

Figure 9 commercialpage.asp: Commercial displayed every tenth time the user views the page.

Unfortunately, the original message—Hello World—is displayed along with the commercial. How do we set up the page so that only the commercial displays? The next section examines an improved version of the web page commercial, which solves this problem.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.