Widget for 'Most Viewed KB Articles in last 30 Days'

Ryan Maddock
Tera Contributor

I am looking to create a Widget on the Service Portal which will show the most viewed articles for the past 30 days. At the moment it shows articles from all-time, therefore some of these are outdated.

 

I have tried using the example provided on this forum, however I am having issues where it is either bringing back articles which are not the most viewed (if I run a report with the same conditions) or just no data at all.

 

(function() {
	data.articles = [];
	options.title = options.title || gs.getMessage("Most Viewed Articles");

	var z = new GlideAggregate('kb_use');

	z.addAggregate('COUNT', 'article');
	if (options.kb_category)
		z.addQuery("article.kb_category", options.kb_category);
	z.addQuery('sys_created_on', '>=', gs.daysAgo(30)); // Most views in the past 30 Days
	z.orderByDesc('article');
	z.setLimit(options.max_number || 5);
	z.query();
	while (z.next()) {
		if (!z.canRead())
			continue;

		var count = z.getAggregate('count', 'article');
		var a = {};
		a.short_description = z.article.short_description.toString();
		a.sys_view_count = count;
		a.sys_id = z.article.toString();
		a.published = z.article.published.toString();
		data.articles.push(a);
	}
})()

 

Is there a way to do this OOTB or if not, does any one have anything similar they have implemented previously?

3 REPLIES 3

sanket16
Giga Guru

Hi @Ryan Maddock ,

Did you tried encoded query?

 

Try using

z.addEncodedQuery('sys_created_on>=javascript:gs.beginningOfLast30Days()')

 

Thanks,

Sanket Mahajan

Asim2462
Tera Contributor

Hi Ryan,

 

Did you find a solution for this?

 

Thanks,

Asim

Hi @Asim2462 

 

I ended up moving away from this solution, and instead just used a simple list widget which is updated manually each month with some spotlighted articles that we want users to see.

 

If I remember correctly though, I did manage to get it working for myself, but then when it came to testing and I noticed that the the kb_use table which stores the required data can't be read by a user due to ACL's, so that was the reason I did not use the original idea and went for a manual version.

 

Thanks,

Ryan