简体   繁体   中英

How to print multiple calls to Google Analytics API to Google Sheets with Apps Script

I'm trying to print multiple results from multiple calls into the same Google sheet. I'm using Google Apps Script to call the Google Analytics API.

I'm currently able to print two sets of data, but the second set coerces to my first call. It's almost like the results of the second call are overridden by the first call. I'm sure my problem is towards the bottom in my attempt to print both sets of results.

Here is my code:

function getReportDataForProfile(firstProfile) {

  var profileId = firstProfile;
  var tableId = 'ga:' + profileId;
  var startDate = getMonthsAgo(3);   // Months ago.
  var endDate = getEndDate (0); // Today.

  var optArgs = {
    'dimensions': 'ga:month',              // Comma separated list of dimensions.
    'segment':'gaid::-lPvLp6LTbyDv3jOuYgEQA', // Female 18-24
    'start-index': '1',
    'max-results': '250'                     // Display the first 250 results.
  };

  var optArgs1 = {
    'dimensions': 'ga:month',              // Comma separated list of dimensions.
    'segment':'gaid::Uu6nRXdoQxipnELhe94ejg', //Female 25 - 30
    'start-index': '1',
    'max-results': '250'                     // 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:goal19Completions', // Comma seperated list of metrics.
      optArgs);

  if (results.getRows()) {
    return results;

  } else {
    throw new Error('No views (profiles) found');
  };

    var results1 = 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:goal19Completions', // Comma seperated list of metrics.
      optArgs1);

  if (results1.getRows()) {
    return results1;

  } else {
    throw new Error('No views (profiles) found');
  };

}

function outputToSpreadsheet(results) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().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]);

  // Print the rows of data.
  sheet.getRange(2, 1, results.getRows().length, headerNames.length)
      .setValues(results.getRows());

}

function outputToSpreadsheet(results1) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Print the headers.
  var headerNames = [];
  for (var i = 0, header; header = results1.getColumnHeaders()[i]; ++i) {
    headerNames.push(header.getName());
  }

  sheet.getRange(1, 3, 1, headerNames.length)
      .setValues([headerNames]);

  // Print the rows of data.
  sheet.getRange(2, 3, results1.getRows().length, headerNames.length)
      .setValues(results1.getRows());

}

Any help would be appreciated.

your javascript logic is not right. you get your first result and then have: if (x) return y; else throw exception if (x) return y; else throw exception

so it will never get past that. use the debugger and step each line. you will see its impossible to get to the 2nd query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM