diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java index cd8fe607c6..f2cd5c04f8 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java @@ -32,7 +32,6 @@ import com.google.ads.googleads.v9.enums.BudgetDeliveryMethodEnum.BudgetDeliveryMethod; import com.google.ads.googleads.v9.enums.BudgetTypeEnum.BudgetType; import com.google.ads.googleads.v9.enums.CampaignStatusEnum.CampaignStatus; -import com.google.ads.googleads.v9.enums.CriterionTypeEnum.CriterionType; import com.google.ads.googleads.v9.enums.DayOfWeekEnum.DayOfWeek; import com.google.ads.googleads.v9.enums.MinuteOfHourEnum.MinuteOfHour; import com.google.ads.googleads.v9.errors.GoogleAdsError; @@ -68,6 +67,7 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; /** Demonstrates how to create a Smart Campaign. */ public class AddSmartCampaign { @@ -213,15 +213,15 @@ private void runExample( List keywordThemeInfos = getKeywordThemeInfos(keywordThemeConstants); // Optionally includes any freeform keywords in verbatim. + // [START add_smart_campaign_13] if (freeFormKeywordText != null) { keywordThemeInfos.add( KeywordThemeInfo.newBuilder().setFreeFormKeywordTheme(freeFormKeywordText).build()); } + // [END add_smart_campaign_13] // Includes the keyword suggestions in the overall SuggestionInfo object. - // [START add_smart_campaign_13] suggestionInfo = suggestionInfo.toBuilder().addAllKeywordThemes(keywordThemeInfos).build(); - // [END add_smart_campaign_13] // [END add_smart_campaign_12] // Generates a suggested daily budget. @@ -241,7 +241,8 @@ private void runExample( createSmartCampaignSettingOperation(customerId, locationId, businessName), createAdGroupOperation(customerId), createAdGroupAdOperation(customerId, adSuggestions))); - operations.addAll(createCampaignCriterionOperations(customerId, keywordThemeInfos)); + operations.addAll( + createCampaignCriterionOperations(customerId, keywordThemeInfos, suggestionInfo)); // Issues a mutate request to add the various entities required for a smart campaign. sendMutateRequest(googleAdsClient, customerId, operations); @@ -599,15 +600,17 @@ private MutateOperation createAdGroupAdOperation( // Adds additional headlines + descriptions if we didn't get enough back from the suggestion // service. - if (adSuggestions.getHeadlinesCount() < NUM_REQUIRED_HEADLINES) { - for (int i = 0; i < NUM_REQUIRED_HEADLINES - adSuggestions.getHeadlinesCount(); ++i) { + int numHeadlines = adBuilder.getSmartCampaignAdBuilder().getHeadlinesCount(); + if (numHeadlines < NUM_REQUIRED_HEADLINES) { + for (int i = 0; i < NUM_REQUIRED_HEADLINES - numHeadlines; ++i) { adBuilder .getSmartCampaignAdBuilder() .addHeadlines(AdTextAsset.newBuilder().setText("Placeholder headline " + i).build()); } } if (adSuggestions.getDescriptionsCount() < NUM_REQUIRED_DESCRIPTIONS) { - for (int i = 0; i < NUM_REQUIRED_DESCRIPTIONS - adSuggestions.getDescriptionsCount(); ++i) { + int numDescriptions = adBuilder.getSmartCampaignAdBuilder().getDescriptionsCount(); + for (int i = 0; i < NUM_REQUIRED_DESCRIPTIONS - numDescriptions; ++i) { adBuilder .getSmartCampaignAdBuilder() .addDescriptions( @@ -630,19 +633,38 @@ private MutateOperation createAdGroupAdOperation( * {@link KeywordThemeInfo}. */ private Collection createCampaignCriterionOperations( - long customerId, List keywordThemeInfos) { - return keywordThemeInfos.stream() - .map( - keywordTheme -> { - MutateOperation.Builder builder = MutateOperation.newBuilder(); - builder - .getCampaignCriterionOperationBuilder() - .getCreateBuilder() - .setCampaign(ResourceNames.campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID)) - .setType(CriterionType.KEYWORD_THEME) - .setKeywordTheme(keywordTheme); - return builder.build(); - }) + long customerId, + List keywordThemeInfos, + SmartCampaignSuggestionInfo suggestionInfo) { + List keywordThemeOperations = + keywordThemeInfos.stream() + .map( + keywordTheme -> { + MutateOperation.Builder builder = MutateOperation.newBuilder(); + builder + .getCampaignCriterionOperationBuilder() + .getCreateBuilder() + .setCampaign(ResourceNames.campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID)) + .setKeywordTheme(keywordTheme); + return builder.build(); + }) + .collect(Collectors.toList()); + + List locationOperations = + suggestionInfo.getLocationList().getLocationsList().stream() + .map( + location -> { + MutateOperation.Builder builder = MutateOperation.newBuilder(); + builder + .getCampaignCriterionOperationBuilder() + .getCreateBuilder() + .setCampaign(ResourceNames.campaign(customerId, SMART_CAMPAIGN_TEMPORARY_ID)) + .setLocation(location); + return builder.build(); + }) + .collect(Collectors.toList()); + + return Stream.concat(keywordThemeOperations.stream(), locationOperations.stream()) .collect(Collectors.toList()); } // [END add_smart_campaign_8] diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddHotelCallout.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddHotelCallout.java index d9c0ec8efa..fdb2664824 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddHotelCallout.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddHotelCallout.java @@ -96,13 +96,13 @@ public static void main(String[] args) { /** Runs the example. */ private void runExample(GoogleAdsClient googleAdsClient, long customerId, String languageCode) { // Creates assets for the hotel callout extensions. - List assetResourceNames = addExtensionAsset(googleAdsClient, customerId, languageCode); + List assetResourceNames = addExtensionAssets(googleAdsClient, customerId, languageCode); // Adds the extensions at the account level, so these will serve in all eligible campaigns. - linkAssetToAccount(googleAdsClient, customerId, assetResourceNames); + linkAssetsToAccount(googleAdsClient, customerId, assetResourceNames); } /** Creates a new asset for the callout. */ - private List addExtensionAsset( + private List addExtensionAssets( GoogleAdsClient googleAdsClient, long customerId, String languageCode) { List hotelCalloutAssets = new ArrayList<>(); // Creates the callouts with text and specified language. @@ -139,7 +139,7 @@ private List addExtensionAsset( } /** Links Asset at the Customer level to serve in all eligible campaigns. */ - private void linkAssetToAccount( + private void linkAssetsToAccount( GoogleAdsClient googleAdsClient, long customerId, List assetResourceNames) { // Creates a CustomerAsset link for each Asset resource name provided, then converts this into a // CustomerAssetOperation to create the Asset.