@@ -104,16 +104,13 @@ def _create_add_customizer_feed(client, customer_id, feed_name):
104
104
105
105
feed_service = client .get_service ("FeedService" )
106
106
107
- try :
108
- response = feed_service .mutate_feeds (
109
- customer_id = customer_id , operations = [feed_operation ]
110
- )
111
- resource_name = response .results [0 ].resource_name
112
- print (f"Added feed with resource name { resource_name } " )
113
- return resource_name
114
- except GoogleAdsException as ex :
115
- _handle_googleads_exception (ex )
116
- # [END add_ad_customizer]
107
+ response = feed_service .mutate_feeds (
108
+ customer_id = customer_id , operations = [feed_operation ]
109
+ )
110
+ resource_name = response .results [0 ].resource_name
111
+ print (f"Added feed with resource name { resource_name } " )
112
+ return resource_name
113
+ # [END add_ad_customizer]
117
114
118
115
119
116
# [START add_ad_customizer_1]
@@ -142,12 +139,9 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):
142
139
search_request .query = query
143
140
search_request .page_size = 1
144
141
145
- try :
146
- results = ga_service .search (request = search_request )
147
- feed = list (results )[0 ].feed
148
- print (f"Found the following attributes for feed with name { feed .name } " )
149
- except GoogleAdsException as ex :
150
- _handle_googleads_exception (ex )
142
+ results = ga_service .search (request = search_request )
143
+ feed = list (results )[0 ].feed
144
+ print (f"Found the following attributes for feed with name { feed .name } " )
151
145
152
146
feed_details = {}
153
147
for feed_attribute in feed .attributes :
@@ -206,17 +200,14 @@ def _create_ad_customizer_mapping(
206
200
207
201
feed_mapping_service = client .get_service ("FeedMappingService" )
208
202
209
- try :
210
- response = feed_mapping_service .mutate_feed_mappings (
211
- customer_id = customer_id , operations = [feed_mapping_op ]
203
+ response = feed_mapping_service .mutate_feed_mappings (
204
+ customer_id = customer_id , operations = [feed_mapping_op ]
205
+ )
206
+ for result in response .results :
207
+ print (
208
+ "Created feed mapping with resource name "
209
+ f"{ result .resource_name } "
212
210
)
213
- for result in response .results :
214
- print (
215
- "Created feed mapping with resource name "
216
- f"{ result .resource_name } "
217
- )
218
- except GoogleAdsException as ex :
219
- _handle_googleads_exception (ex )
220
211
# [END add_ad_customizer_2]
221
212
222
213
@@ -266,14 +257,11 @@ def _create_feed_items(
266
257
267
258
feed_item_service = client .get_service ("FeedItemService" )
268
259
269
- try :
270
- response = feed_item_service .mutate_feed_items (
271
- customer_id = customer_id , operations = feed_item_operations
272
- )
273
- return [feed_item .resource_name for feed_item in response .results ]
274
- except GoogleAdsException as ex :
275
- _handle_googleads_exception (ex )
276
- # [END add_ad_customizer_3]
260
+ response = feed_item_service .mutate_feed_items (
261
+ customer_id = customer_id , operations = feed_item_operations
262
+ )
263
+ return [feed_item .resource_name for feed_item in response .results ]
264
+ # [END add_ad_customizer_3]
277
265
278
266
279
267
# [START add_ad_customizer_4]
@@ -353,17 +341,14 @@ def _create_feed_item_targets(
353
341
customer_id , ad_group_id
354
342
)
355
343
356
- try :
357
- response = feed_item_target_service .mutate_feed_item_targets (
358
- customer_id = customer_id , operations = [feed_item_target_op ]
359
- )
360
- print (
361
- "Added feed item target with resource name "
362
- f"{ response .results [0 ].resource_name } "
363
- )
364
- except GoogleAdsException as ex :
365
- _handle_googleads_exception (ex )
366
- # [END add_ad_customizer_5]
344
+ response = feed_item_target_service .mutate_feed_item_targets (
345
+ customer_id = customer_id , operations = [feed_item_target_op ]
346
+ )
347
+ print (
348
+ "Added feed item target with resource name "
349
+ f"{ response .results [0 ].resource_name } "
350
+ )
351
+ # [END add_ad_customizer_5]
367
352
368
353
369
354
# [START add_ad_customizer_6]
@@ -405,40 +390,19 @@ def _create_ads_with_customizations(
405
390
)
406
391
ad_group_ad_operations .append (ad_group_ad_operation )
407
392
408
- try :
409
- response = ad_group_ad_service .mutate_ad_group_ads (
410
- customer_id = customer_id , operations = ad_group_ad_operations
411
- )
412
- print (f"Added { len (response .results )} ads:" )
413
- for ad in response .results :
414
- print (f"Added an ad with resource name { ad .resource_name } " )
415
- except GoogleAdsException as ex :
416
- _handle_googleads_exception (ex )
417
- # [END add_ad_customizer_6]
418
-
419
-
420
- def _handle_googleads_exception (exception ):
421
- """Prints the details of a GoogleAdsException object.
422
-
423
- Args:
424
- exception: an instance of GoogleAdsException.
425
- """
426
- print (
427
- f'Request with ID "{ exception .request_id } " failed with status '
428
- f'"{ exception .error .code ().name } " and includes the following errors:'
393
+ response = ad_group_ad_service .mutate_ad_group_ads (
394
+ customer_id = customer_id , operations = ad_group_ad_operations
429
395
)
430
- for error in exception .failure .errors :
431
- print (f'\t Error with message "{ error .message } ".' )
432
- if error .location :
433
- for field_path_element in error .location .field_path_elements :
434
- print (f"\t \t On field: { field_path_element .field_name } " )
435
- sys .exit (1 )
396
+ print (f"Added { len (response .results )} ads:" )
397
+ for ad in response .results :
398
+ print (f"Added an ad with resource name { ad .resource_name } " )
399
+ # [END add_ad_customizer_6]
436
400
437
401
438
402
if __name__ == "__main__" :
439
403
# GoogleAdsClient will read the google-ads.yaml configuration file in the
440
404
# home directory if none is specified.
441
- googleads_client = GoogleAdsClient .load_from_storage (version = "v8 " )
405
+ googleads_client = GoogleAdsClient .load_from_storage (version = "v9 " )
442
406
443
407
parser = argparse .ArgumentParser (
444
408
description = (
@@ -463,4 +427,16 @@ def _handle_googleads_exception(exception):
463
427
)
464
428
args = parser .parse_args ()
465
429
466
- main (googleads_client , args .customer_id , args .ad_group_ids )
430
+ try :
431
+ main (googleads_client , args .customer_id , args .ad_group_ids )
432
+ except GoogleAdsException as ex :
433
+ print (
434
+ f'Request with ID "{ ex .request_id } " failed with status '
435
+ f'"{ ex .error .code ().name } " and includes the following errors:'
436
+ )
437
+ for error in ex .failure .errors :
438
+ print (f'Error with message "{ error .message } ".' )
439
+ if error .location :
440
+ for field_path_element in error .location .field_path_elements :
441
+ print (f"\t \t On field: { field_path_element .field_name } " )
442
+ sys .exit (1 )
0 commit comments