Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Tracking Your Website's Visitors and Statistic in ASP.NET
About this Tutorial
This tutorial lays down the basic architecture of a tracking website and highlights the technical aspects while implementing it using ASP.NET with C#. Tracking websites can record the visitor activity on any site (by pasting their tracking snippet).
The recorded data can be used to generate various useful reports. Screenshots and code snippets are shown where-ever necessary. And, you can also download the Tracking & Statistic - Visual Studio 2005 Project. This tutorial is meant for beginner-level developers and can also be used for learning various aspects of ASP.NET and C#. The todo list in the end can further develop your interest in extending this tutorial and learning more.
How tracking works? And how to implement it in ASP.NET
Tracking of a page can be enabled by pasting a simple html code into that page
(normally inserting it before <body>).
This simple html code calls the tracker, sends visitor and page's information to the tracker.
What is this tracking code actually?
Here is an example of ASP.NET using C# that how tracker handles the tracking script
The code above converts the response type to an image and outputs an
invisible image (present as spacer.gif in c:\)
How to access detailed information of a visitor
In ASP.NET, the Request.ServerVariables collection holds a lot of useful information about the visitor. More about it read in Visitor Informations From Request.ServerVariables Collection tutorial.
Getting ip of visitor
Some hands on SQL-Express in Visual Studio 2005
SQL Server Express Edition installs automatically while installing Visual Studio 2005. We can create database tables while working in the same IDE of the visual studio.
In the solution explorer, you can see App_Data item in the menu tree. There we can create new tables, define our constraints, run SQL queries and view database tables.
How to use the ip2country database and to get visitor's country from his ip in ASP.NET
Knowing the country from which your website visitor belongs is an excellent idea. Unfortunately, this information is not present in any of the cookies or server variables.
However, there are free databases available over internet that can provide a mapping of an ip adress to a country. And they are proved to be 99% correct.
There are various ip-country databases available and all of them are in a standard CSV file format. One of them is available at software77. These data are changed from time to time, so you need to update database regularly, one time per month.
Here is what we will do:
The following screenshot shows creating a database table to store ip-country database.
The following code snippet shows how to read a file uploaded by FileBrowser control and how to load its content into the db.
Creating connection with SQL Client
Dont forget to include System.Data.SqlClient.
With the above code snippet, ip-country database can be updated anytime in the table.
To use the ip-country database, the input ip should be in a long-ip format. The attached project contains that function to convert an ip in dotted format into a long-ip format. The prototype of that function is:
public double Dot2LongIP(string DottedIP)
Here is how we can get visitor's location through his ip using the ip-country db.
"select fullname from [ip-country] where starting<"
+ longip + " and ending>" + longip;
In the above snippet, location is a string and command is and object of SqlCommand. Note that ExecuteScalar is useful when we are interested in getting only a single numeric information from the query. ExecuteReader actually returns first column of the first row of the results.
Using calendar control to select dates, building a query
A calendar control can be used to select dates while running a report. Simply drag and drop the calendar control from the toolbox. Use the SelectionChanged event handler to update the reports. Use Calendar's SelectedDate member to get the date selected.
To Do List
As previously mentioned, this article builds an architecture for a tracking website. Tracking script works successfully and records visitor's information into the db. Now, its upto you to generate customized reprots from recorded data. Following can be done as a practice:
Tutorial toolbar: Tell A Friend | Add to favorites | Feedback |