Put Google Ads API result into Dataframe
Question:
I am using Google Ads API SDK for Python. I want to get some Ads data and put them into a Dataframe to transform a bit. I made a call with the code below:
client = GoogleAdsClient.load_from_storage("config.yaml")
customer_id = '<customer_id>'
ga_service = client.get_service("GoogleAdsService")
query = """
SELECT
campaign.id,
campaign.name,
customer.id
FROM campaign
"""
response = ga_service.search_stream(customer_id=customer_id, query=query)
for batch in response:
for row in batch.results:
print(row)
df = pd.DataFrame(row)
print(df)
Here is the response I receive:
customer {
resource_name: "customers/<customer-id>"
id: <customer-id>
}
campaign {
resource_name: "customers/<customer-id>/campaigns/<campaign-id>"
name: "Test_campaign_1"
id: <campaign-id>
}
Traceback (most recent call last):
File "c:UsersUsermain.py", line 36, in <module>
print(dict(row))
TypeError: 'GoogleAdsRow' object is not iterable
I tried using google.protobuf.json_format to convert the result into json/dict format with the below code
from google.protobuf.json_format import MessageToJson
response = ga_service.search_stream(customer_id=customer_id, query=query)
for batch in response:
for row in batch.results:
print(row)
jsonobj = MessageToJson(row)
But I got the below error message:
File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagesprotomessage.py", line 605, in __getattr__
raise AttributeError(str(ex))
AttributeError: 'DESCRIPTOR'
Could you please help me with this? Thank you.
Answers:
Sorry for bothering, but I found this question and got the answer for my question.
I changed my code to below (add ._pb to the response):
response = ga_service.search(customer_id=customer_id, query=query)
dictobj = MessageToDict(response._pb)
df = pd.json_normalize(dictobj,record_path=['results'])
print(df)
and it works!
Many thanks to Toàn!
Regarding the questions mentioned in his previous post, I found the following solution to be effective for my situation:
search_request.query = query
stream = gads_service.search_stream(search_request)
for batch in stream:
dictobj = MessageToDict(batch._pb)
df = pd.json_normalize(dictobj,record_path=['results'])
I am using Google Ads API SDK for Python. I want to get some Ads data and put them into a Dataframe to transform a bit. I made a call with the code below:
client = GoogleAdsClient.load_from_storage("config.yaml")
customer_id = '<customer_id>'
ga_service = client.get_service("GoogleAdsService")
query = """
SELECT
campaign.id,
campaign.name,
customer.id
FROM campaign
"""
response = ga_service.search_stream(customer_id=customer_id, query=query)
for batch in response:
for row in batch.results:
print(row)
df = pd.DataFrame(row)
print(df)
Here is the response I receive:
customer {
resource_name: "customers/<customer-id>"
id: <customer-id>
}
campaign {
resource_name: "customers/<customer-id>/campaigns/<campaign-id>"
name: "Test_campaign_1"
id: <campaign-id>
}
Traceback (most recent call last):
File "c:UsersUsermain.py", line 36, in <module>
print(dict(row))
TypeError: 'GoogleAdsRow' object is not iterable
I tried using google.protobuf.json_format to convert the result into json/dict format with the below code
from google.protobuf.json_format import MessageToJson
response = ga_service.search_stream(customer_id=customer_id, query=query)
for batch in response:
for row in batch.results:
print(row)
jsonobj = MessageToJson(row)
But I got the below error message:
File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagesprotomessage.py", line 605, in __getattr__
raise AttributeError(str(ex))
AttributeError: 'DESCRIPTOR'
Could you please help me with this? Thank you.
Sorry for bothering, but I found this question and got the answer for my question.
I changed my code to below (add ._pb to the response):
response = ga_service.search(customer_id=customer_id, query=query)
dictobj = MessageToDict(response._pb)
df = pd.json_normalize(dictobj,record_path=['results'])
print(df)
and it works!
Many thanks to Toàn!
Regarding the questions mentioned in his previous post, I found the following solution to be effective for my situation:
search_request.query = query
stream = gads_service.search_stream(search_request)
for batch in stream:
dictobj = MessageToDict(batch._pb)
df = pd.json_normalize(dictobj,record_path=['results'])