From 1c53f6a78d6c6c888fb28da9282d6ae152ce1bcf Mon Sep 17 00:00:00 2001 From: Josh Radcliff Date: Wed, 14 Jun 2023 09:48:10 -0400 Subject: [PATCH 1/4] Rename the `hotelads` package in examples to `travel`. --- .../ads/googleads/examples/{hotelads => travel}/AddHotelAd.java | 2 +- .../{hotelads => travel}/AddHotelAdGroupBidModifiers.java | 2 +- .../examples/{hotelads => travel}/AddHotelListingGroupTree.java | 2 +- .../AddPerformanceMaxForTravelGoalsCampaign.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename google-ads-examples/src/main/java/com/google/ads/googleads/examples/{hotelads => travel}/AddHotelAd.java (99%) rename google-ads-examples/src/main/java/com/google/ads/googleads/examples/{hotelads => travel}/AddHotelAdGroupBidModifiers.java (99%) rename google-ads-examples/src/main/java/com/google/ads/googleads/examples/{hotelads => travel}/AddHotelListingGroupTree.java (99%) rename google-ads-examples/src/main/java/com/google/ads/googleads/examples/{hotelads => travel}/AddPerformanceMaxForTravelGoalsCampaign.java (99%) diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAd.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAd.java similarity index 99% rename from google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAd.java rename to google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAd.java index 2ceff134e1..ca99f7a3b1 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAd.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAd.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.ads.googleads.examples.hotelads; +package com.google.ads.googleads.examples.travel; import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime; diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAdGroupBidModifiers.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAdGroupBidModifiers.java similarity index 99% rename from google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAdGroupBidModifiers.java rename to google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAdGroupBidModifiers.java index 4c800d4a41..7d86e3f911 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelAdGroupBidModifiers.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelAdGroupBidModifiers.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.ads.googleads.examples.hotelads; +package com.google.ads.googleads.examples.travel; import com.beust.jcommander.Parameter; import com.google.ads.googleads.examples.utils.ArgumentNames; diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelListingGroupTree.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelListingGroupTree.java similarity index 99% rename from google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelListingGroupTree.java rename to google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelListingGroupTree.java index 99ea8ab806..15a515d445 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddHotelListingGroupTree.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddHotelListingGroupTree.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.ads.googleads.examples.hotelads; +package com.google.ads.googleads.examples.travel; import com.beust.jcommander.Parameter; import com.google.ads.googleads.examples.utils.ArgumentNames; diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddPerformanceMaxForTravelGoalsCampaign.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddPerformanceMaxForTravelGoalsCampaign.java similarity index 99% rename from google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddPerformanceMaxForTravelGoalsCampaign.java rename to google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddPerformanceMaxForTravelGoalsCampaign.java index ec0b64bbd4..78f1e82700 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/hotelads/AddPerformanceMaxForTravelGoalsCampaign.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddPerformanceMaxForTravelGoalsCampaign.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.ads.googleads.examples.hotelads; +package com.google.ads.googleads.examples.travel; import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime; From 384f9b2f8712aa6c8a612a57457c5d07ae09f30b Mon Sep 17 00:00:00 2001 From: Josh Radcliff Date: Wed, 14 Jun 2023 10:10:47 -0400 Subject: [PATCH 2/4] Update placeholder email addresses, phone numbers and names in examples. New values adhere to the recommendations from https://developers.google.com/style/examples#example-domain-names. --- .../advancedoperations/AddCallAd.java | 2 +- .../advancedoperations/AddSmartCampaign.java | 2 +- .../examples/extensions/AddCall.java | 8 +++--- .../remarketing/AddCustomerMatchUserList.java | 18 ++++++------- .../remarketing/UploadCallConversion.java | 6 ++++- .../UploadConversionEnhancement.java | 6 ++--- .../UploadStoreSalesTransactions.java | 25 +++++++++++-------- 7 files changed, 38 insertions(+), 29 deletions(-) diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddCallAd.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddCallAd.java index 889ba14637..f54f087f1a 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddCallAd.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddCallAd.java @@ -121,7 +121,7 @@ public static void main(String[] args) throws IOException { * @param customerId the client customer ID. * @param adGroupId the ad group ID to add a call ad to. * @param phoneCountry the phone country (2-letter code). - * @param phoneNumber the raw phone number, e.g. '(123) 456-7890'. + * @param phoneNumber the raw phone number, e.g. '(800) 555-0100'. * @param conversionActionId the conversion action ID to attribute conversions to. * @throws GoogleAdsException if an API request failed with one or more service errors. */ diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java index 673c479b60..3a5e4d18c2 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/advancedoperations/AddSmartCampaign.java @@ -75,7 +75,7 @@ public class AddSmartCampaign { private static final String COUNTRY_CODE = "US"; private static final String LANGUAGE_CODE = "en"; private static final String LANDING_PAGE_URL = "http://www.example.com"; - private static final String PHONE_NUMBER = "555-555-5555"; + private static final String PHONE_NUMBER = "800-555-0100"; private static final long BUDGET_TEMPORARY_ID = -1; private static final long SMART_CAMPAIGN_TEMPORARY_ID = -2; private static final long AD_GROUP_TEMPORARY_ID = -3; diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddCall.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddCall.java index 5800eb4569..e4eaaa24e3 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddCall.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/extensions/AddCall.java @@ -26,15 +26,15 @@ import com.google.ads.googleads.v14.enums.MinuteOfHourEnum.MinuteOfHour; import com.google.ads.googleads.v14.errors.GoogleAdsError; import com.google.ads.googleads.v14.errors.GoogleAdsException; +import com.google.ads.googleads.v14.resources.Asset; import com.google.ads.googleads.v14.resources.CustomerAsset; +import com.google.ads.googleads.v14.services.AssetOperation; import com.google.ads.googleads.v14.services.AssetServiceClient; import com.google.ads.googleads.v14.services.CustomerAssetOperation; import com.google.ads.googleads.v14.services.CustomerAssetServiceClient; import com.google.ads.googleads.v14.services.MutateAssetsResponse; import com.google.ads.googleads.v14.services.MutateCustomerAssetsResponse; import com.google.ads.googleads.v14.utils.ResourceNames; -import com.google.ads.googleads.v14.resources.Asset; -import com.google.ads.googleads.v14.services.AssetOperation; import com.google.common.collect.ImmutableList; import java.io.FileNotFoundException; import java.io.IOException; @@ -117,7 +117,7 @@ public static void main(String[] args) throws IOException { * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @param phoneCountry the phone country (2-letter code). - * @param phoneNumber the raw phone number, e.g. '(123) 456-7890'. + * @param phoneNumber the raw phone number, e.g. '(800) 555-0100'. * @param conversionActionId the conversion action ID to attribute conversions to. * @throws GoogleAdsException if an API request failed with one or more service errors. */ @@ -142,7 +142,7 @@ private void runExample( * @param googleAdsClient the Google Ads API client. * @param customerId the client customer ID. * @param phoneCountry the phone country (2-letter code). - * @param phoneNumber the raw phone number, e.g. '(123) 456-7890'. + * @param phoneNumber the raw phone number, e.g. '(800) 555-0100'. * @param conversionActionId the conversion action ID to attribute conversions to. * @return resource name of the newly created asset. */ diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/AddCustomerMatchUserList.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/AddCustomerMatchUserList.java index 1f26dd1b7f..616ed0879e 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/AddCustomerMatchUserList.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/AddCustomerMatchUserList.java @@ -375,28 +375,28 @@ private List buildOfflineUserDataJobOperations() // The first user data has an email address and a phone number. Map rawRecord1 = ImmutableMap.builder() - .put("email", "test@gmail.com") + .put("email", "dana@example.com") // Phone number to be converted to E.164 format, with a leading '+' as required. This // includes whitespace that will be removed later. - .put("phone", "+1 234 5678910") + .put("phone", "+1 800 5550101") .build(); // The second user data has an email address, a mailing address, and a phone number. Map rawRecord2 = ImmutableMap.builder() - // Email address that includes a period (.) before the Gmail domain. - .put("email", "test.2@gmail.com") + // Email address that includes a period (.) before the domain. + .put("email", "alex.2@example.com") // Address that includes all four required elements: first name, last name, country // code, and postal code. - .put("firstName", "John") - .put("lastName", "Doe") + .put("firstName", "Alex") + .put("lastName", "Quinn") .put("countryCode", "US") - .put("postalCode", "10011") + .put("postalCode", "94045") // Phone number to be converted to E.164 format, with a leading '+' as required. - .put("phone", "+1 234 5678911") + .put("phone", "+1 800 5550102") .build(); // The third user data only has an email address. Map rawRecord3 = - ImmutableMap.builder().put("email", "test3@gmail.com").build(); + ImmutableMap.builder().put("email", "charlie@example.com").build(); // Adds the raw records to the raw input list. rawRecords.add(rawRecord1); rawRecords.add(rawRecord2); diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadCallConversion.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadCallConversion.java index e3ca6b7050..37d6ad3b4a 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadCallConversion.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadCallConversion.java @@ -46,7 +46,11 @@ private static class UploadCallConversionParams extends CodeSampleParams { @Parameter(names = ArgumentNames.CONVERSION_ACTION_ID) private String conversionActionId; - @Parameter(names = ArgumentNames.CALLER_ID) + @Parameter( + names = ArgumentNames.CALLER_ID, + description = + "The caller ID from which this call was placed. Caller ID is expected to be in E.164" + + " format with preceding '+' sign, e.g. \"+18005550100\"") private String callerId; @Parameter( diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadConversionEnhancement.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadConversionEnhancement.java index dc462c9989..6089d699bb 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadConversionEnhancement.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadConversionEnhancement.java @@ -166,8 +166,8 @@ private void runExample( UserIdentifier.newBuilder() .setAddressInfo( OfflineUserAddressInfo.newBuilder() - .setHashedFirstName(normalizeAndHash(sha256Digest, "Joanna")) - .setHashedLastName(normalizeAndHash(sha256Digest, "Smith")) + .setHashedFirstName(normalizeAndHash(sha256Digest, "Dana")) + .setHashedLastName(normalizeAndHash(sha256Digest, "Quinn")) .setHashedStreetAddress( normalizeAndHash(sha256Digest, "1600 Amphitheatre Pkwy")) .setCity("Mountain View") @@ -183,7 +183,7 @@ private void runExample( UserIdentifier.newBuilder() .setUserIdentifierSource(UserIdentifierSource.FIRST_PARTY) // Uses the normalize and hash method specifically for email addresses. - .setHashedEmail(normalizeAndHashEmailAddress(sha256Digest, "joannasmith@example.com")) + .setHashedEmail(normalizeAndHashEmailAddress(sha256Digest, "dana@example.com")) .build(); // Adds the user identifiers to the enhancement adjustment. diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadStoreSalesTransactions.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadStoreSalesTransactions.java index 936783f4d6..83d7ad13f3 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadStoreSalesTransactions.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/remarketing/UploadStoreSalesTransactions.java @@ -154,7 +154,9 @@ private static class UploadStoreSalesTransactionsParams extends CodeSampleParams private static final String CUSTOM_VALUE = null; public static void main(String[] args) - throws InterruptedException, ExecutionException, TimeoutException, + throws InterruptedException, + ExecutionException, + TimeoutException, UnsupportedEncodingException { UploadStoreSalesTransactionsParams params = new UploadStoreSalesTransactionsParams(); if (!params.parseArguments(args)) { @@ -284,7 +286,9 @@ private void runExample( String countryCode, String languageCode, int quantity) - throws InterruptedException, ExecutionException, TimeoutException, + throws InterruptedException, + ExecutionException, + TimeoutException, UnsupportedEncodingException { String offlineUserDataJobResourceName; try (OfflineUserDataJobServiceClient offlineUserDataJobServiceClient = @@ -444,7 +448,9 @@ private void addTransactionsToOfflineUserDataJob( String countryCode, String languageCode, Integer quantity) - throws InterruptedException, ExecutionException, TimeoutException, + throws InterruptedException, + ExecutionException, + TimeoutException, UnsupportedEncodingException { // Constructs the operation for each transaction. List userDataJobOperations = @@ -492,12 +498,11 @@ private void addTransactionsToOfflineUserDataJob( // Checks if any warnings occurred and displays details. if (response.hasWarning()) { // Converts the Any in response back to a GoogleAdsFailure object. - GoogleAdsFailure warningsFailure = ErrorUtils.getInstance() - .getGoogleAdsFailure(response.getWarning()); + GoogleAdsFailure warningsFailure = + ErrorUtils.getInstance().getGoogleAdsFailure(response.getWarning()); // Prints some information about the warnings encountered. System.out.println( - System.out.printf( - "Encountered %d warning(s).%n", warningsFailure.getErrorsCount())); + System.out.printf("Encountered %d warning(s).%n", warningsFailure.getErrorsCount())); } // [END enable_warnings_2] } else { @@ -540,7 +545,7 @@ private List buildOfflineUserDataJobOperations( UserIdentifier.newBuilder() .setHashedEmail( // Email addresses must be normalized and hashed. - normalizeAndHash(sha256Digest, "customer@example.com")) + normalizeAndHash(sha256Digest, "dana@example.com")) .build(), UserIdentifier.newBuilder() .setAddressInfo(OfflineUserAddressInfo.newBuilder().setState("NY")) @@ -571,8 +576,8 @@ private List buildOfflineUserDataJobOperations( UserIdentifier.newBuilder() .setAddressInfo( OfflineUserAddressInfo.newBuilder() - .setHashedFirstName(normalizeAndHash(sha256Digest, "John")) - .setHashedLastName(normalizeAndHash(sha256Digest, "Doe")) + .setHashedFirstName(normalizeAndHash(sha256Digest, "Dana")) + .setHashedLastName(normalizeAndHash(sha256Digest, "Quinn")) .setCountryCode("US") .setPostalCode("10011"))) .setTransactionAttribute( From c346c008e4c482e24704d666eb271c04606e4872 Mon Sep 17 00:00:00 2001 From: Josh Radcliff Date: Wed, 14 Jun 2023 10:35:06 -0400 Subject: [PATCH 3/4] Add AddThingsToDoAd example. Based on the PHP example from https://github.com/googleads/google-ads-php/pull/887. --- .../examples/travel/AddThingsToDoAd.java | 323 ++++++++++++++++++ .../examples/utils/ArgumentNames.java | 1 + 2 files changed, 324 insertions(+) create mode 100644 google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java new file mode 100644 index 0000000000..4b30ff5153 --- /dev/null +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java @@ -0,0 +1,323 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.ads.googleads.examples.travel; + +import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime; + +import com.beust.jcommander.Parameter; +import com.google.ads.googleads.examples.utils.ArgumentNames; +import com.google.ads.googleads.examples.utils.CodeSampleParams; +import com.google.ads.googleads.lib.GoogleAdsClient; +import com.google.ads.googleads.v14.common.MaximizeConversionValue; +import com.google.ads.googleads.v14.common.TravelAdInfo; +import com.google.ads.googleads.v14.enums.AdGroupAdStatusEnum.AdGroupAdStatus; +import com.google.ads.googleads.v14.enums.AdGroupStatusEnum.AdGroupStatus; +import com.google.ads.googleads.v14.enums.AdGroupTypeEnum.AdGroupType; +import com.google.ads.googleads.v14.enums.AdvertisingChannelSubTypeEnum.AdvertisingChannelSubType; +import com.google.ads.googleads.v14.enums.AdvertisingChannelTypeEnum.AdvertisingChannelType; +import com.google.ads.googleads.v14.enums.BudgetDeliveryMethodEnum.BudgetDeliveryMethod; +import com.google.ads.googleads.v14.enums.CampaignStatusEnum.CampaignStatus; +import com.google.ads.googleads.v14.errors.GoogleAdsError; +import com.google.ads.googleads.v14.errors.GoogleAdsException; +import com.google.ads.googleads.v14.resources.Ad; +import com.google.ads.googleads.v14.resources.AdGroup; +import com.google.ads.googleads.v14.resources.AdGroupAd; +import com.google.ads.googleads.v14.resources.Campaign; +import com.google.ads.googleads.v14.resources.Campaign.NetworkSettings; +import com.google.ads.googleads.v14.resources.Campaign.TravelCampaignSettings; +import com.google.ads.googleads.v14.resources.CampaignBudget; +import com.google.ads.googleads.v14.services.AdGroupAdOperation; +import com.google.ads.googleads.v14.services.AdGroupAdServiceClient; +import com.google.ads.googleads.v14.services.AdGroupOperation; +import com.google.ads.googleads.v14.services.AdGroupServiceClient; +import com.google.ads.googleads.v14.services.CampaignBudgetOperation; +import com.google.ads.googleads.v14.services.CampaignBudgetServiceClient; +import com.google.ads.googleads.v14.services.CampaignOperation; +import com.google.ads.googleads.v14.services.CampaignServiceClient; +import com.google.ads.googleads.v14.services.MutateAdGroupAdResult; +import com.google.ads.googleads.v14.services.MutateAdGroupResult; +import com.google.ads.googleads.v14.services.MutateCampaignBudgetsResponse; +import com.google.ads.googleads.v14.services.MutateCampaignResult; +import com.google.ads.googleads.v14.services.MutateCampaignsResponse; +import com.google.common.collect.ImmutableList; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Collections; + +/** + * Creates a Things to do campaign, an ad group and a Things to do ad. + * + *

