Skip to content

Instantly share code, notes, and snippets.

@dvdsmpsn
Last active May 16, 2022 16:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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.

@MrVP
Copy link

MrVP commented Jul 19, 2017

Great, so that is the code I have in my Custom HTML section - except for my GA code - it is verbatim.
Once that is in there - where on the analytics screen do I find these spaces metrics? Is it something I enter, or is it broken down for me?
Thanks,
Victor

@dvdsmpsn
Copy link
Author

dvdsmpsn commented Aug 30, 2017

@MrVP
Sorry for the delay. I didn't notice that you'd commented here.

I think I missed out creating custom dimensions. I did a little video here about how to create the custom dimensions in Google Analytics.

Having done that, you can easily filter by the new custom dimension Confluence Space Key in the Googhle Analytics user interface.

@JewelyaZ
Copy link

JewelyaZ commented Feb 1, 2019

@dvdsmpsn thanks for the info you've provided so far. I'm not a coder, though I frequently dabble pretty deeply into things resembling code.
If I have a GA property code that is entirely for my Confluence space, do I need to define the custom dimension at all in GA?
Thanks in advance

@EijaPoh
Copy link

EijaPoh commented May 16, 2022

This is a somewhat old thread but I would appreciate insight. I am new to Confluence and tried to add GA tracking code to the site, but there seems to be no custom html option available anymore. So I created a draft page for keeping my GA tracking code and pasted it to the footer of Confluence site as adviced somewhere. I did create custom dimension in for Confluence data in GA as well, but the tracking seems to be completely off and catching only a few visits. What can I do to improve this?

@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