Recording Page Hits with Microsoft's Page Counter ObjectBy Scott Mitchell
We've all seen page counters on Web pages that indicate how many folks have visited a particular Web page. In fact, there are numerous articles on 4Guys detailing a plethora of ways to accomplish this functionality using ASP. (For solutions on creating a page counter check out Creating a Persistent Web Page Counter or Building a Simple Hit Counter in ASP.)
Those custom script solutions for creating a Web page counter work nicely, but wouldn't it be nice to have a slick, efficient COM component to handle hit counts for a Web site as opposed to custom script? The good news is that there is already such a component from Microsoft, the PageCounter component of the IIS Resource Kit. The IIS Resource Kit contains some of the most powerful (and least known) COM components. In this article I will discuss just one of the many IIS Resource Kit components, the PageCounter component, which serves as a page counter for any number of Web pages on a Web site.
Unfortunately the documentation for these components is sketchy at best. From my research I found that these components are installed with IIS 5.0; however, these components may not be installed on IIS 4.0. Take a moment to check to see if you have these components installed. To run this test, create an ASP page on your Web server with this simple line of code:
If you receive a
Server.CreateObject Failed, Invalid class string error message
then the IIS Resource Kit components are not installed. (If you receive no error, just a blank
HTML page, the components are installed.) If you don't have the components installed you can
get them by buying the Microsoft
Internet Information Server Resource Kit book (which ships with a CD with the components).
Also, if you have an MSDN subscription you can obtain these components for free from
MSDN Subscription site.
|If you are Using PWS (or maybe even IIS 4.0?)|
If you are using Personal Web Server (that is, if you are testing this on a Windows 9X box),
and are getting the |
Some readers have also reported that this "workaround" is needed on their IIS 4.0 Web server... worth a try if you're having problems using the PageCounter component.
Working with the PageCounter Object:
The PageCounter object is a compact COM component that serves a single purpose: to count the number of hits for a Web page. Assuming you have the PageCounter object installed on your Web server, you can create an instance of the object with a single line of code:
The PageCounter object contains three methods:
Hits()- this returns the number of hits the Web page has received.
PageHit()- this method increments the hit count by one
Reset()- resets the hit counter for the Web page back to zero
A simple example of incrementing the hit counter and displaying it's current value can be seen below:
Realize that this PageCounter component is tracking the hits for the particular Web page that it appears on. It would make sense to place the above code in an include file. Those Web pages whose page views you were interested in tracking could then simply use a server-side include to import the above code. (To learn more about server-side includes be sure to read The Low-Down on #include! Also, to learn how to fancy up the display of the hit count be sure to read: Representing Numbers Graphically.)
The PageCounter Object in Detail:
So how does the PageCounter component manage to keep track of the hits for a Web page? Since we would like the number of page hits to persist over Web server restarts and machine reboots this information must be stored in some persistent medium - in this case, a text file. The text file used by the PageCounter object, by default, is:
This file is a simple text file containing a list of Web page URLs and their associated hit counts
in the following format:
50 /SomePage.asp 75 /Foo/SomeOtherPage.asp 25 /AnotherPage.asp 175 /YetAnotherPage.asp
Take a moment to locate this file and examine it. If you just started using the PageCounter component you may find that the file's not present, or that the count it records is slightly off. This is because as the hit counter is increased for a page, the hit count is stored in memory for faster access. After every so many hits (25 by default) this page count value is persisted to disk.
One nice thing about the PageCounter component is that you can specify both the text file to write
the page hits to and how many hits are required before the text file is updated with the
current hits for the pages. Unfortunately these settings are not available through a COM property;
rather, you must muck through the registry. To make these changes, edit the
registry entry. You'll note there are two settings:
File_Location, which indicates
the text file to persist the page hits to; and
Save_Count, which specifies the number
of hits needed before the count is saved to disk.
If you have the PageCounter component installed on your Web server you now know of an easy way to track hits for any Web page on your site! This component is easy to use, requiring only a few lines of code and, best of all, the page hit counts are persisted over Web server restarts.