Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -213,15 +213,15 @@ private void runExample(
List<KeywordThemeInfo> 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.
Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -630,19 +633,38 @@ private MutateOperation createAdGroupAdOperation(
* {@link KeywordThemeInfo}.
*/
private Collection<? extends MutateOperation> createCampaignCriterionOperations(
long customerId, List<KeywordThemeInfo> 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<KeywordThemeInfo> keywordThemeInfos,
SmartCampaignSuggestionInfo suggestionInfo) {
List<MutateOperation> 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<MutateOperation> 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> assetResourceNames = addExtensionAsset(googleAdsClient, customerId, languageCode);
List<String> 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<String> addExtensionAsset(
private List<String> addExtensionAssets(
GoogleAdsClient googleAdsClient, long customerId, String languageCode) {
List<HotelCalloutAsset> hotelCalloutAssets = new ArrayList<>();
// Creates the callouts with text and specified language.
Expand Down Expand Up @@ -139,7 +139,7 @@ private List<String> addExtensionAsset(
}

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