My site is done in ASP .NET MVC and SQL Server. I want to add a view counter feature for details page of each products in my e-ccommerce site,that is number of total hits to a page. Which is the best way to achieve this without affecting the current performance in the loading of the page .?
Asked
Active
Viewed 3,748 times
2
-
analyze the log? Maybe every 5 minutes, the counter woulnd't be live but the log will be created anyway.... else increse a column in a DB, or a txt-file file whatever, this should't decrease the speed of the page that much – mercsen Jul 31 '13 at 05:32
-
Thanks for the reply.This is a dynamic details page of each product in my selling site. So need to track view counter of each product details page. – G . R Jul 31 '13 at 05:33
-
another way is to create a procedure in the DB. Each time this function is called it selects a specific Item (and send it to the client) and increases the counter. i think that would be the fastest way and you don't have to worry to keep track of the hits by yourself – mercsen Jul 31 '13 at 05:39
-
"Duplicate" : http://stackoverflow.com/questions/6079241/what-is-the-best-way-to-capture-page-views-per-user-in-asp-net-mvc – Jul 31 '13 at 18:04
3 Answers
1
if you just want to keep track of hits you can do as mercsen said in his second comment,
if you want to display the number of hits on the product details page then you can write an ajax request which fetches the counter periodically

anil
- 168
- 7
1
"without affecting the current performance..."
So I suggest a 3rd party visitor stats counter tool like Google analytics.
Page specific statistics in GA:
http://www.askdavetaylor.com/get_traffic_stats_for_a_specific_page_in_google_analytics.html
Also please take a look at here;
http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html

Lost_In_Library
- 3,265
- 6
- 38
- 70
0
Using cookie:
var productId = 1;
if (Request.Cookies["ViewedPage"] != null)
{
if (Request.Cookies["ViewedPage"][string.Format("pId_{0}",productId )] == null)
{
HttpCookie cookie = (HttpCookie)Request.Cookies["ViewedPage"];
cookie[string.Format("pId_{0}",productId )] = "1";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = productId } );
db.SaveChanges();
}
}
else
{
HttpCookie cookie = new HttpCookie("ViewedPage");
cookie[string.Format("pId_{0}",productId )] = "1";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
db.Execute("UPDATE Products SET ViewCount = ViewCount + 1 WHERE ProductId = @id", new { id = productId } );
db.SaveChanges();
}

Jeyhun Rahimov
- 3,769
- 6
- 47
- 90