I've previously written a number of posts about Confluence and Google Analytics (GA), including how to set up GA on Confluence and pointers for GA reports you should consider. Lastly, I wrote the Confluence Google Analytics Plugin (CGAP) which provides space specific GA reports from within Confluence.

However, I've not been entirely happy because there wasn't a method to allow complete tracking of all space specific Confluence pages using CGAP or the reports that I've previously described.

This has been documented elsewhere, but briefly, the problem is with...

  • pages with punctuation in the title e.g. This page is bad for Analytics, isn't it? (hover on the link to see the URI)
  • short URLs e.g. /x/2Bq9Ag
  • space administration e.g. /spaces/spaceadmin.action?key=spaceKey
  • page edits e.g./pages/editpage.action?pageId=XXXX
  • page creation e.g. /pages/createpage.action?spaceKey=spaceKey&fromPageId=XXXX
  • add/view attachments e.g. /pages/viewpageattachments.action?pageId=XXXX
  • page history e.g. /pages/viewpreviousversions.action?pageId=XXXX
  • view wiki markup e.g. /pages/viewpagesrc.action?pageId=XXXX

...and so on. I think you get the picture.

For any page where there is a real interaction with the wiki — such as page creation or page edits, attachment viewing or uploading — it is very difficult associate that interaction to the space where the interaction took place. Damn. That's the main point really. Wikis are for collaboration — editing and creating content — not just passively reading.

It really bugs me that the URIs in Confluence aren't built up so that they always provide a context of where you are. But what can we do?

This post suggests 2 possible solutions to this problem:

  1. A Javascript only method
  2. A Javascript and velocity templating method

Both have their drawbacks, but they do let you get a little closer to understanding what your users are doing within your Confluence spaces. Try them out and please report back on your preference or your own alternative solution.

The Javascript only method

In this method, we track a custom variable in GA where possible, so that we can attach a page view to the corresponding Confluence space.

Atlassian handily provide the following (example) meta tag on every page that has an associated space:

<meta id="confluence-space-key" name="confluence-space-key" content="~cczdas">

We can check for that meta tag and pass it into a custom variable in Google Analytics.

In Confluence, go to Dashboard | Administration | Look and Feel | Custom HTML

Click on the “Edit” button and add this javascript snippet (with your tracker code) to the “At end of the BODY” section:


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">

window.onload = function(){
try
{
var pageTracker = _gat._getTracker("UA-XXXXX-XX");

// Add a page-level custom variable to record the space-key
if (typeof jQuery('meta[name=confluence-space-key]').attr("content") == 'string')
{
pageTracker._setCustomVar(
1, // This custom var is set to slot #1 of 5
'confluence-space-key', // The name acts as a kind of category for the user activity
jQuery('meta[name=confluence-space-key]').attr("content"), // This value of the custom variable
3 // Sets the scope to page-level
);
}
}
catch (err) {}
}

</script>

This means for each page, we've added a custom variable which can later be check against. We can now therefore easily check which page belongs to which space.

In Google Analytics, the custom variables report looks like this:

confluence-ga-custom-variable-report

This doesn't really tell us much, other than the number of page views for a space and a little more. To get at the real power of custom variables, we need to create a custom segment for each space. This will allow us to see all the interactions for the space.

Here's a short video guide to creating custom segments for Confluence spaces:

Pros

  • It's really simple. Anyone with Confluence administration rights can cut and paste this code.
  • It's pure. This should not be affected by upgrades to the platform.
  • It's easily to support. Lots of people know Javascript.

Cons

  • You're moving into slightly advanced Google Analytics territory here.
  • You'll need to set up a separate custom advanced segment for each space. This may be somewhat dull.

I really like this approach, but haven't managed to successfully add it into CGAP yet whilst maintaining access to historical data.

Javascript and velocity templating method

In this method, we trick Google Analytics into reporting the URL in a the format that works with content drill-down e.g.

  • /pages/viewpage.action?pageId=XXXX becomes /display/spaceKey/Page+Title
  • /x/2Bq9Ag (short URL) becomes /display/spaceKey/Page+Title
  • /pages/editpage.action?pageId=XXX becomes /display/spaceKey/Page+Title/edit
  • /pages/viewpageattachments.action?pageId=XXX becomes /display/spaceKey/Page+Title/view-attachments

We do this by adding a custom meta tag named "analytics.url.fix" from which we read the "fixed" URL using a little Javascript.

Edits

page.vmd in your theme:


## Add Google Analytics support for page edits, attachment views etc.
## Called by $!sitemeshPage.getProperty("page.googleAnalyticsUrlFix") in main.vmd
<content tag="googleAnalyticsUrlFix">
#if ($mode == "view")
<meta name="analytics.url.fix" desc="$req.contextPath/display/$page.spaceKey/$page.title" />
#else
<meta name="analytics.url.fix" desc="$req.contextPath/display/$page.spaceKey/$page.title/$mode" />
#end
</content>

main.vmd in your theme:
Add after the title node in /html/head/title


## Call the googleAnalyticsUrlFix content tag in page.vmd, fail silently:
$!sitemeshPage.getProperty("page.googleAnalyticsUrlFix")

In Confluence, go to Dashboard | Administration | Look and Feel | Custom HTML

Click on the “Edit” button and add this javascript snippet (with your tracker code) to the “At end of the BODY” section:


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
window.onload = function(){
try
{
var pageTracker = _gat._getTracker("UA-XXXXXX-X");

// Test for the meta[name=analytics.url.fix] tag & fix the url
if (typeof jQuery('meta[name=analytics.url.fix]').attr("content") == 'string')
{
// console.debug('corrected pageview');
var pageUri = jQuery('meta[name=analytics.url.fix]').attr("content").replace(/s+/gi, '+');
pageTracker._trackPageview(pageUri);
}
else
{
// console.debug('default pageview');
pageTracker._trackPageview();
}
}
catch (err) {}
}
</script>

Pros

  • You don't have to do anything on the Google Analytics side at all
  • This is just great for the content drilldown screen in Google Analytics.

    • It means that pages with punctuation in the title can now be seen within their correct space.
    • The space owners can now be sure of the stats for viewing and editing pages within their space.
  • This is great for the Google Analytics plugin for Confluence (CGAP)

    • All the page edits should be shown in the analytics report
    • It's easily supportable. Lots of people know Javascript

Cons

  • It's a bit messy.
  • You'll have to apply this to every theme you support.
  • Editing velocity templates isn't everyone's cup of tea.
  • I've no idea what will happen when you upgrade Confluence.
  • It niggles me that we're sending incorrect (though easily readable) pageview data to Google Analytics.
  • It still doesn't tie everything to the space, just the page interactions

To my mind, this is less elegant, but provides pretty much instant results.

Which one to choose?

I really like the simplicity of the first solution. Just a little bit of Javascript to read a meta tag that's already there. It's likely to easily survive an upgrade. But I'm disappointed that as yet, I can't get it to work nicely with CGAP.

For ongoing support reasons, I'd always say don't mess with the platform. Don't do anything which makes it more difficult to upgrade to the latest and greatest version. If you agree, then steer well clear of the latter option. If however, you absolutely have to start complete space specific tracking right now, no matter what, then the latter is then choice for you.