Skip to content

Commit 0f4318e

Browse files
authored
Fixup a couple of code example issues (#501)
1 parent 68587b7 commit 0f4318e

2 files changed

Lines changed: 46 additions & 24 deletions

File tree

google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.google.ads.googleads.v9.enums.BudgetDeliveryMethodEnum.BudgetDeliveryMethod;
3333
import com.google.ads.googleads.v9.enums.BudgetTypeEnum.BudgetType;
3434
import com.google.ads.googleads.v9.enums.CampaignStatusEnum.CampaignStatus;
35-
import com.google.ads.googleads.v9.enums.CriterionTypeEnum.CriterionType;
3635
import com.google.ads.googleads.v9.enums.DayOfWeekEnum.DayOfWeek;
3736
import com.google.ads.googleads.v9.enums.MinuteOfHourEnum.MinuteOfHour;
3837
import com.google.ads.googleads.v9.errors.GoogleAdsError;
@@ -68,6 +67,7 @@
6867
import java.util.Collection;
6968
import java.util.List;
7069
import java.util.stream.Collectors;
70+
import java.util.stream.Stream;
7171

7272
/** Demonstrates how to create a Smart Campaign. */
7373
public 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]

google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddHotelCallout.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public static void main(String[] args) {
9696
/** Runs the example. */
9797
private void runExample(GoogleAdsClient googleAdsClient, long customerId, String languageCode) {
9898
// Creates assets for the hotel callout extensions.
99-
List<String> assetResourceNames = addExtensionAsset(googleAdsClient, customerId, languageCode);
99+
List<String> assetResourceNames = addExtensionAssets(googleAdsClient, customerId, languageCode);
100100
// Adds the extensions at the account level, so these will serve in all eligible campaigns.
101-
linkAssetToAccount(googleAdsClient, customerId, assetResourceNames);
101+
linkAssetsToAccount(googleAdsClient, customerId, assetResourceNames);
102102
}
103103

104104
/** Creates a new asset for the callout. */
105-
private List<String> addExtensionAsset(
105+
private List<String> addExtensionAssets(
106106
GoogleAdsClient googleAdsClient, long customerId, String languageCode) {
107107
List<HotelCalloutAsset> hotelCalloutAssets = new ArrayList<>();
108108
// Creates the callouts with text and specified language.
@@ -139,7 +139,7 @@ private List<String> addExtensionAsset(
139139
}
140140

141141
/** Links Asset at the Customer level to serve in all eligible campaigns. */
142-
private void linkAssetToAccount(
142+
private void linkAssetsToAccount(
143143
GoogleAdsClient googleAdsClient, long customerId, List<String> assetResourceNames) {
144144
// Creates a CustomerAsset link for each Asset resource name provided, then converts this into a
145145
// CustomerAssetOperation to create the Asset.

0 commit comments

Comments
 (0)