Automate Google Analytics reporting using Google Apps Script

AUG 23, 2012
Author Photo By Nick Mihailovski, Google Analytics API Team

Cross-posted with the Google Analytics Blog and the Google Apps Developer Blog

Many people have been asking for a simple way to put Google Analytics data into a Google Spreadsheet. Once the data is inside a Google Spreadsheet, users can easily manipulate Google Analytics data, create new visualizations, and build internal dashboards.

So today we released a new integration that dramatically reduces the work required to put Google Analytics data into any Apps Script supported product, such as Google Docs, Sites, or Spreadsheets.

Here’s an example of Google Analytics data accessed through Apps Script and displayed in a Google Spreadsheet.

Custom API Dashboards - No Code Required

We know that a popular use case of this integration will be to create dashboards that automatically update. To make this easy to do, we’ve added a script to the Spreadsheets script gallery that handles all this work - no code required. The script is called Google Analytics Report Automation (Magic).

This script is a great template for starting your own project, and we’ve had many internal Google teams save hours of time using this tool. Here’s a video demoing how to build a dashboard using this script:

You can find this script by opening or creating a Google Spreadsheet, clicking Tools -> Script Gallery and searching for “analytics magic”.

Writing Your Own Script

Of course many developers will want to write their own code. With the new Analytics – Apps Script integration, you can request the total visitors, visits, and pageviews over time and put this data into a spreadsheet with just the following code:

      // Get Data.
      var results = Analytics.Data.Ga.get(
      tableId,
      startDate,
      endDate,
      'ga:visitors,ga:visits,ga:pageviews',
      {‘dimensions’: ‘ga:date’});

      // Output to spreadsheet.
      var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
      sheet.getRange(2, 1, results.getRows().length, headerNames.length)
      .setValues(results.getRows());

      // Make Sandwich.
      

To get started now, read our Automated Access to Google Analytics Data in Google Spreadsheets tutorial. Also check out the Google Analytics Apps Script reference docs.

Solving Business Problems

Are you ready to start building solutions using Google Analytics and Google Apps Script?

We’d love to hear new ways you use this integration to help manipulate, visualize and present data to solve business problems. To encourage you to try out this integration, we are giving out Google Analytics developer t-shirts to the first 15 developers to build a solution using both APIs.

To be eligible, you must publish your solution to either the Chrome Web Store or the Spreadsheets Script Gallery and include a description of a business problem the script solves. We’ll then collect these scripts and highlight the solutions in an upcoming blog post. After you publish your script, fill out this form to share what you’ve built.

We’re looking forward to seeing what you can do with this integration.



Nick Mihailovski is a Senior Developer Programs Engineer working on the Google Analytics API. In his spare time he likes to travel around the world.


Posted by Scott Knaster, Editor