Introducing the Site Counter API


The Site Counter API consists of a single class, SiteCounters. You get the current instance across the homonymous property of the class HttpContext or from the local page, which is even easier. The class offers various methods that allow simplified access to three underlying methods:

  • Flush signals the Site Counter Provider to take over the data from the cache and save it in the applied data store.

  • GetRows allows read access on the saved data. The method redelivers a DataSet containing a single table.

  • Write logs a new log entry and is used internally from various controls.

Nearly all the methods are overloaded several times and exist in different versions. With the GetGroupRows() method, for example, you can query all entries in a counter group with GetNameRows() for a specific counter name. Internally, however, GetRows() is used here as well. Just some of the parameters are passed over as null.

Note

Please be aware that at this time the capability to query a listing of all counter groups or counter names isn't implemented. I hope this will be changed/enlarged in the Beta version.

Creating a Reports Page

At the beginning of this chapter, I presented a simple page to query the statistical values to you. I have used it again and again in the various examples for the reporting. The page is based on the API and uses the FlushAll() method to acquire the statistical values that haven't been saved yet, and it uses the GetGroupRows() method to receive all the log values of the chosen group. As mentioned before, a listing of the latter can't be queried yet. Therefore, the groups are placed statically in the DropDownList control.

Listing 10-2: Accessing Reported Site Counter Data

start example
 <%@ page language="C#" %> <script runat="server"> void BT_View_Click(object sender, System.EventArgs e) {     this.SiteCounters.FlushAll();     this.GV_Rows.DataSource = this.SiteCounters.GetGroupRows(DateTime.MinValue,         DateTime.MaxValue, this.DDL_Groups.SelectedValue);     this.GV_Rows.DataBind(); } </script> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server">         <p>             <asp:dropdownlist  runat="server">                 <asp:listitem selected="True">Buttons</asp:listitem>                 <asp:listitem>HyperLinks</asp:listitem>                 <asp:listitem>Ads</asp:listitem>                 <asp:listitem>PageCounters</asp:listitem>             </asp:dropdownlist>             <asp:button  runat="server"                 text="View"                 onclick="BT_View_Click" />         </p>         <p>             <asp:gridview  runat="server">             </asp:gridview>         </p>     </form> </body> </html> 
end example

The two date parameters that are passed to the GetGroupRows() method are the start date and end date of the desired report. By indicating MinValue and MaxValue, you'll receive all the values. Otherwise, you may narrow down the delivered date block using the mentioned method.

Creating an Image Counter Control

The example in this section also demonstrates querying counter data. This time, however, it's in combination with page counters. You place the ImageCounter control shown in Listing 10-3 on each individual page to deliver a graphical hit counter for them.

Listing 10-3: Implementing a Graphical Page Counter

start example
 using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; [assembly:TagPrefix("PGK.Web.UI.WebControls.ImageCounter", "PGK")] namespace PGK.Web.UI.WebControls.ImageCounter {     public class ImageCounter: CompositeControl     {         protected DynamicImage dynamicImage;         protected override void CreateChildControls()         {             this.dynamicImage = new DynamicImage();             this.Controls.Add(this.dynamicImage);         }         protected override void OnPreRender(EventArgs e)         {             this.dynamicImage.Image = this.GenerateImage();             base.OnPreRender(e);         }         protected virtual System.Drawing.Image GenerateImage()         {             int count = this.GetCounterValue();             Bitmap bitmap = new Bitmap(100, 40);             Graphics g = Graphics.FromImage(bitmap);             g.Clear(Color.White);             Font font = new Font("Tahoma", 20);             g.DrawString(count.ToString(), font,                 new SolidBrush(Color.Black), new Point(0, 0));             g.Flush();             return bitmap;         }         protected virtual int GetCounterValue()         {             if (this.DesignMode == false)             {                 SiteCounters counters = this.Page.SiteCounters;                 counters.FlushAll();                 return counters.GetTotalCount(DateTime.MinValue, DateTime.MaxValue,                     "PageCounters", null, null, null, null,                     this.Context.Request.Url.PathAndQuery);             }             else             {                 return 123;             }         }     } } 
end example

The ImageCounter control queries the overall counter value for the current URL with the GetTotalCount() method. This method of the SiteCounters class calls GetRows() internally and identifies the total number by simply adding the values delivered from the DataSet. The detected counter will be written with simple GDI+ routines into a bitmap and displayed by a DynamicImage control (the DynamicImage control is one of the novelties of ASP.NET 2.0 and is described in detail in the Chapter 11, which covers the enhanced Page Framework).

The implementation of the control is quite easy, as you'll see shortly. Figure 10-7 shows the result. The graphical details, however, aren't optimized yet.

click to expand
Figure 10-7: Here's my first ASP.NET 2.0 graphical counter (woo hoo!).

 <%@ page language="C#" %> <%@ register namespace="PGK.Web.UI.WebControls.ImageCounter" tagprefix="PGK" %> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server"> Counter:<br />         <pgk:ImageCounter runat="server" />     </form> </body> </html> 

Using Site Counters with Custom Controls

If you've developed your own control and want to benefit from counters, I can recommend the implementation of a set of properties analogous to the standard buttons described in the beginning of this chapter. The real logging takes places with the SiteCounters.Write() method that was implemented with multiple overloads. In the easiest case, you deliver the counter group and counter name as well as the description of the event such as WClick or WView:

 this.SiteCounters.Write(counterGroup, counterName, counterEvent); 

Further overloads allow you, among other things, to deliver the address of destination and two Boolean parameters that define whether the current page and/or the name of the application should be recorded. By default, this information will be recorded.




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