Number Of Active Users Per Day, Per Week And Per Month

Last modified by Vincent Massol on 2021/03/18 11:28

cogStatistics about user activity over time
TypeSnippet
Category
Developed by

Marius Dumitru Florea

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Shows how the number of active users changes over time, per day or per week. By active user we mean a user that has at least viewed a page. The guest user is included, as long as someone viewed a page without logging in. Statistics must be enabled and this code must be saved with programming rights because the search is done using protected API.

{{velocity}}
= Per Day =

##
## Define the period.
##
#set($days = 7)
#set($period = $xwiki.criteriaService.periodFactory.createSinceDaysPeriod($days))
#set($startDate = $util.getDate($xwiki.criteriaService.periodFactory.createDayPeriod($period.start).start))
#set($endDate = $util.getDate($xwiki.criteriaService.periodFactory.createDayPeriod($period.end).end))
##
## Retrieve the stats.
##
#set($hql = "select year(vs.startDate), month(vs.startDate), day(vs.startDate), count(distinct vs.name) from VisitStats vs where ? <= vs.startDate and vs.endDate < ? group by year(vs.startDate), month(vs.startDate), day(vs.startDate) order by year(vs.startDate), month(vs.startDate), day(vs.startDate)")
#set($stats = $context.xWiki.store.search($hql, $days, 0, [$startDate, $endDate], $context.context))
##
## Display the stats.
##
|=Year|=Month|=Day|=Active Users
#foreach($stat in $stats)
#foreach($part in $stat)|(% style="text-align:right;" %)$part#end

#end
{{/velocity}}

{{velocity}}
= Per Week =

##
## Define the period.
##
#set($weeks = 4)
#set($period = $xwiki.criteriaService.periodFactory.createSinceWeeksPeriod($weeks))
#set($startDate = $util.getDate($xwiki.criteriaService.periodFactory.createWeekPeriod($period.start).start))
#set($endDate = $util.getDate($xwiki.criteriaService.periodFactory.createWeekPeriod($period.end).end))
##
## Retrieve the stats.
##
#set($hql = "select year(vs.startDate), week(vs.startDate), count(distinct vs.name) from VisitStats vs where ? <= vs.startDate and vs.endDate < ? group by year(vs.startDate), week(vs.startDate) order by year(vs.startDate), week(vs.startDate)")
#set($stats = $context.xWiki.store.search($hql, $weeks, 0, [$startDate, $endDate], $context.context))
##
## Display the stats.
##
|=Year|=Week|=Active Users
#foreach($stat in $stats)
#foreach($part in $stat)|(% style="text-align:right;" %)$part#end

#end
{{/velocity}}

{{velocity}}
= Per Month =

##
## Define the period.
##
#set($months = 12)
#set($period = $xwiki.criteriaService.periodFactory.createSinceMonthsPeriod($months))
#set($startDate = $util.getDate($xwiki.criteriaService.periodFactory.createMonthPeriod($period.start).start))
#set($endDate = $util.getDate($xwiki.criteriaService.periodFactory.createMonthPeriod($period.end).end))
##
## Retrieve the stats.
##
#set($hql = "select year(vs.startDate), month(vs.startDate), count(distinct vs.name) from VisitStats vs where ? <= vs.startDate and vs.endDate < ? group by year(vs.startDate), month(vs.startDate) order by year(vs.startDate), month(vs.startDate)")
#set($stats = $context.xWiki.store.search($hql, $months, 0, [$startDate, $endDate], $context.context))
##
## Display the stats.
##
|=Year|=Month|=Active Users
#foreach($stat in $stats)
#foreach($part in $stat)|(% style="text-align:right;" %)$part#end

#end
{{/velocity}}
Tags: statistics
     

Get Connected