How to change keyword status via the Python libraries

308 views
Skip to first unread message

tom.wa...@raisingit.com

unread,
Aug 31, 2018, 1:01:12 PM8/31/18
to AdWords API and Google Ads API Forum
Hi community,

I am struggling to work out how to change keyword status via the Google Ads API using the Python library. I have no problem pausing and enabling AdGroups, or adding new keywords, but there is no exact model for this in the sample code, and my alterations are not working as expected. Here is the function I'm trying, based closely on the sample code:

def enableKeyword(client, ad_group_id, criterion_id):
  # Initialize appropriate service.
  ad_group_criterion_service = client.GetService(
      'AdGroupCriterionService', version='v201806')

  # Construct operations and update bids.
  operations = [{
      'operator': 'SET',
      'operand': {
          'adGroupId': ad_group_id,
          'criterion': {
              'id': criterion_id
          },
          'userStatus': 'ENABLED'
      }
  }]
  ad_group_criteria = ad_group_criterion_service.mutate(operations)

  # Display results.
  if 'value' in ad_group_criteria:
    for criterion in ad_group_criteria['value']:
      if criterion['criterion']['Criterion.Type'] == 'Keyword':
        print(('Ad group criterion with ad group id "%s" and criterion id '
               '"%s" currently has bids:'
               % (criterion['adGroupId'], criterion['criterion']['id'])))
        for bid in criterion['biddingStrategyConfiguration']['bids']:
          print('\tType: "%s", value: %s' % (bid['Bids.Type'],
                                             bid['bid']['microAmount']))
  else:
    print('No ad group criteria were updated.')

As far as I can see this follows the same structure as the (apparently) functioning C# code posted here: https://stackoverflow.com/questions/28046602/how-to-update-keyword-status-maxcpc-and-keyword-text-using-google-adwords-api?rq=1
but Python just gives me a KeyError complaining about userStatus - so it's obviously not expected there.

What am I missing?

Cheers,
Tom

Milind Sankeshware (AdWords API Team)

unread,
Aug 31, 2018, 4:21:46 PM8/31/18
to AdWords API and Google Ads API Forum
Hi Tom,

To change the keyword status, you will need to set the UserStatus of your BiddableAdGroupCriterion of type Keyword using the AdGroupCriterionService.mutate() method. We don't have the sample example to update the status of keyword. Please refer my sample code below:

  operations = [{
      'operator': 'SET',
      'operand': {
          'xsi_type': 'BiddableAdGroupCriterion',
          'adGroupId': ad_group_id,
          'criterion': {
              'id': criterion_id,
          },
'userStatus': 'PAUSED'
          
      }
  }]

Looks like you are missing xsi_type': 'BiddableAdGroupCriterion in your code. Could you please give a try and let me know if you are still facing an issue. 

Thanks,
Milind, AdWords API Team

tom.wa...@raisingit.com

unread,
Sep 3, 2018, 5:20:01 AM9/3/18
to AdWords API and Google Ads API Forum
Hi Milind,

That's fixed it - works as expected now - thank you!

Cheers,
Tom
Reply all
Reply to author
Forward
0 new messages