// Get AdWordsUser from "~/adwords.properties".
AdWordsUser user = new AdWordsUser();
// Get v13 traffic estimator service.
TrafficEstimatorInterface trafficEstimatorService =
user.getService(AdWordsService.V13.TRAFFIC_ESTIMATOR_SERVICE);
// Get v200909 AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService =
user.getService(AdWordsService.V200909.ADGROUP_CRITERION_SERVICE);
Long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
// Create selector to get all criteria under ad group.
AdGroupCriterionSelector selector = new AdGroupCriterionSelector();
selector.setIdFilters(new AdGroupCriterionIdFilter[] {
new AdGroupCriterionIdFilter(null, adGroupId, null)});
Map<Keyword, KeywordTraffic> keywordTrafficMap = new HashMap<Keyword, KeywordTraffic>();
// Get all keywords for ad group.
AdGroupCriterionPage page = adGroupCriterionService.get(selector);
if (page.getEntries() != null) {
for (AdGroupCriterion adGroupCriterion : page.getEntries()) {
if (adGroupCriterion.getCriterion() instanceof Keyword) {
keywordTrafficMap.put((Keyword) adGroupCriterion.getCriterion(), null);
}
}
List<KeywordTrafficRequest> keywordTrafficRequests = new ArrayList<KeywordTrafficRequest>();
List<Keyword> keywords = new ArrayList<Keyword>(keywordTrafficMap.keySet());
// Make a traffic estimation request for each keyword.
for (Keyword keyword : keywords) {
keywordTrafficRequests.add(new KeywordTrafficRequest(keyword.getText(),
convertMatchType(keyword.getMatchType()), null));
}
// Get the traffic estimation of all the keywords.
KeywordTraffic[] keywordTraffic =
trafficEstimatorService.checkKeywordTraffic(
keywordTrafficRequests.toArray(new KeywordTrafficRequest[] {}));
// Put result back into map where order is preserved.
for (int i = 0; i < keywords.size(); i++) {
keywordTrafficMap.put(keywords.get(i), keywordTraffic[i]);