Counting Page Hits

IIS 6.0

It may be useful to know how many times someone requests, or hits, your Web pages. Sites with high traffic may attract more advertising revenue for you. Some Web sites use this data to charge advertisers a flat fee per hit. Traffic information also indicates how customers are navigating through your site and where ads should be placed. And pages that never seem to be hit might indicate that design changes are needed.

The PageCounter component (%SystemRoot%\system32\inetsrv\pagecnt.dll) uses an internal object to record Web page hits on the server. At regular intervals, PageCounter saves all information to a text file so that no counts are lost due to power loss or system failure.

IIS 6.0 and later: The Page Counter component is not available.

The PageCounter component uses three methods, as follows:

  • Hits This method displays the number of hits for a Web page. The default is the calling page.

  • PageHit This method increments the hit count for the current page. If you want hits recorded for an ASP page, this method must be called inside that page.

  • Reset This method sets the hit count for a page to zero. The default is the calling page.

Copy and paste the following code in your text editor, and save the file as PageCounter.asp in the x:\Inetpub\Wwwroot\Tutorial directory. View the example with your browser by typing http://localhost/Tutorial/PageCounter.asp in the address bar.

<%@ Language= "VBScript" %> 
<title>Page Counter Example</title>  
<font face="MS Gothic">  

<H3>Page Counter Example</H3>  

<FORM NAME="PageCounter" METHOD="GET" ACTION="PageCounter.asp">  
<INPUT TYPE="CHECKBOX" NAME="reset" VALUE="True">Reset the Counter for this page?<BR>  

 'Instantiate the PageCounter object.  
 Set MyPageCounter = Server.CreateObject("MSWC.PageCounter")  

 'Increment the counter for this page.  

 If Server.HTMLEncode(Request.QueryString("reset")) = "True" Then  
 'Reset the counter for this page.  
 End If  

Hits to this page = <%=MyPageCounter.Hits %><BR>  

In the browser, you should see the following:

Check Box

Reset the Counter for this page?

Hits to this page = 1

Click the Refresh button in your browser or the Submit button on the page to watch the hit count grow. Check the box if you want to reset the counter.