Skip to content

Commit b17856f

Browse files
authored
Add SearchForLanguageAndCarrierConstants example (#271)
1 parent bb75ca9 commit b17856f

3 files changed

Lines changed: 255 additions & 29 deletions

File tree

google-ads-examples/src/main/java/com/google/ads/googleads/examples/targeting/GetGeoTargetConstantsByNames.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package com.google.ads.googleads.examples.targeting;
1616

1717
import com.google.ads.googleads.lib.GoogleAdsClient;
18-
import com.google.ads.googleads.v3.errors.GoogleAdsException;
1918
import com.google.ads.googleads.v3.errors.GoogleAdsError;
19+
import com.google.ads.googleads.v3.errors.GoogleAdsException;
2020
import com.google.ads.googleads.v3.services.GeoTargetConstantServiceClient;
2121
import com.google.ads.googleads.v3.services.GeoTargetConstantSuggestion;
2222
import com.google.ads.googleads.v3.services.SuggestGeoTargetConstantsRequest;
@@ -60,41 +60,49 @@ public static void main(String[] args) {
6060
}
6161
}
6262

63+
/**
64+
* Runs the example.
65+
*
66+
* @param googleAdsClient the Google Ads API client.
67+
* @throws GoogleAdsException if an API request failed with one or more service errors.
68+
*/
6369
private void runExample(GoogleAdsClient googleAdsClient) {
64-
GeoTargetConstantServiceClient geoTargetClient =
65-
googleAdsClient.getLatestVersion().createGeoTargetConstantServiceClient();
70+
try (GeoTargetConstantServiceClient geoTargetClient =
71+
googleAdsClient.getLatestVersion().createGeoTargetConstantServiceClient()) {
6672

67-
SuggestGeoTargetConstantsRequest.Builder requestBuilder =
68-
SuggestGeoTargetConstantsRequest.newBuilder();
73+
SuggestGeoTargetConstantsRequest.Builder requestBuilder =
74+
SuggestGeoTargetConstantsRequest.newBuilder();
6975

70-
// Locale is using ISO 639-1 format. If an invalid locale is given, 'en' is used by default.
71-
requestBuilder.setLocale(StringValue.of("en"));
76+
// Locale is using ISO 639-1 format. If an invalid locale is given, 'en' is used by default.
77+
requestBuilder.setLocale(StringValue.of("en"));
7278

73-
// A list of country codes can be referenced here:
74-
// https://developers.google.com/adwords/api/docs/appendix/geotargeting
75-
requestBuilder.setCountryCode(StringValue.of("FR"));
79+
// A list of country codes can be referenced here:
80+
// https://developers.google.com/adwords/api/docs/appendix/geotargeting
81+
requestBuilder.setCountryCode(StringValue.of("FR"));
7682

77-
requestBuilder
78-
.getLocationNamesBuilder()
79-
.addAllNames(
80-
Stream.of("Paris", "Quebec", "Spain", "Deutschland")
81-
.map(StringValue::of)
82-
.collect(Collectors.toList()));
83+
requestBuilder
84+
.getLocationNamesBuilder()
85+
.addAllNames(
86+
Stream.of("Paris", "Quebec", "Spain", "Deutschland")
87+
.map(StringValue::of)
88+
.collect(Collectors.toList()));
8389

84-
SuggestGeoTargetConstantsResponse response =
85-
geoTargetClient.suggestGeoTargetConstants(requestBuilder.build());
90+
SuggestGeoTargetConstantsResponse response =
91+
geoTargetClient.suggestGeoTargetConstants(requestBuilder.build());
8692

87-
for (GeoTargetConstantSuggestion suggestion : response.getGeoTargetConstantSuggestionsList()) {
88-
System.out.printf(
89-
"%s (%s,%s,%s,%s) is found in locale (%s) with reach (%d) for search term (%s).%n",
90-
suggestion.getGeoTargetConstant().getResourceName(),
91-
suggestion.getGeoTargetConstant().getName().getValue(),
92-
suggestion.getGeoTargetConstant().getCountryCode().getValue(),
93-
suggestion.getGeoTargetConstant().getTargetType().getValue(),
94-
suggestion.getGeoTargetConstant().getStatus().name(),
95-
suggestion.getLocale().getValue(),
96-
suggestion.getReach().getValue(),
97-
suggestion.getSearchTerm().getValue());
93+
for (GeoTargetConstantSuggestion suggestion :
94+
response.getGeoTargetConstantSuggestionsList()) {
95+
System.out.printf(
96+
"%s (%s,%s,%s,%s) is found in locale (%s) with reach (%d) for search term (%s).%n",
97+
suggestion.getGeoTargetConstant().getResourceName(),
98+
suggestion.getGeoTargetConstant().getName().getValue(),
99+
suggestion.getGeoTargetConstant().getCountryCode().getValue(),
100+
suggestion.getGeoTargetConstant().getTargetType().getValue(),
101+
suggestion.getGeoTargetConstant().getStatus().name(),
102+
suggestion.getLocale().getValue(),
103+
suggestion.getReach().getValue(),
104+
suggestion.getSearchTerm().getValue());
105+
}
98106
}
99107
}
100108
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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+
}

google-ads-examples/src/main/java/com/google/ads/googleads/examples/utils/ArgumentNames.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ public final class ArgumentNames {
6363
"--marketingImageAssetResourceName";
6464
public static final String SQUARE_MARKETING_IMAGE_ASSET_RESOURCE_NAME =
6565
"--squareMarketingImageAssetResourceName";
66+
public static final String LANGUAGE_NAME = "--languageName";
67+
public static final String COUNTRY_CODE = "--countryCode";
6668
}

0 commit comments

Comments
 (0)