This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Build Simple Hit Counters for Your Active Server Pages
Manohar Kamath |
Its easy to tabulate page hits, site visits, and visitor tracking in your ASP files. |
So you finally created an ASP-based Web site and now you're waiting for the traffic to pour in. How will you know how many people visit your site? Believe it or not, Web statistics are probably the single most important factor in determining the effectiveness of your site. Large sites such as Microsoft's, Netscape's, and Yahoo have sophisticated methods of computing traffic. These statistics not only provide information on user preferences, but they are also a way of knowing what people expect of your Web site.
To really understand the usage statistics of a large Web site, it is important to get some robust reporting tools that will watch over all the activity of your site as visitors pass through. But sometimes, all you really need is a simple page counter. In this article I will discuss various forms of counters you can write with ASP. While this is by no means an exhaustive list, I have tried to include the most common types. The counters described here will be most useful if you have a Web host account without any detailed hit analysis. If you require more advanced counter analysis, consider using a tool like the Microsoft® Usage Analyst, part of the Microsoft Site Server package. The examples in this article will work only with ASP files, so you cannot use them to track HTML files unless you're doing simple redirects to them. I didn't think of using counters until I noticed that my site was becoming popular. I needed to know how many users visited my site and how often they visited. At that time, my host had not yet installed any hit analysis software so I had to resort to ASP counters. Creating a Web counter is not difficult at all. In fact, even a few years ago when Web pages were just becoming popular, there were counters that you could include on your Web page. In the early days, counters were CGI scripts that looked up an entry in a database, obtained the count, incremented the count, and returned the result to the browser. Back then, there was no concept of maintaining stateif you refreshed the counter page, the count increased. Today, counters are much more complex. Not only do they have to compute traffic on individual pages, they also do so for whole sites. Counters are able to keep track of state, so when a user refreshes the browser the count does not increase. Besides ASP, languages counters can be written in include Visual Basic® 6.0, Perl, C++ with ISAPI, and Java.
A Simple Page Counter
Simple, sure, but this could be a messy way of doing things if you have 20 pages and want to keep a count for each page. Instead of naming a variable for each page, I came up with another scheme.
A Simple, Multiple-page Counter
The variable name of the counter corresponds to the script path, and this code works exactly like the first example. Copy this code into a text file (count.inc, for example), and include the following code in each ASP page in your site:
Now you have a simple but effective way of counting hits on each of your ASP pages. But how do you print the count? This method does not allow for a detailed traffic report, but you can retrieve the count in one of two ways. If the page you're on is the same one for which you want to report the hit count, just use this code:
|
|
If you know the file names in your site, and want to provide cross-page numbers, you can access the count for each by using the hardcoded file name as the index: |
|
A Simple Site Counter
|
|
Application("Visits") gives you the count for the whole site or the whole application. You can copy the code to each global.asa in your entire site so you can keep a tab on the hits for each virtual directory. If your site is well organized, with each virtual directory representing a theme or a section, you can use this running total as a fairly accurate way to determine the most popular sections.
The Indestructible Site Counter
|
|
in Figure 1 is optional, it is useful because it prevents having to read the count.txt file each time I want to retrieve the count. Instead, I can just use Application("Visits"). A sample message could look something like this: |
|
The Visitor Counter
Page Counter and Counter Components
Conclusion
|
From the August 1998 issue of Microsoft Interactive Developer.