Using the Counters Object (2)

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

时间: 2024-10-01 19:41:25

Using the Counters Object (2)的相关文章

Using the Counters Object (1)

object Introduction: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

在IE下获取object(ActiveX)的Param的代码_javascript技巧

为了清晰起见,下面用最简单的HTML和JavaScript来说明.有这么一段HTML(head部分是标准的head,doctype使用xhtml-transitional的DTD): 复制代码 代码如下: <body> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/

这是我在网上摘入的,贴上来与大家一起学习学习。 在JavaScript中应用Object(1)

           在JavaScript中应用Object JavaScript是一种客户端脚本语言,在大多数情况下,它被用来制作在 Web页面上飞来飞去的对象,或用于确认HTML 表单中的输入数据合法性.但是,如果你超越这些表象深入挖掘,就会发现,这种简单易学的语言还有许多许多其它用途.比如,JavaScript中面向对象(Object)的结构体系.你还不知道在JavaScript中面向对象是怎么一回事情吧?没有关系,请跟我来. JavaScript的对象模型所提供的功能和灵活性并不如Ja

在JavaScript中应用Object (3)

八. 综合应用 最后一个例子演示JavaScript对象的重要性.首先设置好一个 Calendar(日历)对象,然后根据需要显示任何一个月的月历.执行过程不复杂,只需要指定月和年为对象属性,然后让构造器做其它事情即可:<script language="JavaScript">/* Calendar object, calendar.js Usage: obj = new Calendar(mm, yyyy); created 15.Mar.2001 copyright M

在JavaScript中应用Object (2)

五. 对象应用例程 再演示另一个关于Thermometer(温度计)对象的例子,它负责将不同的温度刻度进行转换:<script language="JavaScript">// constructorfunction Thermometer(degrees, scale){// methodsthis.convertToCelsius = convertToCelsius;this.convertToFahrenheit = convertToFahrenheit;this

PHOTOSHOP极品滤镜KPT6.0(八)

滤镜 在KPT6.0中的第七个滤镜是KPT SceneBuilder(KPT 场景建立).初看这个滤镜好像好复杂,的确这个滤镜的设置很多,因为这个滤镜可以看作是一个3D物体的渲染器.使用它可以渲染3D studio的物体或者是KPT场景的物体.不过如果你学过3D软件的话这个滤镜就很好上手(反正我只花了10分钟就基本搞懂了).我们先来初步了解以下这个滤镜的界面. 这个滤镜的界面和其他滤镜KPT6.0滤镜的界面有所区别,整个界面处于一个窗口中.在这个窗口的中部有三个标签分别是Edit 2D(编辑二维

C#中的“装箱”(boxing)与“拆箱”(unboxing)

装箱和拆箱:任何值类型.引用类型可以和object(对象)类型之间进行转换.装箱转换是指将一个值类型隐式或显式地转换成一个object类型,或者把这个值类型转换成一个被该值类型应用的接口类型(interface-type).把一个值类型的值装箱,就是创建一个object实例并将这个值复制给这个object,装箱后的object对象中的数据位于堆中,堆中的地址在栈中.被装箱的类型的值是作为一个拷贝赋给对象的.如:int i = 10;object obj = i; //隐式装箱object obj

Javascript 面向对象编程(一)封装

学习Javascript,最难的地方是什么? 我觉得,Object(对象)最难.因为Javascript的Object模型很独特,和其他语言都不一样,初学者不容易掌握. 下面就是我的学习笔记,希望对大家学习这个部分有所帮助.我主要参考了以下两本书籍: <面向对象的Javascript>(Object-Oriented JavaScript) <Javascript高级程序设计(第二版)>(Professional JavaScript for Web Developers, 2nd

Data Binding 用户指南(Android)

1. 介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- 它是一个support库,因此你可以在所有的Android平台最低能到Android 2.1(API等级7+)上使用它. 需要:Android Studio 1.3.0-beta1 或更高版本. 测试版本 请注意:Data Binding库当前是测试版本.在Data Binding处于测试阶段时,开