Targeting Idea Service suddenly returning 0 results

123 views
Skip to first unread message

Nick Debonair

unread,
May 13, 2016, 12:42:20 PM5/13/16
to AdWords API Forum
Hi all!

I'm trying to use the AdWordsAPI for getting keyword STATS using the TargetingIdeaService.

Last month (April 2016) everything was working fine, but now the TargetingIdeaPage is returning totalNumEntries = 0, and I can't work out why.

Has anything changed since then? I've pasted the Python script I'm using below.

Thanks for your help!

Nick

keywordList = [......]
locationList = [{'id': '1000'}]
attributeList = ['KEYWORD_TEXT', 'SEARCH_VOLUME', 'CATEGORY_PRODUCTS_AND_SERVICES', 'COMPETITION', 'AVERAGE_CPC', 'TARGETED_MONTHLY_SEARCHES']
networkDict = {'targetGoogleSearch': True, 'targetSearchNetwork': False, 'targetPartnerSearchNetwork': False}

outputFilename = 'keywordAnalysis.csv'

PAGE_SIZE = 100

def main(client):
  
  maxQuerySize = 800
  queryOffset = 0
  moreQueries = True
  keywordAnalysis = {}
  
  while moreQueries:
      # Initialize appropriate service.
      targeting_idea_service = client.GetService(
          'TargetingIdeaService', version='v201601')
    
      # Construct selector object and retrieve related keywords.
      offset = 0
      selector = {
          'searchParameters': [
              {
                  'xsi_type': 'RelatedToQuerySearchParameter',
                  'queries': keywordList[queryOffset:(queryOffset + maxQuerySize)]
              },
              {
                  # Language setting (optional).
                  # The ID can be found in the documentation:
                  'xsi_type': 'LanguageSearchParameter',
                  'languages': [{'id': '1000'}]
              },
              {
                  'xsi_type': 'NetworkSearchParameter',
                  'networkSetting': networkDict
              }          
          ],
    
          'ideaType': 'KEYWORD',
          'requestType': 'STATS',
          'requestedAttributeTypes': attributeList,
          'paging': {
              'startIndex': str(offset),
              'numberResults': str(PAGE_SIZE)
          }
      }
      # if locationList is not empty, add the location search parameters to the selector:
      if locationList:
          locationSearchParameter = {
                  # Location setting (optional).
                  # The ID can be found in the documentation:
                  'xsi_type': 'LocationSearchParameter',
                  'locations': locationList
                  }
          selector['searchParameters'].append(locationSearchParameter)
      print selector
      # store the data in keywordAnalysis as Keyword-Stat pairs, ie 'wedding planning':{AVERAGE_CPC: 2, SEARCH_VOLUME: 65, etc}:
      
      more_pages = True
      print 'Attempting Batch %s of %s:' % ((queryOffset/maxQuerySize)+1, len(keywordList)/maxQuerySize)
      noData = False
      while more_pages:
        Error = True
        while Error:
            try:
                page = targeting_idea_service.get(selector)
            except suds.WebFault, f:
                print f
                reason = f.fault.detail.ApiExceptionFault.errors.reason
                print 'Reason: %s' % (reason)
                if reason == "RATE_EXCEEDED":                
                    pause = float(f.fault.detail.ApiExceptionFault.errors.retryAfterSeconds)
                    print 'Pausing for %s seconds...' % (pause)
                    print
                    time.sleep(pause)
                else:
                    print f.fault
            else:
                Error = False
                print page
                if 'entries' in page:
                  for result in page['entries']:
                    attributes = {}
                    for attribute in result['data']:
                      # AVERAGE_CPC is in a different format to other attributes
                      if attribute['key'] == 'AVERAGE_CPC':
                          attributes[attribute['key']] = getattr(getattr(attribute.value, 'value', 0),'microAmount', 0) / 1000000.0
                      # For TARGETED_MONTHLY_SEARCHES, separate out each year-month and use as keys. Also populate MonthList to use as column headers later
                      elif attribute['key'] == 'TARGETED_MONTHLY_SEARCHES':
                          monthList = []
                          for MonthlySearchVolume in attribute.value.value:
                              yearMonth = str(MonthlySearchVolume.year)+'-'+str(MonthlySearchVolume.month)
                              monthList.append(yearMonth)
                              attributes[yearMonth] = getattr(MonthlySearchVolume, 'count', '0')
                      else:
                          attributes[attribute['key']] = getattr(attribute['value'], 'value', '0')
                    # output the stats for this keyword as a keyword-stat pair (see above)
                    keyword = attributes.pop('KEYWORD_TEXT')
                    keywordAnalysis[keyword] = attributes
                else:
                  noData = True
                offset += PAGE_SIZE
                selector['paging']['startIndex'] = str(offset)
                more_pages = offset < int(page['totalNumEntries'])
  
      queryOffset += maxQuerySize
      moreQueries = queryOffset < len(keywordList)
      if queryOffset == maxQuerySize:
          outputHeaders = attributeList[:-1] + monthList
          with open(outputFilename, 'w') as outputFile:
              wr = csv.writer(outputFile, dialect = 'excel', lineterminator = '\n')
              wr.writerow(outputHeaders)
      if noData:
          print '\tError: no data found in Batch'
          print
      else:
          with open(outputFilename, 'a') as outputFile:
              wr = csv.writer(outputFile, dialect = 'excel', lineterminator = '\n')
              for keyword in keywordAnalysis.keys():
                  row = [keyword]
                  for attribute in outputHeaders[1:]:
                      row.append(keywordAnalysis[keyword][attribute])
                  wr.writerow(row)
          print '\tBatch %s completed' % (queryOffset/maxQuerySize)
          print '\tNumber of keywords returned in this batch: %s' % (len(keywordAnalysis.keys()))
          print
          keywordAnalysis = {}

