Search(SearchGoogleAdsRequest) returns (SearchGoogleAdsResponse) problem

458 views
Skip to first unread message

chao cai

unread,
Apr 15, 2020, 3:14:13 AM4/15/20
to AdWords API and Google Ads API Forum
hi,
   I used Google ads api to get the report data, but I encountered a paging problem
    
  How to understand the two things: limit in SQL and setPageSize in request

  The first request page token is empty, limit 2000 returned 2000 When the second request page token was passed, the next page token returned for the first time, limit 2000 returned 1000 data, and the next page token returned empty, so it ended.
Because two requests were sent, the data after deduplication was 2000。 

What should I do if I want to get data in pages??

here is my code:

public String getCampaignReport(String customerId, int size, String pageToken) {
    // Creates a request that will retrieve all campaigns using pages of the specified page size.

String query = "SELECT customer.id, "

+ "campaign.id, "
+ "campaign.name, "
+ "segments.ad_network_type, segments.date,"
+ "metrics.impressions, "
+ "metrics.clicks, metrics.average_cost, metrics.interactions,"
+ "metrics.cost_per_conversion "
+ "FROM campaign where segments.date BETWEEN '2018-10-10' AND '2020-04-12'";

if (size > 0) {
query = query + " LIMIT " + size;
}

log.info("pageToken {} , query sql {}", pageToken, query);
SearchGoogleAdsRequest.Builder builder =
SearchGoogleAdsRequest.newBuilder()
.setCustomerId(customerId)
.setPageSize(1000)
.setQuery(query);

if (!Strings.isNullOrEmpty(pageToken)) {
builder.setPageToken(pageToken);
}
// Issues the search request.
SearchPagedResponse searchPagedResponse = googleAdsServiceClient.search(builder.build());
if (searchPagedResponse == null) {
log.info("searchPagedResponse null");
return "";
}

List<Long> sizeList = Lists.newLinkedList();
Set<Long> sizeSet = Sets.newHashSet();
// Iterates over all rows in all pages and prints the requested field values for the campaign
// in each row.
for (GoogleAdsRow googleAdsRow : searchPagedResponse.iterateAll()) {
Campaign campaign = googleAdsRow.getCampaign();
Segments segments = googleAdsRow.getSegments();
Metrics metrics = googleAdsRow.getMetrics();
log.info("Campaign with ID {} and name {} was found. "
+ "segments date {}, network_type {}"
+ "Campaign {}, {}, {}, {}, {}, {}",
campaign.getId().getValue(), campaign.getName().getValue(),
segments.getDate().getValue(), segments.getAdNetworkType(),
metrics.getImpressions().getValue(), metrics.getClicks().getValue(),
metrics.getCostPerConversion().getValue(),
metrics.getAverageCost().getValue(), metrics.getInteractions().getValue(),
metrics.getAverageCost().getValue() * metrics.getInteractions().getValue());

sizeList.add(campaign.getId().getValue());
sizeSet.add(campaign.getId().getValue());
}

log.info("list size {}, set size {}, next page token {}", sizeList.size(), sizeSet.size(), searchPagedResponse.getNextPageToken());
return searchPagedResponse.getNextPageToken();
}

public void getAllCampaignReport(String customerId, int size) {

String nextPageToken = getCampaignReport(customerId, size, null);
while (!Strings.isNullOrEmpty(nextPageToken)) {
nextPageToken = getCampaignReport(customerId, size, nextPageToken);
}
}

Google Ads API Forum Advisor Prod

unread,
Apr 15, 2020, 2:53:50 PM4/15/20
to daok...@gmail.com, adwor...@googlegroups.com
Hi Chao,

Thank you for reaching out. The LIMIT clause allows you to specify the maximum number of objects you want to return in your report. In your case, you set the limit to 2000 and 2000 items returned so this is the intended behavior. You can find more information on limits here.

Setting the page_size allows you to break your report up into multiple responses so that the page token doesn't expire when retrieving large or complex data-sets. Our client libraries handle paging automatically so you only have to iterate through the rows of the response. When all the rows in the current page have been returned, the client library will fetch a new page of rows automatically on your behalf until the entire data set has been retrieved. You can find more information on paging here including an example for how to iterate through the rows. Please try and follow this example when using the page_size in your report.

I hope this cleared up your confusion. Let me know if you have any additional questions.

Regards,
Mitchell
Google Ads API Team

ref:_00D1U1174p._5001UZYBPv:ref
Reply all
Reply to author
Forward
0 new messages