|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.hotelads; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.lib.GoogleAdsException; |
| 22 | +import com.google.ads.googleads.v0.common.Ad; |
| 23 | +import com.google.ads.googleads.v0.common.HotelAdInfo; |
| 24 | +import com.google.ads.googleads.v0.common.ManualCpc; |
| 25 | +import com.google.ads.googleads.v0.enums.AdGroupAdStatusEnum.AdGroupAdStatus; |
| 26 | +import com.google.ads.googleads.v0.enums.AdGroupStatusEnum.AdGroupStatus; |
| 27 | +import com.google.ads.googleads.v0.enums.AdGroupTypeEnum.AdGroupType; |
| 28 | +import com.google.ads.googleads.v0.enums.AdvertisingChannelTypeEnum.AdvertisingChannelType; |
| 29 | +import com.google.ads.googleads.v0.enums.BudgetDeliveryMethodEnum.BudgetDeliveryMethod; |
| 30 | +import com.google.ads.googleads.v0.enums.CampaignStatusEnum.CampaignStatus; |
| 31 | +import com.google.ads.googleads.v0.errors.GoogleAdsError; |
| 32 | +import com.google.ads.googleads.v0.resources.AdGroup; |
| 33 | +import com.google.ads.googleads.v0.resources.AdGroupAd; |
| 34 | +import com.google.ads.googleads.v0.resources.Campaign; |
| 35 | +import com.google.ads.googleads.v0.resources.Campaign.HotelSettingInfo; |
| 36 | +import com.google.ads.googleads.v0.resources.Campaign.NetworkSettings; |
| 37 | +import com.google.ads.googleads.v0.resources.CampaignBudget; |
| 38 | +import com.google.ads.googleads.v0.services.AdGroupAdOperation; |
| 39 | +import com.google.ads.googleads.v0.services.AdGroupAdServiceClient; |
| 40 | +import com.google.ads.googleads.v0.services.AdGroupOperation; |
| 41 | +import com.google.ads.googleads.v0.services.AdGroupServiceClient; |
| 42 | +import com.google.ads.googleads.v0.services.CampaignBudgetOperation; |
| 43 | +import com.google.ads.googleads.v0.services.CampaignBudgetServiceClient; |
| 44 | +import com.google.ads.googleads.v0.services.CampaignOperation; |
| 45 | +import com.google.ads.googleads.v0.services.CampaignServiceClient; |
| 46 | +import com.google.ads.googleads.v0.services.MutateAdGroupAdResult; |
| 47 | +import com.google.ads.googleads.v0.services.MutateAdGroupResult; |
| 48 | +import com.google.ads.googleads.v0.services.MutateCampaignBudgetsResponse; |
| 49 | +import com.google.ads.googleads.v0.services.MutateCampaignResult; |
| 50 | +import com.google.ads.googleads.v0.services.MutateCampaignsResponse; |
| 51 | +import com.google.common.collect.ImmutableList; |
| 52 | +import com.google.protobuf.BoolValue; |
| 53 | +import com.google.protobuf.Int64Value; |
| 54 | +import com.google.protobuf.StringValue; |
| 55 | +import java.io.FileNotFoundException; |
| 56 | +import java.io.IOException; |
| 57 | +import java.util.Collections; |
| 58 | + |
| 59 | +/** |
| 60 | + * This example creates a hotel campaign, a hotel ad group and a hotel ad group ad. |
| 61 | + * |
| 62 | + * <p>Prerequisite: You need to have access to the Hotel Ads Center, which can be granted during |
| 63 | + * integration with Google Hotels. The integration instructions can be found at: |
| 64 | + * https://support.google.com/hotelprices/answer/6101897. |
| 65 | + */ |
| 66 | +public class AddHotelAd { |
| 67 | + |
| 68 | + private static class AddHotelAdParams extends CodeSampleParams { |
| 69 | + |
| 70 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 71 | + private Long customerId; |
| 72 | + |
| 73 | + @Parameter(names = ArgumentNames.HOTEL_CENTER_ACCOUNT_ID, required = true) |
| 74 | + private Long hotelCenterAccountId; |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + AddHotelAdParams params = new AddHotelAdParams(); |
| 79 | + if (!params.parseArguments(args)) { |
| 80 | + |
| 81 | + // Either pass the required parameters for this example on the command line, or insert them |
| 82 | + // into the code here. See the parameter class definition above for descriptions. |
| 83 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 84 | + params.hotelCenterAccountId = Long.parseLong("INSERT_HOTEL_CENTER_ACCOUNT_ID_HERE"); |
| 85 | + } |
| 86 | + |
| 87 | + GoogleAdsClient googleAdsClient; |
| 88 | + try { |
| 89 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 90 | + } catch (FileNotFoundException fnfe) { |
| 91 | + System.err.printf( |
| 92 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 93 | + return; |
| 94 | + } catch (IOException ioe) { |
| 95 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + new AddHotelAd().runExample(googleAdsClient, params.customerId, params.hotelCenterAccountId); |
| 101 | + } catch (GoogleAdsException gae) { |
| 102 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 103 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 104 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 105 | + // GoogleAdsException. |
| 106 | + System.err.printf( |
| 107 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 108 | + gae.getRequestId()); |
| 109 | + int i = 0; |
| 110 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 111 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Runs the example. |
| 118 | + * |
| 119 | + * @param googleAdsClient the Google Ads API client. |
| 120 | + * @param customerId the client customer ID. |
| 121 | + * @param hotelCenterAccountId the Hotel Center account ID. |
| 122 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 123 | + */ |
| 124 | + private void runExample( |
| 125 | + GoogleAdsClient googleAdsClient, long customerId, long hotelCenterAccountId) { |
| 126 | + |
| 127 | + // Creates a budget to be used by the campaign that will be created below. |
| 128 | + String budgetResourceName = addCampaignBudget(googleAdsClient, customerId); |
| 129 | + |
| 130 | + // Creates a hotel campaign. |
| 131 | + String campaignResourceName = |
| 132 | + addHotelCampaign(googleAdsClient, customerId, budgetResourceName, hotelCenterAccountId); |
| 133 | + |
| 134 | + // Creates a hotel ad group. |
| 135 | + String adGroupResourceName = addHotelAdGroup(googleAdsClient, customerId, campaignResourceName); |
| 136 | + |
| 137 | + // Creates a hotel ad group ad. |
| 138 | + addHotelAdGroupAd(googleAdsClient, customerId, adGroupResourceName); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Creates a new campaign budget in the specified client account. |
| 143 | + * |
| 144 | + * @param googleAdsClient the Google Ads API client. |
| 145 | + * @param customerId the client customer ID. |
| 146 | + * @return resource name of the newly created budget. |
| 147 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 148 | + */ |
| 149 | + private String addCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) { |
| 150 | + CampaignBudget budget = |
| 151 | + CampaignBudget.newBuilder() |
| 152 | + .setName(StringValue.of("Interplanetary Cruise Budget #" + System.currentTimeMillis())) |
| 153 | + .setDeliveryMethod(BudgetDeliveryMethod.STANDARD) |
| 154 | + .setAmountMicros(Int64Value.of(5_000_000)) |
| 155 | + .build(); |
| 156 | + |
| 157 | + CampaignBudgetOperation op = CampaignBudgetOperation.newBuilder().setCreate(budget).build(); |
| 158 | + |
| 159 | + try (CampaignBudgetServiceClient campaignBudgetServiceClient = |
| 160 | + googleAdsClient.getCampaignBudgetServiceClient()) { |
| 161 | + MutateCampaignBudgetsResponse response = |
| 162 | + campaignBudgetServiceClient.mutateCampaignBudgets( |
| 163 | + Long.toString(customerId), ImmutableList.of(op)); |
| 164 | + String budgetResourceName = response.getResults(0).getResourceName(); |
| 165 | + System.out.printf("Added a budget with resource name: '%s'%n", budgetResourceName); |
| 166 | + return budgetResourceName; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Creates a new hotel campaign in the specified client account. |
| 172 | + * |
| 173 | + * @param googleAdsClient the Google Ads API client. |
| 174 | + * @param customerId the client customer ID. |
| 175 | + * @param budgetResourceName the resource name of the budget for the campaign. |
| 176 | + * @param hotelCenterAccountId the Hotel Center account ID. |
| 177 | + * @return resource name of the newly created campaign. |
| 178 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 179 | + */ |
| 180 | + private String addHotelCampaign( |
| 181 | + GoogleAdsClient googleAdsClient, |
| 182 | + long customerId, |
| 183 | + String budgetResourceName, |
| 184 | + long hotelCenterAccountId) { |
| 185 | + |
| 186 | + // Configures the hotel settings. |
| 187 | + HotelSettingInfo hotelSettingInfo = |
| 188 | + HotelSettingInfo.newBuilder().setHotelCenterId(Int64Value.of(hotelCenterAccountId)).build(); |
| 189 | + |
| 190 | + // Configures the campaign network options. Only Google Search is allowed for |
| 191 | + // hotel campaigns. |
| 192 | + NetworkSettings networkSettings = |
| 193 | + NetworkSettings.newBuilder().setTargetGoogleSearch(BoolValue.of(true)).build(); |
| 194 | + |
| 195 | + // Create the campaign. |
| 196 | + Campaign campaign = |
| 197 | + Campaign.newBuilder() |
| 198 | + .setName(StringValue.of("Interplanetary Cruise #" + System.currentTimeMillis())) |
| 199 | + // Configures settings related to hotel campaigns including advertising channel type |
| 200 | + // and hotel setting info. |
| 201 | + .setAdvertisingChannelType(AdvertisingChannelType.HOTEL) |
| 202 | + .setHotelSetting(hotelSettingInfo) |
| 203 | + // Recommendation: Set the campaign to PAUSED when creating it to prevent |
| 204 | + // the ads from immediately serving. Set to ENABLED once you've added |
| 205 | + // targeting and the ads are ready to serve |
| 206 | + .setStatus(CampaignStatus.PAUSED) |
| 207 | + // Sets the bidding strategy. Only Manual CPC can be used for hotel campaigns. |
| 208 | + .setManualCpc(ManualCpc.newBuilder().build()) |
| 209 | + // Sets the budget. |
| 210 | + .setCampaignBudget(StringValue.of(budgetResourceName)) |
| 211 | + // Adds the networkSettings configured above. |
| 212 | + .setNetworkSettings(networkSettings) |
| 213 | + .build(); |
| 214 | + |
| 215 | + // Creates a campaign operation. |
| 216 | + CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build(); |
| 217 | + |
| 218 | + // Issues a mutate request to add the campaign. |
| 219 | + try (CampaignServiceClient campaignServiceClient = googleAdsClient.getCampaignServiceClient()) { |
| 220 | + MutateCampaignsResponse response = |
| 221 | + campaignServiceClient.mutateCampaigns( |
| 222 | + Long.toString(customerId), Collections.singletonList(operation)); |
| 223 | + MutateCampaignResult result = response.getResults(0); |
| 224 | + System.out.printf( |
| 225 | + "Added a hotel campaign with resource name: '%s'%n", result.getResourceName()); |
| 226 | + return result.getResourceName(); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Creates a new hotel ad group in the specified campaign. |
| 232 | + * |
| 233 | + * @param googleAdsClient the Google Ads API client. |
| 234 | + * @param customerId the client customer ID. |
| 235 | + * @param campaignResourceName the resource name of the campaign that the new ad group will belong |
| 236 | + * to. |
| 237 | + * @return resource name of the newly created ad group. |
| 238 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 239 | + */ |
| 240 | + private String addHotelAdGroup( |
| 241 | + GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) { |
| 242 | + // Creates an ad group. |
| 243 | + AdGroup adGroup = |
| 244 | + AdGroup.newBuilder() |
| 245 | + .setName(StringValue.of("Earth to Mars Cruises #" + System.currentTimeMillis())) |
| 246 | + .setCampaign(StringValue.of(campaignResourceName)) |
| 247 | + // Sets the ad group type to HOTEL_ADS. This cannot be set to other types. |
| 248 | + .setType(AdGroupType.HOTEL_ADS) |
| 249 | + .setCpcBidMicros(Int64Value.of(1_000_000L)) |
| 250 | + .setStatus(AdGroupStatus.ENABLED) |
| 251 | + .build(); |
| 252 | + |
| 253 | + // Creates an ad group operation. |
| 254 | + AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build(); |
| 255 | + |
| 256 | + // Issues a mutate request to add an ad group. |
| 257 | + try (AdGroupServiceClient adGroupServiceClient = googleAdsClient.getAdGroupServiceClient()) { |
| 258 | + MutateAdGroupResult mutateAdGroupResult = |
| 259 | + adGroupServiceClient |
| 260 | + .mutateAdGroups(Long.toString(customerId), Collections.singletonList(operation)) |
| 261 | + .getResults(0); |
| 262 | + System.out.printf( |
| 263 | + "Added a hotel ad group with resource name: '%s'%n", |
| 264 | + mutateAdGroupResult.getResourceName()); |
| 265 | + return mutateAdGroupResult.getResourceName(); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Creates a new hotel ad group ad in the specified ad group. |
| 271 | + * |
| 272 | + * @param googleAdsClient the Google Ads API client. |
| 273 | + * @param customerId the client customer ID. |
| 274 | + * @param adGroupResourceName the resource name of the ad group that the new ad group ad will |
| 275 | + * belong to. |
| 276 | + * @return resource name of the newly created ad group ad. |
| 277 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 278 | + */ |
| 279 | + private String addHotelAdGroupAd( |
| 280 | + GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) { |
| 281 | + // Creates a new hotel ad. |
| 282 | + Ad ad = Ad.newBuilder().setHotelAd(HotelAdInfo.newBuilder().build()).build(); |
| 283 | + // Creates a new ad group ad and sets the hotel ad to it. |
| 284 | + AdGroupAd adGroupAd = |
| 285 | + AdGroupAd.newBuilder() |
| 286 | + // Sets the ad to the ad created above. |
| 287 | + .setAd(ad) |
| 288 | + .setStatus(AdGroupAdStatus.PAUSED) |
| 289 | + // Sets the ad group. |
| 290 | + .setAdGroup(StringValue.of(adGroupResourceName)) |
| 291 | + .build(); |
| 292 | + |
| 293 | + // Creates an ad group ad operation. |
| 294 | + AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build(); |
| 295 | + |
| 296 | + // Issues a mutate request to add an ad group ad. |
| 297 | + try (AdGroupAdServiceClient adGroupAdServiceClient = |
| 298 | + googleAdsClient.getAdGroupAdServiceClient()) { |
| 299 | + MutateAdGroupAdResult mutateAdGroupAdResult = |
| 300 | + adGroupAdServiceClient |
| 301 | + .mutateAdGroupAds(Long.toString(customerId), Collections.singletonList(operation)) |
| 302 | + .getResults(0); |
| 303 | + System.out.printf( |
| 304 | + "Added a hotel ad group ad with resource name: '%s'%n", |
| 305 | + mutateAdGroupAdResult.getResourceName()); |
| 306 | + return mutateAdGroupAdResult.getResourceName(); |
| 307 | + } |
| 308 | + } |
| 309 | +} |
0 commit comments