3232import com .google .ads .googleads .v9 .enums .BudgetDeliveryMethodEnum .BudgetDeliveryMethod ;
3333import com .google .ads .googleads .v9 .enums .BudgetTypeEnum .BudgetType ;
3434import com .google .ads .googleads .v9 .enums .CampaignStatusEnum .CampaignStatus ;
35- import com .google .ads .googleads .v9 .enums .CriterionTypeEnum .CriterionType ;
3635import com .google .ads .googleads .v9 .enums .DayOfWeekEnum .DayOfWeek ;
3736import com .google .ads .googleads .v9 .enums .MinuteOfHourEnum .MinuteOfHour ;
3837import com .google .ads .googleads .v9 .errors .GoogleAdsError ;
6867import java .util .Collection ;
6968import java .util .List ;
7069import java .util .stream .Collectors ;
70+ import java .util .stream .Stream ;
7171
7272/** Demonstrates how to create a Smart Campaign. */
7373public class AddSmartCampaign {
@@ -213,15 +213,15 @@ private void runExample(
213213 List <KeywordThemeInfo > keywordThemeInfos = getKeywordThemeInfos (keywordThemeConstants );
214214
215215 // Optionally includes any freeform keywords in verbatim.
216+ // [START add_smart_campaign_13]
216217 if (freeFormKeywordText != null ) {
217218 keywordThemeInfos .add (
218219 KeywordThemeInfo .newBuilder ().setFreeFormKeywordTheme (freeFormKeywordText ).build ());
219220 }
221+ // [END add_smart_campaign_13]
220222
221223 // Includes the keyword suggestions in the overall SuggestionInfo object.
222- // [START add_smart_campaign_13]
223224 suggestionInfo = suggestionInfo .toBuilder ().addAllKeywordThemes (keywordThemeInfos ).build ();
224- // [END add_smart_campaign_13]
225225 // [END add_smart_campaign_12]
226226
227227 // Generates a suggested daily budget.
@@ -241,7 +241,8 @@ private void runExample(
241241 createSmartCampaignSettingOperation (customerId , locationId , businessName ),
242242 createAdGroupOperation (customerId ),
243243 createAdGroupAdOperation (customerId , adSuggestions )));
244- operations .addAll (createCampaignCriterionOperations (customerId , keywordThemeInfos ));
244+ operations .addAll (
245+ createCampaignCriterionOperations (customerId , keywordThemeInfos , suggestionInfo ));
245246
246247 // Issues a mutate request to add the various entities required for a smart campaign.
247248 sendMutateRequest (googleAdsClient , customerId , operations );
@@ -599,15 +600,17 @@ private MutateOperation createAdGroupAdOperation(
599600
600601 // Adds additional headlines + descriptions if we didn't get enough back from the suggestion
601602 // service.
602- if (adSuggestions .getHeadlinesCount () < NUM_REQUIRED_HEADLINES ) {
603- for (int i = 0 ; i < NUM_REQUIRED_HEADLINES - adSuggestions .getHeadlinesCount (); ++i ) {
603+ int numHeadlines = adBuilder .getSmartCampaignAdBuilder ().getHeadlinesCount ();
604+ if (numHeadlines < NUM_REQUIRED_HEADLINES ) {
605+ for (int i = 0 ; i < NUM_REQUIRED_HEADLINES - numHeadlines ; ++i ) {
604606 adBuilder
605607 .getSmartCampaignAdBuilder ()
606608 .addHeadlines (AdTextAsset .newBuilder ().setText ("Placeholder headline " + i ).build ());
607609 }
608610 }
609611 if (adSuggestions .getDescriptionsCount () < NUM_REQUIRED_DESCRIPTIONS ) {
610- for (int i = 0 ; i < NUM_REQUIRED_DESCRIPTIONS - adSuggestions .getDescriptionsCount (); ++i ) {
612+ int numDescriptions = adBuilder .getSmartCampaignAdBuilder ().getDescriptionsCount ();
613+ for (int i = 0 ; i < NUM_REQUIRED_DESCRIPTIONS - numDescriptions ; ++i ) {
611614 adBuilder
612615 .getSmartCampaignAdBuilder ()
613616 .addDescriptions (
@@ -630,19 +633,38 @@ private MutateOperation createAdGroupAdOperation(
630633 * {@link KeywordThemeInfo}.
631634 */
632635 private Collection <? extends MutateOperation > createCampaignCriterionOperations (
633- long customerId , List <KeywordThemeInfo > keywordThemeInfos ) {
634- return keywordThemeInfos .stream ()
635- .map (
636- keywordTheme -> {
637- MutateOperation .Builder builder = MutateOperation .newBuilder ();
638- builder
639- .getCampaignCriterionOperationBuilder ()
640- .getCreateBuilder ()
641- .setCampaign (ResourceNames .campaign (customerId , SMART_CAMPAIGN_TEMPORARY_ID ))
642- .setType (CriterionType .KEYWORD_THEME )
643- .setKeywordTheme (keywordTheme );
644- return builder .build ();
645- })
636+ long customerId ,
637+ List <KeywordThemeInfo > keywordThemeInfos ,
638+ SmartCampaignSuggestionInfo suggestionInfo ) {
639+ List <MutateOperation > keywordThemeOperations =
640+ keywordThemeInfos .stream ()
641+ .map (
642+ keywordTheme -> {
643+ MutateOperation .Builder builder = MutateOperation .newBuilder ();
644+ builder
645+ .getCampaignCriterionOperationBuilder ()
646+ .getCreateBuilder ()
647+ .setCampaign (ResourceNames .campaign (customerId , SMART_CAMPAIGN_TEMPORARY_ID ))
648+ .setKeywordTheme (keywordTheme );
649+ return builder .build ();
650+ })
651+ .collect (Collectors .toList ());
652+
653+ List <MutateOperation > locationOperations =
654+ suggestionInfo .getLocationList ().getLocationsList ().stream ()
655+ .map (
656+ location -> {
657+ MutateOperation .Builder builder = MutateOperation .newBuilder ();
658+ builder
659+ .getCampaignCriterionOperationBuilder ()
660+ .getCreateBuilder ()
661+ .setCampaign (ResourceNames .campaign (customerId , SMART_CAMPAIGN_TEMPORARY_ID ))
662+ .setLocation (location );
663+ return builder .build ();
664+ })
665+ .collect (Collectors .toList ());
666+
667+ return Stream .concat (keywordThemeOperations .stream (), locationOperations .stream ())
646668 .collect (Collectors .toList ());
647669 }
648670 // [END add_smart_campaign_8]
0 commit comments