Hi Alex,
The code in these examples from the Google API docs are what I used.
Specifically the section
"
Generate the metrics" of their example for generate-forecast-metrics. The main thing to note here is that you need to change
keyword_plan_service.generate_forecast_metrics tokeyword_plan_service.generate_historical_metrics
def main(client, customer_id, keyword_plan_id):
keyword_plan_service = client.get_service("KeywordPlanService")
resource_name = keyword_plan_service.keyword_plan_path(customer_id, keyword_plan_id)
#######
# INSERT YOUR
metric_request.historical_metrics_options HERE
#######
metric_request = client.get_type("GenerateHistoricalMetricsRequest")
metric_request.keyword_plan=resource_name
metric_request.historical_metrics_options.year_month_range.start.year=2019
metric_request.historical_metrics_options.year_month_range.start.month=client.get_type("MonthOfYearEnum").MonthOfYear.JANUARY
metric_request.historical_metrics_options.year_month_range.end.year=2021
metric_request.historical_metrics_options.year_month_range.end.month=client.get_type("MonthOfYearEnum").MonthOfYear.MAY
# response = keyword_plan_service.generate_forecast_metrics( keyword_plan=resource_name) # THIS IS THEIR ORIGINAL REQUEST CODE
response = keyword_plan_service.generate_historical_metrics(metricrequest)
for i, result in enumerate(response.metrics) : print(result.search_query)
# DO SOME STUFF WITH THE RESULTS