Query for getting all campaign with the given name - Google Ads

458 views
Skip to first unread message

Wayne

unread,
Nov 2, 2021, 8:19:45 AM11/2/21
to AdWords API and Google Ads API Forum
Hi team
I'm trying to fetch all the campaigns with a given name using the below query:

SearchGoogleAdsRequest request =
SearchGoogleAdsRequest.newBuilder()
.setCustomerId(Long.toString(customerId))
.setPageSize(1_000)
.setQuery(
String.format(
"SELECT campaign_budget.id, campaign_budget.name, "
+ "campaign_budget.resource_name FROM campaign_budget "
+ "WHERE campaign.name = '%s'",
campaignName))
.build();

// Retrieves the budget.
GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient();
GoogleAdsServiceClient.SearchPagedResponse searchPagedResponse = googleAdsServiceClient.search(request);
System.out.println(searchPagedResponse.getPage().getResponse().getResults(0).getCampaignBudget());


However, I'm getting the below error:

Headers: Metadata(content-type=application/grpc,request-id=******,date=Tue, 02 Nov 2021 12:13:04 GMT,alt-svc=h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43")
Body: null
Failure message: errors {
  error_code {
    query_error: EXPECTED_REFERENCED_FIELD_IN_SELECT_CLAUSE
  }
  message: "The following field must be present in SELECT clause: \'campaign.name\'."
}
request_id: "*******************"

Status: Status{code=INVALID_ARGUMENT, description=Request contains an invalid argument., cause=null}.

I'm using GoogleAds API.
Kindly tell if it is possible to get a campaign with a particular name. (NOT resource name - just the campaign name).

Thanks!


Google Ads API Forum Advisor

unread,
Nov 3, 2021, 2:20:42 AM11/3/21
to cos.squa...@gmail.com, adwor...@googlegroups.com
Hi Wayne,

You can use our Query builder and Query validator prior to using your GAQL in your API requests. As for filtering campaign names, you can use operators such as LIKE as seen in the query below :

SELECT campaign_budget.id, campaign_budget.name, campaign_budget.resource_name, campaign.name FROM campaign_budget WHERE campaign.name LIKE 'Campaign_Name'

Let me know how this goes on your end.

Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


ref:_00D1U1174p._5004Q2QHEw3:ref

Wayne

unread,
Nov 3, 2021, 2:38:01 AM11/3/21
to AdWords API and Google Ads API Forum
Thanks a lot Peter! That helped! :)
However, will it be possible to return an entire campaign? I mean here in the query we have to SELECT the fields we want, is it possible to return the whole campaign? Using query? Or a GET call? I just have the campaign name. So how to get the whole campaign?
Thanks!

Google Ads API Forum Advisor

unread,
Nov 3, 2021, 3:24:28 AM11/3/21
to cos.squa...@gmail.com, adwor...@googlegroups.com
Hi Wayne,

Glad that it worked on your end.

You can indeed use a get request to retrieve all the details of a Campaign resource. However, get requests are more costly in terms of usage as discussed here. The alternative would be to use search requests instead, which you can find out more via this guide and here as well.
Reply all
Reply to author
Forward
0 new messages