Monday, May 10, 2010

Simple C# Page Hit Counter


Image : http://www.flickr.com


If you have or are interested in developing your own website, there probably will come a time when you want to get a general idea of how many times your website is being visited. There are more elaborate ways to do this of course, some developers refer you to reading the server logs, others will suggest using a third party product and implementing it's code within your own website.

For the rest of you who would rather just get a general idea if your personal website is getting roughly 10 hits per day or 1000, this little code snippet will help get you on your way. I have developed elaborate site analysis applications that provide statistics on what pages are being viewed, how long the visitor remains at that specific page, what browser they are using, who referred them to the website to begin with as well as what their I.P address is and the list goes on. For the sake of this article though, we'll be using the k.i.s.s method.

The level of this code requires only a minimal amount of knowledge of the .NET environment, and of course your website should be hosted on a windows server with the .NET environment.

This code can be copied and pasted in the footer area of any .aspx file in your website. Generally, you would put it in the footer of the home page, or at least that's a good place to start.

The first thing you'll want to do is create an empty text file, call it counter.txt and save it to your root directory. The next step is even easier, copy and paste the code below into your .aspx file and save it. Be sure to import the system.IO class into your page something like this


public string counter()
{

StreamReader re = File.OpenText(Server.MapPath("counter.txt"));
string input = null;
string mycounter = "";

while ((input = re.ReadLine()) != null)
{
mycounter = mycounter + input;
}

re.Close();

int myInt = int.Parse(mycounter);
myInt = myInt + 1;

TextWriter tw = new StreamWriter(Server.MapPath("counter.txt"));

tw.WriteLine(Convert.ToString(myInt));

tw.Close();


re = File.OpenText(Server.MapPath("counter.txt"));
input = null;
mycounter = "";

while ((input = re.ReadLine()) != null)
{
mycounter = mycounter + input;
}
re.Close();


return mycounter;

}




'copy this code to the bottom of your .aspx page.







A brief description of what is going on in this code goes as follows:



a. create a method called counter

b. Call the StreamReader class from the system.IO library and read your text file

c. Store the value of the text file in a variable

d. Close the StreamReader

e. Open the StreamWriter

f. Add 1 to the variable holding the value from the text file

g. Write the new incremented by 1 value to the text file

h. Close the StreamWriter


This last line

is the line that calls the method when someone visits your homepage for instance. You can put all of the code at the bottom of the page, or if you are a little more experienced with c# you could place it in a "code-behind" file or an "include" of class methods to keep the code a bit more clean.

Nothing fancy, but gives me an idea of how active the site has been recently.
Hope this helps you in the same way that it has helped me.

Happy Coding!

No comments:

Post a Comment