title: How to get the total value using Google Analytics API?
body: <p>I'm trying to get data from Google Analytics using API with apps script.
  I already secceed to get values of each metrics by each dimension but total values are not shown.
  On the web view, I can see the total values of each metrics at top of the table.  Is there any way to get this through API? </p>
  
  <p>Here is the code to make the request.</p>
  
  <pre><code>function ChannelReportPC(firstProfile) { //
  
    var profileId = firstProfile.getId();
    var tableId = 'ga:' + profileId;
    var startDate = electiveStartDate();   
    var endDate = electiveEndDate();      
  
    var optArgs = { 
      'dimensions': 'ga:medium',              // Comma separated list of dimensions.
      'sort': '-ga:sessions',       // Sort by sessions descending, then keyword.
      'segment': 'sessions::condition::ga:deviceCategory==desktop,ga:deviceCategory==tablet',  // Process desktop or tablet traffic.
      'start-index': '1',
      'max-results': '20'                     // Display the first 250 results.
    };
  
    // 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).
      'ga:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviewsPerSession,ga:avgSessionDuration,ga:transactions,ga:transactionRevenue,ga:transactionsPerSession', // Comma seperated list of metrics.
       optArgs
       );
  
    var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
  
    if (results.getRows()) {
      return results;
  
    } else {
      throw new Error('No views (profiles) found');
    }
  }
  </code></pre>
  
  <p>Here is the code to output the result on sheet.</p>
  
  <pre><code>function outputToSpreadsheet(results) {　
  
    sheet = SpreadsheetApp.getActiveSheet()
  
    // Print the headers.
    var headerNames = [];
    for (var i = 0, header; header = results.getColumnHeaders()[i]; ++i) {
      headerNames.push(header.getName());
    }
    sheet.getRange(1, 1, 1, headerNames.length)
        .setValues([headerNames])
        .setBackground('#eeeeee');
  
  
    // Print the rows of data.
    sheet.getRange(2, 1, results.getRows().length, headerNames.length)
        .setValues(results.getRows());
  
    var output = [];
    var totals = results.totalsForAllResults;
    for (metricName in totals) {
      output.push(
          'Metric Name  = ', metricName, '\n',
          'Metric Total = ', totals[metricName], '\n');
    }
  }
  </code></pre>