Prerequisite: You need to have an access to the Things to Do Center. The integration + * instructions can be found at: https://support.google.com/google-ads/answer/13387362. + */ +public class AddThingsToDoAd { + + private static class AddThingsToDoAdParams extends CodeSampleParams { + + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) + private Long customerId; + + @Parameter(names = ArgumentNames.THINGS_TO_DO_CENTER_ACCOUNT_ID, required = true) + private Long thingsToDoCenterAccountId; + } + + public static void main(String[] args) { + AddThingsToDoAdParams params = new AddThingsToDoAdParams(); + if (!params.parseArguments(args)) { + + // Either pass the required parameters for this example on the command line, or insert them + // into the code here. See the parameter class definition above for descriptions. + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); + params.thingsToDoCenterAccountId = + Long.parseLong("INSERT_THINGS_TO_DO_CENTER_ACCOUNT_ID_HERE"); + } + + GoogleAdsClient googleAdsClient = null; + try { + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); + } catch (FileNotFoundException fnfe) { + System.err.printf( + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); + System.exit(1); + } catch (IOException ioe) { + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); + System.exit(1); + } + + try { + new AddThingsToDoAd() + .runExample(googleAdsClient, params.customerId, params.thingsToDoCenterAccountId); + } catch (GoogleAdsException gae) { + // GoogleAdsException is the base class for most exceptions thrown by an API request. + // Instances of this exception have a message and a GoogleAdsFailure that contains a + // collection of GoogleAdsErrors that indicate the underlying causes of the + // GoogleAdsException. + System.err.printf( + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", + gae.getRequestId()); + int i = 0; + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { + System.err.printf(" Error %d: %s%n", i++, googleAdsError); + } + System.exit(1); + } + } + + /** + * Runs the example. + * + * @param googleAdsClient the Google Ads API client. + * @param customerId the client customer ID. + * @param thingsToDoCenterAccountId the Things to Do Center account ID. + * @throws GoogleAdsException if an API request failed with one or more service errors. + */ + private void runExample( + GoogleAdsClient googleAdsClient, long customerId, long thingsToDoCenterAccountId) { + + // Creates a budget to be used by the campaign that will be created below. + String budgetResourceName = addCampaignBudget(googleAdsClient, customerId); + + // Creates a Things to do campaign. + String campaignResourceName = + addThingsToDoCampaign( + googleAdsClient, customerId, budgetResourceName, thingsToDoCenterAccountId); + + // Creates an ad group. + String adGroupResourceName = addAdGroup(googleAdsClient, customerId, campaignResourceName); + + // Creates an ad group ad. + addAddGroupAd(googleAdsClient, customerId, adGroupResourceName); + } + + /** + * Creates a new campaign budget in the specified client account. + * + * @param googleAdsClient the Google Ads API client. + * @param customerId the client customer ID. + * @return resource name of the newly created budget. + * @throws GoogleAdsException if an API request failed with one or more service errors. + */ + private String addCampaignBudget(GoogleAdsClient googleAdsClient, long customerId) { + CampaignBudget budget = + CampaignBudget.newBuilder() + .setName("Interplanetary Cruise Budget #" + getPrintableDateTime()) + .setDeliveryMethod(BudgetDeliveryMethod.STANDARD) + // Sets the amount of the budget. + .setAmountMicros(5_000_000) + // Makes the budget explicitly shared. You cannot set it to false for Things to do + // campaigns. + .setExplicitlyShared(true) + .build(); + + // Creates a campaign budget operation. + CampaignBudgetOperation op = CampaignBudgetOperation.newBuilder().setCreate(budget).build(); + + // Issues a mutate request. + try (CampaignBudgetServiceClient campaignBudgetServiceClient = + googleAdsClient.getLatestVersion().createCampaignBudgetServiceClient()) { + MutateCampaignBudgetsResponse response = + campaignBudgetServiceClient.mutateCampaignBudgets( + Long.toString(customerId), ImmutableList.of(op)); + String budgetResourceName = response.getResults(0).getResourceName(); + System.out.printf("Added a budget with resource name: '%s'%n", budgetResourceName); + return budgetResourceName; + } + } + + /** + * Creates a new Things to do campaign in the specified client account. + * + * @param googleAdsClient the Google Ads API client. + * @param customerId the client customer ID. + * @param budgetResourceName the resource name of the budget for the campaign. + * @param thingsToDoCenterAccountId the Things to Do Center account ID. + * @return resource name of the newly created campaign. + * @throws GoogleAdsException if an API request failed with one or more service errors. + */ + // [START add_things_to_do_ad] + private String addThingsToDoCampaign( + GoogleAdsClient googleAdsClient, + long customerId, + String budgetResourceName, + long thingsToDoCenterAccountId) { + // [START add_things_to_do_ad_1] + // Creates the campaign. + Campaign campaign = + Campaign.newBuilder() + .setName("Interplanetary Cruise #" + getPrintableDateTime()) + // Configures settings related to Things to do campaigns including advertising channel + // type, advertising channel sub type and travel campaign settings. + .setAdvertisingChannelType(AdvertisingChannelType.TRAVEL) + .setAdvertisingChannelSubType(AdvertisingChannelSubType.TRAVEL_ACTIVITIES) + .setTravelCampaignSettings( + TravelCampaignSettings.newBuilder().setTravelAccountId(thingsToDoCenterAccountId)) + // Recommendation: Sets the campaign to PAUSED when creating it to prevent + // the ads from immediately serving. Set to ENABLED once you've added + // targeting and the ads are ready to serve + .setStatus(CampaignStatus.PAUSED) + // Sets the bidding strategy to MaximizeConversionValue. Only this type can be used + // for Things to do campaigns. + .setMaximizeConversionValue(MaximizeConversionValue.newBuilder()) + // Sets the budget. + .setCampaignBudget(budgetResourceName) + // Configures the campaign network options. Only Google Search is allowed for + // Things to do campaigns. + .setNetworkSettings(NetworkSettings.newBuilder().setTargetGoogleSearch(true)) + .build(); + // [END add_things_to_do_ad_1] + + // Creates a campaign operation. + CampaignOperation operation = CampaignOperation.newBuilder().setCreate(campaign).build(); + + // Issues a mutate request to add the campaign. + try (CampaignServiceClient campaignServiceClient = + googleAdsClient.getLatestVersion().createCampaignServiceClient()) { + MutateCampaignsResponse response = + campaignServiceClient.mutateCampaigns( + Long.toString(customerId), Collections.singletonList(operation)); + MutateCampaignResult result = response.getResults(0); + System.out.printf( + "Added a Things to do campaign with resource name: '%s'%n", result.getResourceName()); + return result.getResourceName(); + } + } + // [END add_things_to_do_ad] + + /** + * Creates a new ad group in the specified Things to do campaign. + * + * @param googleAdsClient the Google Ads API client. + * @param customerId the client customer ID. + * @param campaignResourceName the resource name of the campaign that the new ad group will belong + * to. + * @return resource name of the newly created ad group. + * @throws GoogleAdsException if an API request failed with one or more service errors. + */ + // [START add_things_to_do_ad_2] + private String addAdGroup( + GoogleAdsClient googleAdsClient, long customerId, String campaignResourceName) { + // Creates an ad group. + AdGroup adGroup = + AdGroup.newBuilder() + .setName("Earth to Mars Cruises #" + getPrintableDateTime()) + .setCampaign(campaignResourceName) + // Sets the ad group type to TRAVEL_ADS. This cannot be set to other types. + .setType(AdGroupType.TRAVEL_ADS) + .setStatus(AdGroupStatus.ENABLED) + .build(); + + // Creates an ad group operation. + AdGroupOperation operation = AdGroupOperation.newBuilder().setCreate(adGroup).build(); + + // Issues a mutate request to add an ad group. + try (AdGroupServiceClient adGroupServiceClient = + googleAdsClient.getLatestVersion().createAdGroupServiceClient()) { + MutateAdGroupResult mutateAdGroupResult = + adGroupServiceClient + .mutateAdGroups(Long.toString(customerId), Collections.singletonList(operation)) + .getResults(0); + System.out.printf( + "Added an ad group with resource name: '%s'%n", mutateAdGroupResult.getResourceName()); + return mutateAdGroupResult.getResourceName(); + } + } + // [END add_things_to_do_ad_2] + + /** + * Creates a new ad group ad in the specified ad group. + * + * @param googleAdsClient the Google Ads API client. + * @param customerId the client customer ID. + * @param adGroupResourceName the resource name of the ad group that the new ad group ad will + * belong to. + * @return resource name of the newly created ad group ad. + * @throws GoogleAdsException if an API request failed with one or more service errors. + */ + // [START add_things_to_do_ad_3] + private String addAddGroupAd( + GoogleAdsClient googleAdsClient, long customerId, String adGroupResourceName) { + // Creates a new travel ad. + Ad ad = Ad.newBuilder().setTravelAd(TravelAdInfo.newBuilder()).build(); + // Creates a new ad group ad and sets its ad to the travel ad. + AdGroupAd adGroupAd = + AdGroupAd.newBuilder() + // Sets the ad to the ad created above. + .setAd(ad) + // Set the ad group ad to enabled. Setting this to paused will cause an error for + // Things to do campaigns. Pausing should happen at either the ad group or campaign + // level. + .setStatus(AdGroupAdStatus.ENABLED) + // Sets the ad group. + .setAdGroup(adGroupResourceName) + .build(); + + // Creates an ad group ad operation. + AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build(); + + // Issues a mutate request to add an ad group ad. + try (AdGroupAdServiceClient adGroupAdServiceClient = + googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) { + MutateAdGroupAdResult mutateAdGroupAdResult = + adGroupAdServiceClient + .mutateAdGroupAds(Long.toString(customerId), Collections.singletonList(operation)) + .getResults(0); + System.out.printf( + "Added an ad group ad with resource name: '%s'%n", + mutateAdGroupAdResult.getResourceName()); + return mutateAdGroupAdResult.getResourceName(); + } + } + // [END add_things_to_do_ad_3] +} diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java index 87f5396f68..54c74d6d14 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java @@ -111,6 +111,7 @@ public final class ArgumentNames { public static final String SALES_COUNTRY = "--salesCountry"; public static final String SITELINK_TEXT = "--sitelinkText"; public static final String START_DATE_TIME = "--startDateTime"; + public static final String THINGS_TO_DO_CENTER_ACCOUNT_ID = "--thingsToDoCenterAccountId"; public static final String USER_AGENT = "--userAgent"; public static final String USER_LIST_ID = "--userListId"; public static final String USER_LIST_IDS = "--userListIds"; From 15bc11b65d981ed47fa268e62ff416454a4cf277 Mon Sep 17 00:00:00 2001 From: Josh Radcliff Date: Wed, 14 Jun 2023 12:37:17 -0400 Subject: [PATCH 4/4] Update copyright year --- .../google/ads/googleads/examples/travel/AddThingsToDoAd.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java index 4b30ff5153..11d2b04474 100644 --- a/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java +++ b/google-ads-examples/src/main/java/com/google/ads/googleads/examples/travel/AddThingsToDoAd.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.