Thank Ernie,
I used this script of campaign performance report and was able to get the response However, when I was trying to parse this response into the panda data frame, I encountered the error. I believe that the step when I was trying to parse the response to pandas data returned the unknown error, which Google Ads interpreted as message: " Version v8 is deprecated. Requests to this version will be blocked." Is there an efficient way to parse the response to pandas dataframe? I provided more details below. Thank you for being patient with my long story.
# Script:
# this worked
from google.ads.googleads.client import GoogleAdsClient
import pandas as pd
# from google.protobuf.json_format import MessageToJson, MessageToDict
from google.protobuf.json_format import MessageToDict
# credentials
credentials_dict = {'developer_token': 'XXX',
'client_id': 'XXX',
'client_secret': 'XXX',
'refresh_token': 'XXX',
'login_customer_id': 'XXX'}
client = GoogleAdsClient.load_from_dict(credentials_dict)
ga_service = client.get_service("GoogleAdsService")
search_request = client.get_type("SearchGoogleAdsStreamRequest")
query = """
SELECT
segments.date,
campaign.id,
campaign.name,
campaign.status,
metrics.impressions,
metrics.clicks,
metrics.cost_micros,
metrics.conversions,
metrics.interactions,
metrics.engagements,
metrics.ctr,
customer.id,
customer.descriptive_name
FROM campaign
WHERE
segments.date DURING YESTERDAY
"""
customer_ids = [7988377523, 8518065321, 1556372374 ]
all_data = []
while customer_ids:
customer_id = int(customer_ids.pop(0))
search_request.customer_id = str(customer_id)
search_request.query = query
stream = ga_service.search_stream(search_request)
for batch in stream:
for row in batch.results:
customer = row.customer
customer_id =
customer.id campaign = row.campaign
campaign_id =
campaign.id
metrics = row.metrics
clicks = metrics.clicks
segments = row.segments
date = segments.date
print(date, campaign_id, clicks, cost_micros, customer_id
)
# not worked with the unknown request error when I was trying to further parse the above response into pandas data frame.
row_dict = {}
row_dict['date'] = date
row_dict['campaign_id'] = campaign_id
row_dict['campaign_name'] =
row.campaign.name row_dict['campaign_status'] = row.campaign.status
row_dict["impressions"] = row.metrics.impressions
row_dict["clicks"] = row.metrics.clicks
row_dict['cost_micros'] = row.metrics.cost_micros
row_dict["conversions"] = row.metrics.conversions
row_dict["interactions"] = row.metrics.interactions
row_dict["engagements"] = row.metrics.engagements
row_dict['customer_id'] =
row.customer.id row_dict['customer_name'] = row.customer.descriptive_name
all_data.append(row_dict)
cpr = pd.DataFrame(all_data)
cpr.head(3)
# api response
results {
customer {
resource_name: "customers/7988377523"
id: 7988377523
descriptive_name: "XXX"
}
campaign {
resource_name: "XXX"
status: ENABLED
name: "XXX"
id: 1717373231
}
metrics {
clicks: 0
cost_micros: 0
}
segments {
date: "2022-06-23"
}
}
# error
GoogleAdsException: (<_SingleThreadedRendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Request contains an invalid argument."
debug_error_string = "{"created":"@1656644641.028318815","description":"Error received from peer ipv4:
142.251.33.202:443","file":"src/core/lib/surface/call.cc","file_line":903,"grpc_message":"Request contains an invalid argument.","grpc_status":3}"
>, <_SingleThreadedRendezvous of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Request contains an invalid argument."
debug_error_string = "{"created":"@1656644641.028318815","description":"Error received from peer ipv4:
142.251.33.202:443","file":"src/core/lib/surface/call.cc","file_line":903,"grpc_message":"Request contains an invalid argument.","grpc_status":3}"
>, errors {
error_code {
request_error: UNKNOWN
}
message: " Version v8 is deprecated. Requests to this version will be blocked."
}
request_id: "daGjvZa7ZeZEQo3Rl_-GAA"
, 'daGjvZa7ZeZEQo3Rl_-GAA'