2525
2626from google .ads .googleads .client import GoogleAdsClient
2727from google .ads .googleads .errors import GoogleAdsException
28-
29-
30- def main (client , customer_id , ad_group_id ):
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+ )
54+
55+
56+ def main (client : GoogleAdsClient , customer_id : str , ad_group_id : str ) -> None :
3157 """The main method that creates all necessary entities for the example.
3258
3359 Args:
3460 client: an initialized GoogleAdsClient instance.
3561 customer_id: a client customer ID.
3662 ad_group_id: an ad group ID.
3763 """
38- text_customizer_name = f"Planet_{ uuid4 ().hex [:8 ]} "
39- price_customizer_name = f"Price_{ uuid4 ().hex [:8 ]} "
64+ text_customizer_name : str = f"Planet_{ uuid4 ().hex [:8 ]} "
65+ price_customizer_name : str = f"Price_{ uuid4 ().hex [:8 ]} "
4066
41- text_customizer_resource_name = create_text_customizer_attribute (
67+ text_customizer_resource_name : str = create_text_customizer_attribute (
4268 client , customer_id , text_customizer_name
4369 )
44- price_customizer_resource_name = create_price_customizer_attribute (
70+ price_customizer_resource_name : str = create_price_customizer_attribute (
4571 client , customer_id , price_customizer_name
4672 )
4773 link_customizer_attributes (
@@ -61,7 +87,9 @@ def main(client, customer_id, ad_group_id):
6187
6288
6389# [START add_ad_customizer]
64- def create_text_customizer_attribute (client , customer_id , customizer_name ):
90+ def create_text_customizer_attribute (
91+ client : GoogleAdsClient , customer_id : str , customizer_name : str
92+ ) -> str :
6593 """Creates a text customizer attribute and returns its resource name.
6694
6795 Args:
@@ -72,22 +100,26 @@ def create_text_customizer_attribute(client, customer_id, customizer_name):
72100 Returns:
73101 a resource name for a text customizer attribute.
74102 """
75- customizer_attribute_service = client . get_service (
76- "CustomizerAttributeService"
103+ customizer_attribute_service : CustomizerAttributeServiceClient = (
104+ client . get_service ( "CustomizerAttributeService" )
77105 )
78106
79107 # Creates a text customizer attribute. The customizer attribute name is
80108 # arbitrary and will be used as a placeholder in the ad text fields.
81- operation = client .get_type ("CustomizerAttributeOperation" )
82- text_attribute = operation .create
109+ operation : CustomizerAttributeOperation = client .get_type (
110+ "CustomizerAttributeOperation"
111+ )
112+ text_attribute : CustomizerAttribute = operation .create
83113 text_attribute .name = customizer_name
84114 text_attribute .type_ = client .enums .CustomizerAttributeTypeEnum .TEXT
85115
86- response = customizer_attribute_service .mutate_customizer_attributes (
87- customer_id = customer_id , operations = [operation ]
116+ response : MutateCustomizerAttributesResponse = (
117+ customizer_attribute_service .mutate_customizer_attributes (
118+ customer_id = customer_id , operations = [operation ]
119+ )
88120 )
89121
90- resource_name = response .results [0 ].resource_name
122+ resource_name : str = response .results [0 ].resource_name
91123 print (
92124 f"Added text customizer attribute with resource name '{ resource_name } '"
93125 )
@@ -96,7 +128,9 @@ def create_text_customizer_attribute(client, customer_id, customizer_name):
96128
97129
98130# [START add_ad_customizer_1]
99- def create_price_customizer_attribute (client , customer_id , customizer_name ):
131+ def create_price_customizer_attribute (
132+ client : GoogleAdsClient , customer_id : str , customizer_name : str
133+ ) -> str :
100134 """Creates a price customizer attribute and returns its resource name.
101135
102136 Args:
@@ -107,22 +141,26 @@ def create_price_customizer_attribute(client, customer_id, customizer_name):
107141 Returns:
108142 a resource name for a text customizer attribute.
109143 """
110- customizer_attribute_service = client . get_service (
111- "CustomizerAttributeService"
144+ customizer_attribute_service : CustomizerAttributeServiceClient = (
145+ client . get_service ( "CustomizerAttributeService" )
112146 )
113147
114148 # Creates a price customizer attribute. The customizer attribute name is
115149 # arbitrary and will be used as a placeholder in the ad text fields.
116- operation = client .get_type ("CustomizerAttributeOperation" )
117- price_attribute = operation .create
150+ operation : CustomizerAttributeOperation = client .get_type (
151+ "CustomizerAttributeOperation"
152+ )
153+ price_attribute : CustomizerAttribute = operation .create
118154 price_attribute .name = customizer_name
119155 price_attribute .type_ = client .enums .CustomizerAttributeTypeEnum .PRICE
120156
121- response = customizer_attribute_service .mutate_customizer_attributes (
122- customer_id = customer_id , operations = [operation ]
157+ response : MutateCustomizerAttributesResponse = (
158+ customizer_attribute_service .mutate_customizer_attributes (
159+ customer_id = customer_id , operations = [operation ]
160+ )
123161 )
124162
125- resource_name = response .results [0 ].resource_name
163+ resource_name : str = response .results [0 ].resource_name
126164 print (
127165 f"Added price customizer attribute with resource name '{ resource_name } '"
128166 )
@@ -132,12 +170,12 @@ def create_price_customizer_attribute(client, customer_id, customizer_name):
132170
133171# [START add_ad_customizer_2]
134172def link_customizer_attributes (
135- client ,
136- customer_id ,
137- ad_group_id ,
138- text_customizer_resource_name ,
139- price_customizer_resource_name ,
140- ):
173+ client : GoogleAdsClient ,
174+ customer_id : str ,
175+ ad_group_id : str ,
176+ text_customizer_resource_name : str ,
177+ price_customizer_resource_name : str ,
178+ ) -> None :
141179 """Restricts the ad customizer attributes to work with a specific ad group.
142180
143181 This prevents the customizer attributes from being used elsewhere and makes
@@ -150,13 +188,19 @@ def link_customizer_attributes(
150188 text_customizer_resource_name: the resource name of the text customizer attribute.
151189 price_customizer_resource_name: the resource name of the price customizer attribute.
152190 """
153- googleads_service = client .get_service ("GoogleAdsService" )
154- ad_group_customizer_service = client .get_service ("AdGroupCustomizerService" )
191+ googleads_service : GoogleAdsServiceClient = client .get_service (
192+ "GoogleAdsService"
193+ )
194+ ad_group_customizer_service : AdGroupCustomizerServiceClient = (
195+ client .get_service ("AdGroupCustomizerService" )
196+ )
155197
156198 # Binds the text attribute customizer to a specific ad group to make sure
157199 # it will only be used to customize ads inside that ad group.
158- mars_operation = client .get_type ("AdGroupCustomizerOperation" )
159- mars_customizer = mars_operation .create
200+ mars_operation : AdGroupCustomizerOperation = client .get_type (
201+ "AdGroupCustomizerOperation"
202+ )
203+ mars_customizer : AdGroupCustomizer = mars_operation .create
160204 mars_customizer .customizer_attribute = text_customizer_resource_name
161205 mars_customizer .value .type_ = client .enums .CustomizerAttributeTypeEnum .TEXT
162206 mars_customizer .value .string_value = "Mars"
@@ -166,8 +210,10 @@ def link_customizer_attributes(
166210
167211 # Binds the price attribute customizer to a specific ad group to make sure
168212 # it will only be used to customize ads inside that ad group.
169- price_operation = client .get_type ("AdGroupCustomizerOperation" )
170- price_customizer = price_operation .create
213+ price_operation : AdGroupCustomizerOperation = client .get_type (
214+ "AdGroupCustomizerOperation"
215+ )
216+ price_customizer : AdGroupCustomizer = price_operation .create
171217 price_customizer .customizer_attribute = price_customizer_resource_name
172218 price_customizer .value .type_ = (
173219 client .enums .CustomizerAttributeTypeEnum .PRICE
@@ -177,8 +223,11 @@ def link_customizer_attributes(
177223 customer_id , ad_group_id
178224 )
179225
180- response = ad_group_customizer_service .mutate_ad_group_customizers (
181- 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+ )
182231 )
183232
184233 for result in response .results :
@@ -191,12 +240,12 @@ def link_customizer_attributes(
191240
192241# [START add_ad_customizer_3]
193242def create_ad_with_customizations (
194- client ,
195- customer_id ,
196- ad_group_id ,
197- text_customizer_name ,
198- price_customizer_name ,
199- ):
243+ client : GoogleAdsClient ,
244+ customer_id : str ,
245+ ad_group_id : str ,
246+ text_customizer_name : str ,
247+ price_customizer_name : str ,
248+ ) -> None :
200249 """Creates a responsive search ad (RSA).
201250
202251 The RSA uses the ad customizer attributes to populate the placeholders.
@@ -208,41 +257,45 @@ def create_ad_with_customizations(
208257 text_customizer_name: name of the text customizer.
209258 price_customizer_name: name of the price customizer.
210259 """
211- googleads_service = client .get_service ("GoogleAdsService" )
212- ad_group_ad_service = 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+ )
213266
214267 # Creates a responsive search ad using the attribute customizer names as
215268 # placeholders and default values to be used in case there are no attribute
216269 # customizer values.
217- operation = client .get_type ("AdGroupAdOperation" )
218- ad_group_ad = operation .create
270+ operation : AdGroupAdOperation = client .get_type ("AdGroupAdOperation" )
271+ ad_group_ad : AdGroupAd = operation .create
219272 ad_group_ad .ad .final_urls .append ("https://www.example.com" )
220273 ad_group_ad .ad_group = googleads_service .ad_group_path (
221274 customer_id , ad_group_id
222275 )
223276
224- headline_1 = client .get_type ("AdTextAsset" )
277+ headline_1 : AdTextAsset = client .get_type ("AdTextAsset" )
225278 headline_1 .text = (
226279 f"Luxury cruise to {{CUSTOMIZER.{ text_customizer_name } :Venus}}"
227280 )
228281 headline_1 .pinned_field = client .enums .ServedAssetFieldTypeEnum .HEADLINE_1
229282
230- headline_2 = client .get_type ("AdTextAsset" )
283+ headline_2 : AdTextAsset = client .get_type ("AdTextAsset" )
231284 headline_2 .text = f"Only {{CUSTOMIZER.{ price_customizer_name } :10.0€}}"
232285
233- headline_3 = client .get_type ("AdTextAsset" )
286+ headline_3 : AdTextAsset = client .get_type ("AdTextAsset" )
234287 headline_3 .text = f"Cruise to {{CUSTOMIZER.{ text_customizer_name } :Venus}} for {{CUSTOMIZER.{ price_customizer_name } :10.0€}}"
235288
236289 ad_group_ad .ad .responsive_search_ad .headlines .extend (
237290 [headline_1 , headline_2 , headline_3 ]
238291 )
239292
240- description_1 = client .get_type ("AdTextAsset" )
293+ description_1 : AdTextAsset = client .get_type ("AdTextAsset" )
241294 description_1 .text = (
242295 f"Tickets are only {{CUSTOMIZER.{ price_customizer_name } :10.0€}}!"
243296 )
244297
245- description_2 = client .get_type ("AdTextAsset" )
298+ description_2 : AdTextAsset = client .get_type ("AdTextAsset" )
246299 description_2 .text = (
247300 f"Buy your tickets to {{CUSTOMIZER.{ text_customizer_name } :Venus}} now!"
248301 )
@@ -251,10 +304,12 @@ def create_ad_with_customizations(
251304 [description_1 , description_2 ]
252305 )
253306
254- response = ad_group_ad_service .mutate_ad_group_ads (
255- 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+ )
256311 )
257- resource_name = response .results [0 ].resource_name
312+ resource_name : str = response .results [0 ].resource_name
258313 print (f"Added an ad with resource name '{ resource_name } '" )
259314 # [END add_ad_customizer_3]
260315
@@ -282,11 +337,13 @@ def create_ad_with_customizations(
282337 required = True ,
283338 help = "An ad group ID." ,
284339 )
285- args = parser .parse_args ()
340+ args : argparse . Namespace = parser .parse_args ()
286341
287342 # GoogleAdsClient will read the google-ads.yaml configuration file in the
288343 # home directory if none is specified.
289- googleads_client = GoogleAdsClient .load_from_storage (version = "v21" )
344+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
345+ version = "v21"
346+ )
290347
291348 try :
292349 main (googleads_client , args .customer_id , args .ad_group_id )
0 commit comments