Most Viewed Articles

Last modified by André Lung on 2023/10/01 16:13

chart_lineStatistics about the most viewed articles for a period of time
TypeSnippet
Category
Developed by

Marius Dumitru Florea

Rating
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Displays the list of most viewed articles for the past N days. You can easily configure the space where to look for articles, the article class, the number of days and how many articles to show. Statistics have to be enabled and this code must be saved with programming rights to work because the search is done using protected API.

{{velocity}}
#set($days = 7)
#if("$!request.days" != "")
#set($rDays = $util.parseInt($request.days))
#if($rDays > 0)
#set($days = $rDays)
#end
#end
#set($top = 100)
#if("$!request.top" != "")
#set($rTop = $util.parseInt($request.top))
#if($rTop > 0)
#set($top = $rTop)
#end
#end
{{html}}
<form method="post">
<label for="days">Days:</label>
<input type="text" id="days" name="days" value="$days" size="2"/>
<label for="top">Top:</label>
<input type="text" name="top" value="$top" size="3"/>
<input type="submit" value="Update"/>
</form>
{{/html}}
##
## Define the scope.
##
#set($space = "Blog")
#set($className = "Blog.BlogPostClass")
#set($scope = "select doc.fullName from XWikiDocument doc, BaseObject obj where doc.fullName = obj.name and doc.space = ? and obj.className = ?")
##
## Define the period.
##
#set($period = $xwiki.criteriaService.periodFactory.createSinceDaysPeriod($days))
##
## Retrieve the stats.
##
#set($action = "view")
#set($hql = "select ds.name, sum(ds.pageViews) from DocumentStats ds where ds.name in (${scope}) and ds.action = ? and ? <= ds.period and ds.period <= ? group by ds.name order by sum(ds.pageViews) desc")
#set($stats = $context.xWiki.store.search($hql, $top, 0, [$space, $className, $action, $period.startCode, $period.endCode], $context.context))
##
## Display the stats.
##
== Top $top most viewed articles in space $space for the past $days days ==

|=No.|=Page|=View Count
#foreach($stat in $stats)
|(% style="text-align:right;" %)$velocityCount|[[$stat.get(0)]]|(% style="text-align:right;" %)$stat.get(1)
#end
{{/velocity}}

Get Connected