|
| 1 | +// Copyright 2020 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.campaignmanagement; |
| 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.v2.common.ExpandedTextAdInfo; |
| 22 | +import com.google.ads.googleads.v2.common.PolicyTopicEntry; |
| 23 | +import com.google.ads.googleads.v2.enums.AdGroupAdStatusEnum.AdGroupAdStatus; |
| 24 | +import com.google.ads.googleads.v2.errors.GoogleAdsError; |
| 25 | +import com.google.ads.googleads.v2.errors.GoogleAdsException; |
| 26 | +import com.google.ads.googleads.v2.errors.PolicyFindingErrorEnum.PolicyFindingError; |
| 27 | +import com.google.ads.googleads.v2.resources.Ad; |
| 28 | +import com.google.ads.googleads.v2.resources.AdGroupAd; |
| 29 | +import com.google.ads.googleads.v2.services.AdGroupAdOperation; |
| 30 | +import com.google.ads.googleads.v2.services.AdGroupAdServiceClient; |
| 31 | +import com.google.ads.googleads.v2.services.MutateAdGroupAdsResponse; |
| 32 | +import com.google.ads.googleads.v2.utils.ResourceNames; |
| 33 | +import com.google.common.collect.ImmutableList; |
| 34 | +import com.google.protobuf.StringValue; |
| 35 | +import java.io.FileNotFoundException; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.List; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +/** |
| 41 | + * Shows how to use the validateOnly header to validate an expanded text ad. No ads will be |
| 42 | + * created, but exceptions will still be thrown. |
| 43 | + */ |
| 44 | +public class ValidateTextAd { |
| 45 | + |
| 46 | + private static class ValidateTextAdParams extends CodeSampleParams { |
| 47 | + |
| 48 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 49 | + private Long customerId; |
| 50 | + |
| 51 | + @Parameter(names = ArgumentNames.AD_GROUP_ID, required = true) |
| 52 | + private Long adGroupId; |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) { |
| 56 | + ValidateTextAdParams params = new ValidateTextAdParams(); |
| 57 | + if (!params.parseArguments(args)) { |
| 58 | + |
| 59 | + // Either pass the required parameters for this example on the command line, or insert them |
| 60 | + // into the code here. See the parameter class definition above for descriptions. |
| 61 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 62 | + params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE"); |
| 63 | + } |
| 64 | + |
| 65 | + GoogleAdsClient googleAdsClient; |
| 66 | + try { |
| 67 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 68 | + } catch (FileNotFoundException fnfe) { |
| 69 | + System.err.printf( |
| 70 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 71 | + return; |
| 72 | + } catch (IOException ioe) { |
| 73 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + new ValidateTextAd().runExample(googleAdsClient, params.customerId, params.adGroupId); |
| 79 | + } catch (GoogleAdsException gae) { |
| 80 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 81 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 82 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 83 | + // GoogleAdsException. |
| 84 | + System.err.printf( |
| 85 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 86 | + gae.getRequestId()); |
| 87 | + int i = 0; |
| 88 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 89 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Runs the example. |
| 96 | + * |
| 97 | + * @param googleAdsClient the Google Ads API client. |
| 98 | + * @param customerId the client customer ID. |
| 99 | + * @param adGroupId the ad group ID. |
| 100 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 101 | + */ |
| 102 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId, long adGroupId) { |
| 103 | + String adGroupResourceName = ResourceNames.adGroup(customerId, adGroupId); |
| 104 | + |
| 105 | + // Creates the expanded text ad info. |
| 106 | + ExpandedTextAdInfo expandedTextAdInfo = |
| 107 | + ExpandedTextAdInfo.newBuilder() |
| 108 | + .setDescription(StringValue.of("Luxury Cruise to Mars")) |
| 109 | + .setHeadlinePart1(StringValue.of("Visit the Red Planet in style")) |
| 110 | + // Adds a headline that will trigger a policy violation to demonstrate error handling. |
| 111 | + .setHeadlinePart2(StringValue.of("Low-gravity fun for everyone!!")) |
| 112 | + .build(); |
| 113 | + |
| 114 | + // Wraps the info in an Ad object. |
| 115 | + Ad ad = |
| 116 | + Ad.newBuilder() |
| 117 | + .setExpandedTextAd(expandedTextAdInfo) |
| 118 | + .addFinalUrls(StringValue.of("http://www.example.com")) |
| 119 | + .build(); |
| 120 | + |
| 121 | + // Builds the final ad group ad representation. |
| 122 | + AdGroupAd adGroupAd = |
| 123 | + AdGroupAd.newBuilder() |
| 124 | + .setAdGroup(StringValue.of(adGroupResourceName)) |
| 125 | + .setStatus(AdGroupAdStatus.PAUSED) |
| 126 | + .setAd(ad) |
| 127 | + .build(); |
| 128 | + |
| 129 | + AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build(); |
| 130 | + |
| 131 | + try (AdGroupAdServiceClient adGroupAdServiceClient = |
| 132 | + googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) { |
| 133 | + // Issues the mutate request setting validateOnly=true. |
| 134 | + MutateAdGroupAdsResponse response = |
| 135 | + adGroupAdServiceClient.mutateAdGroupAds( |
| 136 | + Long.toString(customerId), ImmutableList.of(operation), false, true); |
| 137 | + |
| 138 | + // Since validation is ON, result will be null. |
| 139 | + System.out.println("Expanded text ad validated successfully."); |
| 140 | + } catch (GoogleAdsException e) { |
| 141 | + // This block will be hit if there is a validation error from the server. |
| 142 | + System.out.println("There were validation error(s) while adding expanded text ad."); |
| 143 | + |
| 144 | + if (e.getGoogleAdsFailure() != null) { |
| 145 | + // Note: Depending on the ad type, you may get back policy violation errors as |
| 146 | + // either PolicyFindingError or PolicyViolationError. ExpandedTextAds return |
| 147 | + // errors as PolicyFindingError, so only this case is illustrated here. See |
| 148 | + // https://developers.google.com/google-ads/api/docs/policy-exemption/overview |
| 149 | + // for additional details. |
| 150 | + List<GoogleAdsError> policyFindingErrors = |
| 151 | + e.getGoogleAdsFailure().getErrorsList().stream() |
| 152 | + .filter( |
| 153 | + err -> |
| 154 | + err.getErrorCode().getPolicyFindingError() |
| 155 | + == PolicyFindingError.POLICY_FINDING) |
| 156 | + .collect(Collectors.toList()); |
| 157 | + int count = 1; |
| 158 | + for (GoogleAdsError policyFindingError : policyFindingErrors) { |
| 159 | + if (policyFindingError.getDetails().hasPolicyFindingDetails()) { |
| 160 | + List<PolicyTopicEntry> policyTopicEntries = |
| 161 | + policyFindingError |
| 162 | + .getDetails() |
| 163 | + .getPolicyFindingDetails() |
| 164 | + .getPolicyTopicEntriesList(); |
| 165 | + for (PolicyTopicEntry policyTopicEntry : policyTopicEntries) { |
| 166 | + System.out.printf( |
| 167 | + "%d Policy topic entry with topic '%s' and type '%s' was found.%n", |
| 168 | + count, policyTopicEntry.getTopic().getValue(), policyTopicEntry.getType()); |
| 169 | + } |
| 170 | + count++; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } catch (Exception e) { |
| 175 | + System.out.printf("Failure: Message '%s'.%n", e.getMessage()); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments