public class AdWords2009 {
public static void main(String args[]) throws IOException,
ServiceException {
Keyword kw1 = new Keyword();
kw1.setText("silver shoes");
kw1.setMatchType(KeywordMatchType.EXACT);
Keyword kw2 = new Keyword();
kw2.setText("black shoes");
kw2.setMatchType(KeywordMatchType.EXACT);
Keyword kw3 = new Keyword();
kw3.setText("Leather rain boots");
kw3.setMatchType(KeywordMatchType.EXACT);
System.out.println("===one keyword at a time with STATS===");
printStats(new Keyword[]{kw1}, RequestType.STATS);
printStats(new Keyword[]{kw2}, RequestType.STATS);
printStats(new Keyword[]{kw3}, RequestType.STATS);
System.out.println("===ALL keywords at a time with STATS===");
printStats(new Keyword[]{kw1, kw2, kw3}, RequestType.STATS);
}
public static void printStats(Keyword kw[], RequestType rt) throws
RemoteException, ServiceException {
Map<String, String> credentials = new HashMap<String, String>();
credentials.put("email", SEMConstants.ADWORDS_EMAIL);
credentials.put("password", SEMConstants.ADWORDS_PASSWD);
credentials.put("useragent", SEMConstants.ADWORDS_USER_AGENT);
credentials.put("developerToken",
SEMConstants.ADWORDS_DEVELOPER_TOKEN);
credentials.put("applicationToken",
SEMConstants.ADWORDS_APPLICATION_TOKEN);
credentials.put("version", "v200909");
credentials.put("alternateUrl", "https://adwords.google.com");
credentials.put("clientId", "...");
AdWordsUser adWordsUser = new AdWordsUser(credentials);
adWordsUser.setAuthToken("...");
TargetingIdeaServiceInterface tisi = (TargetingIdeaServiceInterface)
adWordsUser.getService(AdWordsService.V200909.TARGETING_IDEA_SERVICE);
TargetingIdeaSelector tis = new TargetingIdeaSelector();
tis.setIdeaType(IdeaType.KEYWORD);
tis.setRequestType(rt);
RelatedToKeywordSearchParameter sp1 = new
RelatedToKeywordSearchParameter();
KeywordMatchTypeSearchParameter sp2 = new
KeywordMatchTypeSearchParameter(null, new KeywordMatchType[]
{KeywordMatchType.EXACT});
AttributeType at[] = new AttributeType[]
{AttributeType.TARGETED_MONTHLY_SEARCHES};
CountryTargetSearchParameter sp3 = new
CountryTargetSearchParameter();
CountryTarget ct = new CountryTarget();
ct.setCountryCode("US");
sp3.setCountryTargets(new CountryTarget[]{ct});
tis.setRequestedAttributeTypes(at);
tis.setLocaleCode("en_US");
tis.setCurrencyCode("USD");
tis.setPaging(new Paging(0, 100));
sp1.setKeywords(kw);
tis.setSearchParameters(new SearchParameter[]{sp1, sp2, sp3});
int index = 0;
TargetingIdeaPage result = tisi.get(tis);
for (TargetingIdea tip : result.getEntries()) {
Type_AttributeMapEntry map[] = tip.getData();
for (Type_AttributeMapEntry ta : map) {
MonthlySearchVolumeAttribute msva = (MonthlySearchVolumeAttribute)
ta.getValue();
MonthlySearchVolume msv[] = msva.getValue();
Long inorder[] = new Long[12];
for (MonthlySearchVolume m : msv) {
if (m.getMonth() != null && m.getCount() != null) {
inorder[m.getMonth() - 1] = m.getCount();
}
}
System.out.println(kw[index++].getText() + "," + inorder[0] + ","
+ inorder[1] + "," + inorder[2] + "," + inorder[3] + "," + inorder[4]
+ ","
+ inorder[5] + "," + inorder[6] + "," + inorder[7] + "," +
inorder[8] + "," + inorder[9] + "," + inorder[10] + "," +
inorder[11]);
}
}
}
}
In my tests the keyword stats were returned in the same order that
they were passed in (using one RelatedToKeywordSearchParameter with
many keywords). Also, if you request the attribute KEYWORD you can
get back the keyword value again with your results.
Best,
- Eric Koleda, AdWords API Team