Analytics reporting with Google Apps Script at the UK Cabinet Office


Guest author Ashraf Chohan works at the Government Digital Service (GDS), part of the UK Cabinet Office. Originally posted on the Google Apps Developer Blog by Arun Nagarajan.

Recently, when we were preparing the launch of GOV.UK, my team was tasked with creating a series of high-level metrics reports which could be quickly compiled and presented to managers without technical or analytical backgrounds. These reports would be sent daily to ministers and senior civil servants of several government departments, with the data customised for each department.

We decided to use Adobe InDesign to manage the visual appearance of the reports. InDesign’s data-merge functionality, which can automatically import external data into the layout, made it easy to create custom departmental reports. The challenge was to automate the data collection using the Google Analytics API, then organize the data in an appropriate format for InDesign’s importer.

In a previous post on this blog, Nick Mihailovski introduced a tool which allows automation of Google Analytics Reporting using Google Apps Script. This seemed an ideal solution because the team only had basic developer knowledge, much of the data we needed was not accessible from the Google Analytics UI, and some of the data required specific formatting prior to being exported.

We started by building the core reports in a Google spreadsheet that pulls in all of the required raw data. Because we wanted to create daily reports, the start and end dates for our queries referenced a cell which defaulted to yesterday’s date [=(TODAY())-1].


These queries were dynamically fed into the Google Analytics API through Apps Script:
// All variables read from each of the “query” cells  
var optArgs = {
'dimensions': dimensions,
'sort': sort
'segment': segment
'filters': filters,
'start-index': '1',
'max-results': '250'
};

// Make a request to the API.
var results = Analytics.Data.Ga.get(
tableId
, // Table id (format ga:xxxxxx).
startDate
, // Start-date (format yyyy-MM-dd).
endDate
, // End-date (format yyyy-MM-dd).
endDate
, // Comma seperated list of metrics.
optArgs
);
Next, we created additional worksheets that referenced the raw data so that we could apply the first stage of formatting. This is where storing the data in a spreadsheet really helps, as data formatting is not really possible in the Google Analytics UI.

For example, the final report had a 47-character limit for page titles, so we restricted the cells in the spreadsheet to 44 characters and automatically truncated long URLs by appending “...”.


Once the initial formatting was complete, we used formulas to copy the data into a summary sheet specially laid out so it could be exported as a CSV file that merges seamlessly into InDesign.


Below is an example of how a report looks on publication. Nearly everything on the page was extracted from the API tool, including the department name and the day number. Because most of the data was automated, it required minimal effort on our part to assemble these reports each morning.


We discovered that an added bonus of pulling data into a Google spreadsheet was that it also allowed us to publish the data to a Google site. This helped us display data to stakeholders without adding lots of users to our Google Analytics account.


The tools let us present Google Analytics data in deeper, more creative ways. That’s really important as we share information with more and more non-technical people, whether they’re inside GDS or beyond.

Posted by John Milinovich, Google Analytics team

Post a Comment