You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
What I want:
A simple anonymous "Page Hit Counter" for each confluence page, displayed right to the breadcrumbs.
What I achived:
An EventListener (PageViewEvent) counts pageviews and stores them in a custom variable "HitCount" for every page:
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.ContentPropertyManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.event.events.content.page.PageEvent
import org.apache.log4j.*
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.HitCounter")
log.setLevel(Level.INFO)
def contentPropertyManager = ComponentLocator.getComponent(ContentPropertyManager)
def Pevent = event as PageEvent
def page = Pevent.getPage()
def HitCount = null
//Get Hit Counter from Page. If not availiable, set it to 1
if (!contentPropertyManager.getStringProperty(page, "HitCount")){
contentPropertyManager.setStringProperty(page, "HitCount", "0")
log.info "New HitCounter set for page " + page.title
}
//Raise HitCount
HitCount = contentPropertyManager.getStringProperty(page, "HitCount").toInteger() + 1
log.info "HitCounter raised for page " + page.title +" to " + HitCount
//Save HitCount
contentPropertyManager.setStringProperty(page, "HitCount", HitCount.toString())
log.info "HitCounter saved for page " + page.title
The EventListener works (no failures) and I managed to display the results in a custom macro:
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.ContentPropertyManager
import com.atlassian.sal.api.component.ComponentLocator
def contentPropertyManager = ComponentLocator.getComponent(ContentPropertyManager)
def entity = context.getEntity()
if (entity instanceof Page) {
def page = entity as Page
if (!contentPropertyManager.getStringProperty(page, "HitCount")){
"1"
}
else {
contentPropertyManager.getStringProperty(page, "HitCount")
}
}
What I need:
I want the HitCounter to be displayed in a web panel that is shown at atl.page.metadata.banner. When only writing a static string, the code works. But with the call of getHitCount(), the writer writes nothing. I'm new to java/groovy so please be kind ;)
The code of the webpanel:
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.ContentPropertyManager
import com.atlassian.sal.api.component.ComponentLocator
writer.write("<div style=\"padding: 5px 5px 5px 0;\">Page Views: " + getHitCount() + "</div>")
String getHitCount(){
def contentPropertyManager = ComponentLocator.getComponent(ContentPropertyManager)
def page = context.page as Page
if (!contentPropertyManager.getStringProperty(page, "HitCount")){
return "1"
}
else {
return contentPropertyManager.getStringProperty(page, "HitCount")
}
}
I think it's because there is no "page" object in that context.
When I create a panel in that same context
writer.write("""<div style="padding: 5px 5px 5px 0;">Test Panel: ${context.keySet()}</div>""")
I see just "Dashboard Test Panel: [action]"
So the only object inside the atl.page.metadata.banner is the "action" object
Byt outputing context.action.getClass() I was able to find my way to
Where I found that we can get the current page.
writer.write("""<div style="padding: 5px 5px 5px 0;">Test Panel: ${context.page.id}</div>""")
This returned the page id.
So I think if you just change
def page = context.page as Page
to
def page = context.action.page as Page
It might work
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.