3131
3232import argparse
3333import sys
34+ from typing import List , Iterable
3435
3536from google .ads .googleads .client import GoogleAdsClient
3637from google .ads .googleads .errors import GoogleAdsException
38+ from google .ads .googleads .v20 .services .types .google_ads_service import (
39+ GoogleAdsRow ,
40+ )
41+ from google .ads .googleads .v20 .services .types .recommendation_service import (
42+ ApplyRecommendationOperation ,
43+ ApplyRecommendationResult ,
44+ )
3745
3846
39- def main (client , customer_id ) :
47+ def main (client : GoogleAdsClient , customer_id : str ) -> None :
4048 """The main method that creates all necessary entities for the example.
4149
4250 Args:
@@ -46,7 +54,9 @@ def main(client, customer_id):
4654 detect_and_apply_recommendations (client , customer_id )
4755
4856
49- def detect_and_apply_recommendations (client , customer_id ):
57+ def detect_and_apply_recommendations (
58+ client : GoogleAdsClient , customer_id : str
59+ ) -> None :
5060 """Detects recommendations and applies them.
5161
5262 Args:
@@ -56,7 +66,7 @@ def detect_and_apply_recommendations(client, customer_id):
5666
5767 # [START detect_keyword_recommendations]
5868 googleads_service = client .get_service ("GoogleAdsService" )
59- query = f"""
69+ query : str = f"""
6070 SELECT
6171 recommendation.campaign,
6272 recommendation.keyword_recommendation
@@ -65,10 +75,12 @@ def detect_and_apply_recommendations(client, customer_id):
6575 recommendation.type = KEYWORD"""
6676
6777 # Detects keyword recommendations that exist for the customer account.
68- response = googleads_service .search (customer_id = customer_id , query = query )
78+ response : Iterable [GoogleAdsRow ] = googleads_service .search (
79+ customer_id = customer_id , query = query
80+ )
6981
70- operations = []
71- for row in response . results :
82+ operations : List [ ApplyRecommendationOperation ] = []
83+ for row in response :
7284 recommendation = row .recommendation
7385 print (
7486 f"Keyword recommendation ('{ recommendation .resource_name } ') "
@@ -94,12 +106,13 @@ def detect_and_apply_recommendations(client, customer_id):
94106
95107
96108# [START build_apply_recommendation_operation]
97- def build_recommendation_operation (client , recommendation ):
109+ def build_recommendation_operation (
110+ client : GoogleAdsClient , recommendation : str
111+ ) -> ApplyRecommendationOperation :
98112 """Creates a ApplyRecommendationOperation to apply the given recommendation.
99113
100114 Args:
101115 client: an initialized GoogleAdsClient instance.
102- customer_id: a client customer ID.
103116 recommendation: a resource name for the recommendation to be applied.
104117 """
105118 # If you have a recommendation ID instead of a resource name, you can create
@@ -110,7 +123,9 @@ def build_recommendation_operation(client, recommendation):
110123 # customer_id, recommendation.id
111124 # )
112125
113- operation = client .get_type ("ApplyRecommendationOperation" )
126+ operation : ApplyRecommendationOperation = client .get_type (
127+ "ApplyRecommendationOperation"
128+ )
114129
115130 # Each recommendation type has optional parameters to override the
116131 # recommended values. Below is an example showing how to override a
@@ -127,7 +142,11 @@ def build_recommendation_operation(client, recommendation):
127142
128143
129144# [START apply_recommendation]
130- def apply_recommendations (client , customer_id , operations ):
145+ def apply_recommendations (
146+ client : GoogleAdsClient ,
147+ customer_id : str ,
148+ operations : List [ApplyRecommendationOperation ],
149+ ) -> None :
131150 """Applies a batch of recommendations.
132151
133152 Args:
@@ -137,20 +156,22 @@ def apply_recommendations(client, customer_id, operations):
137156 """
138157 # Issues a mutate request to apply the recommendations.
139158 recommendation_service = client .get_service ("RecommendationService" )
140- response = recommendation_service .apply_recommendation (
141- customer_id = customer_id , operations = operations
159+ response : ApplyRecommendationResult = (
160+ recommendation_service .apply_recommendation (
161+ customer_id = customer_id , operations = operations
162+ )
142163 )
143164
144165 for result in response .results :
145166 print (
146167 "Applied a recommendation with resource name: "
147- f"'{ result [ 0 ] .resource_name } '."
168+ f"'{ result .resource_name } '."
148169 )
149170 # [END apply_recommendation]
150171
151172
152173if __name__ == "__main__" :
153- parser = argparse .ArgumentParser (
174+ parser : argparse . ArgumentParser = argparse .ArgumentParser (
154175 description = (
155176 "Retrieves keyword recommendations for specified customer and "
156177 "applies them."
@@ -164,11 +185,13 @@ def apply_recommendations(client, customer_id, operations):
164185 required = True ,
165186 help = "The Google Ads customer ID." ,
166187 )
167- args = parser .parse_args ()
188+ args : argparse . Namespace = parser .parse_args ()
168189
169190 # GoogleAdsClient will read the google-ads.yaml configuration file in the
170191 # home directory if none is specified.
171- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
192+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
193+ version = "v20"
194+ )
172195
173196 try :
174197 main (googleads_client , args .customer_id )
0 commit comments