2

我的网站是在 ASP .NET MVC 和 SQL Server 中完成的。我想为我的电子商务网站中每个产品的详细信息页面添加一个查看计数器功能,即页面的总点击次数。在不影响当前页面加载性能的情况下,实现这一点的最佳方法是什么?

4

3 回答 3

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

于 2013-07-31T05:47:53.053 回答
1

“不影响当前性能……”

因此,我建议使用第三方访问者统计计数器工具,例如 Google 分析。

GA 中的页面特定统计信息:

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

也请看这里;

http://analytics.blogspot.com/2011/09/whats-happening-on-your-site-right-now.html

于 2013-07-31T06:16:42.157 回答
0

使用 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();
}
于 2013-07-31T05:56:59.333 回答