|
| 1 | +// Copyright 2021 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.advancedoperations; |
| 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.v8.enums.AdvertisingChannelTypeEnum.AdvertisingChannelType; |
| 24 | +import com.google.ads.googleads.v8.enums.SeasonalityEventScopeEnum.SeasonalityEventScope; |
| 25 | +import com.google.ads.googleads.v8.errors.GoogleAdsError; |
| 26 | +import com.google.ads.googleads.v8.errors.GoogleAdsException; |
| 27 | +import com.google.ads.googleads.v8.resources.BiddingDataExclusion; |
| 28 | +import com.google.ads.googleads.v8.services.BiddingDataExclusionOperation; |
| 29 | +import com.google.ads.googleads.v8.services.BiddingDataExclusionServiceClient; |
| 30 | +import com.google.ads.googleads.v8.services.MutateBiddingDataExclusionsResponse; |
| 31 | +import com.google.common.collect.ImmutableList; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Adds a customer-level data exclusion that excludes conversions from being used by Smart Bidding |
| 37 | + * for the time interval specified. |
| 38 | + * |
| 39 | + * <p>For more information on using data exclusions, see |
| 40 | + * https://developers.google.com/google-ads/api/docs/campaigns/bidding/data-exclusions. |
| 41 | + */ |
| 42 | +public class AddBiddingDataExclusion { |
| 43 | + |
| 44 | + private static class AddBiddingDataExclusionParams extends CodeSampleParams { |
| 45 | + |
| 46 | + @Parameter( |
| 47 | + names = ArgumentNames.CUSTOMER_ID, |
| 48 | + required = true, |
| 49 | + description = |
| 50 | + "The client customer ID of the Google Ads account that the data exclusion will" |
| 51 | + + " be added to.") |
| 52 | + private Long customerId; |
| 53 | + |
| 54 | + @Parameter( |
| 55 | + names = ArgumentNames.START_DATE_TIME, |
| 56 | + required = true, |
| 57 | + description = |
| 58 | + "The start date time in yyyy-MM-dd HH:mm:ss format of the data exclusion period.") |
| 59 | + private String startDateTime; |
| 60 | + |
| 61 | + @Parameter( |
| 62 | + names = ArgumentNames.END_DATE_TIME, |
| 63 | + required = true, |
| 64 | + description = |
| 65 | + "The end date time in yyyy-MM-dd HH:mm:ss format of the data exclusion period.") |
| 66 | + private String endDateTime; |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) throws IOException { |
| 70 | + AddBiddingDataExclusionParams params = new AddBiddingDataExclusionParams(); |
| 71 | + if (!params.parseArguments(args)) { |
| 72 | + // Either pass the required parameters for this example on the command line, or insert them |
| 73 | + // into the code here. See the parameter class definition above for descriptions. |
| 74 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 75 | + params.startDateTime = "INSERT_START_DATE_TIME_HERE"; |
| 76 | + params.endDateTime = "INSERT_END_DATE_TIME_HERE"; |
| 77 | + } |
| 78 | + |
| 79 | + GoogleAdsClient googleAdsClient = null; |
| 80 | + try { |
| 81 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 82 | + } catch (FileNotFoundException fnfe) { |
| 83 | + System.err.printf( |
| 84 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 85 | + System.exit(1); |
| 86 | + } catch (IOException ioe) { |
| 87 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 88 | + System.exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + new AddBiddingDataExclusion() |
| 93 | + .runExample(googleAdsClient, params.customerId, params.startDateTime, params.endDateTime); |
| 94 | + } catch (GoogleAdsException gae) { |
| 95 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 96 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 97 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 98 | + // GoogleAdsException. |
| 99 | + System.err.printf( |
| 100 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 101 | + gae.getRequestId()); |
| 102 | + int i = 0; |
| 103 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 104 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 105 | + } |
| 106 | + System.exit(1); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Adds a "CUSTOMER" scoped data exclusion for the client customer ID and dates specified. |
| 112 | + * |
| 113 | + * @param googleAdsClient the GoogleAdsClient |
| 114 | + * @param customerId the client ID of the Google Ads account |
| 115 | + * @param startDateTime the start time of the data exclusion (in yyyy-MM-dd HH:mm:ss format) in |
| 116 | + * the account's timezone |
| 117 | + * @param endDateTime the end time of the data exclusion (in yyyy-MM-dd HH:mm:ss format) in the |
| 118 | + * account's timezone |
| 119 | + */ |
| 120 | + private void runExample( |
| 121 | + GoogleAdsClient googleAdsClient, Long customerId, String startDateTime, String endDateTime) { |
| 122 | + |
| 123 | + try (BiddingDataExclusionServiceClient DataExclusionServiceClient = |
| 124 | + googleAdsClient.getLatestVersion().createBiddingDataExclusionServiceClient()) { |
| 125 | + |
| 126 | + // [START add_bidding_data_exclusion] |
| 127 | + BiddingDataExclusion DataExclusion = |
| 128 | + BiddingDataExclusion.newBuilder() |
| 129 | + // A unique name is required for every data exclusion. |
| 130 | + .setName("Data exclusion #" + getPrintableDateTime()) |
| 131 | + // The CHANNEL scope applies the data exclusion to all campaigns of specific |
| 132 | + // advertising channel types. In this example, the the exclusion will only apply to |
| 133 | + // Search campaigns. Use the CAMPAIGN scope to instead limit the scope to specific |
| 134 | + // campaigns. |
| 135 | + .setScope(SeasonalityEventScope.CHANNEL) |
| 136 | + .addAdvertisingChannelTypes(AdvertisingChannelType.SEARCH) |
| 137 | + // If setting scope CAMPAIGN, add individual campaign resource name(s) according to |
| 138 | + // the commented out line below. |
| 139 | + // .addCampaigns("INSERT_CAMPAIGN_RESOURCE_NAME_HERE") |
| 140 | + .setStartDateTime(startDateTime) |
| 141 | + .setEndDateTime(endDateTime) |
| 142 | + .build(); |
| 143 | + |
| 144 | + BiddingDataExclusionOperation operation = |
| 145 | + BiddingDataExclusionOperation.newBuilder().setCreate(DataExclusion).build(); |
| 146 | + |
| 147 | + MutateBiddingDataExclusionsResponse response = |
| 148 | + DataExclusionServiceClient.mutateBiddingDataExclusions( |
| 149 | + customerId.toString(), ImmutableList.of(operation)); |
| 150 | + System.out.printf( |
| 151 | + "Added data exclusion with resource name: %s%n", |
| 152 | + response.getResults(0).getResourceName()); |
| 153 | + // [END add_bidding_data_exclusion] |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments