Analytics Service

The Analytics service allows you to use the Google Analytics Management API and Reporting APIs in Apps Script. These APIs gives Analytics users the ability to get information about the structure of their account and run reports on how it is performing.


For detailed information on this service, see the reference documentation for the various Google Analytics APIs:

Like all advanced services in Apps Script, the Analytics service uses the same objects, methods, and parameters as the public API. For more information, see How method signatures are determined.

To report issues and find other support, see the corresponding support pages:

Sample code

The sample code below version 3 of the Management API.

List account structure

The sample lists all of the Google Analytics accounts, web properties, and profiles that the current user can access.

 * Lists Analytics accounts.
function listAccounts() {
  var accounts = Analytics.Management.Accounts.list();
  if (accounts.items && accounts.items.length) {
    for (var i = 0; i < accounts.items.length; i++) {
      var account = accounts.items[i];
      Logger.log('Account: name "%s", id "%s".',,;

      // List web properties in the account.
  } else {
    Logger.log('No accounts found.');

 * Lists web properites for an Analytics account.
 * @param  {string} accountId The account ID.
function listWebProperties(accountId) {
  var webProperties = Analytics.Management.Webproperties.list(accountId);
  if (webProperties.items && webProperties.items.length) {
    for (var i = 0; i < webProperties.items.length; i++) {
      var webProperty = webProperties.items[i];
      Logger.log('\tWeb Property: name "%s", id "%s".',,

      // List profiles in the web property.
  } else {
    Logger.log('\tNo web properties found.');

 * Logs a list of Analytics accounts profiles.
 * @param  {string} accountId     The Analytics account ID
 * @param  {string} webPropertyId The web property ID
function listProfiles(accountId, webPropertyId) {
  // Note: If you experience "Quota Error: User Rate Limit Exceeded" errors
  // due to the number of accounts or profiles you have, you may be able to
  // avoid it by adding a Utilities.sleep(1000) statement here.

  var profiles = Analytics.Management.Profiles.list(accountId,
  if (profiles.items && profiles.items.length) {
    for (var i = 0; i < profiles.items.length; i++) {
      var profile = profiles.items[i];
      Logger.log('\t\tProfile: name "%s", id "%s".',,
  } else {
    Logger.log('\t\tNo web properties found.');

Run a report

The sample runs a report to retrieve the top 25 keywords and traffic sources and stores the results in a new spreadsheet.

 * Runs a report of an Analytics profile ID. Creates a sheet with the report.
 * @param  {string} profileId The profile ID.
function runReport(profileId) {
  var today = new Date();
  var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);

  var startDate = Utilities.formatDate(oneWeekAgo, Session.getScriptTimeZone(),
  var endDate = Utilities.formatDate(today, Session.getScriptTimeZone(),

  var tableId = 'ga:' + profileId;
  var metric = 'ga:visits';
  var options = {
    'dimensions': 'ga:source,ga:keyword',
    'sort': '-ga:visits,ga:source',
    'filters': 'ga:medium==organic',
    'max-results': 25
  var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric,

  if (report.rows) {
    var spreadsheet = SpreadsheetApp.create('Google Analytics Report');
    var sheet = spreadsheet.getActiveSheet();

    // Append the headers.
    var headers = {

    // Append the results.
    sheet.getRange(2, 1, report.rows.length, headers.length)

    Logger.log('Report spreadsheet created: %s',
  } else {
    Logger.log('No rows returned.');