Using The Counters Object (2)

Source: Internet
Author: User
Tags split
Object 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's, even if the WEB server is rebooted the ' value in these counters won't be reset back to zero. This is possible because the "data for each counter are stored in a text file, Counters.txt." This file would most likely is located in \winnt\system32\ or \winnt\system32\inetsrv\data\. The file ' s structure is pretty straight foward. Each counter this create has its own line in the text file. On each line the name of the counter comes-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 a ASP page we could use some very simple filesyst Emobject code to open this file and read the contents, displaying the name of the counter and its value. The function presented below can 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 ' lines one at a time
Dim StrLine
Do as 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 this values into a two-dimensional array and then sort the array in descending Ord Er by the counter values. Then your could selectively display, say, the top ten counters or whatnot. If This is interests you are 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 is to track the popularity of various search terms entered by yo ur users. For example, if you are have a search engine on your site (like the search engine where 4Guys) you'll be curious what H keywords are the most popular. To track this metric, all your would need to do are add the following code to the search page:

...

' This assumes the user ' s search term is 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 screens, can use the displaycounters () function to list the various search terms and their Asso Ciated popularity.

Conclusion:
This wraps the examination of the counters object. The counters object is a powerful version of the PageCounter object capable of providing multiple counters across yo ur entire Web site.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.