Confluence - Count number of 'Likes' on a page

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 3, 2013

Hi!

Is it possible to count the number of 'Likes' that a Confluence page received? If yes, how to do so?

Thanks in advance!

12 answers

1 accepted

4 votes
Answer accepted
Septa Cahyadiputra
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2013

Hi,

You could use MySQL plugin and retrieve the result of this SQL query to be rendered into the page:

SELECT C.TITLE, COUNT(l.CONTENTID) FROM likes l JOIN content c ON c.contentid = l.contentid group by l.CONTENTID;

Hope it helps.

Cheers,
Septa Cahyadiputra

Anna Rumawas April 21, 2013

hi septa,

if i want to use this plugin, what should i fill in the datasource part?

Septa Cahyadiputra
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 23, 2013

Hi Anna,

Regarding datasource connection, you could configure it by following our guide here. If you have any question regarding the plugin and how to set it up, feel free to contact the developer.

He will be happy to help you.

Cheers,
Septa Cahyadiputra

Jason D Smith July 8, 2021

SELECT c.TITLE, COUNT(l.CONTENTID) FROM LIKES l JOIN CONTENT c ON c.contentid = l.contentid group by l.CONTENTID order by count(l.CONTENTID) desc;

is a better version of this query that worked for me on mysql as it sorts it for you.

4 votes
CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2016

Here is a complete user macro which will return the number of likes for a given page (optional parameter of page id). If no page id is given, the current page will be used.

I needed this in order to show the number of likes in an auto-generated table, using the Reporting plugin.

## Macro name: pagelikes
## Visibility: Visible to all users in the Macro Browser
## Macro title: Page Likes
## Description: Returns the number of likes for a given page
## Categories: Confluence content
## Macro body processing: No macro body
##
## Developed by: Charles Hall
## Date created: 27.04.2016
## Installed by: Charles Hall
## @param PageID:title=Page ID|type=int|required=false|desc=ID of page to count likes
##
## Check for page id parameter, otherwise use default of current page
#if (!$paramPageID)
#set ($paramPageID=$content.id)
#end
##
#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $likeManager=$containerContext.getComponent('likeManager') )
##
#set ($targetPage = $action.page)
## Set up integer object to handle string conversion of page id
#set($Integer = 0)
##
#if ($pageManager)
#set ($targetPage = $pageManager.getPage($Integer.parseInt($paramPageID)))
#end
##
#set($count = $likeManager.countLikes($targetPage))
#if ($likeManager)
<span class="likecount">$count</span>
#end

Nicolas Casel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 26, 2016

Brilliant @CharlesH !

I would like to write a similar user macro to get the number of comments posted on a specific page. Where did you find the list of variables and methods like '$paramPageID', '$content.class.forName', etc... ?

CharlesH
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 26, 2016

Hi Nicolas,

$paramPageID is just a parameter for the macro, of my own creation. You can create your own, as required. Just follow the Atlassian guidelines on declaring these (described here).

For the methods which instantiate various Confluence objects, this forum is a pretty good source for that, as is this page: https://confluence.atlassian.com/display/DISC/Shared+User+Macros.

You can crib other peoples code pretty easily, but the full API is documented by Atlassian - see https://docs.atlassian.com/confluence/latest/com/atlassian/confluence/like/PageManager.html

This may be a case of "too much information" if you've never used it before. However, you're probably trying to create a macro which does some operation on a page, so I'd advise starting out with the pageManager class and seeing what methods it offers.

Cheers,

Charles

Nicolas Casel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2016

Thanks a lot Charles.

You make me checking out this so useful documentation:

https://confluence.atlassian.com/display/DISC/Home

I've never heard about this since I've been working with Confluence (18 months)...

However, your third link (PageManager) is broken.

Nicolas Casel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 2, 2016

Besides, I've managed to write a macro to get the number of comments for a specific page. I did a copy-paste of your code and simply replaced:

' likeManager ' by 'commentManager'

and

' countLikes ' by ' countComments '

Nicolas Casel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2016

Hi @Charles Shin ,

Since we upgraded to v 5.10.5, I get this error message when inserting the macro in a wiki page:

Error rendering macro 'nb-of-likes' : Error occurred rendering template content

Any idea of the reason?

RJ Confluence Admins August 17, 2017

This works great on the page, but I am trying to use it in a page properties macro.  When I run the page properties report, the result of that column is only "$count" instead of the actual count #.    Anyone have any thoughts on that?

3 votes
Chung Park Chan
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 4, 2013

Yes, you can do so. There will be number of Likes and who liked stated next to the Like button.

Septa Cahyadiputra
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2013

Just to add, on your Dashboard there should be a "Popular" tab where you could see the latest popular pages with its amount of "Like" and comment for each pages.

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2013

