Cannot read property "length" from undefined, Youtube Analytics API Error
<p>I'm new to <code>Youtube Analytics Api</code> and I'm following <strong><a href="https://developers.google.com/youtube/analytics/v1/code_samples/apps-script#export_youtube_analytics_data_to_google_sheets" rel="nofollow noreferrer">this guide</a></strong> in google to save my analytics report in google sheet using <code>Google Apps Script</code>. Everytime I run the script, I get an error like this,</p>

<blockquote>
  <p>TypeError: Cannot read property "length" from undefined.</p>
</blockquote>

<p>Here is the code,</p>

<pre><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);
}
</code></pre>

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