|
| 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.targeting; |
| 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.v3.errors.GoogleAdsError; |
| 22 | +import com.google.ads.googleads.v3.errors.GoogleAdsException; |
| 23 | +import com.google.ads.googleads.v3.resources.CarrierConstant; |
| 24 | +import com.google.ads.googleads.v3.resources.LanguageConstant; |
| 25 | +import com.google.ads.googleads.v3.services.GoogleAdsRow; |
| 26 | +import com.google.ads.googleads.v3.services.GoogleAdsServiceClient; |
| 27 | +import com.google.ads.googleads.v3.services.SearchGoogleAdsStreamRequest; |
| 28 | +import com.google.ads.googleads.v3.services.SearchGoogleAdsStreamResponse; |
| 29 | +import com.google.api.gax.rpc.ServerStream; |
| 30 | +import java.io.FileNotFoundException; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Illustrates how to: |
| 35 | + * |
| 36 | + * <ul> |
| 37 | + * <li>Search for language constants where the name includes a given string. |
| 38 | + * <li>Search for all the available mobile carrier constants with a given country code. |
| 39 | + * </ul> |
| 40 | + */ |
| 41 | +public class SearchForLanguageAndCarrierConstants { |
| 42 | + |
| 43 | + private static class SearchForLanguageAndCarrierConstantsParams extends CodeSampleParams { |
| 44 | + |
| 45 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 46 | + private Long customerId; |
| 47 | + |
| 48 | + @Parameter(names = ArgumentNames.LANGUAGE_NAME) |
| 49 | + private String languageName = "eng"; |
| 50 | + |
| 51 | + @Parameter( |
| 52 | + names = ArgumentNames.COUNTRY_CODE, |
| 53 | + description = |
| 54 | + "Country code for carrier constant filtering. A list of country codes can be" |
| 55 | + + " referenced here:" |
| 56 | + + " https://developers.google.com/adwords/api/docs/appendix/geotargeting.") |
| 57 | + private String countryCode = "US"; |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + SearchForLanguageAndCarrierConstantsParams params = |
| 62 | + new SearchForLanguageAndCarrierConstantsParams(); |
| 63 | + if (!params.parseArguments(args)) { |
| 64 | + |
| 65 | + // Either pass the required parameters for this example on the command line, or insert them |
| 66 | + // into the code here. See the parameter class definition above for descriptions. |
| 67 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 68 | + |
| 69 | + // Optional: specify the language name or country code. |
| 70 | + // params.languageName = "INSERT_LANGUAGE_NAME_HERE"; |
| 71 | + // params.countryCode = "INSERT_COUNTRY_CODE_HERE"; |
| 72 | + } |
| 73 | + |
| 74 | + GoogleAdsClient googleAdsClient; |
| 75 | + try { |
| 76 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 77 | + } catch (FileNotFoundException fnfe) { |
| 78 | + System.err.printf( |
| 79 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 80 | + return; |
| 81 | + } catch (IOException ioe) { |
| 82 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + new SearchForLanguageAndCarrierConstants() |
| 88 | + .runExample(googleAdsClient, params.customerId, params.languageName, params.countryCode); |
| 89 | + } catch (GoogleAdsException gae) { |
| 90 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 91 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 92 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 93 | + // GoogleAdsException. |
| 94 | + System.err.printf( |
| 95 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 96 | + gae.getRequestId()); |
| 97 | + int i = 0; |
| 98 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 99 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Runs the example. |
| 106 | + * |
| 107 | + * @param googleAdsClient the Google Ads API client. |
| 108 | + * @param customerId the client customer ID. |
| 109 | + * @param languageName the string to use for searching for language constants. |
| 110 | + * @param countryCode the country code for searching for carrier constants. |
| 111 | + * @throws GoogleAdsException if an API request failed with one or more service errors. |
| 112 | + */ |
| 113 | + private void runExample( |
| 114 | + GoogleAdsClient googleAdsClient, long customerId, String languageName, String countryCode) { |
| 115 | + searchForLanguageConstants(googleAdsClient, customerId, languageName); |
| 116 | + searchForCarrierConstants(googleAdsClient, customerId, countryCode); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Searches for language constants where the name includes the specified language name. |
| 121 | + * |
| 122 | + * @param googleAdsClient the Google Ads API client. |
| 123 | + * @param customerId the client customer ID. |
| 124 | + * @param languageName the string to use for searching for language constants. |
| 125 | + */ |
| 126 | + private void searchForLanguageConstants( |
| 127 | + GoogleAdsClient googleAdsClient, long customerId, String languageName) { |
| 128 | + try (GoogleAdsServiceClient googleAdsServiceClient = |
| 129 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 130 | + |
| 131 | + // Creates a query that retrieves each language constant with the language name parameter in |
| 132 | + // the name. |
| 133 | + String searchQuery = |
| 134 | + "SELECT language_constant.id, " |
| 135 | + + "language_constant.code, " |
| 136 | + + "language_constant.name, " |
| 137 | + + "language_constant.targetable " |
| 138 | + + "FROM language_constant " |
| 139 | + + "WHERE language_constant.name LIKE '%" |
| 140 | + + languageName |
| 141 | + + "%'"; |
| 142 | + |
| 143 | + // Constructs the SearchGoogleAdsStreamRequest. |
| 144 | + SearchGoogleAdsStreamRequest request = |
| 145 | + SearchGoogleAdsStreamRequest.newBuilder() |
| 146 | + .setCustomerId(Long.toString(customerId)) |
| 147 | + .setQuery(searchQuery) |
| 148 | + .build(); |
| 149 | + |
| 150 | + // Creates and issues a search Google Ads stream request that will retrieve all of the |
| 151 | + // requested field values for the matching language constants. |
| 152 | + ServerStream<SearchGoogleAdsStreamResponse> stream = |
| 153 | + googleAdsServiceClient.searchStreamCallable().call(request); |
| 154 | + |
| 155 | + for (SearchGoogleAdsStreamResponse response : stream) { |
| 156 | + for (GoogleAdsRow googleAdsRow : response.getResultsList()) { |
| 157 | + LanguageConstant languageConstant = googleAdsRow.getLanguageConstant(); |
| 158 | + System.out.printf( |
| 159 | + "Language with ID %d, code '%s', name '%s', and targetable '%s' was found.%n", |
| 160 | + languageConstant.getId().getValue(), |
| 161 | + languageConstant.getCode().getValue(), |
| 162 | + languageConstant.getName().getValue(), |
| 163 | + languageConstant.getTargetable().getValue()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Searches for all the available mobile carrier constants with a given country code. |
| 171 | + * |
| 172 | + * @param googleAdsClient the Google Ads API client. |
| 173 | + * @param customerId the client customer ID. |
| 174 | + * @param countryCode the country code to use for filtering. |
| 175 | + */ |
| 176 | + private void searchForCarrierConstants( |
| 177 | + GoogleAdsClient googleAdsClient, long customerId, String countryCode) { |
| 178 | + try (GoogleAdsServiceClient googleAdsServiceClient = |
| 179 | + googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) { |
| 180 | + |
| 181 | + // Creates a query that retrieves each language constant with the language name parameter in |
| 182 | + // the name. |
| 183 | + String searchQuery = |
| 184 | + String.format( |
| 185 | + "SELECT carrier_constant.id, " |
| 186 | + + "carrier_constant.name, " |
| 187 | + + "carrier_constant.country_code " |
| 188 | + + "FROM carrier_constant " |
| 189 | + + "WHERE carrier_constant.country_code = '%s'", |
| 190 | + countryCode); |
| 191 | + |
| 192 | + // Constructs the SearchGoogleAdsStreamRequest. |
| 193 | + SearchGoogleAdsStreamRequest request = |
| 194 | + SearchGoogleAdsStreamRequest.newBuilder() |
| 195 | + .setCustomerId(Long.toString(customerId)) |
| 196 | + .setQuery(searchQuery) |
| 197 | + .build(); |
| 198 | + |
| 199 | + // Creates and issues a search Google Ads stream request that will retrieve all of the |
| 200 | + // requested field values for the matching carrier constants. |
| 201 | + ServerStream<SearchGoogleAdsStreamResponse> stream = |
| 202 | + googleAdsServiceClient.searchStreamCallable().call(request); |
| 203 | + |
| 204 | + for (SearchGoogleAdsStreamResponse response : stream) { |
| 205 | + for (GoogleAdsRow googleAdsRow : response.getResultsList()) { |
| 206 | + CarrierConstant carrierConstant = googleAdsRow.getCarrierConstant(); |
| 207 | + System.out.printf( |
| 208 | + "Carrier with ID %d, name '%s', and country code '%s' was found.%n", |
| 209 | + carrierConstant.getId().getValue(), |
| 210 | + carrierConstant.getName().getValue(), |
| 211 | + carrierConstant.getCountryCode().getValue()); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments