In yesterday's article, Recording Page Hits with Microsoft's Page Counter Object we examined using Microsoft's PageCounter object to track the number of hits for any particular Web page. Unfortunately this component is only built for counting page view hits. If we wanted to count something else, say each time a user entered a particular search query into our site's search page, we'd be hard pressed with using the PageCounter object. Fortunately Microsoft also provides developers with a more generic counting object, the Counters object. This object can be used to track an indeterminent number of developer-defined counters.
Both the PageCounter and Counters components are made available with Microsoft's IIS Resource Kit. Unfortunately there is shoddy documentation for the IIS Resource Kit - it seems to be isntalled on IIS 5 (Windows 2000) by default, but older versions of IIS may not have it properly installed. Refer to the previous article, Recording Page Hits with Microsoft's Page Counter Object, for more information on the IIS Resource Kit and how to obtain it if you don't have it installed.
Assuming you have the IIS Resource Kit components properly installed you should be able to create an instance of the Counters object using the following code:
<% Set obj = Server.CreateObject("MSWC.Counters") %>
If you get a Server.CreateObject Failed, Invalid class string error then the components are not properly installed. (If this is the case you should try changing MSWC.Counters to IISSample.Counters and cross your fingers.) If you receive no such error message then the component is properly installed.
Using the Counters Object:
The Counters object allows you to create any number of persistent counters. When using the Counters object it is smartest to create a sinlge application-wide instance of this object. We can most easily do this by adding the following line to the top of our Global.asa file:
<OBJECT RUNAT=Server SCOPE=Application ID=objCounter PROGID="MSWC.Counters"></OBJECT>
To learn more about Global.asa be sure to read: Everything you Wanted to Know about Global.asa but were afraid to Ask and Global.asa Events as well as the Creating a Global.asa File section of the sample chapter of Sams Teach Yourself ASP 3.0 in 21 Days!
Once this line is added to Global.asa you can access the Counters object from any ASP page in your Web site by the object's ID that we specified in the OBJECT tag: objCounter. Like the PageCounter object discussed in yesterday's article, the Counters object is a fairly simple object consisting of only four methods:
Get(CounterName) - Retrieves the current value of the counter specified by the name CounterName
Increment(CounterName) - Increments the current value of the counter specified by the name CounterName
Remove(CounterName) - Removes the counter specified by the name CounterName from the text file counters.txt. This text file is where the counter information is persisted.
Set(CounterName, Value) - Sets the value of the counter specified by the name CounterName to the value specified by Value
So, once we have added the OBJECT tag above to our Global.asa file we can start working with the Counters object from any ASP page on our Web site. We could use the Counters object much like we used the PageCounter object. For example, if we wanted to create a counter to see how many folks visited our homepage we could use the following code on our homepage:
'Increment the homepage counter
'Display how many hits we've had
Response.Write "There have been " & objCounter.Get("HomePage") & " hits."
The counter name HomePage doesn't represent any special sort of name. You can simply create a counter by specifying a unique name. Once the counter is created you can increment it, change it, get its current value, or remove it from any page on your Web site. So, if on another page you had the same code as above and ten visitors hit your home page and then five visitors hit this second page, the value for the HomePage counter will be 15 - 10 hits from the homepage and 5 hits from the secondary page.
In Part 2 we'll examine the Counters object in more detail, looking at how and where its values are persisted. We'll also look at some code to display all of the values of the various counters we may have created!