|
| 1 | +// Copyright 2023 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.travel; |
| 16 | + |
| 17 | +import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime; |
| 18 | + |
| 19 | +import com.beust.jcommander.Parameter; |
| 20 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 21 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 22 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 23 | +import com.google.ads.googleads.v14.common.MaximizeConversionValue; |
| 24 | +import com.google.ads.googleads.v14.common.TravelAdInfo; |
| 25 | +import com.google.ads.googleads.v14.enums.AdGroupAdStatusEnum.AdGroupAdStatus; |
| 26 | +import com.google.ads.googleads.v14.enums.AdGroupStatusEnum.AdGroupStatus; |
| 27 | +import com.google.ads.googleads.v14.enums.AdGroupTypeEnum.AdGroupType; |
| 28 | +import com.google.ads.googleads.v14.enums.AdvertisingChannelSubTypeEnum.AdvertisingChannelSubType; |
| 29 | +import com.google.ads.googleads.v14.enums.AdvertisingChannelTypeEnum.AdvertisingChannelType; |
| 30 | +import com.google.ads.googleads.v14.enums.BudgetDeliveryMethodEnum.BudgetDeliveryMethod; |
| 31 | +import com.google.ads.googleads.v14.enums.CampaignStatusEnum.CampaignStatus; |
| 32 | +import com.google.ads.googleads.v14.errors.GoogleAdsError; |
| 33 | +import com.google.ads.googleads.v14.errors.GoogleAdsException; |
| 34 | +import com.google.ads.googleads.v14.resources.Ad; |
| 35 | +import com.google.ads.googleads.v14.resources.AdGroup; |
| 36 | +import com.google.ads.googleads.v14.resources.AdGroupAd; |
| 37 | +import com.google.ads.googleads.v14.resources.Campaign; |
| 38 | +import com.google.ads.googleads.v14.resources.Campaign.NetworkSettings; |
| 39 | +import com.google.ads.googleads.v14.resources.Campaign.TravelCampaignSettings; |
| 40 | +import com.google.ads.googleads.v14.resources.CampaignBudget; |
| 41 | +import com.google.ads.googleads.v14.services.AdGroupAdOperation; |
| 42 | +import com.google.ads.googleads.v14.services.AdGroupAdServiceClient; |
| 43 | +import com.google.ads.googleads.v14.services.AdGroupOperation; |
| 44 | +import com.google.ads.googleads.v14.services.AdGroupServiceClient; |
| 45 | +import com.google.ads.googleads.v14.services.CampaignBudgetOperation; |
| 46 | +import com.google.ads.googleads.v14.services.CampaignBudgetServiceClient; |
| 47 | +import com.google.ads.googleads.v14.services.CampaignOperation; |
| 48 | +import com.google.ads.googleads.v14.services.CampaignServiceClient; |
| 49 | +import com.google.ads.googleads.v14.services.MutateAdGroupAdResult; |
| 50 | +import com.google.ads.googleads.v14.services.MutateAdGroupResult; |
| 51 | +import com.google.ads.googleads.v14.services.MutateCampaignBudgetsResponse; |
| 52 | +import com.google.ads.googleads.v14.services.MutateCampaignResult; |
| 53 | +import com.google.ads.googleads.v14.services.MutateCampaignsResponse; |
| 54 | +import com.google.common.collect.ImmutableList; |
| 55 | +import java.io.FileNotFoundException; |
| 56 | +import java.io.IOException; |
| 57 | +import java.util.Collections; |
| 58 | + |
| 59 | +/** |
| 60 | + * Creates a Things to do campaign, an ad group and a Things to do ad. |
| 61 | + * |
| 62 | + * <p>Prerequisite: You need to have an access to the Things to Do Center. The integration |
| 63 | + * instructions can be found at: https://support.google.com/google-ads/answer/13387362. |
| 64 | + */ |
| 65 | +public class AddThingsToDoAd { |
| 66 | + |
| 67 | + private static class AddThingsToDoAdParams extends CodeSampleParams { |
| 68 | + |
| 69 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 70 | + private Long customerId; |
| 71 | + |
| 72 | + @Parameter(names = ArgumentNames.THINGS_TO_DO_CENTER_ACCOUNT_ID, required = true) |
| 73 | + private Long thingsToDoCenterAccountId; |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + AddThingsToDoAdParams params = new AddThingsToDoAdParams(); |
| 78 | + if (!params.parseArguments(args)) { |
| 79 | + |
| 80 | + // Either pass the required parameters for this example on the command line, or insert them |
| 81 | + // into the code here. See the parameter class definition above for descriptions. |
| 82 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 83 | + params.thingsToDoCenterAccountId = |
| 84 | + Long.parseLong("INSERT_THINGS_TO_DO_CENTER_ACCOUNT_ID_HERE"); |
| 85 | + } |
| 86 | + |
| 87 | + GoogleAdsClient googleAdsClient = null; |
| 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 | + System.exit(1); |
| 94 | + } catch (IOException ioe) { |
| 95 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 96 | + System.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + new AddThingsToDoAd() |
| 101 | + .runExample(googleAdsClient, params.customerId, params.thingsToDoCenterAccountId); |
| 102 | + } catch (GoogleAdsException gae) { |
| 103 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 104 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 105 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 106 | + // GoogleAdsException. |
| 107 | + System.err.printf( |
| 108 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 109 | + gae.getRequestId()); |
| 110 | + int i = 0; |
| 111 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 112 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 113 | + } |
| 114 | + System.exit(1); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Runs the example. |
| 120 | + * |
| 121 | + * @param googleAdsClient the Google Ads API client. |
| 122 | + * @param customerId the client customer ID. |
| 123 | + * @param thingsToDoCenterAccountId the Things to Do Center account ID. |
| 124 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 125 | + */ |
| 126 | + private void runExample( |
| 127 | + GoogleAdsClient googleAdsClient, long customerId, long thingsToDoCenterAccountId) { |
| 128 | + |
| 129 | + // Creates a budget to be used by the campaign that will be created below. |
| 130 | + String budgetResourceName = addCampaignBudget(googleAdsClient, customerId); |
| 131 | + |
| 132 | + // Creates a Things to do campaign. |
| 133 | + String campaignResourceName = |
| 134 | + addThingsToDoCampaign( |
| 135 | + googleAdsClient, customerId, budgetResourceName, thingsToDoCenterAccountId); |
| 136 | + |
| 137 | + // Creates an ad group. |
| 138 | + String adGroupResourceName = addAdGroup(googleAdsClient, customerId, campaignResourceName); |
| 139 | + |
| 140 | + // Creates an ad group ad. |
| 141 | + addAddGroupAd(googleAdsClient, customerId, adGroupResourceName); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a new campaign budget in the specified client account. |
| 146 | + * |
| 147 | + * @param googleAdsClient the Google Ads API client. |
| 148 | + * @param customerId the client customer ID. |
| 149 | + * @return resource name of the newly created budget. |
| 150 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 151 | + */ |
| 152 | + private String addCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) { |
| 153 | + CampaignBudget budget = |
| 154 | + CampaignBudget.newBuilder() |
| 155 | + .setName("Interplanetary Cruise Budget #" + getPrintableDateTime()) |
| 156 | + .setDeliveryMethod(BudgetDeliveryMethod.STANDARD) |
| 157 | + // Sets the amount of the budget. |
| 158 | + .setAmountMicros(5_000_000) |
| 159 | + // Makes the budget explicitly shared. You cannot set it to false for Things to do |
| 160 | + // campaigns. |
| 161 | + .setExplicitlyShared(true) |
| 162 | + .build(); |
| 163 | + |
| 164 | + // Creates a campaign budget operation. |
| 165 | + CampaignBudgetOperation op = CampaignBudgetOperation.newBuilder().setCreate(budget).build(); |
| 166 | + |
| 167 | + // Issues a mutate request. |
| 168 | + try (CampaignBudgetServiceClient campaignBudgetServiceClient = |
| 169 | + googleAdsClient.getLatestVersion().createCampaignBudgetServiceClient()) { |
| 170 | + MutateCampaignBudgetsResponse response = |
| 171 | + campaignBudgetServiceClient.mutateCampaignBudgets( |
| 172 | + Long.toString(customerId), ImmutableList.of(op)); |
| 173 | + String budgetResourceName = response.getResults(0).getResourceName(); |
| 174 | + System.out.printf("Added a budget with resource name: '%s'%n", budgetResourceName); |
| 175 | + return budgetResourceName; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Creates a new Things to do campaign in the specified client account. |
| 181 | + * |
| 182 | + * @param googleAdsClient the Google Ads API client. |
| 183 | + * @param customerId the client customer ID. |
| 184 | + * @param budgetResourceName the resource name of the budget for the campaign. |
| 185 | + * @param thingsToDoCenterAccountId the Things to Do Center account ID. |
| 186 | + * @return resource name of the newly created campaign. |
| 187 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 188 | + */ |
| 189 | + // [START add_things_to_do_ad] |
| 190 | + private String addThingsToDoCampaign( |
| 191 | + GoogleAdsClient googleAdsClient, |
| 192 | + long customerId, |
| 193 | + String budgetResourceName, |
| 194 | + long thingsToDoCenterAccountId) { |
| 195 | + // [START add_things_to_do_ad_1] |
| 196 | + // Creates the campaign. |
| 197 | + Campaign campaign = |
| 198 | + Campaign.newBuilder() |
| 199 | + .setName("Interplanetary Cruise #" + getPrintableDateTime()) |
| 200 | + // Configures settings related to Things to do campaigns including advertising channel |
| 201 | + // type, advertising channel sub type and travel campaign settings. |
| 202 | + .setAdvertisingChannelType(AdvertisingChannelType.TRAVEL) |
| 203 | + .setAdvertisingChannelSubType(AdvertisingChannelSubType.TRAVEL_ACTIVITIES) |
| 204 | + .setTravelCampaignSettings( |
| 205 | + TravelCampaignSettings.newBuilder().setTravelAccountId(thingsToDoCenterAccountId)) |
| 206 | + // Recommendation: Sets the campaign to PAUSED when creating it to prevent |
| 207 | + // the ads from immediately serving. Set to ENABLED once you've added |
| 208 | + // targeting and the ads are ready to serve |
| 209 | + .setStatus(CampaignStatus.PAUSED) |
| 210 | + // Sets the bidding strategy to MaximizeConversionValue. Only this type can be used |
| 211 | + // for Things to do campaigns. |
| 212 | + .setMaximizeConversionValue(MaximizeConversionValue.newBuilder()) |
| 213 | + // Sets the budget. |
| 214 | + .setCampaignBudget(budgetResourceName) |
| 215 | + // Configures the campaign network options. Only Google Search is allowed for |
| 216 | + // Things to do campaigns. |
| 217 | + .setNetworkSettings(NetworkSettings.newBuilder().setTargetGoogleSearch(true)) |
| 218 | + .build(); |
| 219 | + // [END add_things_to_do_ad_1] |
| 220 | + |
| 221 | + // Creates a campaign operation. |
| 222 | + CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build(); |
| 223 | + |
| 224 | + // Issues a mutate request to add the campaign. |
| 225 | + try (CampaignServiceClient campaignServiceClient = |
| 226 | + googleAdsClient.getLatestVersion().createCampaignServiceClient()) { |
| 227 | + MutateCampaignsResponse response = |
| 228 | + campaignServiceClient.mutateCampaigns( |
| 229 | + Long.toString(customerId), Collections.singletonList(operation)); |
| 230 | + MutateCampaignResult result = response.getResults(0); |
| 231 | + System.out.printf( |
| 232 | + "Added a Things to do campaign with resource name: '%s'%n", result.getResourceName()); |
| 233 | + return result.getResourceName(); |
| 234 | + } |
| 235 | + } |
| 236 | + // [END add_things_to_do_ad] |
| 237 | + |
| 238 | + /** |
| 239 | + * Creates a new ad group in the specified Things to do campaign. |
| 240 | + * |
| 241 | + * @param googleAdsClient the Google Ads API client. |
| 242 | + * @param customerId the client customer ID. |
| 243 | + * @param campaignResourceName the resource name of the campaign that the new ad group will belong |
| 244 | + * to. |
| 245 | + * @return resource name of the newly created ad group. |
| 246 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 247 | + */ |
| 248 | + // [START add_things_to_do_ad_2] |
| 249 | + private String addAdGroup( |
| 250 | + GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) { |
| 251 | + // Creates an ad group. |
| 252 | + AdGroup adGroup = |
| 253 | + AdGroup.newBuilder() |
| 254 | + .setName("Earth to Mars Cruises #" + getPrintableDateTime()) |
| 255 | + .setCampaign(campaignResourceName) |
| 256 | + // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types. |
| 257 | + .setType(AdGroupType.TRAVEL_ADS) |
| 258 | + .setStatus(AdGroupStatus.ENABLED) |
| 259 | + .build(); |
| 260 | + |
| 261 | + // Creates an ad group operation. |
| 262 | + AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build(); |
| 263 | + |
| 264 | + // Issues a mutate request to add an ad group. |
| 265 | + try (AdGroupServiceClient adGroupServiceClient = |
| 266 | + googleAdsClient.getLatestVersion().createAdGroupServiceClient()) { |
| 267 | + MutateAdGroupResult mutateAdGroupResult = |
| 268 | + adGroupServiceClient |
| 269 | + .mutateAdGroups(Long.toString(customerId), Collections.singletonList(operation)) |
| 270 | + .getResults(0); |
| 271 | + System.out.printf( |
| 272 | + "Added an ad group with resource name: '%s'%n", mutateAdGroupResult.getResourceName()); |
| 273 | + return mutateAdGroupResult.getResourceName(); |
| 274 | + } |
| 275 | + } |
| 276 | + // [END add_things_to_do_ad_2] |
| 277 | + |
| 278 | + /** |
| 279 | + * Creates a new ad group ad in the specified ad group. |
| 280 | + * |
| 281 | + * @param googleAdsClient the Google Ads API client. |
| 282 | + * @param customerId the client customer ID. |
| 283 | + * @param adGroupResourceName the resource name of the ad group that the new ad group ad will |
| 284 | + * belong to. |
| 285 | + * @return resource name of the newly created ad group ad. |
| 286 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 287 | + */ |
| 288 | + // [START add_things_to_do_ad_3] |
| 289 | + private String addAddGroupAd( |
| 290 | + GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) { |
| 291 | + // Creates a new travel ad. |
| 292 | + Ad ad = Ad.newBuilder().setTravelAd(TravelAdInfo.newBuilder()).build(); |
| 293 | + // Creates a new ad group ad and sets its ad to the travel ad. |
| 294 | + AdGroupAd adGroupAd = |
| 295 | + AdGroupAd.newBuilder() |
| 296 | + // Sets the ad to the ad created above. |
| 297 | + .setAd(ad) |
| 298 | + // Set the ad group ad to enabled. Setting this to paused will cause an error for |
| 299 | + // Things to do campaigns. Pausing should happen at either the ad group or campaign |
| 300 | + // level. |
| 301 | + .setStatus(AdGroupAdStatus.ENABLED) |
| 302 | + // Sets the ad group. |
| 303 | + .setAdGroup(adGroupResourceName) |
| 304 | + .build(); |
| 305 | + |
| 306 | + // Creates an ad group ad operation. |
| 307 | + AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build(); |
| 308 | + |
| 309 | + // Issues a mutate request to add an ad group ad. |
| 310 | + try (AdGroupAdServiceClient adGroupAdServiceClient = |
| 311 | + googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) { |
| 312 | + MutateAdGroupAdResult mutateAdGroupAdResult = |
| 313 | + adGroupAdServiceClient |
| 314 | + .mutateAdGroupAds(Long.toString(customerId), Collections.singletonList(operation)) |
| 315 | + .getResults(0); |
| 316 | + System.out.printf( |
| 317 | + "Added an ad group ad with resource name: '%s'%n", |
| 318 | + mutateAdGroupAdResult.getResourceName()); |
| 319 | + return mutateAdGroupAdResult.getResourceName(); |
| 320 | + } |
| 321 | + } |
| 322 | + // [END add_things_to_do_ad_3] |
| 323 | +} |
0 commit comments