Reputation: 142
I want to get the number of visitors of pages in ASP.Net MVC. Do you know how can I do this?
Upvotes: 3
Views: 11760
Reputation: 452
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.
Here are the steps I followed to implement it.
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)
Upvotes: 2
Reputation: 8129
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.
Upvotes: 1
Reputation: 17365
You can always use Google Analytics: http://www.google.com/analytics/
Upvotes: 2
Reputation: 48537
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.
Upvotes: 2