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 .?
我的站点是在asp.net。net MVC和SQL Server中完成的。我想在我的电子商务站点中为每个产品的详细页面添加一个视图计数器功能,即一个页面的总点击量。这是实现这一点的最好方法,而不会影响页面加载的当前性能。
3 个解决方案
#1
1
if you just want to keep track of hits you can do as mercsen said in his second comment,
如果你只是想跟踪点击你可以像mercsen在他的第二个评论中说的那样,
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
如果您想要在产品详细信息页面上显示点击次数,那么您可以编写一个ajax请求,定期获取计数器
#2
1
"without affecting the current performance..."
“不影响目前的表现……”
So I suggest a 3rd party visitor stats counter tool like Google analytics.
所以我建议使用第三方访客统计计数器工具,如谷歌分析。
Page specific statistics in GA:
GA中特定页面统计:
http://www.askdavetaylor.com/get_traffic_stats_for_a_specific_page_in_google_analytics.html
http://www.askdavetaylor.com/get_traffic_stats_for_a_specific_page_in_google_analytics.html
http://3by400.com/index.php/get-support/tutorials16/item/finding-page-specific-statistics-in-google-analytics
Also please take a look at here;
也请大家看看这里;
http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html
http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html
#3
0
Using cookie:
使用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();
}
#1
1
if you just want to keep track of hits you can do as mercsen said in his second comment,
如果你只是想跟踪点击你可以像mercsen在他的第二个评论中说的那样,
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
如果您想要在产品详细信息页面上显示点击次数,那么您可以编写一个ajax请求,定期获取计数器
#2
1
"without affecting the current performance..."
“不影响目前的表现……”
So I suggest a 3rd party visitor stats counter tool like Google analytics.
所以我建议使用第三方访客统计计数器工具,如谷歌分析。
Page specific statistics in GA:
GA中特定页面统计:
http://www.askdavetaylor.com/get_traffic_stats_for_a_specific_page_in_google_analytics.html
http://www.askdavetaylor.com/get_traffic_stats_for_a_specific_page_in_google_analytics.html
http://3by400.com/index.php/get-support/tutorials16/item/finding-page-specific-statistics-in-google-analytics
Also please take a look at here;
也请大家看看这里;
http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html
http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html
#3
0
Using cookie:
使用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();
}