Introduction
A tracker is an application that tracks website stats like unique users, page views (hits), referrers, popular pages, client browsers, and average time that users spend on a web site etc. This information is used by the webmaster to gauge things like the popular content on his website, and main referrers. He can then work more on the content his website's users like to spend their time on.
This tutorial is the part one of a multipart series of tutorials in which we will learn how to develop such an application using ASP.NET 2.0 (C#) and Microsoft SQL Server 2005. We will start by creating a basic tracker (more like a counter) which will track unique users and page views (hits) on our website. In later tutorials and throughout this series, we will continue to add more features and functionality to our application.
Our application will not require you to have access to IIS log files or any ISAPI extensions/filters. You will just have to insert a piece of client-side HTML code in the pages you want to be tracked. That HTML code will request a transparent image file from one of our ASP.NET page. ASP.NET will send that 1x1 pixel transparent image back to the browser, at the same it will log that request in the memory. Later, from the memory, those requests will be flushed to the database. Simple stuff, but very efficient and very effective.
Before we move on, to give you an idea what the report that our website statistics application will generate will look like, have a look below:
Figure – Sample Report
Most of the code will be quite simple and only a basic familiarity with C# and ASP.NET 2.0 is required.
A Little Background
At Stardeveloper.com, I have managed to use several counters and trackers from different companies with varying degrees of success. The one that I use now is a called "FastCounter". It's free version is nothing more than a very simple counter which provides just page views (hits) on a daily and weekly basis. This data is kept only for last 2 months after which you cannot access previous months' data.
Seeing the limitations of it, I took it as a challenge and decided to make one of my own for Stardeveloper.com that will provide me with more info than a simple counter and which I should be able to share with other people at Stardeveloper.com. The code has been developed keeping performance in mind, so that even the websites with a million hits per day will not notice any fluctuation in their performance at all.
Creating a Website Statistics Application:- (Multipart Series)
- How to Track Unique Users and Page Views?
- Tracking Referring Domains and URLs to our Website
- Creating Line, Bar, and Area Charts
- Tracking and Displaying Real-time Visitor Time Zones in a Chart
Overview of the Application
Our tracker application will consist of a database backend and an ASP.NET front-end. The database backend consists of just a single table with 4 stored procedures as listed below:
- Tables: SD_Tut_Tracker.
- Stored Procedures: SD_Tut_IncrementTracker, SD_Tut_GetDailyReport, SD_Tut_GetMonthlyReport, SD_Tut_GetYearlyReport.
The details of them will be described later. The ASP.NET front-end will consist of 2 ASP.NET pages, along with Web.config file and Global.asax files as listed below:
- ASP.NET pages: track.aspx, report.aspx.
- Images: trans.gif.
- Web.config & Global.asax.
Now that we know what our application will consist of, you can already download the complete code and files that I will walk you through this tutorial, from here: 2007040101.zip.
In the zip file is present a file with the name of 'db.sql'. This is a SQL file that we will use to create the required structures (table and stored procedures) in our database.
At the end of this article, we will see the HTML code that is required to be inserted on the web pages you want to be tracked.
Ok, let us not waste any more time and get started on developing the database backend.