Home > Articles > Web Development

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020