Skip to content

Instantly share code, notes, and snippets.

@dvdsmpsn
Last active May 16, 2022 16:40
Show Gist options
  • Save dvdsmpsn/3397967c5e566bbb59b91b9db8901e3f to your computer and use it in GitHub Desktop.
Save dvdsmpsn/3397967c5e566bbb59b91b9db8901e3f to your computer and use it in GitHub Desktop.

In reply to https://community.atlassian.com/t5/Confluence-questions/Google-Analytics-track-one-space/qaq-p/595032

I get the following error on posting the reply:

Error

Your reply was not accepted. Please check for inappropriate language or spam.

Here's the answer I posted:


Hi @Victor Prasad,

In Google Analytics Admin interface, You'll have to create a custom dimension called "Confluence Space Key".

In Confluence Admin > Look and Feel > Custom HTML > At the END of the HEAD, you should paste this:

<script>
  // Standard Google Universal Analytics code
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  AJS.toInit(function(){
    ga('create', 'UA-XXXX-Y', 'auto');
    // Add a page-level custom variable to record the space-key
    if (typeof AJS.params.spaceKey === 'string') {
      ga('set', 'dimension1', AJS.params.spaceKey); // Set a `spaceKey` dimension at page level
    }
    ga('send', 'pageview');
  });
</script>

The bit that says:

    if (typeof AJS.params.spaceKey === 'string') {
      ga('set', 'dimension1', AJS.params.spaceKey); // Set a `spaceKey` dimension at page level
    }

...is equivalent to this in your comment:

var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension1', dimensionValue);

I hope this helps. Btw: If you need help on adding custom dimensions in Google Analytics, some of the video on this page will be relevant.

@dvdsmpsn
Copy link
Author

This answer is for Confluence Server/ Data Center only.

You can use Google Analytics in Confluence on Confluence Cloud, then use the drilldown report in Google Analytics to look at analytics on one space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment