@@ -104,16 +104,13 @@ def _create_add_customizer_feed(client, customer_id, feed_name):
104104
105105 feed_service = client .get_service ("FeedService" )
106106
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]
117114
118115
119116# [START add_ad_customizer_1]
@@ -142,12 +139,9 @@ def _get_feed_attributes(client, customer_id, feed_resource_name):
142139 search_request .query = query
143140 search_request .page_size = 1
144141
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 } " )
151145
152146 feed_details = {}
153147 for feed_attribute in feed .attributes :
@@ -206,17 +200,14 @@ def _create_ad_customizer_mapping(
206200
207201 feed_mapping_service = client .get_service ("FeedMappingService" )
208202
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 } "
212210 )
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 )
220211 # [END add_ad_customizer_2]
221212
222213
@@ -266,14 +257,11 @@ def _create_feed_items(
266257
267258 feed_item_service = client .get_service ("FeedItemService" )
268259
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]
277265
278266
279267# [START add_ad_customizer_4]
@@ -353,17 +341,14 @@ def _create_feed_item_targets(
353341 customer_id , ad_group_id
354342 )
355343
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]
367352
368353
369354# [START add_ad_customizer_6]
@@ -405,34 +390,13 @@ def _create_ads_with_customizations(
405390 )
406391 ad_group_ad_operations .append (ad_group_ad_operation )
407392
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
429395 )
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]
436400
437401
438402if __name__ == "__main__" :
@@ -463,4 +427,16 @@ def _handle_googleads_exception(exception):
463427 )
464428 args = parser .parse_args ()
465429
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