Using Site Counters


Using site counters is very easy. A couple of controls provide you with a set of properties for recording clicks. You can use different types of buttons for statistics with site counters—a DataList control doesn't offer an adequate measurement, of course.

The storage of recorded values works according to a predefined scheme with a total of eight values and the actual counter. The different values can be considered a kind of a hierarchy and in combination lead to the desired counter, the counter row. The hierarchy looks like this:

 Application     Counter Group         Counter Name             Page Url                 Counter Event                     NavigateUrl                         Start and End Time 

The counter row is incremented with each new record. You can specify the number of counter rows that should be tracked each day to achieve more differentiated results. Figure 10-1 shows a cutout of the automatically generated database of the default provider for Access.

click to expand
Figure 10-1: Site counters are tracked using counter rows.

Tracking Button Clicks

At present there are five button types to support the click recording via site counters: Button, LinkButton, and ImageButton, as well as the controls PhoneLink and ImageMap. The controls offer a uniform set of properties that are summarized in the Properties window in the category SiteCounters:

  • CountClicks indicates whether or not site counters should be used for this control.

  • CounterGroups defines the desired group.

  • CounterName gives the name of the counter.

  • RowsPerDay defines the number of counter rows per day. The default is -1, whereby the global setting from the machine.config file is used (one line per day).

  • SiteCountersProvider allows you to specify an individual provider especially for the chosen control. Normally this property will not be assigned.

  • TrackApplicationName indicates whether the name of the application should be saved.

  • TrackPageUrl specifies whether the URL of the current page should be saved.

I've created a small example in Listing 10-1 with a Button and a LinkButton control to demonstrate the details. As you can see from the following listing, both controls log in to the same group, Buttons, but each uses a different counter name.

Listing 10-1: Automatically Logging Button Clicks to the Site Counter System

start example
 <%@ page language="C#" %> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server">         <asp:button  runat="server"             text="Click me!"             countergroup="Buttons"             countclicks="True"             countername="First button"         />         <br />         <br />         <asp:linkbutton  runat="server"             countergroup="Buttons"             countclicks="True"             countername="Second button">             Click me, too         </asp:linkbutton>     </form> </body> </html> 
end example

Each click on one of the buttons shown in Figure 10-2 will be logged automatically; nothing further is required from your side. This is also possible with the other counter-enabled controls mentioned previously.

click to expand
Figure 10-2: Using site counters is quite easy.

Creating a Site Counter Report

A logging system without any kind of evaluation is useless. Unfortunately, there is no corresponding report tool being shipped with ASP.NET at present. However, the ASP.NET team plans to enhance the Web Application Administration Tool in this sense, probably in the Beta version. A corresponding register clip, "Reports," already exists, as you can see in Figure 10-3.

click to expand
Figure 10-3: The Beta version will most likely ship with a reporting tool.

As long as there is no such reporting tool yet, you must make one yourself. I've created a very simple report page that lists all the saved counter rows of a group. The page is based on the Site Counter API, which I cover later in this chapter. Figure 10-4 shows the page in action. Currently, the counter group Buttons from the last example is active.

click to expand
Figure 10-4: A simple reporting tool lists the button clicks.

Tracking Hyperlink Clicks

You may think that the site counter is limited to controls with server-side event treatment. This, however, is not the case, because the HyperLink control also allows you to measure clicks:

 <asp:hyperlink  runat="server"     countclicks="True"     countergroup="HyperLinks"     navigateurl="http://www.qualisite.de">     Even HyperLinks are tracked </asp:hyperlink> 

The measurement is made possible with a particular URL that may in this example look like this:

 http://localhost:6152/SiteCounters/counters.axd?d=046rKcoZNDzumM29BViU8ECjTGxIdA     OKCJh5O5CXgYhXBGAwVujYRcKoU3FCE6vMr2iOF9uh4Bu2AiWQylV7fKrImlhidOUg-     nwSIDAKZFtLIlGLsxK-*F*bxcADy8d2pdQrxrGfDBX7H8GsDYh351UJ-     LOLZ0Ub2IY*7YSlVd*wOYbtab4NT7Wfzg7r7yfivhzag1so8oOGGaOmO0SHZ     jPAhzi88dpjSHrxTPvBsBEiQQNZlwqlfq9qy1L4dGVxtXoWTQ*nvxw1 

The URL is generated by the Site Counter API and refers to a special HttpHandler handler (System.Web.Handlers.SiteCountersHandler). The handler decrypts the data delivered as a query string, logs the click (including the source and destination address), and redirects to the actual destination.

Why such a long URL? Why isn't it sufficient to just pass an ID? Well, an ID would have to be stored in the application or in a database so that it still works, even after many months. In this URL, however, all information, such as source address, destination address, CounterGroup, and CounterName, is encrypted so that it can be used permanently without any additional dependency. Think of search engines and bookmarks in this context.

Tracking Ads

In combination with the AdRotator control that you may know from earlier versions, site counters make a lot of sense. The AdRotator control is different from the controls you've seen so far, as it can distinguish between the two counter events View and Click. You can activate tracking of these events independently by means of two properties.

The following cutout comes from the ASP.NET Internet Site template and is shipped with VS .NET. The logging of views and clicks was activated in the control. Additionally, a counter group was defined but not a counter name.

 <asp:adrotator  runat="server"     height="242px" width="122px" backcolor="#00C000"     advertisementfile="advertisements\ads_tall.xml"     countergroup="Ads"     countclicks="True"     countviews="True"     trackapplicationname="true"     tracknavigateurl="true"     trackpageurl="true" /> 

You can define the counter name directly and differentiate it for each ad in the advertisement XML file. This way, you can clearly distinguish how often an advertisement has been shown and how many clicks it has received, as shown in the following code. Figure 10-5 shows the corresponding statistic.

click to expand
Figure 10-5: You can easily determine which ad is the most successful.

 <?xml version="1.0" encoding="utf-8" ?> <Advertisements>     <Ad>         <ImageUrl>joesflowers.jpg</ImageUrl>         <NavigateUrl>http://www.microsoft.com</NavigateUrl>         <AlternateText>http://www.microsoft.net</AlternateText>         <Keyword>Orange</Keyword>         <Impressions>80</Impressions>         <CounterName>Red Joe Flower</CounterName>     </Ad>     ...     </Advertisements> 




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net