1818https://developers.google.com/google-ads/api/docs/keyword-planning/generate-historical-metrics
1919"""
2020
21+ from typing import Iterable
2122import argparse
2223import sys
2324
2425from google .ads .googleads .client import GoogleAdsClient
2526from google .ads .googleads .errors import GoogleAdsException
26- from google .ads .googleads .v20 .enums .types .keyword_plan_network import (
27- KeywordPlanNetworkEnum ,
27+ from google .ads .googleads .v20 .common .types .keyword_plan_common import (
28+ KeywordPlanHistoricalMetrics ,
29+ MonthlySearchVolume ,
2830)
2931from google .ads .googleads .v20 .services .services .google_ads_service .client import (
3032 GoogleAdsServiceClient ,
3537from google .ads .googleads .v20 .services .types .keyword_plan_idea_service import (
3638 GenerateKeywordHistoricalMetricsRequest ,
3739 GenerateKeywordHistoricalMetricsResponse ,
38- KeywordMetrics ,
39- MonthlySearchVolume ,
40+ GenerateKeywordHistoricalMetricsResult ,
4041)
4142
4243
@@ -61,8 +62,8 @@ def generate_historical_metrics(client: GoogleAdsClient, customer_id: str):
6162 googleads_service : GoogleAdsServiceClient = client .get_service (
6263 "GoogleAdsService"
6364 )
64- keyword_plan_idea_service : KeywordPlanIdeaServiceClient = client . get_service (
65- "KeywordPlanIdeaService"
65+ keyword_plan_idea_service : KeywordPlanIdeaServiceClient = (
66+ client . get_service ( "KeywordPlanIdeaService" )
6667 )
6768 request : GenerateKeywordHistoricalMetricsRequest = client .get_type (
6869 "GenerateKeywordHistoricalMetricsRequest"
@@ -87,9 +88,9 @@ def generate_historical_metrics(client: GoogleAdsClient, customer_id: str):
8788 )
8889 )
8990
90- result : KeywordMetrics
91- for result in response . results :
92- metrics : KeywordMetrics = result .keyword_metrics
91+ results : Iterable [ GenerateKeywordHistoricalMetricsResult ] = response . results
92+ for result in results :
93+ metrics : KeywordPlanHistoricalMetrics = result .keyword_metrics
9394 # These metrics include those for both the search query and any variants
9495 # included in the response.
9596 print (
@@ -126,8 +127,8 @@ def generate_historical_metrics(client: GoogleAdsClient, customer_id: str):
126127
127128 # Approximate number of searches on this query for the past twelve
128129 # months.
129- month : MonthlySearchVolume
130- for month in metrics . monthly_search_volumes :
130+ months : Iterable [ MonthlySearchVolume ] = metrics . monthly_search_volumes
131+ for month in months :
131132 print (
132133 f"\t Approximately { month .monthly_searches } searches in "
133134 f"{ month .month .name } , { month .year } "
0 commit comments