2121
2222import argparse
2323import sys
24- from typing import Any
2524from uuid import uuid4
2625
2726from google .ads .googleads .client import GoogleAdsClient
2827from google .ads .googleads .errors import GoogleAdsException
28+ from google .ads .googleads .v21 .common .types import AdTextAsset
29+ from google .ads .googleads .v21 .resources .types import (
30+ AdGroupAd ,
31+ AdGroupCustomizer ,
32+ CustomizerAttribute ,
33+ )
34+ from google .ads .googleads .v21 .services .services .ad_group_ad_service import (
35+ AdGroupAdServiceClient ,
36+ )
37+ from google .ads .googleads .v21 .services .services .ad_group_customizer_service import (
38+ AdGroupCustomizerServiceClient ,
39+ )
40+ from google .ads .googleads .v21 .services .services .customizer_attribute_service import (
41+ CustomizerAttributeServiceClient ,
42+ )
43+ from google .ads .googleads .v21 .services .services .google_ads_service import (
44+ GoogleAdsServiceClient ,
45+ )
46+ from google .ads .googleads .v21 .services .types import (
47+ AdGroupAdOperation ,
48+ AdGroupCustomizerOperation ,
49+ CustomizerAttributeOperation ,
50+ MutateAdGroupAdsResponse ,
51+ MutateAdGroupCustomizersResponse ,
52+ MutateCustomizerAttributesResponse ,
53+ )
2954
3055
3156def main (client : GoogleAdsClient , customer_id : str , ad_group_id : str ) -> None :
@@ -75,19 +100,23 @@ def create_text_customizer_attribute(
75100 Returns:
76101 a resource name for a text customizer attribute.
77102 """
78- customizer_attribute_service : Any = client . get_service (
79- "CustomizerAttributeService"
103+ customizer_attribute_service : CustomizerAttributeServiceClient = (
104+ client . get_service ( "CustomizerAttributeService" )
80105 )
81106
82107 # Creates a text customizer attribute. The customizer attribute name is
83108 # arbitrary and will be used as a placeholder in the ad text fields.
84- operation : Any = client .get_type ("CustomizerAttributeOperation" )
85- text_attribute : Any = operation .create
109+ operation : CustomizerAttributeOperation = client .get_type (
110+ "CustomizerAttributeOperation"
111+ )
112+ text_attribute : CustomizerAttribute = operation .create
86113 text_attribute .name = customizer_name
87114 text_attribute .type_ = client .enums .CustomizerAttributeTypeEnum .TEXT
88115
89- response : Any = customizer_attribute_service .mutate_customizer_attributes (
90- customer_id = customer_id , operations = [operation ]
116+ response : MutateCustomizerAttributesResponse = (
117+ customizer_attribute_service .mutate_customizer_attributes (
118+ customer_id = customer_id , operations = [operation ]
119+ )
91120 )
92121
93122 resource_name : str = response .results [0 ].resource_name
@@ -112,19 +141,23 @@ def create_price_customizer_attribute(
112141 Returns:
113142 a resource name for a text customizer attribute.
114143 """
115- customizer_attribute_service : Any = client . get_service (
116- "CustomizerAttributeService"
144+ customizer_attribute_service : CustomizerAttributeServiceClient = (
145+ client . get_service ( "CustomizerAttributeService" )
117146 )
118147
119148 # Creates a price customizer attribute. The customizer attribute name is
120149 # arbitrary and will be used as a placeholder in the ad text fields.
121- operation : Any = client .get_type ("CustomizerAttributeOperation" )
122- price_attribute : Any = operation .create
150+ operation : CustomizerAttributeOperation = client .get_type (
151+ "CustomizerAttributeOperation"
152+ )
153+ price_attribute : CustomizerAttribute = operation .create
123154 price_attribute .name = customizer_name
124155 price_attribute .type_ = client .enums .CustomizerAttributeTypeEnum .PRICE
125156
126- response : Any = customizer_attribute_service .mutate_customizer_attributes (
127- customer_id = customer_id , operations = [operation ]
157+ response : MutateCustomizerAttributesResponse = (
158+ customizer_attribute_service .mutate_customizer_attributes (
159+ customer_id = customer_id , operations = [operation ]
160+ )
128161 )
129162
130163 resource_name : str = response .results [0 ].resource_name
@@ -155,15 +188,19 @@ def link_customizer_attributes(
155188 text_customizer_resource_name: the resource name of the text customizer attribute.
156189 price_customizer_resource_name: the resource name of the price customizer attribute.
157190 """
158- googleads_service : Any = client .get_service ("GoogleAdsService" )
159- ad_group_customizer_service : Any = client .get_service (
160- "AdGroupCustomizerService"
191+ googleads_service : GoogleAdsServiceClient = client .get_service (
192+ "GoogleAdsService"
193+ )
194+ ad_group_customizer_service : AdGroupCustomizerServiceClient = (
195+ client .get_service ("AdGroupCustomizerService" )
161196 )
162197
163198 # Binds the text attribute customizer to a specific ad group to make sure
164199 # it will only be used to customize ads inside that ad group.
165- mars_operation : Any = client .get_type ("AdGroupCustomizerOperation" )
166- mars_customizer : Any = mars_operation .create
200+ mars_operation : AdGroupCustomizerOperation = client .get_type (
201+ "AdGroupCustomizerOperation"
202+ )
203+ mars_customizer : AdGroupCustomizer = mars_operation .create
167204 mars_customizer .customizer_attribute = text_customizer_resource_name
168205 mars_customizer .value .type_ = client .enums .CustomizerAttributeTypeEnum .TEXT
169206 mars_customizer .value .string_value = "Mars"
@@ -173,8 +210,10 @@ def link_customizer_attributes(
173210
174211 # Binds the price attribute customizer to a specific ad group to make sure
175212 # it will only be used to customize ads inside that ad group.
176- price_operation : Any = client .get_type ("AdGroupCustomizerOperation" )
177- price_customizer : Any = price_operation .create
213+ price_operation : AdGroupCustomizerOperation = client .get_type (
214+ "AdGroupCustomizerOperation"
215+ )
216+ price_customizer : AdGroupCustomizer = price_operation .create
178217 price_customizer .customizer_attribute = price_customizer_resource_name
179218 price_customizer .value .type_ = (
180219 client .enums .CustomizerAttributeTypeEnum .PRICE
@@ -184,8 +223,11 @@ def link_customizer_attributes(
184223 customer_id , ad_group_id
185224 )
186225
187- response : Any = ad_group_customizer_service .mutate_ad_group_customizers (
188- customer_id = customer_id , operations = [mars_operation , price_operation ]
226+ response : MutateAdGroupCustomizersResponse = (
227+ ad_group_customizer_service .mutate_ad_group_customizers (
228+ customer_id = customer_id ,
229+ operations = [mars_operation , price_operation ],
230+ )
189231 )
190232
191233 for result in response .results :
@@ -215,41 +257,45 @@ def create_ad_with_customizations(
215257 text_customizer_name: name of the text customizer.
216258 price_customizer_name: name of the price customizer.
217259 """
218- googleads_service : Any = client .get_service ("GoogleAdsService" )
219- ad_group_ad_service : Any = client .get_service ("AdGroupAdService" )
260+ googleads_service : GoogleAdsServiceClient = client .get_service (
261+ "GoogleAdsService"
262+ )
263+ ad_group_ad_service : AdGroupAdServiceClient = client .get_service (
264+ "AdGroupAdService"
265+ )
220266
221267 # Creates a responsive search ad using the attribute customizer names as
222268 # placeholders and default values to be used in case there are no attribute
223269 # customizer values.
224- operation : Any = client .get_type ("AdGroupAdOperation" )
225- ad_group_ad : Any = operation .create
270+ operation : AdGroupAdOperation = client .get_type ("AdGroupAdOperation" )
271+ ad_group_ad : AdGroupAd = operation .create
226272 ad_group_ad .ad .final_urls .append ("https://www.example.com" )
227273 ad_group_ad .ad_group = googleads_service .ad_group_path (
228274 customer_id , ad_group_id
229275 )
230276
231- headline_1 : Any = client .get_type ("AdTextAsset" )
277+ headline_1 : AdTextAsset = client .get_type ("AdTextAsset" )
232278 headline_1 .text = (
233279 f"Luxury cruise to {{CUSTOMIZER.{ text_customizer_name } :Venus}}"
234280 )
235281 headline_1 .pinned_field = client .enums .ServedAssetFieldTypeEnum .HEADLINE_1
236282
237- headline_2 : Any = client .get_type ("AdTextAsset" )
283+ headline_2 : AdTextAsset = client .get_type ("AdTextAsset" )
238284 headline_2 .text = f"Only {{CUSTOMIZER.{ price_customizer_name } :10.0€}}"
239285
240- headline_3 : Any = client .get_type ("AdTextAsset" )
286+ headline_3 : AdTextAsset = client .get_type ("AdTextAsset" )
241287 headline_3 .text = f"Cruise to {{CUSTOMIZER.{ text_customizer_name } :Venus}} for {{CUSTOMIZER.{ price_customizer_name } :10.0€}}"
242288
243289 ad_group_ad .ad .responsive_search_ad .headlines .extend (
244290 [headline_1 , headline_2 , headline_3 ]
245291 )
246292
247- description_1 : Any = client .get_type ("AdTextAsset" )
293+ description_1 : AdTextAsset = client .get_type ("AdTextAsset" )
248294 description_1 .text = (
249295 f"Tickets are only {{CUSTOMIZER.{ price_customizer_name } :10.0€}}!"
250296 )
251297
252- description_2 : Any = client .get_type ("AdTextAsset" )
298+ description_2 : AdTextAsset = client .get_type ("AdTextAsset" )
253299 description_2 .text = (
254300 f"Buy your tickets to {{CUSTOMIZER.{ text_customizer_name } :Venus}} now!"
255301 )
@@ -258,8 +304,10 @@ def create_ad_with_customizations(
258304 [description_1 , description_2 ]
259305 )
260306
261- response : Any = ad_group_ad_service .mutate_ad_group_ads (
262- customer_id = customer_id , operations = [operation ]
307+ response : MutateAdGroupAdsResponse = (
308+ ad_group_ad_service .mutate_ad_group_ads (
309+ customer_id = customer_id , operations = [operation ]
310+ )
263311 )
264312 resource_name : str = response .results [0 ].resource_name
265313 print (f"Added an ad with resource name '{ resource_name } '" )
@@ -293,17 +341,9 @@ def create_ad_with_customizations(
293341
294342 # GoogleAdsClient will read the google-ads.yaml configuration file in the
295343 # home directory if none is specified.
296- < << << << HEAD
297- googleads_client = GoogleAdsClient .load_from_storage (version = "v21" )
298- == == == =
299- << << << < HEAD
300- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
301- == == == =
302344 googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
303- version = "v19 "
345+ version = "v21 "
304346 )
305- >> >> >> > 83 bf66141 (Add type hints to files in examples / advanced_operations )
306- > >> >> >> f8ca36136 (Add type hints to files in examples / advanced_operations )
307347
308348 try :
309349 main (googleads_client , args .customer_id , args .ad_group_id )
0 commit comments