HistoricalMetricsOptions

803 views
Skip to first unread message

Brad Wilcox

unread,
Jun 21, 2021, 6:37:40 PM6/21/21
to AdWords API and Google Ads API Forum
I am trying to define the YearMonth settings in the HistoricalMetricsOptions from this documentation: https://developers.google.com/google-ads/api/rest/reference/rest/v8/HistoricalMetricsOptions
I haven't been able to figure out how to use and haven't seen any example pieces of code. Can anyone help me with understanding how this object can be set?

Eric Shuman

unread,
Jun 22, 2021, 3:01:40 PM6/22/21
to AdWords API and Google Ads API Forum
Hey Brad,

I was able to set the HistoricalMetricsOptions like this (in Python):

keyword_plan_service = client.get_service("KeywordPlanService")
metric_request = client.get_type("GenerateHistoricalMetricsRequest")

resource_name = keyword_plan_service.keyword_plan_path(customer_id, keyword_plan_id)

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_historical_metrics(metric_request)

Google Ads API Forum Advisor

unread,
Jun 24, 2021, 11:36:04 AM6/24/21
to brad....@globalstrategies.com, adwor...@googlegroups.com
Hi Brad,

Thank you for reaching out to us. In looking over the document that you shared with us. As well as looking at Erics response. I would suggest looking at what he mentioned for Python. Unsure what library you are using. But basically with the way that JSON is setup you will need to go through each object and can set it up. With the link that you have given it shows you the step thru the JSON object and what needs to be setup at each location. When you look at what Eric showed you that is exactly what he is doing.

Regards,
Google Logo
William Pescherine
Google Ads API Team
 


ref:_00D1U1174p._5004Q2IxZ3x:ref

Alex

unread,
Aug 26, 2021, 5:48:09 AM8/26/21
to AdWords API and Google Ads API Forum
Hey Eric,

Are you able to share your code. I'm also trying to get this working. And can't seem to get it from what you've posted.

Thanks

Eric Shuman

unread,
Aug 30, 2021, 2:21:15 PM8/30/21
to AdWords API and Google Ads API Forum
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 to
keyword_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

Google Ads API Forum Advisor

unread,
Sep 1, 2021, 10:25:44 PM9/1/21
to eric....@globalstrategies.com, adwor...@googlegroups.com
Hi everyone,

Thank you for reaching out to our API support team.

The guides that Eric provided are correct for forecasting and for historical metrics. That said, if you are also looking for implementation solutions for Python, then you can reach out to their client library owners via this link instead.

Should you encounter API specific errors, let us know so that our API team can further assist you.

Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


ref:_00D1U1174p._5004Q2MiwkG:ref
Reply all
Reply to author
Forward
0 new messages