defcreate_ad_group_operation(client:GoogleAdsClient,customer_id:str)->MutateOperation:"""Creates a MutateOperation that creates a new ad group.A temporary ID will be used in the campaign resource name for thisad group to associate it with the Smart campaign created in earlier steps.A temporary ID will also be used for its own resource name so that we canassociate an ad group ad with it later in the process.Only one ad group can be created for a given Smart campaign.Args:client: an initialized GoogleAdsClient instance.customer_id: a client customer ID.Returns:a MutateOperation that creates a new ad group."""mutate_operation:MutateOperation=client.get_type("MutateOperation")ad_group_operation:AdGroupOperation=mutate_operation.ad_group_operationad_group:AdGroup=ad_group_operation.create# Set the ad group ID to a temporary ID.ad_group.resource_name=client.get_service("AdGroupService").ad_group_path(customer_id,_AD_GROUP_TEMPORARY_ID)ad_group.name=f"Smart campaign ad group #{uuid4()}"# Set the campaign ID to a temporary ID.ad_group.campaign=client.get_service("CampaignService").campaign_path(customer_id,_BUDGET_TEMPORARY_ID)# The ad group type must be set to SMART_CAMPAIGN_ADS.ad_group.type_=client.enums.AdGroupTypeEnum.SMART_CAMPAIGN_ADSreturnmutate_operation
# Creates a mutate_operation that creates a new ad group.# A temporary ID will be used in the campaign resource name for this# ad group to associate it with the Smart campaign created in earlier steps.# A temporary ID will also be used for its own resource name so that we can# associate an ad group ad with it later in the process.# Only one ad group can be created for a given Smart campaign.defcreate_ad_group_operation(client,customer_id)mutate_operation=client.operation.mutatedo|m|m.ad_group_operation=client.operation.create_resource.ad_groupdo|ag|# Set the ad group ID to a temporary ID.ag.resource_name=client.path.ad_group(customer_id,AD_GROUP_TEMPORARY_ID)ag.name="Smart campaign ad group ##{(Time.new.to_f*1000).to_i}"# Set the campaign ID to a temporary ID.ag.campaign=client.path.campaign(customer_id,SMART_CAMPAIGN_TEMPORARY_ID)# The ad group type must be set to SMART_CAMPAIGN_ADS.ag.type=:SMART_CAMPAIGN_ADSendendmutate_operationend
# Creates a MutateOperation that creates a new ad group.# A temporary ID will be used in the campaign resource name for this ad group to# associate it with the Smart campaign created in earlier steps. A temporary ID# will also be used for its own resource name so that we can associate an ad group ad# with it later in the process.# Only one ad group can be created for a given Smart campaign.sub_create_ad_group_operation{my($customer_id)=@_;returnGoogle::Ads::GoogleAds::V21::Services::GoogleAdsService::MutateOperation->new({adGroupOperation=>Google::Ads::GoogleAds::V21::Services::AdGroupService::AdGroupOperation->new({create=>Google::Ads::GoogleAds::V21::Resources::AdGroup->new({# Set the ad group ID to a temporary ID.resourceName=>Google::Ads::GoogleAds::V21::Utils::ResourceNames::ad_group($customer_id,AD_GROUP_TEMPORARY_ID),name=>"Smart campaign ad group #".uniqid(),# Set the campaign ID to a temporary ID.campaign=>Google::Ads::GoogleAds::V21::Utils::ResourceNames::campaign($customer_id,SMART_CAMPAIGN_TEMPORARY_ID),# The ad group type must be set to SMART_CAMPAIGN_ADS.type=>SMART_CAMPAIGN_ADS})})});}
Although a Smart campaign can contain only a single ad group, that ad group
can contain multiple ads. Because the ad text for Smart campaigns is selected
dynamically, it's possible that the headlines and descriptions from multiple
different ads could be combined.
defcreate_ad_group_ad_operation(client:GoogleAdsClient,customer_id:str,ad_suggestions:SmartCampaignAdInfo,)->MutateOperation:"""Creates a MutateOperation that creates a new ad group ad.A temporary ID will be used in the ad group resource name for thisad group ad to associate it with the ad group created in earlier steps.Args:client: an initialized GoogleAdsClient instance.customer_id: a client customer ID.ad_suggestions: a SmartCampaignAdInfo object with ad creativesuggestions.Returns:a MutateOperation that creates a new ad group ad."""mutate_operation:MutateOperation=client.get_type("MutateOperation")ad_group_ad_operation:AdGroupAdOperation=(mutate_operation.ad_group_ad_operation)ad_group_ad:AdGroupAd=ad_group_ad_operation.create# Set the ad group ID to a temporary ID.ad_group_ad.ad_group=client.get_service("AdGroupService").ad_group_path(customer_id,_AD_GROUP_TEMPORARY_ID)# Set the type to SMART_CAMPAIGN_AD.ad_group_ad.ad.type_=client.enums.AdTypeEnum.SMART_CAMPAIGN_ADad:SmartCampaignAdInfo=ad_group_ad.ad.smart_campaign_ad# The SmartCampaignAdInfo object includes headlines and descriptions# retrieved from the SmartCampaignSuggestService.SuggestSmartCampaignAd# method. It's recommended that users review and approve or update these# creatives before they're set on the ad. It's possible that some or all of# these assets may contain empty texts, which should not be set on the ad# and instead should be replaced with meaningful texts from the user. Below# we just accept the creatives that were suggested while filtering out empty# assets. If no headlines or descriptions were suggested, then we manually# add some, otherwise this operation will generate an INVALID_ARGUMENT# error. Individual workflows will likely vary here.ad.headlines.extend([assetforassetinad_suggestions.headlinesifasset.text])num_missing_headlines:int=_REQUIRED_NUM_HEADLINES-len(ad.headlines)# If there are fewer headlines than are required, we manually add additional# headlines to make up for the difference.foriinrange(num_missing_headlines):headline:AdTextAsset=client.get_type("AdTextAsset")headline.text=f"placeholder headline{i}"ad.headlines.append(headline)ad.descriptions.extend(assetforassetinad_suggestions.descriptionsifasset.text)num_missing_descriptions:int=_REQUIRED_NUM_DESCRIPTIONS-len(ad.descriptions)# If there are fewer descriptions than are required, we manually add# additional descriptions to make up for the difference.foriinrange(num_missing_descriptions):description:AdTextAsset=client.get_type("AdTextAsset")description.text=f"placeholder description{i}"ad.descriptions.append(description)returnmutate_operation
# Creates a mutate_operation that creates a new ad group ad.# A temporary ID will be used in the ad group resource name for this# ad group ad to associate it with the ad group created in earlier steps.defcreate_ad_group_ad_operation(client,customer_id,ad_suggestions)mutate_operation=client.operation.mutatedo|m|m.ad_group_ad_operation=client.operation.create_resource.ad_group_addo|aga|# Set the ad group ID to a temporary ID.aga.ad_group=client.path.ad_group(customer_id,AD_GROUP_TEMPORARY_ID)aga.ad=client.resource.addo|ad|# Set the type to SMART_CAMPAIGN_AD.ad.type=:SMART_CAMPAIGN_ADad.smart_campaign_ad=client.resource.smart_campaign_ad_infodo|sca|# The SmartCampaignAdInfo object includes headlines and descriptions# retrieved from the SmartCampaignSuggestService.SuggestSmartCampaignAd# method. It's recommended that users review and approve or update these# creatives before they're set on the ad. It's possible that some or all of# these assets may contain empty texts, which should not be set on the ad# and instead should be replaced with meaningful texts from the user. Below# we just accept the creatives that were suggested while filtering out empty# assets. If no headlines or descriptions were suggested, then we manually# add some, otherwise this operation will generate an INVALID_ARGUMENT# error. Individual workflows will likely vary here.sca.headlines+=ad_suggestions.headlines.filter(&:text)ifad_suggestionsifsca.headlines.size<REQUIRED_NUM_HEADLINES(REQUIRED_NUM_HEADLINES-sca.headlines.size).timesdo|i|sca.headlines<<client.resource.ad_text_assetdo|asset|asset.text="placeholder headline#{i}"endendendsca.descriptions+=ad_suggestions.descriptions.filter(&:text)ifad_suggestionsifsca.descriptions.size<REQUIRED_NUM_DESCRIPTIONS(REQUIRED_NUM_DESCRIPTIONS-sca.descriptions.size).timesdo|i|sca.descriptions<<client.resource.ad_text_assetdo|asset|asset.text="placeholder description#{i}"endendendendendendendmutate_operationend
# Creates a MutateOperation that creates a new ad group ad.# A temporary ID will be used in the ad group resource name for this ad group ad# to associate it with the ad group created in earlier steps.sub_create_ad_group_ad_operation{my($customer_id,$ad_suggestions)=@_;my$mutate_operation=Google::Ads::GoogleAds::V21::Services::GoogleAdsService::MutateOperation->new({adGroupAdOperation=>Google::Ads::GoogleAds::V21::Services::AdGroupAdService::AdGroupAdOperation->new({create=>Google::Ads::GoogleAds::V21::Resources::AdGroupAd->new({adGroup=># Set the ad group ID to a temporary ID.Google::Ads::GoogleAds::V21::Utils::ResourceNames::ad_group($customer_id,AD_GROUP_TEMPORARY_ID),ad=>Google::Ads::GoogleAds::V21::Resources::Ad->new({# Set the type to SMART_CAMPAIGN_AD.type=>SMART_CAMPAIGN_AD,smartCampaignAd=>Google::Ads::GoogleAds::V21::Common::SmartCampaignAdInfo->new({headlines=>[],descriptions=>[]})})})})});# The SmartCampaignAdInfo object includes headlines and descriptions# retrieved from the SmartCampaignSuggestService.SuggestSmartCampaignAd# method. It's recommended that users review and approve or update these# creatives before they're set on the ad. It's possible that some or all of# these assets may contain empty texts, which should not be set on the ad# and instead should be replaced with meaningful texts from the user. Below# we just accept the creatives that were suggested while filtering out empty# assets. If no headlines or descriptions were suggested, then we manually# add some, otherwise this operation will generate an INVALID_ARGUMENT# error. Individual workflows will likely vary here.my$smart_campaign_ad=$mutate_operation->{adGroupAdOperation}{create}{ad}{smartCampaignAd};foreachmy$asset(@{$ad_suggestions->{headlines}}){push@{$smart_campaign_ad->{headlines}},$assetifdefined$asset->{text};}# If there are fewer headlines than are required, we manually add additional# headlines to make up for the difference.my$num_missing_headlines=REQUIRED_NUM_HEADLINES-scalar@{$smart_campaign_ad->{headlines}};for(my$i=0;$i<$num_missing_headlines;$i++){push@{$smart_campaign_ad->{headlines}},Google::Ads::GoogleAds::V21::Common::AdTextAsset->new({text=>"placeholder headline ".$i});}foreachmy$asset(@{$ad_suggestions->{descriptions}}){push@{$smart_campaign_ad->{descriptions}},$assetifdefined$asset->{text};}# If there are fewer descriptions than are required, we manually add# additional descriptions to make up for the difference.my$num_missing_descriptions=REQUIRED_NUM_DESCRIPTIONS-scalar@{$smart_campaign_ad->{descriptions}};for(my$i=0;$i<$num_missing_descriptions;$i++){push@{$smart_campaign_ad->{descriptions}},Google::Ads::GoogleAds::V21::Common::AdTextAsset->new({text=>"placeholder description ".$i});}return$mutate_operation;}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-03 UTC."],[[["\u003cp\u003eSmart campaigns in Google Ads utilize a single ad group of type \u003ccode\u003eSMART_CAMPAIGN_ADS\u003c/code\u003e, linked to the campaign using a temporary ID.\u003c/p\u003e\n"],["\u003cp\u003eMultiple ads can be created within this ad group, each containing up to three headlines and two descriptions, with Smart campaigns dynamically combining elements from different ads for display.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code snippets demonstrate how to create ad groups and ads using the Google Ads API, including using suggested creative elements and placeholders.\u003c/p\u003e\n"],["\u003cp\u003eTemporary IDs are crucial for linking the campaign, ad group, and ads during the creation process within the API.\u003c/p\u003e\n"],["\u003cp\u003eIt is strongly recommended to review and potentially modify the suggested creative elements from \u003ccode\u003eSmartCampaignAdInfo\u003c/code\u003e before finalizing the ad creation.\u003c/p\u003e\n"]]],[],null,[]]