Google Analytics - track one space
Asked by Victor Prasad:

Hello,

We use Google Analytics for our whole Confluence instance (just one .js tag putin the custom html)

If I want to track just one space. I went ot behaviour - all pages and did a search for the space key - but I think that just takes me to the main page. How do I track all visits to one space?

Thanks

Mr. V


Answers:
author: David at David Simpson Apps
body: Add a custom dimension which tracks the space key, then in Google Analytics you can simply filter on the space key to see the results for just that space.

See my blogpost - [Tracking Atlassian Confluence usage with Google Analytics](https://davidsimpson.me/2009/03/18/tracking-atlassian-confluence-usage-with-google-analytics/#update2014-08-14) - for how to do this, or copy the code below:

```
<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>??
```

comments: author: Victor Prasad
body: Wow! That is fantastic!

We do not use the Google Analytics Plugin, instead, we just added the tracking code to the Custom HTML.

Will this still work?

Substitute my GA code instead UA-XXXX-Y?

Thanks!


author: David at David Simpson Apps
body: Yes, that's correct :)


author: Victor Prasad
body: Great!

I don't have the Google Analytics Plugin - will I still be able to get this info easily?

For this part:

"You now have a Custom Dimension named **Confluence Space Key** which is available for use within the Google Analytics interface. Next, you'll need to populate this custom dimension with some data. "

I see this:

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

dimensionValue is the space key?

And where does this code go? In my custom HTML?

Sorry for the noob questions.

Thanks,

<br />


author: David at David Simpson Apps
body: I've replied to your answer here:

<https://gist.github.com/dvdsmpsn/3397967c5e566bbb59b91b9db8901e3f>


author: web service
body: Hi,

There is a new and recommended Global Site Tag (gtag.js) tracking code as follows.  
What's the respective way of adding Space tracking to a custom dimension? Thank you

```

<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXX"></script>
<script>
 window.dataLayer = window.dataLayer || [];
 function gtag(){dataLayer.push(arguments)};
 gtag('js', new Date());

gtag('config', 'UA-XXXX');
</script>


?
```


author: Susan Ostreicher
body: [@web service](/t5/user/viewprofilepage/user-id/1151548), I just came across this article. Does this help answer your question?

[Custom dimensions and metrics with gtag.js](https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets)


author: Susan Ostreicher
body: ```
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-Y"></script>
<script>
 window.dataLayer = window.dataLayer || [];
 function gtag(){dataLayer.push(arguments);}
 gtag('js', new Date());

 

// Maps 'dimension1' to 'Confluence_Space_Key'.
gtag('config', 'UA-XXXXXXXXX-Y', {
 'custom_map': {'dimension1': 'Confluence_Space_Key'}
});

// Sends an event that passes 'Confluence_Space_Key' as a parameter.
if (typeof AJS.params.spaceKey === 'string') {
gtag('event', 'pageview', {'Confluence_Space_Key': AJS.params.spaceKey});
}

</script>
```

I'm working through the same issue and trying to piece together the code, though without really understanding what I'm looking at.

[@David at David Simpson Apps](/t5/user/viewprofilepage/user-id/781462), not sure if you can confirm if the above would work for gtag.js, but your blog posts have been so helpful and I'd love to someone more knowledgeable than I am take a look at this!


author: Helen Gosper
body: I gather this won't work for Confluence cloud - anyone?

Unless there is somewhere else to put the code, 'custom html' not seeming to be available in Admin?


author: David at David Simpson Apps
body: Helen, you can use [Google Analytics in Confluence](https://marketplace.atlassian.com/apps/1216936/google-analytics-in-confluence?hosting=cloud&tab=overview) on Cloud, then use the drilldown report in Google Analytics to look at analytics on one space.


author: Nicole
body: [@Helen Gosper](/t5/user/viewprofilepage/user-id/544620)

Just a note, Atlassian limits the data (location and page views) we can see in Google Analytics about public (anonymous, unauthenticated) users. My thought is that they are pushing Confluence users to purchase [Premium](https://www.atlassian.com/software/confluence/premium) to get [Analytics](https://confluence.atlassian.com/confcloud/analytics-972332388.html) which includes page views and authenticated user activity.


author: Adriane Hunt
body: Hi David. We're not quite ready to move to Cloud. Can you suggest custom HTML for G4 in Confluence Server?  

Regards, Adriane


author: Gonchik Tsymzhitov
body: [@Adriane Hunt](/t5/user/viewprofilepage/user-id/2686464) did you find some solution?


author: Adriane Hunt
body: We used the same gtag.js script and just replaced the UA property ID with the GA4 property ID.