5757
5858
5959# [START add_performance_max_campaign]
60- def main (
61- client ,
62- customer_id ,
63- ):
60+ def main (client , customer_id , audience_id ):
6461 """The main method that creates all necessary entities for the example.
6562
6663 Args:
6764 client: an initialized GoogleAdsClient instance.
6865 customer_id: a client customer ID.
66+ audience_id: an optional audience ID.
6967 """
7068 # [START add_performance_max_campaign_1]
7169 googleads_service = client .get_service ("GoogleAdsService" )
@@ -77,22 +75,11 @@ def main(
7775 #
7876 # Create the headlines.
7977 headline_asset_resource_names = _create_multiple_text_assets (
80- client ,
81- customer_id ,
82- [
83- "Travel" ,
84- "Travel Reviews" ,
85- "Book travel" ,
86- ],
78+ client , customer_id , ["Travel" , "Travel Reviews" , "Book travel" ,],
8779 )
8880 # Create the descriptions.
8981 description_asset_resource_names = _create_multiple_text_assets (
90- client ,
91- customer_id ,
92- [
93- "Take to the air!" ,
94- "Fly to the sky!" ,
95- ],
82+ client , customer_id , ["Take to the air!" , "Fly to the sky!" ,],
9683 )
9784
9885 # The below methods create and return MutateOperations that we later
@@ -103,18 +90,13 @@ def main(
10390 # successfully or fail entirely, leaving no orphaned entities. See:
10491 # https://developers.google.com/google-ads/api/docs/mutating/overview
10592 campaign_budget_operation = _create_campaign_budget_operation (
106- client ,
107- customer_id ,
93+ client , customer_id ,
10894 )
109- performance_max_campaign_operation = (
110- _create_performance_max_campaign_operation (
111- client ,
112- customer_id ,
113- )
95+ performance_max_campaign_operation = _create_performance_max_campaign_operation (
96+ client , customer_id ,
11497 )
11598 campaign_criterion_operations = _create_campaign_criterion_operations (
116- client ,
117- customer_id ,
99+ client , customer_id ,
118100 )
119101 asset_group_operations = _create_asset_group_operation (
120102 client ,
@@ -123,28 +105,37 @@ def main(
123105 description_asset_resource_names ,
124106 )
125107
108+ mutate_operations = [
109+ # It's important to create these entities in this order because
110+ # they depend on each other.
111+ campaign_budget_operation ,
112+ performance_max_campaign_operation ,
113+ # Expand the list of multiple operations into the list of
114+ # other mutate operations
115+ * campaign_criterion_operations ,
116+ * asset_group_operations ,
117+ ]
118+
119+ # Append an asset group signal operation is an audience ID is given.
120+ if audience_id :
121+ mutate_operations .append (
122+ _create_asset_group_signal_operation (
123+ client , customer_id , audience_id
124+ )
125+ )
126+
126127 # Send the operations in a single Mutate request.
127128 response = googleads_service .mutate (
128- customer_id = customer_id ,
129- mutate_operations = [
130- # It's important to create these entities in this order because
131- # they depend on each other.
132- campaign_budget_operation ,
133- performance_max_campaign_operation ,
134- # Expand the list of multiple operations into the list of
135- # other mutate operations
136- * campaign_criterion_operations ,
137- * asset_group_operations ,
138- ],
129+ customer_id = customer_id , mutate_operations = mutate_operations
139130 )
131+
140132 _print_response_details (response )
141133 # [END add_performance_max_campaign_1]
142134
143135
144136# [START add_performance_max_campaign_2]
145137def _create_campaign_budget_operation (
146- client ,
147- customer_id ,
138+ client , customer_id ,
148139):
149140 """Creates a MutateOperation that creates a new CampaignBudget.
150141
@@ -182,8 +173,7 @@ def _create_campaign_budget_operation(
182173
183174# [START add_performance_max_campaign_3]
184175def _create_performance_max_campaign_operation (
185- client ,
186- customer_id ,
176+ client , customer_id ,
187177):
188178 """Creates a MutateOperation that creates a new Performance Max campaign.
189179
@@ -252,8 +242,7 @@ def _create_performance_max_campaign_operation(
252242
253243# [START add_performance_max_campaign_4]
254244def _create_campaign_criterion_operations (
255- client ,
256- customer_id ,
245+ client , customer_id ,
257246):
258247 """Creates a list of MutateOperations that create new campaign criteria.
259248
@@ -284,8 +273,8 @@ def _create_campaign_criterion_operations(
284273 campaign_criterion .campaign = campaign_service .campaign_path (
285274 customer_id , _PERFORMANCE_MAX_CAMPAIGN_TEMPORARY_ID
286275 )
287- campaign_criterion .location .geo_target_constant = (
288- geo_target_constant_service . geo_target_constant_path ( "1023191" )
276+ campaign_criterion .location .geo_target_constant = geo_target_constant_service . geo_target_constant_path (
277+ "1023191"
289278 )
290279 campaign_criterion .negative = False
291280 operations .append (mutate_operation )
@@ -296,8 +285,8 @@ def _create_campaign_criterion_operations(
296285 campaign_criterion .campaign = campaign_service .campaign_path (
297286 customer_id , _PERFORMANCE_MAX_CAMPAIGN_TEMPORARY_ID
298287 )
299- campaign_criterion .location .geo_target_constant = (
300- geo_target_constant_service . geo_target_constant_path ( "1022762" )
288+ campaign_criterion .location .geo_target_constant = geo_target_constant_service . geo_target_constant_path (
289+ "1022762"
301290 )
302291 campaign_criterion .negative = True
303292 operations .append (mutate_operation )
@@ -311,9 +300,9 @@ def _create_campaign_criterion_operations(
311300 # Set the language.
312301 # For a list of all language codes, see:
313302 # https://developers.google.com/google-ads/api/reference/data/codes-formats#expandable-7
314- campaign_criterion .language .language_constant = (
315- googleads_service . language_constant_path ( "1000" ) # English
316- )
303+ campaign_criterion .language .language_constant = googleads_service . language_constant_path (
304+ "1000"
305+ ) # English
317306 operations .append (mutate_operation )
318307
319308 return operations
@@ -346,8 +335,7 @@ def _create_multiple_text_assets(client, customer_id, texts):
346335
347336 # Send the operations in a single Mutate request.
348337 response = googleads_service .mutate (
349- customer_id = customer_id ,
350- mutate_operations = operations ,
338+ customer_id = customer_id , mutate_operations = operations ,
351339 )
352340 asset_resource_names = []
353341 for result in response .mutate_operation_responses :
@@ -395,8 +383,7 @@ def _create_asset_group_operation(
395383 asset_group .final_mobile_urls .append ("http://www.example.com" )
396384 asset_group .status = client .enums .AssetGroupStatusEnum .PAUSED
397385 asset_group .resource_name = asset_group_service .asset_group_path (
398- customer_id ,
399- _ASSET_GROUP_TEMPORARY_ID ,
386+ customer_id , _ASSET_GROUP_TEMPORARY_ID ,
400387 )
401388 operations .append (mutate_operation )
402389
@@ -420,8 +407,7 @@ def _create_asset_group_operation(
420407 asset_group_asset = mutate_operation .asset_group_asset_operation .create
421408 asset_group_asset .field_type = client .enums .AssetFieldTypeEnum .HEADLINE
422409 asset_group_asset .asset_group = asset_group_service .asset_group_path (
423- customer_id ,
424- _ASSET_GROUP_TEMPORARY_ID ,
410+ customer_id , _ASSET_GROUP_TEMPORARY_ID ,
425411 )
426412 asset_group_asset .asset = resource_name
427413 operations .append (mutate_operation )
@@ -434,8 +420,7 @@ def _create_asset_group_operation(
434420 client .enums .AssetFieldTypeEnum .DESCRIPTION
435421 )
436422 asset_group_asset .asset_group = asset_group_service .asset_group_path (
437- customer_id ,
438- _ASSET_GROUP_TEMPORARY_ID ,
423+ customer_id , _ASSET_GROUP_TEMPORARY_ID ,
439424 )
440425 asset_group_asset .asset = resource_name
441426 operations .append (mutate_operation )
@@ -523,8 +508,7 @@ def _create_and_link_text_asset(client, customer_id, text, field_type):
523508 asset_group_asset = mutate_operation .asset_group_asset_operation .create
524509 asset_group_asset .field_type = field_type
525510 asset_group_asset .asset_group = asset_group_service .asset_group_path (
526- customer_id ,
527- _ASSET_GROUP_TEMPORARY_ID ,
511+ customer_id , _ASSET_GROUP_TEMPORARY_ID ,
528512 )
529513 asset_group_asset .asset = asset_service .asset_path (
530514 customer_id , next_temp_id
@@ -574,8 +558,7 @@ def _create_and_link_image_asset(
574558 asset_group_asset = mutate_operation .asset_group_asset_operation .create
575559 asset_group_asset .field_type = field_type
576560 asset_group_asset .asset_group = asset_group_service .asset_group_path (
577- customer_id ,
578- _ASSET_GROUP_TEMPORARY_ID ,
561+ customer_id , _ASSET_GROUP_TEMPORARY_ID ,
579562 )
580563 asset_group_asset .asset = asset_service .asset_path (
581564 customer_id , next_temp_id
@@ -624,6 +607,39 @@ def _print_response_details(response):
624607 )
625608
626609
610+ # [START add_performance_max_campaign_9]
611+ def _create_asset_group_signal_operation (client , customer_id , audience_id ):
612+ """Creates a list of MutateOperations that may create asset group signals.
613+
614+ Args:
615+ client: an initialized GoogleAdsClient instance.
616+ customer_id: a client customer ID.
617+ audience_id: an optional audience ID.
618+
619+ Returns:
620+ MutateOperations that create new asset group signals.
621+ """
622+ if not audience_id :
623+ return None
624+
625+ googleads_service = client .get_service ("GoogleAdsService" )
626+ asset_group_resource_name = googleads_service .asset_group_path (
627+ customer_id , _ASSET_GROUP_TEMPORARY_ID
628+ )
629+
630+ mutate_operation = client .get_type ("MutateOperation" )
631+ operation = mutate_operation .asset_group_signal_operation .create
632+ # To learn more about Audience Signals, see:
633+ # https://developers.google.com/google-ads/api/docs/performance-max/asset-groups#audience_signals
634+ operation .asset_group = asset_group_resource_name
635+ operation .audience .audience = googleads_service .audience_path (
636+ customer_id , audience_id
637+ )
638+
639+ return mutate_operation
640+ # [END add_performance_max_campaign_9]
641+
642+
627643# [END add_performance_max_campaign]
628644
629645if __name__ == "__main__" :
@@ -642,14 +658,14 @@ def _print_response_details(response):
642658 required = True ,
643659 help = "The Google Ads customer ID." ,
644660 )
661+ parser .add_argument (
662+ "-a" , "--audience_id" , type = str , help = "The ID of an audience." ,
663+ )
645664
646665 args = parser .parse_args ()
647666
648667 try :
649- main (
650- googleads_client ,
651- args .customer_id ,
652- )
668+ main (googleads_client , args .customer_id , args .audience_id )
653669 except GoogleAdsException as ex :
654670 print (
655671 f'Request with ID "{ ex .request_id } " failed with status '
0 commit comments