s a Web developer, tracking Web site traffic is just one of the many activities you need to do. You might want to know which pages are the most popular, and more importantly, you might need that data to bill your advertisers. In any case, you can get the site statistics from the Web server log file or you can contract the service to a third-party site that tracks the page usage.
But in ASP.NET 2.0, you can use the built-in Site Counters service to track clicks and views for your Web application. In this article, I will show you how to create a simple ASP.NET 2.0-based Web site and track page views and clicks easily using the Site Counters service.
Creating the Sample Application
The sample Web site I'll create is very simple: It has a few hyperlink controls as well as an AdRotator
control. An AdRotator
control displays a banner ad randomly every time the page that hosts it is loaded. The set of advertisements banners is saved in an XML document. Figure 1
shows the layout of the default Web page for my Web application (default.aspx):
For each hyperlink control I have set its CountClicks
property to True so that the Site Counters service can track the click of each hyperlink control (see Figure 2
Figure 1. Populate the Web page with the various controls.
Figure 2. To enable tracking for the Hyperlink control, set the CountClicks property to True.
But that's not the only property you need to be concerned about. You use the CounterGroup
property to specify a name with which to group click-events for similar controls. For Hyperlink
controls, the default CounterGroup
name is "Hyperlink." The CounterName
property is used to identify each unique Hyperlink
control. Finally, the RowsPerDay
property specifies the number of rows the counter should register. For example, if I specify 1440 in the RowsPerDay
, the Site Counters service will record the clicks every minute--a single day has 1440 minutes (24 hours * 60 minutes). Later on, you will see how this is represented in the database.
Here is the source view for the four Hyperlink
<asp:HyperLink ID="HyperLink1" Runat="server"
<asp:HyperLink ID="HyperLink2" Runat="server"
RowsPerDay="1440">[Apparel & Accessories]
<asp:HyperLink ID="HyperLink3" Runat="server"
<asp:HyperLink ID="HyperLink4" Runat="server"
Note that the CounterGroup
property is not present in the source view of the Hyperlink
control. This is because I used the default value of "HyperLink." If you use a non-default value, the property shows up in the source view.
To make the sample app functional, you need to add four new Web pages to your project--Books.aspx
, and DVD.aspx
. These will serve the purpose of legitimizing the Hyperlink
controls in your application.