use of com.google.api.ads.adwords.axis.v201809.o.LanguageSearchParameter in project googleads-java-lib by googleads.
the class GetKeywordIdeas method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the optional ID of the seed ad group.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, @Nullable Long adGroupId) throws RemoteException {
// Get the TargetingIdeaService.
TargetingIdeaServiceInterface targetingIdeaService = adWordsServices.get(session, TargetingIdeaServiceInterface.class);
// Create selector.
TargetingIdeaSelector selector = new TargetingIdeaSelector();
selector.setRequestType(RequestType.IDEAS);
selector.setIdeaType(IdeaType.KEYWORD);
selector.setRequestedAttributeTypes(new AttributeType[] { AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.AVERAGE_CPC, AttributeType.COMPETITION, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES });
// Set selector paging (required for targeting idea service).
Paging paging = new Paging();
paging.setStartIndex(0);
paging.setNumberResults(10);
selector.setPaging(paging);
List<SearchParameter> searchParameters = new ArrayList<>();
// Create related to query search parameter.
RelatedToQuerySearchParameter relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
relatedToQuerySearchParameter.setQueries(new String[] { "bakery", "pastries", "birthday cake" });
searchParameters.add(relatedToQuerySearchParameter);
// Language setting (optional).
// The ID can be found in the documentation:
// https://developers.google.com/adwords/api/docs/appendix/languagecodes
// See the documentation for limits on the number of allowed language parameters:
// https://developers.google.com/adwords/api/docs/reference/latest/TargetingIdeaService.LanguageSearchParameter
LanguageSearchParameter languageParameter = new LanguageSearchParameter();
Language english = new Language();
english.setId(1000L);
languageParameter.setLanguages(new Language[] { english });
searchParameters.add(languageParameter);
// Create network search parameter (optional).
NetworkSetting networkSetting = new NetworkSetting();
networkSetting.setTargetGoogleSearch(true);
networkSetting.setTargetSearchNetwork(false);
networkSetting.setTargetContentNetwork(false);
networkSetting.setTargetPartnerSearchNetwork(false);
NetworkSearchParameter networkSearchParameter = new NetworkSearchParameter();
networkSearchParameter.setNetworkSetting(networkSetting);
searchParameters.add(networkSearchParameter);
// Optional: Use an existing ad group to generate ideas.
if (adGroupId != null) {
SeedAdGroupIdSearchParameter seedAdGroupIdSearchParameter = new SeedAdGroupIdSearchParameter();
seedAdGroupIdSearchParameter.setAdGroupId(adGroupId);
searchParameters.add(seedAdGroupIdSearchParameter);
}
selector.setSearchParameters(searchParameters.toArray(new SearchParameter[searchParameters.size()]));
// Get keyword ideas.
TargetingIdeaPage page = targetingIdeaService.get(selector);
// Display keyword ideas.
for (TargetingIdea targetingIdea : page.getEntries()) {
Map<AttributeType, Attribute> data = Maps.toMap(targetingIdea.getData());
StringAttribute keyword = (StringAttribute) data.get(AttributeType.KEYWORD_TEXT);
IntegerSetAttribute categories = (IntegerSetAttribute) data.get(AttributeType.CATEGORY_PRODUCTS_AND_SERVICES);
String categoriesString = "(none)";
if (categories != null && categories.getValue() != null) {
categoriesString = Joiner.on(", ").join(Ints.asList(categories.getValue()));
}
Long averageMonthlySearches = ((LongAttribute) data.get(AttributeType.SEARCH_VOLUME)).getValue();
Money averageCpc = ((MoneyAttribute) data.get(AttributeType.AVERAGE_CPC)).getValue();
Double competition = ((DoubleAttribute) data.get(AttributeType.COMPETITION)).getValue();
System.out.printf("Keyword with text '%s', average monthly search volume %d, " + "average CPC %d, and competition %.2f " + "was found with categories: %s%n", keyword.getValue(), averageMonthlySearches, averageCpc.getMicroAmount(), competition, categoriesString);
}
if (page.getEntries() == null) {
System.out.println("No related keywords were found.");
}
}
Aggregations