Skip to content

Instantly share code, notes, and snippets.

@dvdsmpsn
Last active September 26, 2016 22:43
Show Gist options
  • Save dvdsmpsn/8125475489fef01d01c66c4a76ce4553 to your computer and use it in GitHub Desktop.
Save dvdsmpsn/8125475489fef01d01c66c4a76ce4553 to your computer and use it in GitHub Desktop.
Google Analytics Setup for Atlassian Cloud Add-Ons

Google Analytics Setup for Atlassian Cloud Add-Ons

Add analytics.js to your add-on pages. Update the tracking code and Add-On / Macro Name and you should be good to go.

Custom Dimensions

You'll need to add some custom dimensions in Google Analytics. Use the names in comments in the section of analytics.js below // Add into custom dimensions

Personally Identifiable Information (PII)

Note that you must not store PII such as user_id which is sent over to Google Analytics in every request as a request parameter. Note userKey is not PII, so can be used.

To remove user_id, in Google Analytics > Admin > Reporting View Settings, add user_id in Exclude URL Query Parameters

// a helper function
var getParameter = function(param) {
var codedParams = (new RegExp(param + '=([^&]+)')).exec(window.location.search);
if (codedParams != null) {
return decodeURIComponent(codedParams[1]);
}
return '';
};
// Measure with Google Analytics
var analytics = function() {
// Vanilla Google Analytics script
(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');
// Collect the data
var contextPath = getParameter('xdm_e') + getParameter('cp');
var userKey = getParameter("user_key");
var timezone = getParameter("tz");
var loc = getParameter("loc");
var license = getParameter("lic");
var cv = getParameter("cv");
// Send the data to Google Analytics
ga('create', 'UA-XXXXX-YY', 'auto');
// Add into custom dimensions
ga('set', 'dimension1', contextPath); // Cloud Server Base URL
ga('set', 'dimension2', userKey); // User Key
ga('set', 'dimension3', timezone); // Timezone
ga('set', 'dimension4', loc); // Location
ga('set', 'dimension5', license); // License
ga('set', 'dimension6', cv); // CV
ga('set', 'dimension7', 'Add-On / Macro Name Here'); // Add-On Name
ga('send', 'pageview');
};
analytics();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment