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:
#
https://developers.google.com/adwords/api/docs/appendix/languagecodes
'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:
#
https://developers.google.com/adwords/api/docs/appendix/geotargeting
'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...')
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+:
https://googleadsdeveloper.blogspot.com/
https://plus.google.com/+GoogleAdsDevelopers/posts
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/593c6741-4f9c-4c2d-8e33-c86e691ef95d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.