google-apps-scriptyoutube-analytics-api

Cannot read property "length" from undefined, Youtube Analytics API Error


I'm new to Youtube Analytics Api and I'm following this guide in google to save my analytics report in google sheet using Google Apps Script. Everytime I run the script, I get an error like this,

TypeError: Cannot read property "length" from undefined.

Here is the code,

  function spreadsheetAnalytics() {
  // Get the channel ID
  var myChannels = YouTube.Channels.list('id', {mine: true});
  var channel = myChannels.items[0];
  var channelId = channel.id;
  // Set the dates for our report
  var today = new Date();
  var oneMonthAgo = new Date();
  oneMonthAgo.setMonth(today.getMonth() - 1);
  var todayFormatted = Utilities.formatDate(today, 'UTC', 'yyyy-MM-dd')
  var oneMonthAgoFormatted = Utilities.formatDate(oneMonthAgo, 'UTC', 'yyyy-MM-dd');

    var analyticsResponse = YouTubeAnalytics.Reports.query(
        'channel==' + channelId,
        oneMonthAgoFormatted,
        todayFormatted,
        'views,likes,dislikes,shares',
        {
          dimensions: 'day',
          sort: '-day'
        });

  var ssName = 'YouTube channel report ' + oneMonthAgoFormatted + ' - ' + todayFormatted;
  var numRows = analyticsResponse.rows.length;  // ERROR IN THIS LINE
  var numCols = analyticsResponse.columnHeaders.length;

  var ssNew = SpreadsheetApp.create(ssName, numRows + 1, numCols);
  var sheet = ssNew.getSheets()[0];

  // Get the range for the title columns
  // Remember, spreadsheets are 1-indexed, whereas arrays are 0-indexed
  var headersRange = sheet.getRange(1, 1, 1, numCols);
  var headers = [];

  // These column headers will correspond with the metrics requested
  // in the initial call: views, likes, dislikes, shares
  for(var i in analyticsResponse.columnHeaders) {
    var columnHeader = analyticsResponse.columnHeaders[i];
    var columnName = columnHeader.name;
    headers[i] = columnName;
  }

  headersRange.setValues([headers]);

  headersRange.setFontWeight('bold');
  sheet.setFrozenRows(1);

  var dataRange = sheet.getRange(2, 1, numRows, numCols);
  dataRange.setValues(analyticsResponse.rows);

  var dateHeaders = sheet.getRange(1, 1, numRows, 1);
  dateHeaders.setFontWeight('bold');
  sheet.setFrozenColumns(1);

  var range = sheet.getRange(1, 1, numRows, numCols);
  var chart = sheet.newChart()
                   .asColumnChart()
                   .setStacked()
                   .addRange(range)
                   .setPosition(4, 2, 10, 10)
                   .build();
  sheet.insertChart(chart);
}

I haven't edited any code from the guide. Just following exactly what is given. How can I get rid of this error?


Solution

  • Just like the error says rows is undefined. This is because there is no data being returned for that query.

    From the docs found at: https://developers.google.com/youtube/analytics/v1/reference/reports/query#response

    If no data is available for the given query, the rows element will be omitted from the response.