I’m working with the Google Ads API and trying to fetch keyword ideas from an existing keyword plan using Python. However, my query isn’t returning any rows, and I’m not sure why. Here’s the relevant part of my code (API V15):
def extract_google_ads_keywords(self, keyword_plan_name='sqlxpert.de'):
try:
ga_service = self.client.get_service("GoogleAdsService")
#ga_service = self.client.get_service("KeywordPlanIdeaService")
# Define the query to fetch the keyword ideas
keyword_plan_query = f"""
SELECT
keyword_plan.id,
keyword_plan.name,
keyword_plan.resource_name
FROM
keyword_plan
WHERE
keyword_plan.name = '{keyword_plan_name}'
"""
print("Customer ID:", self.customer_id)
print("Query:", keyword_plan_query)
# Execute the query to get the keyword plan campaigns
keyword_plan_stream = ga_service.search_stream(customer_id=self.customer_id, query=keyword_plan_query)
# Initialize an empty list to hold all rows' data
results = []
# Iterate through the returned results
for batch in keyword_plan_stream:
for row in batch.results:
print(row) # Debug: print each row to see what is returned
data = {
'KeywordPlanID': row.keyword_plan.id,
'Name': row.keyword_plan.name,
'ResourceName': row.keyword_plan.resource_name
}
results.append(data)
if not results:
print("No data returned from the query.")
# Convert the list of dictionaries to a DataFrame
df = pd.DataFrame(results)
print(df.head())
I have got a “correct” query, correct customer_id and no error from google. Just no rows. Other queries e.g. regarding campaign do work also If i e.g. add keyword_plan.campaign i get an error debug_error_string = “UNKNOWN:Error received from peer ipv4:142.111.111.202:443 {created_time:”2024-07-04T16:46:00.5007365+00:00”, grpc_status:3, grpc_message:”Request contains an invalid argument. So the syntax seems to be ok.
This is the info i want to get back.
My main goal is actually to get the number of searches for the keywords within the plan.
But to make it easier i just made a simple query, although the ones regarding keyword.text etc. are not working either.
grpc not really my link
this i thought would be useful
What am i doing wrong? Wrong Service? Wrong Query? if you could help me i would be really happy!