开发者

Umbraco CMS: How to make a hit counter

I need to make a hit counter for Umbraco CMS. is there a way to do it? I would like to 开发者_如何转开发track popular articles and display them on the homepage.


You will have to implement this yourself, this doesn't come out of the box. Here's how you could go about it:

First of all you need to know what you want to be counting: The number of times an url is requested? Or he number of times a node is viewed (the same node may be available under several url's)? Hits or visits, should the counter go up if a user reloads a page?

For the actual implementation, you'll need three parts:


1. The client request

Whenever a page is viewed which is supposed to be tracked, you'll need to trigger a request to the server which allows you to increment counter for that page.

There are several ways to achieve that, I recommend to do this by including a picture request (1px transparent gif).

For example:

<img src="/hits/4882/counter.gif" />

4882 is the node id of the article page and is different on every article page.

You should probably include the code which generates the url on the master page (template).


2. The server-side handler

On the server side, you'll need a handler which responds to the picture request. A HttpHandler is perfect for this job. You can create it in a separate project, drop the dll in the bin folder and add the necessary web.config settings.

The handler has to do the following:

  • Identify from which url/node the picture request is coming from - either by using the 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).
  • Increment the counter for that page - you'll need to add a table to the database, something simple with a column for the url or node id and a column with the number of requests should do.
  • Send back an image and make sure, the client doesn't cache it (Set expires header etc.)

3. Displaying the most popular links

In order to display the top articles on a page, you'll need an Umbraco user control which queries the table in the database and displays the most popular links.


Blimey, is it the early 90's again? What about Google analytics?


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="counter.ascx.cs" Inherits="counter" %>

Code for the code behind:

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

Then you need to have an xml file in the root directory to make the code work as well. The XML file will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<counter>
  <count>
     <hits>0</hits>
  </count>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