You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Analytics API获取受众数据时的数量限制问题求助

Fixing Google Analytics API Audience List Pagination Limit

Looks like you're hitting the default pagination limit of the Google Analytics Management API! The RemarketingAudience.list endpoint returns a maximum of 1000 items per request by default, which is why you're only seeing 999 instead of your full 2400 audiences.

Here's what's wrong with your current code and how to fix it:


Issues in Your Original Code

  • No pagination handling: You're only making one API call, which can't return more than 1000 items
  • Risk of index out-of-bounds errors: Accessing request.items[i+154] is unreliable, especially when the returned items are fewer than expected
  • Hardcoded loop limit (i <3000) doesn't account for the actual number of audiences you have

Solution: Add Pagination Logic

The API supports start-index and max-results parameters to fetch multiple pages of data. We'll use a loop to keep requesting pages until there are no more audiences left to retrieve.

Here's the revised code with proper pagination:

function main() {
  var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadshe');
  var sheet = spreadsheet.getSheetByName('Sh');
  
  // Set up header row
  sheet.getRange(1,1).setValue("audienceName");
  sheet.getRange(1,2).setValue("audienceId");
  sheet.getRange(1,3).setValue("audienceDefinition");
  sheet.getRange(1,4).setValue("audienceDescription");
  
  listRemarketingAudiences('xxxxx', 'UA-xxxxx-1', sheet);
}

function listRemarketingAudiences(accountId, propertyId, sheet) {
  var startIndex = 1;
  var maxResults = 1000; // API's maximum allowed items per page
  var currentRow = 2; // Start writing after the header row
  
  do {
    // Make API request with pagination parameters
    var request = Analytics.Management.RemarketingAudience.list(
      accountId, 
      propertyId,
      {
        'start-index': startIndex,
        'max-results': maxResults
      }
    );
    
    // Exit loop if no more items are returned
    if (!request.items || request.items.length === 0) {
      break;
    }
    
    // Process each audience in the current page
    request.items.forEach(function(audience) {
      sheet.getRange(currentRow, 1).setValue(audience.name);
      sheet.getRange(currentRow, 2).setValue(audience.id);
      sheet.getRange(currentRow, 3).setValue(audience.audienceDefinition);
      // Handle empty descriptions to avoid writing "undefined"
      sheet.getRange(currentRow, 4).setValue(audience.description || '');
      currentRow++;
    });
    
    // Update start index for the next page
    startIndex += maxResults;
    
  // Continue looping only if we got a full page (meaning more items exist)
  } while (request.items.length === maxResults);
  
  console.log(`Successfully retrieved ${currentRow - 2} audiences!`);
}

Key Changes Explained

  1. Pagination Parameters: We added start-index and max-results to the API request to control which page of results we're fetching.
  2. Safe Loop Structure: The do-while loop keeps requesting pages as long as we get a full set of 1000 items (a sign there are more audiences to pull).
  3. Error-Resilient Processing: Using forEach on request.items ensures we only process existing items, eliminating index out-of-bounds errors.
  4. Dynamic Row Tracking: currentRow increments with each audience, so we don't rely on hardcoded row limits.
  5. Empty Value Handling: Added || '' to avoid writing undefined when an audience has no description.

This should pull all 2400 of your audiences into the spreadsheet without hitting the initial limit.

内容的提问来源于stack exchange,提问作者maciekef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:01:02