Updating Keyword final URL

39 views
Skip to first unread message

Hussein Negm

unread,
Apr 18, 2018, 6:35:29 AM4/18/18
to AdWords API Forum
My goal is retrieve all the keywords in an adGroup and update the final URL the following code (and other similar versions ) give me the error 

TypeNotFound: Type not found: 'finalUrls'



def main(client, adgroup_id):
  # Initialize appropriate service.
  ad_group_criterion_service = client.GetService(
      'AdGroupCriterionService', version='v201802')

  # Construct selector and get all ad group criteria.
  offset = 0
  selector = {
      'fields': ['Id', 'CriteriaType', 'KeywordMatchType', 'KeywordText','FinalUrls'],
      'predicates': [
          {
              'field': 'AdGroupId',
              'operator': 'EQUALS',
              'values': [adgroup_id]
          },
          {
              'field': 'CriteriaType',
              'operator': 'EQUALS',
              'values': ['KEYWORD'],
          }
      ],
      'paging': {
          'startIndex': str(offset),
          'numberResults': str(PAGE_SIZE)
      },
      'ordering': [{'field': 'KeywordText', 'sortOrder': 'ASCENDING'}]
  }
  more_pages = True
  operations = []
  while more_pages:
    page = ad_group_criterion_service.get(selector)

    # Display results.
    if 'entries' in page:
      for keyword in page['entries']:
        print ('Keyword ID "%s", type "%s", text "%s", and match type '
               '"%s" was found.' % (
                   keyword['criterion']['id'],
                   keyword['criterion']['type'],
                   keyword['criterion']['text'],
                   keyword['criterion']['matchType']))
        pprint(keyword)
        
        trgt={
            'criterion':keyword['criterion'],
            'finalUrls':{"urls":['https://URL.url.url']}
        }
        operations.append({'operator': 'SET', 'operand': trgt  })
      
    else:
      print ('No keywords were found.')
    offset += PAGE_SIZE
    selector['paging']['startIndex'] = str(offset)
    more_pages = offset < int(page['totalNumEntries'])
  ad_group_criteria = ad_group_criterion_service.mutate(operations)['value']
if __name__ == '__main__':
  # Initialize client object.
  CLIENT_ID = '####' 
  CLIENT_SECRET = '####'
  REFRESH_TOKEN = '###'
  # AdWords API information.
  DEVELOPER_TOKEN = '###'
  USER_AGENT = '####' 
  CLIENT_CUSTOMER_ID = '###' 
  oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
  adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT,client_customer_id=CLIENT_CUSTOMER_ID)
  adgroupsList=[{'id':54627912232,'nh':'','category':''},{'id':57139439947,'nh':'','category':''},{'id':60015897731,'nh':'','category':''}]
  for adgroup in adgroupsList:
    main(adwords_client, adgroup["id"])


Hussein Negm

unread,
Apr 18, 2018, 9:06:38 AM4/18/18
to AdWords API Forum
Ok so I had a few more trials until I got it to work. I will post the final code hoping it would help someone in the future


        operations.append({
      'operator': 'SET',
      'operand': {
          'xsi_type': 'BiddableAdGroupCriterion',
          'adGroupId': adgroup_id,
          'criterion': {
              'id': keyword['criterion']['id'],
          },
          'finalUrls':{'urls':'https://just.a.url'}
      }
  })
      
    else:
      print ('No keywords were found.')
    offset += PAGE_SIZE
    selector['paging']['startIndex'] = str(offset)
    more_pages = offset < int(page['totalNumEntries'])
  ad_group_criteria = ad_group_criterion_service.mutate(operations)
if __name__ == '__main__':
  # Initialize client object.
  CLIENT_ID = '#####' 
  CLIENT_SECRET = '#####'
  REFRESH_TOKEN = '####'
  # AdWords API information.
  DEVELOPER_TOKEN = '####'
  USER_AGENT = '###' 
  CLIENT_CUSTOMER_ID = '###' 
  oauth2_client = oauth2.GoogleRefreshTokenClient(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
  adwords_client = adwords.AdWordsClient( DEVELOPER_TOKEN, oauth2_client, USER_AGENT,client_customer_id=CLIENT_CUSTOMER_ID)
  adgroupsList=[{'id':54627912232,'nh':'','category':''},{'id':57139439947,'nh':'','category':''},{'id':60015897731,'nh':'','category':''}]
  for adgroup in adgroupsList:
    main(adwords_client, adgroup["id"])
    #ideas(adwords_client, adgroup["id"])

Dhanya Sundararaju (AdWords API Team)

unread,
Apr 18, 2018, 1:22:12 PM4/18/18
to AdWords API Forum
Hi Hussein,

You are correct, you would need to set the finalUrl in the BiddableAdGroupCriterion as shown in your last email. Please let me know if you face any more issues.

Regards,
Dhanya, AdWords API Team
Reply all
Reply to author
Forward
0 new messages