[英]Umbraco CMS: How to make a hit counter
我需要为 Umbraco CMS 做一个计数器。 有办法吗? 我想跟踪热门文章并将其显示在主页上。
你必须自己实现这个,这不是开箱即用的。 以下是关于它的 go 方法:
首先,您需要知道要计算什么:请求 url 的次数? 或者他查看一个节点的次数(同一个节点可能在多个 url 下可用)? 点击或访问,如果用户重新加载页面,计数器 go 是否应该向上?
对于实际实现,您需要三个部分:
每当查看应该跟踪的页面时,您都需要触发对服务器的请求,该请求允许您增加该页面的计数器。
有几种方法可以实现这一点,我建议通过包含图片请求(1px 透明 gif)来实现。
例如:
<img src="/hits/4882/counter.gif" />
4882是文章页面的节点id,每个文章页面都不一样。
您可能应该在母版页(模板)上包含生成 url 的代码。
在服务器端,您需要一个响应图片请求的处理程序。 HttpHandler非常适合这项工作。 您可以在单独的项目中创建它,将 dll 放到 bin 文件夹中,然后添加必要的 web.config 设置。
处理程序必须执行以下操作:
HTTP_REFERRER
header of the request, or by using the information in the url (like the node id in the url of the image as in my example above).为了在页面上显示热门文章,您需要一个 Umbraco 用户控件,它可以查询数据库中的表格并显示最流行的链接。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="counter.ascx.cs" Inherits="counter" %>
后面代码的代码:
C#
protected void Page_Load(object sender, EventArgs e)
{
this.countMe();
DataSet tmpDs = new DataSet();
tmpDs.ReadXml(Server.MapPath("~/counter.xml"));
lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
}
private void countMe()
{
DataSet tmpDs = new DataSet();
tmpDs.ReadXml(Server.MapPath("~/counter.xml"));
int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());
hits += 1;
tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();
tmpDs.WriteXml(Server.MapPath("~/counter.xml"));
}
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
Me.countMe()
Dim tmpDs As New DataSet()
tmpDs.ReadXml(Server.MapPath("~/counter.xml"))
lblCounter.Text = tmpDs.Tables(0).Rows(0)("hits").ToString()
End Sub
Private Sub countMe()
Dim tmpDs As New DataSet()
tmpDs.ReadXml(Server.MapPath("~/counter.xml"))
Dim hits As Integer = Int32.Parse(tmpDs.Tables(0).Rows(0)("hits").ToString())
hits += 1
tmpDs.Tables(0).Rows(0)("hits") = hits.ToString()
tmpDs.WriteXml(Server.MapPath("~/counter.xml"))
End Sub
然后你需要在根目录中有一个 xml 文件才能使代码也能正常工作。 XML 文件将如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<counter>
<count>
<hits>0</hits>
</count>
天哪,又是90年代初吗? 谷歌分析呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.