通过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
- Pagination Parameters: We added
start-indexandmax-resultsto the API request to control which page of results we're fetching. - Safe Loop Structure: The
do-whileloop keeps requesting pages as long as we get a full set of 1000 items (a sign there are more audiences to pull). - Error-Resilient Processing: Using
forEachonrequest.itemsensures we only process existing items, eliminating index out-of-bounds errors. - Dynamic Row Tracking:
currentRowincrements with each audience, so we don't rely on hardcoded row limits. - Empty Value Handling: Added
|| ''to avoid writingundefinedwhen an audience has no description.
This should pull all 2400 of your audiences into the spreadsheet without hitting the initial limit.
内容的提问来源于stack exchange,提问作者maciekef
相关产品推荐
相关产品推荐

