1616This example shows how to create a complete Responsive Search ad.
1717
1818Includes creation of: budget, campaign, ad group, ad group ad,
19- keywords, geo targeting, and image extensions .
19+ keywords, and geo targeting .
2020
2121More details on Responsive Search ads can be found here:
2222https://support.google.com/google-ads/answer/7684791
2626import sys
2727import uuid
2828
29- import requests
30-
3129from google .ads .googleads .client import GoogleAdsClient
3230from google .ads .googleads .errors import GoogleAdsException
3331
3432# Keywords from user.
35- _KEYWORD_TEXT_EXACT_1 = "example of exact match"
36- _KEYWORD_TEXT_PHRASE_1 = "example of phrase match"
37- _KEYWORD_TEXT_BROAD_1 = "example of broad match"
33+ KEYWORD_TEXT_EXACT = "example of exact match"
34+ KEYWORD_TEXT_PHRASE = "example of phrase match"
35+ KEYWORD_TEXT_BROAD = "example of broad match"
3836
3937# Geo targeting from user.
40- _GEO_LOCATION_1 = "Buenos aires"
41- _GEO_LOCATION_2 = "San Isidro"
42- _GEO_LOCATION_3 = "Mar del Plata"
38+ GEO_LOCATION_1 = "Buenos aires"
39+ GEO_LOCATION_2 = "San Isidro"
40+ GEO_LOCATION_3 = "Mar del Plata"
4341
44- # _LOCALE and _COUNTRY_CODE are used for geo targeting.
45- # _LOCALE is using ISO 639-1 format. If an invalid _LOCALE is given,
42+ # LOCALE and COUNTRY_CODE are used for geo targeting.
43+ # LOCALE is using ISO 639-1 format. If an invalid LOCALE is given,
4644# 'es' is used by default.
47- _LOCALE = "es"
45+ LOCALE = "es"
4846
4947# A list of country codes can be referenced here:
5048# https://developers.google.com/google-ads/api/reference/data/geotargets
51- _COUNTRY_CODE = "AR"
49+ COUNTRY_CODE = "AR"
5250
5351
54- def main (
55- client , customer_id , omit_image_extensions , customizer_attribute_name = None
56- ):
52+ def main (client , customer_id , customizer_attribute_name = None ):
5753 """
5854 The main method that creates all necessary entities for the example.
5955
6056 Args:
6157 client: an initialized GoogleAdsClient instance.
6258 customer_id: a client customer ID.
63-
64- Returns:
65- A responsive search ad with all settings required to run campaign.
59+ customizer_attribute_name: The name of the customizer attribute to be
60+ created
6661 """
67-
6862 if customizer_attribute_name :
6963 customizer_attribute_resource_name = create_customizer_attribute (
7064 client , customer_id , customizer_attribute_name
@@ -93,15 +87,7 @@ def main(
9387
9488 add_geo_targeting (client , customer_id , campaign_resource_name )
9589
96- # This is optional but recommended for RSA.
97- # To add image extensions, the account has to follow these requirements:
98- # https://support.google.com/google-ads/answer/9566341
99- # If the account meets the requirements, set below variable to True.
100- if omit_image_extensions :
101- add_images (client , customer_id , campaign_resource_name )
10290
103-
104- # [START add_responsive_search_ad_full_customizer_1]
10591def create_customizer_attribute (client , customer_id , customizer_attribute_name ):
10692 """Creates a customizer attribute with the given customizer attribute name.
10793
@@ -119,7 +105,7 @@ def create_customizer_attribute(client, customer_id, customizer_attribute_name):
119105 # Create a customizer attribute with the specified name.
120106 customizer_attribute = operation .create
121107 customizer_attribute .name = customizer_attribute_name
122- # Specifie the type to be 'PRICE' so that we can dynamically customize the
108+ # Specify the type to be 'PRICE' so that we can dynamically customize the
123109 # part of the ad's description that is a price of a product/service we
124110 # advertise.
125111 customizer_attribute .type_ = client .enums .CustomizerAttributeTypeEnum .PRICE
@@ -139,10 +125,6 @@ def create_customizer_attribute(client, customer_id, customizer_attribute_name):
139125 return resource_name
140126
141127
142- # [END add_responsive_search_ad_full_customizer_1]
143-
144-
145- # [START add_responsive_search_full_customizer_2]
146128def link_customizer_attribute_to_customer (
147129 client , customer_id , customizer_attribute_resource_name
148130):
@@ -184,11 +166,9 @@ def link_customizer_attribute_to_customer(
184166 )
185167
186168
187- # [END add_responsive_search_full_customizer_2]
188-
189-
190169def create_ad_text_asset (client , text , pinned_field = None ):
191170 """Create an AdTextAsset.
171+
192172 Args:
193173 client: an initialized GoogleAdsClient instance.
194174 text: text for headlines and descriptions.
@@ -285,7 +265,7 @@ def create_campaign(client, customer_id, campaign_budget):
285265 # The bidding strategy for Maximize Clicks is TargetSpend.
286266 # The target_spend_micros is deprecated so don't put any value.
287267 # See other bidding strategies you can select in the link below.
288- # https://developers.google.com/google-ads/api/reference/rpc/v11 /Campaign#campaign_bidding_strategy
268+ # https://developers.google.com/google-ads/api/reference/rpc/latest /Campaign#campaign_bidding_strategy
289269 campaign .target_spend .target_spend_micros = 0
290270 campaign .campaign_budget = campaign_budget
291271
@@ -369,7 +349,7 @@ def create_ad_group_ad(
369349 ad_group_ad .ad_group = ad_group_resource_name
370350
371351 # Set responsive search ad info.
372- # https://developers.google.com/google-ads/api/reference/rpc/v11 /ResponsiveSearchAdInfo
352+ # https://developers.google.com/google-ads/api/reference/rpc/latest /ResponsiveSearchAdInfo
373353
374354 # The list of possible final URLs after all cross-domain redirects for the ad.
375355 ad_group_ad .ad .final_urls .append ("https://www.example.com/" )
@@ -450,7 +430,7 @@ def add_keywords(client, customer_id, ad_group_resource_name):
450430 ad_group_criterion = ad_group_criterion_operation .create
451431 ad_group_criterion .ad_group = ad_group_resource_name
452432 ad_group_criterion .status = client .enums .AdGroupCriterionStatusEnum .ENABLED
453- ad_group_criterion .keyword .text = _KEYWORD_TEXT_EXACT_1
433+ ad_group_criterion .keyword .text = KEYWORD_TEXT_EXACT
454434 ad_group_criterion .keyword .match_type = (
455435 client .enums .KeywordMatchTypeEnum .EXACT
456436 )
@@ -469,7 +449,7 @@ def add_keywords(client, customer_id, ad_group_resource_name):
469449 ad_group_criterion = ad_group_criterion_operation .create
470450 ad_group_criterion .ad_group = ad_group_resource_name
471451 ad_group_criterion .status = client .enums .AdGroupCriterionStatusEnum .ENABLED
472- ad_group_criterion .keyword .text = _KEYWORD_TEXT_PHRASE_1
452+ ad_group_criterion .keyword .text = KEYWORD_TEXT_PHRASE
473453 ad_group_criterion .keyword .match_type = (
474454 client .enums .KeywordMatchTypeEnum .PHRASE
475455 )
@@ -488,7 +468,7 @@ def add_keywords(client, customer_id, ad_group_resource_name):
488468 ad_group_criterion = ad_group_criterion_operation .create
489469 ad_group_criterion .ad_group = ad_group_resource_name
490470 ad_group_criterion .status = client .enums .AdGroupCriterionStatusEnum .ENABLED
491- ad_group_criterion .keyword .text = _KEYWORD_TEXT_BROAD_1
471+ ad_group_criterion .keyword .text = KEYWORD_TEXT_BROAD
492472 ad_group_criterion .keyword .match_type = (
493473 client .enums .KeywordMatchTypeEnum .BROAD
494474 )
@@ -502,14 +482,11 @@ def add_keywords(client, customer_id, ad_group_resource_name):
502482 # Add operation
503483 operations .append (ad_group_criterion_operation )
504484
505- # Set all operations together.
506- campaign_criterion_operations = operations
507-
508485 # Add keywords
509486 ad_group_criterion_response = (
510487 ad_group_criterion_service .mutate_ad_group_criteria (
511488 customer_id = customer_id ,
512- operations = [ * campaign_criterion_operations ] ,
489+ operations = operations ,
513490 )
514491 )
515492 for result in ad_group_criterion_response .results :
@@ -533,12 +510,12 @@ def add_geo_targeting(client, customer_id, campaign_resource_name):
533510 # GeoTargetConstantService.suggest_geo_target_constants() and directly
534511 # apply GeoTargetConstant.resource_name.
535512 gtc_request = client .get_type ("SuggestGeoTargetConstantsRequest" )
536- gtc_request .locale = _LOCALE
537- gtc_request .country_code = _COUNTRY_CODE
513+ gtc_request .locale = LOCALE
514+ gtc_request .country_code = COUNTRY_CODE
538515
539516 # The location names to get suggested geo target constants.
540517 gtc_request .location_names .names .extend (
541- [_GEO_LOCATION_1 , _GEO_LOCATION_2 , _GEO_LOCATION_3 ]
518+ [GEO_LOCATION_1 , GEO_LOCATION_2 , GEO_LOCATION_3 ]
542519 )
543520
544521 results = geo_target_constant_service .suggest_geo_target_constants (
@@ -548,8 +525,9 @@ def add_geo_targeting(client, customer_id, campaign_resource_name):
548525 operations = []
549526 for suggestion in results .geo_target_constant_suggestions :
550527 print (
551- f"geo_target_constant: { suggestion .geo_target_constant } "
552- f"is found in _LOCALE ({ suggestion .locale } ) "
528+ "geo_target_constant: "
529+ f"{ suggestion .geo_target_constant .resource_name } "
530+ f"is found in LOCALE ({ suggestion .locale } ) "
553531 f"with reach ({ suggestion .reach } ) "
554532 f"from search term ({ suggestion .search_term } )."
555533 )
@@ -575,89 +553,6 @@ def add_geo_targeting(client, customer_id, campaign_resource_name):
575553 print (f'Added campaign criterion "{ result .resource_name } ".' )
576554
577555
578- def add_images (client , customer_id , campaign_resource_name ):
579- # Step 6.1 - Add Image Asset.
580-
581- # Download PNG image from URL.
582- url = "https://gaagl.page.link/bjYi"
583- image_content = requests .get (url ).content
584-
585- asset_service = client .get_service ("AssetService" )
586- asset_operation = client .get_type ("AssetOperation" )
587- asset = asset_operation .create
588- asset .type_ = client .enums .AssetTypeEnum .IMAGE
589-
590- # Data field is the raw bytes data of an image.
591- asset .image_asset .data = image_content
592- asset .image_asset .file_size = len (image_content )
593-
594- # MIME type of the image (IMAGE_JPEG, IMAGE_PNG, etc.).
595- # See more types on the link below.
596- # https://developers.google.com/google-ads/api/reference/rpc/v11/MimeTypeEnum.MimeType
597- asset .image_asset .mime_type = client .enums .MimeTypeEnum .IMAGE_PNG
598- # Use your favorite image library to determine dimensions
599- asset .image_asset .full_size .height_pixels = 1200
600- asset .image_asset .full_size .width_pixels = 1200
601- asset .image_asset .full_size .url = url
602- # Provide a unique friendly name to identify your asset.
603- # When there is an existing image asset with the same content but a different
604- # name, the new name will be dropped silently.
605- asset .name = f"Testing Image via API { uuid .uuid4 ()} "
606-
607- mutate_asset_response = asset_service .mutate_assets (
608- customer_id = customer_id , operations = [asset_operation ]
609- )
610- print ("Uploaded file(s):" )
611- for row in mutate_asset_response .results :
612- print (f"\t Resource name: { row .resource_name } " )
613-
614- image_asset_resource_name = mutate_asset_response .results [0 ].resource_name
615-
616- # Step 6.2 - Create Image Extension
617- extension_feed_item_service = client .get_service ("ExtensionFeedItemService" )
618- extension_feed_item_operation = client .get_type (
619- "ExtensionFeedItemOperation"
620- )
621- extension_feed_item = extension_feed_item_operation .create
622- extension_feed_item .image_feed_item .image_asset = image_asset_resource_name
623-
624- extension_feed_response = (
625- extension_feed_item_service .mutate_extension_feed_items (
626- customer_id = customer_id , operations = [extension_feed_item_operation ]
627- )
628- )
629- image_resource_name = extension_feed_response .results [0 ].resource_name
630-
631- print (
632- "Created an image extension with resource name: "
633- f"'{ image_resource_name } '"
634- )
635-
636- # Step 6.3 - Link Image Extension to RSA
637- campaign_extension_setting_service = client .get_service (
638- "CampaignExtensionSettingService"
639- )
640- campaign_extension_setting_operation = client .get_type (
641- "CampaignExtensionSettingOperation"
642- )
643- ces = campaign_extension_setting_operation .create
644- ces .campaign = campaign_resource_name
645- ces .extension_type = client .enums .ExtensionTypeEnum .IMAGE
646- ces .extension_feed_items .append (image_resource_name )
647-
648- campaign_extension_response = (
649- campaign_extension_setting_service .mutate_campaign_extension_settings (
650- customer_id = customer_id ,
651- operations = [campaign_extension_setting_operation ],
652- )
653- )
654-
655- print (
656- "Created a campaign extension setting with resource name: "
657- f"'{ campaign_extension_response .results [0 ].resource_name } '"
658- )
659-
660-
661556if __name__ == "__main__" :
662557 # GoogleAdsClient will read the google-ads.yaml configuration file in the
663558 # home directory if none is specified.
@@ -674,13 +569,6 @@ def add_images(client, customer_id, campaign_resource_name):
674569 required = True ,
675570 help = "The Google Ads customer ID." ,
676571 )
677- parser .add_argument (
678- "-o" ,
679- "--omit_image_extensions" ,
680- type = bool ,
681- default = False ,
682- help = "Whether or not the campaign will use image extensions." ,
683- )
684572
685573 # The name of the customizer attribute used in the ad customizer, which
686574 # must be unique for a given customer account. To run this example multiple
@@ -706,7 +594,6 @@ def add_images(client, customer_id, campaign_resource_name):
706594 main (
707595 googleads_client ,
708596 args .customer_id ,
709- args .omit_image_extensions ,
710597 args .customizer_attribute_name ,
711598 )
712599 except GoogleAdsException as ex :
0 commit comments