Over the last couple of days we've been busy creating an Umbraco
package that deals with Content View Counters - it enables the web
master to track the number of times content has been viewed on the
site.
The Documentation and package has just been uploaded to the
Umbraco Project Repository and can be downloaded from here.
This post deals with a few of the features of the package, which
was built agains Umbraco 4.7 and dotNet 4.0
Introduction
TheRefactored Content Viewspackage is essentially a content
views (number of times viewed) counter. The current
functionality offered by this package includes:
- Optional Data Type that allows for configuring view counters
with various categories and the ability to instruct Macros etc. to
"hide" the view Count yet still increment it.
- Optional incrementing when displaying the view count (useful
when you want to display the view count in a content listing, for
example)
- Example Razor Script and Macro.
- Library methods to manipulate the counters and retrieve details
as an XML fragment for use with XSLT.
Basic Usage.
To simply retrieve and/or increment the counter for a specific
content item, call the following library method. The category
and increment parameters are optional, with default values shown
initalics:
ViewCount.GetViewCount(nodeId, category: "<empty string>", increment: false);
There is no requirement to configure a DataType; supplying the
node id of any valid Content-based node (Member, Document, Media,
etc.) will create the Views record in the database if it doesn't
exist. However configuring and using a DataType will allow
you to control the advanced features of the counter.
Out of the box
Out of the box you get a default DataType (View Count) and a
sample Razor Macro that displays the current View Count of the node
being displayed. If you have set up the Document Type with
the View Count DataType, the macro will check whether the View
Count should be displayed or not.
Macro Parameters for Page Views:
- Category (text) - optionally specifies the Category to record
the Page Count against.
- Increment (bool) - set to true to increment the Page Count when
the macro is called.
Macro Script Contents:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines;
@using umbraco.NodeFactory;
@using Refactored.UmbracoViewCounter;
@if (!ViewCount.HideCounter(Model.Id, category: Parameter.Category)) {
<span># Views:@ViewCount.GetViewCount(@Model.Id, category: @Parameter.Category, increment: @Parameter.Increment == "1").ToString("N0")</span>
} else {
ViewCount.Increment(Model.Id, category: Parameter.Category);
}
Setting up a Data Type
The Data Type has the following Parameters:

- Category- Specifyinga different category for multiple DataTypes
allows you to differentiate between multiple View Counts in a
single content item. You can then render the content in
different views and have a different View Count for each
rendering.
- Hide View Count- Allows you to control (in conjunction with the
API and Razor or XSLT macros, for example) whether to hide or show
the view count at a Data Type level.
- Enable View History- Turns on recording of View Count History
data including the time the view was incremented. Also
recorded is Reset command events. This data is stored in the
refViewCountHistory table and persists even if the current view
count is reset. This is off by default.
- Disable Counter Reset- Turning this on disables the Reset
action on Content configured with a View Count DataType.