if __name__ == '__main__':
  # Initialize client object.
  thisDir = os.path.dirname(os.path.abspath(__file__))
  adwords_client = adwords.AdWordsClient.LoadFromStorage(thisDir + '\googleads.yaml')
  print 'Accessing API, and retrieving keyword data...'
  main(adwords_client)
  print 'Should be all done. Results are in ' + outputFilename
  raw_input('Press any key to continue...')

Anthony Madrigal

unread,
May 13, 2016, 2:30:49 PM5/13/16
to AdWords API Forum
Hi Nick,

There were no updates to the TargetingIdeaService as far as I know. Could you please provide me with your SOAP request and response via reply privately to author so that I can take a further look?

Thanks,
Anthony
AdWords API Team

Nick Debonair

unread,
May 16, 2016, 6:23:02 AM5/16/16
to AdWords API Forum
Hi Anthony,

I have just sent you the SOAP request and response privately!

Thanks,

Nick

Michael Cloonan (AdWords API Team)

unread,
May 16, 2016, 9:49:59 AM5/16/16
to AdWords API Forum
Hello,

I have one more follow up question to confirm before I start digging into this. Are you making the exact same request as you were before, when it was returning results? Hopefully you have a log from that time, but I realize it was some time ago, so otherwise please think if anything has changed in the request at all.

Regards,
Mike, AdWords API Team

Nick Debonair

unread,
May 16, 2016, 11:40:52 AM5/16/16
to AdWords API Forum
Hi Michael,

Unfortunately you are correct - I don't have a log from back when the request was working correctly, and I'm almost certain that nothing has changed in the request since it was last working.

Having said that, I've just done a little test on some other keywords and it is working - I believe the problem came from sending keywords for which there is no data (obscure keywords and combinations thereof). Does the API just return an empty page if there are no results for all the keywords sent?

Cheers,

Nick

Michael Cloonan (AdWords API Team)

unread,
May 16, 2016, 1:18:25 PM5/16/16
to AdWords API Forum
Hello,

Yes, that is the expected behavior if the TargetingIdeaService has no applicable results. Were you getting results in the past for the exact same set of keywords? Perhaps that was the difference.

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