I'm more trying to acheive this:


Report the number of Likes of 10 differents pages in a table where people can see which are the ''Top 10'' pages right now.

Septa Cahyadiputra
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2013

You might want to try popular macro. That macro should be able to listed pages on your instance according to the amount of likes.

Hope it helps.

Cheers,
Septa Cahyadiputra

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 4, 2013

thanks, but that won't do the trick for what I need.

I want to report the number of Likes exactly for each page in a table, not only a list of popular pages according to the number of likes.

so basically, somethign liek this:

Page 1 ----> 37 Likes
Page 2 ----> 28 Likes
Page 3 ----> 22 Likes
Page 4 ----> 16 Likes
etc..

SanaS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 3, 2014

Hi Mr. Ubisoft, if you want to use a user macro instead of the SQL Plugin, it is possible and you can use additional parameters such as space and maximum/minimum number of likes/pages.

1 vote
SanaS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 26, 2014

Try this:

## get like manager...

#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))

#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))

#set($containerManager=$getInstanceMethod.invoke(null,null))

#set($containerContext=$containerManager.containerContext)

#set($likeManager=$containerContext.getComponent('likeManager'))

#set($count = $likeManager.countLikes($action.page))

#if ($likeManager)

<div class="likecount">$count</div>

#end

Vijay Krishna B
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2014

@Sana
Where we have to add the above snippet.

SanaS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 3, 2014

You add this to a user macro that you can create from the Administration Screen.

1 vote
Michael Orthey March 11, 2014

Hi, you can't access it (likeManger) from a macro. You have to write a confluence plugin in Java. At least that is my understanding. Here is a list of objects accessible from velocity.

1 vote
Michael Orthey March 5, 2014

I wrote a prototype plugin for this. If you are interested have a look.

0 votes
Vijay Patil January 22, 2017

Hi, I tried to modify the code to show unresolved comments but it fails!

Any help will be greatly appreciated.

## Macro name: pagelikes
## Visibility: Visible to all users in the Macro Browser
## Macro title: Page Likes
## Description: Returns the number of likes for a given page
## Categories: Confluence content
## Macro body processing: No macro body
##
## Developed by: Charles Hall
## Date created: 27.04.2016
## Installed by: Charles Hall
## @param PageID:title=Page ID|type=int|required=false|desc=ID of page to count likes
##
## Check for page id parameter, otherwise use default of current page
#if (!$paramPageID)
#set ($paramPageID=$content.id)
#end
##
#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $likeManager=$containerContext.getComponent('likeManager') )
#set( $commentManager=$containerContext.getComponent('commentManager') )
##
#set ($targetPage = $action.page)
## Set up integer object to handle string conversion of page id
#set($Integer = 0)
##
#if ($pageManager)
#set ($targetPage = $pageManager.getPage($Integer.parseInt($paramPageID)))
#end
##
#set($count = $likeManager.countLikes($targetPage))
#if ($likeManager)
&lt;span class="likecount"&gt;$count&lt;/span&gt;
#end
#set($com_count = $commentManager.countComments($targetPage))
#if ($commentManager)
&lt;span class="commentcount"&gt;$com_count&lt;/span&gt;
#end
#set($unres_com_count = $commentManager.countUnresolvedComments($targetPage))
#if ($commentManager)
&lt;span class="unrescommentcount"&gt;$unres_com_count&lt;/span&gt;
#end
0 votes
SanaS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 24, 2014

Hi,

You put that in a user macro, and then you can display it on any page you like :)

0 votes
Michael Orthey March 11, 2014

Hi, you can't access it from a macro. You have to write a confluence plugin in Java. At least that is my understanding. Here is a list of objects accessilbe from velocity.

0 votes
Christian Capito July 22, 2013

How can I get an instance of the likeManager in my macro?

I have the line <td>${likeManager.getLikes($content)}</td>
And it's not returning anything.

Angel Arcos March 11, 2014

I'm also looking to use this LikeManager object in a macro, but cannot get it... Any help would be appreciated

0 votes
UdayK
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2013

we can get the no of likes as count in this way, instead of MYSQL Plugin.

In Your macro, itearate the PageList and call the below method.

likeManager.getLikes(pageObject);

It will give List<Like> Object. You can get the size from the list.

THis is the easy way of getting the Likes count.

0 votes
Anna Rumawas April 23, 2013

Hi Septa,

it's done now and implemented :)

Thanks a lot

Septa Cahyadiputra
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 23, 2013

Great to hear that :)

UdayK
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2013

Can you please share the code and implementation details. We are trying to get the same. Please do share the info about the above functionality

Hsu Yao Chang July 2, 2016

Dear partners, 

How can we show like count listed by space and by month? Thank you. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events