Running Semi-Automated Tasks on Google Analytics

Tired of manually clicking through the Google Analytics interface to retrieve data? Want to take advantage of the Google Management API, but the functions you need just aren’t available? Well then, let me share with you my temporary solution and long-term vision for creating an easy interface for such tasks.

As a web analytics implementation consultant at Cardinal Path, I work extensively with Google Analytics and Google Tag Manager. The standard approach that I often take before attempting any form of tagging or code changes is to take a peek at the client’s existing configuration and data. This initial audit checks for data integrity, overlooked resources, and implementation practices.

My role offers me the opportunity to work with enterprise-level clients with hundreds of views. This means that I may end up manually clicking tens of thousands of times with a high likelihood for repetition when making a global change whether for maintenance or to resolve an issue.

As a web programmer, I refuse to let the web take advantage of me and thus started my hunt for an automatic and robust solution for repetitive tasks within Google Analytics.

A Sample use case: Adding Annotations to multiple views

A recent client audit revealed that the Annotations feature isn’t being used consistently, if at all, during certain time periods. This is a powerful Google Analytics feature that allows anyone who is analyzing data within the reports to realize the reasons for possible spikes in data.

The possible overlook of this feature does not come as a total surprise for this particular client due to the sheer amount of views that are being maintained. As result, we would like to offer insights on how to carry out a possible solution as well as how to approach similar tasks in the future.

Using Google Chrome Console

In the aforementioned use case, the client needs a way to start annotating, and often, the same annotations are required for multiple views / properties. A quick way to achieve this is to automate a series of manual tasks in succession in the browser console. In the case of Chrome, I rely on my handy dandy chrome inspector (shortcut: CTRL + SHIFT + J on Windows, or CMD + OPT + J on Mac).

Before you drop the code provided below into your Chrome console, ensure you are inside the Google Analytics reporting interface and find the starting view by searching for its property in the dropdown menu at the top (as highlighted in the green box in the screenshot below). This step ensures the code runs through the list of views following and including the selected view for that property (the code stops running once it finds a new property).

Google Analytics Semi Automated Tasks

If I select the view MyLabel – David Xue -liftyourspirit in the screenshot above, the code will affect all the views down to the last view in this property, MyLabel – David Xue – Youtube.

Running the semi-automated Google Analytics tasks

You may run the code after pasting it into the console tab by pressing ‘enter.’ Caution: this will add annotations to your views, so be sure you are in a test property with test views, or just delete them manually afterwards (better yet, modify this current code for deletion across the property).

count = 0;
start = setInterval(function() {
var current_view = $('li[aria-selected=true]');
setTimeout(function() {
if ([0] == undefined) {
console.log('Swept ' + count + ' views');
}, 2000);

setTimeout(function() {
}, 6000);
}, 8000);

getNextViewInReporting = function() {
var current_view = $('li[aria-selected=true]');

setAnnotation = function() {
var view = $('.ID-accounts-summary-1').text()
var date = 'Sep 13, 2015' //Enter the date
var annotation = "A test annotation" //Enter the annotation
var visibility = "shared" //Default is shared, other is private
$('#AnnotationDrawer_wrapper').css('display', '');
setTimeout(function() {
$('input[name="date"]').val(date) //"Sep 17, 2015"
$('._GATb._GACm').find('tbody textarea[name="text"]').val(annotation)
$('._GATb._GACm').find('tbody textarea[name="text"]').click()
if (visibility == 'private') {
$('#AnnotationsDrawer_private_radiobutton').prop("checked", true)
} else {
$('#AnnotationsDrawer_public_radiobutton').prop("checked", true)
}, 1000)

setTimeout(function() {
$('._GATb._GACm').find('form').find('a._GAE._GADq b b b').click();
console.log(count + ' view: ' + view + ' added annotation: ' + annotation)
}, 1800);

The actions that soon follows mimic how we would go around to manually produce the annotation. Upon the scheduled eight-seconds interval, the code will first ensure the drawer near the timeline is open, so the ‘Create new annotation’ object is exposed. Then we click this object in order to fill the expanded form with pre-filled data from our code. Lastly, we click on the save button before the process is repeated until the last view in the property.

Please note that the code works as of October 2015, but Google may change their HTML markups so make sure to test the code before you use it..

Further Development

By modifying the code provided above you can enlarge the use cases to include the following (but the sky’s the limit!):

  1. Check for campaign data
  2. Check for social network data
  3. Check and edit view configurations
  4. Check and edit most of what is provided in the Google Management API; reason for this duplicate method is that there is quota limit, which you can reference here
  5. Check, create and edit calculated metrics

Since we can semi-automate so much without relying on the Google Management API, my next step would be to create a long term solution in the form of a plugin that will automate and provide a quick summary of the general audit we typically perform at Cardinal Path. Note that Cardinal Path provides a more in-depth and personalized audit with our current clients, so definitely reach out if you would like to learn more.

Related Posts