Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Track Your Site Statistics Using ASP.NET's Site Counters Service : Page 3

Learn how to use the new Site Counters service in ASP.NET 2.0 to track your site usage.


advertisement
Controls Supporting the Site Counters Service
The controls listed in Table 1 support the Site Counters service:

Table 1. List of controls supporting the Site Counters service

Control

Description



AdRotator

Displays an advertisement banner on a Web page

Hyperlink

A control that displays a link to another Web page

Button

Displays a push button control on the Web page

ImageButton

A control that displays an image and responds to mouse clicks on the image

LinkButton

Displays a hyperlink style button control on a Web page

PhoneLink

Represents a link (designed for phone devices)

ImageMap

Displays a clickable image map


Even though the ImageButton control supports the Site Counters service, it does not work in Visual Studio 2005 Beta 1. I will take this opportunity to show you a workaround for this bug; the solution will illustrate not only how to make ImageButton work with the service but also show you how to use the SiteCounter class to manually record clicks for any control that isn't supported.

In Figure 7 I've modified the default.aspx page to include four ImageButton controls that replace the four Hyperlink controls I used earlier.
Figure 7. Default.aspx now uses four ImageButton controls in place of the four Hyperlink controls.

The following is the source view of the ImageButton controls:

<asp:ImageButton ID="ImageButton1" Runat="server" ImageUrl="~/Images/tab_books.gif" PostBackUrl="~/redirect.aspx?URL=books.aspx" CountClicks="True" CounterName="Books" RowsPerDay="1440" /> <asp:ImageButton ID="ImageButton2" Runat="server" ImageUrl="~/Images/tab_apparel.gif" PostBackUrl="~/redirect.aspx?URL=apparel.aspx" CountClicks="True" CounterName="Apparel" RowsPerDay="1440" /> <asp:ImageButton ID="ImageButton3" Runat="server" ImageUrl="~/Images/tab_electronics.gif" PostBackUrl="~/redirect.aspx?URL=electronics.aspx" CountClicks="True" CounterName="Electronics" RowsPerDay="1440" /> <asp:ImageButton ID="ImageButton4" Runat="server" ImageUrl="~/Images/tab_dvd.gif" PostBackUrl="~/redirect.aspx?URL=dvd.aspx" CountClicks="True" CounterName="DVD" RowsPerDay="1440" />

Notice that each control is linked to a redirect.aspx page via the PostBackUrl property. A query string is also passed to the redirect.aspx page. This query string contains the destination page for each ImageButton control.

Here is the redirect.aspx page (which I have added to my project):

Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Me.Load Dim URL As String = Request.QueryString("URL") If URL IsNot Nothing Then SiteCounters.Write("Links (manual)", URL, _ "Click", URL, True, _ True, Nothing, 1) SiteCounters.FlushAll() Response.Redirect(URL) End If End Sub

In the Page_Load event of the redirect.aspx page, I extract the query string using the Request.QueryString method. Then, I write the Site Counter entries to the database using the Write method of the SiteCounters class. The Write method is overloaded and allows you to specify information such as Counter group, Counter name, Counter event, and Navigate URL.

I use the FlushAll method in the redirect.aspx page to write the Site Counter information immediately to the database. The Site Counter service normally caches the data before writing it to the database at regular intervals. After you've used the FlushAll method, redirect the user to the destination page by using the Response.Redirect method.

With that done, I can now track the clicks of the ImageButton controls.

Viewing Site Statistics
Apart from viewing the site statistics directly from the database, a much more practical approach would be to programmatically retrieve the values from the database. The Site Counters service supports several methods to allow you to retrieve site statistics from the database (see Figure 8).

To illustrate how to retrieve the site statistics, I have added a Button control and a GridView control to the page (see Figure 9).


Figure 8. Use the SiteCounters class to manually get site traffic statistics from the database.
 
Figure 9. Add a Button and a GridView control to the page.

In the code-behind of the Get Site Counter Stats button, code the following:

Sub btnGetStats_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim ds As DataSet ds = SiteCounters.GetRows(DateTime.MinValue, _ DateTime.MaxValue, Nothing, Nothing) GridView1.DataSource = ds GridView1.DataBind() End Sub

Figure 10. The site statistics, culled from the Site Counters class, are shown in a GridView control.
The statistics retrieved are returned as a Dataset object, which is then bound to a GridView control (see Figure 10).

The Site Counters service has really simplified the tasks of tracking page usage. No longer do you need to write utilities to extract information from the log files; everything can be done automatically within ASP.NET. Best of all, the Site Counters service works natively with the many Web server controls, in particular the AdRotator control, which will support your efforts to make your site a commercial success.



Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap