CS Dev Guide: Views
Views counts are a main part of Community Server statistics for all applications including weblog, file gallery, photo gallery and ... Usually views statistics get updated behind the scene but sometimes you may need to work with them directly. This post discusses about this topic shortly.
As a background, you should know that there are two types of views in Community Server: web views and aggregate views. Web views are those that come from web browsers and aggregate views are those that come from feed readers. It's worth to know the technique to catch aggregate views: Community Server adds the address of an HttpHandler to all post bodies in feed readers which contains the identifier of the post in query string and displays an image. After that you view a post in feed reader, this handler will be loaded and it increments the aggregate views of a post automatically.
CommunityServer.Components.View represents views in Community Server API. It has three properties for the identifier of the post as well as the web and aggregate views for that post. It also has a constructor that gets the identifier to create an instance of View object for a specific post.
CommunityServer.Components.Views is the method factory to increment the web and aggregate views and save the changes with its static methods.
In below code and IncrementAggViews() method, I simply increment the aggregate views count of the current post.
void IncrementAggViews()
{
CSContext context = CSContext.Current;
if (context.PostID > 0)
{
// If there is any post to increment its views
Views.AddAggCount(context.PostID);
Views.SaveQueue();
}
}
GetWebViews() is a function that returns the web views count for current post as an integer number. It uses the View object and its WebCount property to get access to this information.
int GetWebViews()
{
CSContext context = CSContext.Current;
if (context.PostID > 0)
{
// If there is any post
View view = new View(context.PostID);
return view.WebCount;
}
return 0;
}
Note that Post class has a Views property that represents the web views for a post in general. Post doesn't have a property for aggregate views because a some divisions of a post may be rendered in web browsers only. But some derived classes like WeblogPost have an AggViews property that returns the number of aggregate views for a post. All these properties use above mechanism to return their values behind the scene.
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
3 Comments : 07.30.07
#1
Dave Burke
07.31.2007 @ 11:55 PM