To read the article online, visit http://www.4GuysFromRolla.com/webtech/032201-1.2.shtml

Using the Counters Object, Part 2

By Scott Mitchell


  • Read Part 1

  • 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 \WINNT\system32\ or \WINNT\system32\inetsrv\Data\. 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.

    Function DisplayCounters()
      'The full physical filename of the counters text file
      Const strFileName = "C:\WINNT\system32\inetsrv\Data\counters.txt"
    
      Dim objFSO, objTS
      Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    
      Set objTS = objFSO.OpenTextFile(strFileName)
    
      'Read in the lines one at a time
      Dim strLine
      Do While Not objTS.AtEndOfStream
    
        'Read in the line from the file
        strLine = objTS.ReadLine()
        
        'Display the counter name and value
        Response.Write split(strLine,":")(0) & " - " & _
                       FormatNumber(split(strLine, ":")(1), 0) & "<br>"
    
      Loop
    
      'Clean up...
      objTS.Close
      Set objTS = Nothing
      Set objFSO = Nothing
    End Function
    

    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:

      ...
    
      'This assumes that the user's search term was passed through the
      'querystring as "SearchTerm"
      Dim strSearchTerm
      strSearchTerm = Request.QueryString("SearchTerm")
    
      'If the user entered a search term than increment the counter  
      If Len(strSearchTerm) > 0 then
        objCounter.Increment(strSearchTerm)
      End If
      
      ...
    

    Then, in some reporting screen, you can use the DisplayCounters() function to list the various search terms and their associated popularity.

    Conclusion:
    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.

    Happy Programming!

  • By Scott Mitchell


  • Article Information
    Article Title: 4GuysFromRolla.com - Using the Counters Object, Part 2
    Article Author: Scott Mitchell
    Published Date: Wednesday, March 22, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/032201-1.2.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers