开发者

How to count number of visitors of pages? (MVC)

I want to get the number of visitors of pag开发者_开发百科es in ASP.Net MVC. Do you know how can I do this?


You'll need to store some information in your database:

Visitor IP address, Visiting URL (means which pages user/guest visited)

Create a function for every page and show your total count based on your database column Visiting URL and IP Address.

Something like (below) will just track the number of visitors to the page, but you can extend this if you want unique visits:

void Session_Start(object sender, EventArgs e)
{
    // More secure than storing it application variables(does not rest on application start
    SqlConnection con = null;
    SqlCommand cmd = null;
    try
    {
        con = new SqlConnection();
        con.ConnectionString = "your connection string";
        cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "Update tbl_users Set no_of_users=no_of_users+1";
        cmd.ExecuteNonQuery();
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (con.State.Equals(ConnectionState.Open)) con.Close();
    }    
}

Alternatively you could call a Stored Procedure which increments the visit count.


You can always use Google Analytics: http://www.google.com/analytics/


I was able to do this using Session variables, sql server db in ASP.NET MVC.

Note: I used session variables instead of storing client IP address. As saving all the client address will overflow your database. This works similar to the Stackoverflow pages viewed property showing for every question at the top right.

This is how it worked for me.

  • Whenever a user opens a particular web page in your website, the counter increments and the updated count will be updated in sql server database(it could be anyother database) tables.
  • If the same user visits the same webpage again within the same session, the counter will not be incrmented. That means if user refreshes a web page multiple times with in the same session, the counter will not be incremented, as it is a single visit by that user.
  • If the same visitor closes his session(say he closes the browser) and reopens the same web page the counter will be incremented considering it as a separate visit.

Here are the steps I followed to implement it.

  1. Have a method which will be automatically called before every Action method.
  2. Inside this method, initiate a session if it is not done already and check if the user has visited this page already.
  3. Now, update the counter variable accordingly and save the count to db if required.

Here is the complete procedure I have followed. Hope it helps.

Source: Count number of visitors of a page in asp.net mvc (HIT counter)


I would use a Tool like AWStats http://awstats.sourceforge.net/

Counting Unique Visitors is hard, because you cannot identify a Unique Visitor exactly. You can only guess (IP, Cookies, UserAgend, etc.).

Counting Visitors could be done by counting the start of a session. But, if your visitor does not accept even temp. cookies, you'll count the visitor too often.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