GAQL String Query | How to Select Campaign by Campaign ID

870 views
Skip to first unread message

reid hommedahl

unread,
Jul 12, 2021, 3:39:22 PM7/12/21
to AdWords API and Google Ads API Forum
Hello,

I'm trying to write a string query that returns a few different things-- but I would like to filter which campaigns are returned by campaign ID. Here's the string query I have wrote so far:

String query = 
"SELECT campaign.id, campaign.name, campaign.vanity_pharma.vanity_pharma_text FROM campaign 
WHERE campaign.id 11031113693 
ORDER BY campaign.id";

The error says:

Response
--------
Headers: Metadata(content-type=application/grpc,request-id=wQ_YJlu7-Ph0bn9VuLHJNg,date=Mon, 12 Jul 2021 19:30:17 GMT,alt-svc=h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":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: UNEXPECTED_INPUT
  }
  message: "Error in query: unexpected input 11031113693."
}


It worked before I entered the "WHERE" part of the query, but now it doesn't seem to be working. Any help you could provide on how to write this query would be really appreciated.

Thanks!

Pete Lavetsky (AdWords API Guru)

unread,
Jul 13, 2021, 8:52:04 AM7/13/21
to AdWords API and Google Ads API Forum
Hey Reid,

Use the GAQL query builder to validate your queries : https://developers.google.com/google-ads/api/fields/v8/campaign_query_builder

SELECT campaign.id, campaign.name, campaign.vanity_pharma.vanity_pharma_text FROM campaign WHERE campaign.id = 11031113693

Pete

reid hommedahl

unread,
Jul 13, 2021, 2:57:24 PM7/13/21
to AdWords API and Google Ads API Forum
Hi Pete,

Thanks for the help here! I was able to successfully filter by campaign ID.

How would I write this if there are multiple campaign IDs? I tried using commas, ampersands, and even writing another WHERE statement. All resulted in errors.

Any help you could provide here would be really appreciated. 

Thanks again,
Reid

Google Ads API Forum Advisor

unread,
Jul 14, 2021, 8:57:39 AM7/14/21
to adwor...@googlegroups.com
Hello,

Thanks for reaching out. For this case, you can use the IN operator in place of the '=' operator, as specified in the GAQL grammar. For example:

SELECT campaign.id, campaign.name, campaign.vanity_pharma.vanity_pharma_text FROM campaign WHERE campaign.id IN (1234, 2345, 3456)

Regards,
Matt
Google Ads API Team

Google Logo
Matt
Google Ads API Team
 


ref:_00D1U1174p._5004Q2KbebY:ref

Mat

unread,
Jul 14, 2021, 10:07:49 AM7/14/21
to AdWords API and Google Ads API Forum
Hi Reid,

that depends on the programming language, you are using.
With Python you could do something like this, if you want to use a variable instead of typing the ids manually into the query:

  campaign_ids_as_string = ', '.join(str(id) for id in campaign_ids)
  query = (
    'SELECT '
    'campaign.id, '
    'campaign.name, '
    '... '
    'FROM campaign '
    f'WHERE campaign.id IN ({campaign_ids_as_string})'
    )

If you don't need that auxiliary variable "campaign_ids_as_string" anywhere else, you could even write 

...
    f'WHERE campaign.id IN ({', '.join(str(id) for id in campaign_ids)})'
...

Regards
Mat
Reply all
Reply to author
Forward
0 new messages