Reputation: 23
Google Ads API, Their new version of the Google Adwords API returns data in a format called "GoogleAdsRow". I have not found any use at all for the information to be presented in this format and it is quite confusing. I want to print my file to a basic .csv format, but there is not any type of delimiter as far as i can tell. S far I have only been able to print each entire row as a single cell. Not Helpful :-).
My main function looks like this following. I have provided two examples of what I have tried with a comment block identifying both attempts:
def main(client, customer_id, page_size):
ga_service = client.get_service('GoogleAdsService', version='v2')
query = ('SELECT ad_group.id, ad_group_criterion.type, '
'ad_group_criterion.criterion_id, '
'ad_group_criterion.keyword.text, '
'ad_group_criterion.keyword.match_type FROM ad_group_criterion '
'WHERE ad_group_criterion.type = KEYWORD')
results = ga_service.search(customer_id, query=query, page_size=page_size)
try:
with open(path, "w", encoding = "utf-8", newline = "") as f:
#with open(path, "w") as csv:
csv_writer = csv.writer(f, delimiter=',')
for row in results:
campaign = row.campaign
csv_writer.writerow([row]) #Prints entire returned row as a single cell
csv_writer.writerow(row) #Tells me there is no delimiter
The iterable error is as follows
<ipython-input-15-e736ee2d05c9> in main(client, customer_id, page_size)
17 campaign = row.campaign
18 #csv_writer.writerow([row]) #Prints entire returned row as a single cell
---> 19 csv_writer.writerow(row) #Tells me there is no delimiter
20
21
Error: iterable expected, not GoogleAdsRow
Upvotes: 2
Views: 1207
Reputation: 968
You need to separate out the individual row items that are returned and add them into the CSV separately. One for each column.
With your response, you need to pull the elements of the google row returned and put them into your CSV at the appropriate row.
row.campaign.value,
row.segments.date.value,
row.metrics.cost_micros.value / 1000000,
row.metrics.clicks.value etc.
Unfortunately, you can't write to a CSV directly from the google row as it's a protobuff and the properties of the row need to be pulled out as you see fit.
Upvotes: 3