Confluence - Count number of 'Likes' on a page

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

This widget could not be displayed.

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

hi septa,

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

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

This widget could not be displayed.

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

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.

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.

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

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..

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.

This widget could not be displayed.

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

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... ?

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

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.

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 '

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?

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?

This widget could not be displayed.

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

This widget could not be displayed.

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.

This widget could not be displayed.

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

@Sana
Where we have to add the above snippet.

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

This widget could not be displayed.

Hi Septa,

it's done now and implemented :)

Thanks a lot

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

Dear partners, 

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

This widget could not be displayed.

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.

This widget could not be displayed.

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.

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

This widget could not be displayed.

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.

This widget could not be displayed.

Hi,

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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

515 views 7 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you