Confluence - Count number of 'Likes' on a page
Asked by Ubisoft:

Hi!

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

Thanks in advance!


Answers:
author: Septa Cahyadiputra
body: Hi,

You could use [MySQL plugin](https://marketplace.atlassian.com/plugins/org.swift.confluence.sql) 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

comments: author: Anna Rumawas
body: hi septa,

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


author: Septa Cahyadiputra
body: Hi Anna,

Regarding datasource connection, you could configure it by following our guide [here](https://confluence.atlassian.com/display/DOC/Configuring+a+MySQL+Datasource+in+Apache+Tomcat). 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


author: Jason D Smith
body: 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.


author: CharlesH
body: 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](http://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

comments: author: Nicolas Casel
body: 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... ?


author: CharlesH
body: 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](https://confluence.atlassian.com/doc/user-macro-template-syntax-223906130.html#UserMacroTemplateSyntax-parametersUsingparametersinyourusermacro)).

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


author: Nicolas Casel
body: 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.


author: Nicolas Casel
body: 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 '


author: Nicolas Casel
body: 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?


author: RJ Confluence Admins
body: 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?


author: Chung Park Chan
body: Yes, you can do so. There will be number of Likes and who liked stated next to the Like button.

comments: author: Septa Cahyadiputra
body: 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.


author: Ubisoft
body: I'm more trying to acheive this:

<br />

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


author: Septa Cahyadiputra
body: You might want to try [popular macro](https://confluence.atlassian.com/display/DOC/Popular+Labels+Macro). That macro should be able to listed pages on your instance according to the amount of likes.

Hope it helps.

Cheers,  
Septa Cahyadiputra


author: Ubisoft
body: 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..


author: SanaS
body: 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.


author: SanaS
body: 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

comments: author: Vijay Krishna B
body: @Sana  
Where we have to add the above snippet.


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


author: Michael Orthey
body: 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](https://developer.atlassian.com/display/CONFDEV/Confluence+Objects+Accessible+From+Velocity "Objects accessible from Velocity") of objects accessible from velocity.


author: Michael Orthey
body: I wrote a prototype plugin for this. If you are interested have a [look](https://github.com/ic30r/ConfluencePluginLikeCounter).


author: Vijay Patil
body: 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
```


author: SanaS
body: Hi,

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


author: Michael Orthey
body: 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](https://developer.atlassian.com/display/CONFDEV/Confluence+Objects+Accessible+From+Velocity "Objects accessible from Velocity") of objects accessilbe from velocity.


author: Christian Capito
body: 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.

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


author: UdayK
body: 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.


author: Anna Rumawas
body: Hi Septa,

it's done now and implemented :)

Thanks a lot

comments: author: Septa Cahyadiputra
body: Great to hear that :)


author: UdayK
body: 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


author: Hsu Yao Chang
body: Dear partners,

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


Tags: like