Counting page hits is a requirement for many large Web sites and hosting companies, and some excellent commercial packages provide a range of services to track and analyze data. However, there is often a need for a smaller, more localized set of services to track page hits or click-throughs. This has been implemented in ASP.NET 2.0 with an easy-to-use Site Counters Service, which tracks clicks and hits in a configurable data provider (Access and SQL Server being the supplied providers). The Site Counters Service tracks a number of pieces of information, such as the current page, the target page, named counter groups and counters, and the application name . Each unique combination of these results, within a configurable time period, results in the creation of a new counter and a new row in the database. Subsequent instances cause the counter to be updated. The database provides the columns shown in Table 12.8. Table 12.8. Site Counters Database Columns
Table 12.9. Site Counters Database Schema
The schema for these is shown in Table 12.9. Data will be truncated if it exceeds the lengths specified by the schema. Using Site CountersThe underlying implementation of site counters is an easy-to-use API that can be used directly or built into controls. The following list shows which controls have built-in support for the Site Counters Service:
As these implement the service, they automatically have the option of exposing the properties shown in Table 12.10. Table 12.10. Site Counter Properties Added to Controls
By default, tracking is disabled for these controls, but it's simple to enable. For example, at its simplest it's just a matter of setting the CountClicks property to True : <asp:Hyperlink runat="server" NavigateUrl="products.aspx" Text="Product List" CountClicks="True" /> Every time this link is clicked, the details will be tracked. To add more details to the tracking, you can set more of the properties to differentiate different links: <asp:Hyperlink runat="server" NavigateUrl="products.aspx" Text="Product List" CountClicks="True" CounterGroup="Products" CounterName="Overview"/> <asp:Hyperlink runat="server" NavigateUrl="products.aspx?ProductID=123" Text="Special Offer" CountClicks="True" CounterGroup="Products" CounterName="Special"/> Here both links have the same CounterGroup but a different CounterName , ensuring they are tracked as separate items. To track items with different URLs as though they are the same (e.g., all hits on products.aspx no matter what the query string or which page the URL is on), the same CounterName can be used. Any session information stored as part of the URL is stripped before being logged.
Using the Site Counters APIAccess to the site counter data is available through an API exposed via the SiteCounters property on the Page . There are only two properties Enabled , to indicate whether the service is enabled, and a SiteCountersProviderCollection , which is a collection of all available site counters. The methods of the API are detailed in Table 12.11. The Web Administration Tool (covered in Chapter 6) will have details of the site counters (this is unavailable in the Technology Preview), but the Get methods allow you to provide your own interface if required. The Write method allows you to control the tracking of hits from within code. For example, although site counters do not allow absolute URLs, you could implement tracking of these manually, as shown in Listing 12.26. Table 12.11. SiteCounter Methods
Listing 12.26 Manually Adding URLs to the Site Counters Service<script runat="server"> Sub NavigateAway(Sender As Object, E As EventArgs) SiteCounters.Write("Navigate", "Away", "Click", _ "http://www.asp.net/", True, True) Response.Redirect("http://www.asp.net/") End Sub </script> <form runat="server"> <asp:LinkButton runat="Server" onClick="NavigateAway" Text="ASP.NET" /> </form> The Write method is overloaded and has several forms. See the documentation for complete details. Configuring Site and Page CountersThe default installation of ASP.NET 2.0 ships with two providers for page trackingone for Microsoft Access and one for Microsoft SQL Serverand these are configured in the application configuration file. The syntax is shown in Listing 12.27. Listing 12.27 Site Counters Configuration<siteCounters enabled="[truefalse]" defaultProvider=" String " handlerPath=" String " handlerType=" String " rowsPerDay=" Integer " type=" String "> <providers> <add name=" String " type=" string " provider-specific-configuration /> <remove name=" string " /> <clear/> </providers> <pageCounters enabled="[truefalse]" defaultProvider=" String ]" rowsPerDay="[ Integer ]" trackApplicationName="[truefalse]" trackPageUrl="[truefalse]" counterGroup="[ String ]" counterName="[ String ]"> <pagesToCount> <add path=" String " /> <remove path=" String " /> <clear/> </pagesToCount> </pageCounters> </siteCounters> The configuration is broken down into two sections. The first defines the properties and providers for the Site Counters Service itself (see Table 12.12). The <providers> element is where the actual site counter providers are added (see Table 12.13). The properties for the Access and SQL Server providers are the same. The attributes of the <pageCounters> element map directly to the properties we have already discussed, although by default page counters are disabled. Table 12.12. Site Counters Configuration Properties
Table 12.13. Site Counter Database Provider Properties
The <pagesToCount> element details which pages will have counter information logged. This supports wildcards, so a value of * indicates all pages, for example: <pagesToCount> <add path="*" /> </pagesToCount> Wildcards can also be included as part of the path , for example: <pagesToCount> <add path="/*/default.aspx" /> </pagesToCount> This ensures that only the default.aspx page is tracked, but only if one level down from the application root directory.
Configuring the Counter DatabaseYou don't need to do anything specific to start using site counters, even though they are logged to a database. Because the default provider is Microsoft Access, when you first hit a page with site counters enabled, a new directory called DATA is created under your application directory if it doesn't already exist. Then a template database is copied into this directory and called ASPNetDB.mdb . For SQL Server there is no automatic installation, but a SQL Script, called InstallSiteCounters.sql , is included in the .NET Framework's installation directory. |