Using the Counters Object, Part 2By Scott Mitchell
In Part 1 we looked at the Counters object and what purpose it serves; we also examined its four methods. In this part we're going to look at the Counters object in a bit more depth and look at some code to track the popularity of various search terms and code to display the values of all of the counters!
Persisting Counter Data:
The Counters object persists its various counters' information. That is, even if the Web server is rebooted the value in these counters will not be reset back to zero. This is possible because the data for each counter is stored in a text file,
counters.txt. This file will
most likely be located in
The file's structure is pretty straight foward. Each counter that you create has its own line in
the text file. On each line the name of the counter comes first, followed by a colon, followed
by the value of the counter. For example:
HomePage:1935 SomeOtherPage:234 Product Queries:45 Advertisement Displays:35221
Therefore, if we wanted to display the values of all of our counters in an ASP page we could use some very simple FileSystemObject code to open this file and read the contents of each line, displaying the name of the counter and its value. The function presented below can be cut and pasted into your application to list the contents of each counter.
With a little more work you could read these values into a two-dimensional array and then sort the array in descending order by the counter values. Then you could selectively display, say, the top ten counters or whatnot. If this interests you be sure to check out: Sorting a Two-Dimensional Array with BubbleSort.
Tracking the Popularity of Various Search Terms:
One very useful application for the Counters object would be to track the popularity of various search terms entered by your users. For example, if you have a search engine on your site (like the search engine here at 4Guys) you may be curious what search keywords are the most popular. To track this metric, all you would need to do is add the following code to the search page:
Then, in some reporting screen, you can use the
DisplayCounters() function to list
the various search terms and their associated popularity.
This wraps up our examination of the Counters object. The Counters object is a more powerful version of the PageCounter object, capable of providing multiple counters across your entire Web site.