Access page views from a groovy script
Asked by Christophe-Marie Duquesne:

Hi,  

I would like to add a label to all the pages which did not get any view in the past year. I intend to use [scriptrunner](https://docs.adaptavist.com/sr4c/6.57.0/features/script-console) to automate that process, which lets me access Confluence through the groovy API.  

I have been digging in the [javadoc](https://docs.atlassian.com/ConfluenceServer/javadoc/latest) to find out how to access the page views for a given page, but I have not been successful.  

It seems, however, that [this data would be available through the rest API](https://developer.atlassian.com/cloud/confluence/rest/api-group-analytics/#api-wiki-rest-api-analytics-content-contentid-views-get), but I do not know how to invoke it from groovy (and I am afraid of the potential performance issues).  

How can I achieve my goal? Is there a way to access the number of views through the groovy API? Is there a different, simpler way way to label those pages automatically?  

Thank you!


Answers:
author: WW
body: Unfortunately, view page data is not captured by Confluence itself, let alone is it available through the REST API.

You'd have to start capturing page views.

You can turn on [Analytics](https://confluence.atlassian.com/doc/analytics-1044780333.html), get another plugin, or do some fancy coding with database table updates.

If you've got Confluence capturing the page view data somehow, then:

Analytics has [this Java API](https://docs.atlassian.com/ConfluenceServer/javadoc/6.6.10/com/atlassian/confluence/it/analytics/package-summary.html). You could start with that. Not sure if it'll give you all you need.

If you go to the Developer Tools Console in your web browser (hit F12) and visit a space's Activity page, you'll see a whole lot of mess. Perhaps you can interpret it, but good luck with that. There doesn't seem to be an easy, obvious API or code call there, other than this:

```
<myconfluence>/spaces/usage/report.action?key=ABC&period=week
```

which returns HTML and not even the data you want.

If you go the plugin route, it might have a REST API.

If you're doing fancy coding, then hats off to you.

One other thing you can try is the [Confluence REST API Browser](https://marketplace.atlassian.com/apps/1211542/atlassian-rest-api-browser?tab=overview&hosting=server) and see if you see anything analytic in there. Unfortunately, the version never seems to be the right fit for our instance, so I can't check for you. If you see [CONFSERVER-76071](https://jira.atlassian.com/browse/CONFSERVER-76071), the **REST API Browser does show the analytics REST API calls.**

PS That REST API you linked to is for Cloud.

One more thing (promise), if you're a digger, you may want to explore this Java API: [com.atlassian.confluence.api.impl.service.event.mau](https://docs.atlassian.com/ConfluenceServer/javadoc/7.18.3/). Although the Impl one is, I *think*, only for Atlassian internal use, and it's a stretch at best, but that's what captures user activity and sends it to analytics.

comments: author: Christophe-Marie Duquesne
body: Thank you for the extensive answer. It is a bit disappointing to discover that this data is not exposed to the api user... I will report here if I find a solution.


author: Christophe-Marie Duquesne
body: After digging down the rabbit hole, I have an idea that could work: Leverage scriptrunner to write my own page tracking.  

I know it sounds crazy, but with a [Custom Event Listener](https://docs.adaptavist.com/sr4c/latest/features/event-listeners/custom-event-listener), it does not look too complicated: basically, one can listen for the [PageViewEvent](https://docs.atlassian.com/ConfluenceServer/javadoc/7.12.3/com/atlassian/confluence/event/events/content/page/PageViewEvent.html). The modified [Page](https://docs.atlassian.com/ConfluenceServer/javadoc/7.12.3/com/atlassian/confluence/pages/Page.html) should be available through this event, and pages happen to be instances of [ContentEntityObject](https://docs.atlassian.com/ConfluenceServer/javadoc/7.12.3/com/atlassian/confluence/core/ContentEntityObject.html), which Confluence lets you [associate with arbitrary content](https://developer.atlassian.com/server/confluence/how-do-i-associate-my-own-properties-with-a-contententityobject/). So I should be able to store the date of the latest view for a page directly in Confluence, the same way a regular plugin developer does.  

Then I am unsure how to proceed yet:

* A possible way would be to periodically run a script that [lists all pages](https://docs.adaptavist.com/sr4c/6.57.0/features/script-console), and for each of them, retrieves the date of the last page view and labels those for which this date is too old. While it would work, I am not in love with the potential performance problem generated by looping over **all** the pages of our Confluence instance in a single batch, every time the script runs.
* Another way could be to do this in 2 steps: first write a [custom search extractor](https://docs.adaptavist.com/sr4c/6.57.0/features/search-extractors/custom-search-extractor) which adds an index for such pages, then use the builtin [CQL escalation service](https://docs.adaptavist.com/sr4c/6.57.0/features/jobs/built-in-jobs/cql-escalation-services) to label those pages. This might be the better solution, since I suspect that Confluence is going to be more clever than me in the way it schedules its indexation.

I will post here if I implement anything.


Tags: api

confluence-server

groovy

java

rest

script

scriptrunner

server