views:

31

answers:

0

I'm attempting to use the Adwords API to lookup the daily clicks and est. CPC for a keyword, however using the example code in the v201003 code, I can never seem to get a bid landscape to return anything. Here's my code... any ideas?

PS: I have no problem getting targeting ideas, which is really annoying to be able to get values for everything EXCEPT the keyword I care about.

    public class EstimateAdwordsCPC {

        /**
         * @param args
         */
        public static void main(String[] args) 
        {

            try {
                // Log SOAP XML request and response.
                AdWordsServiceLogger.log();

                AdWordsUser user = new AdWordsUser();

                // Get the BidLandscapeService.
                BidLandscapeServiceInterface bidLandscapeService =
                    user.getService(AdWordsService.V201003.BID_LANDSCAPE_SERVICE);

                long adGroupId = getAdGroupId(user);
                long criterionId = getCriterionId(user, adGroupId);

                // Create selector.
                CriterionBidLandscapeSelector selector = new CriterionBidLandscapeSelector();

                // Create id filter.
                BidLandscapeIdFilter idFilter = new BidLandscapeIdFilter();
                idFilter.setCriterionId(criterionId);
                idFilter.setAdGroupId(adGroupId);
                selector.setIdFilters(new BidLandscapeIdFilter[] {idFilter});

                // Get bid landscape for ad group criteria.
                BidLandscape[] bidLandsacpes = bidLandscapeService.getBidLandscape(selector);

                // Display bid landscapes.
                if (bidLandsacpes != null) {
                  for (BidLandscape bidLandscape : bidLandsacpes) {
                    if (bidLandscape instanceof CriterionBidLandscape) {
                      CriterionBidLandscape criterionBidLandscape = (CriterionBidLandscape) bidLandscape;
                      System.out.println("Criterion bid landscape with ad group id \""
                          + criterionBidLandscape.getAdGroupId() + "\", criterion id \""
                          + criterionBidLandscape.getCriterionId() + "\", start date \""
                          + criterionBidLandscape.getStartDate() + "\", end date \""
                          + criterionBidLandscape.getEndDate() + "\", with landscape points: ");

                      for (BidLandscapeLandscapePoint bidLanscapePoint
                          : criterionBidLandscape.getLandscapePoints()) {
                        System.out.println("\t{bid: " + bidLanscapePoint.getBid().getMicroAmount()
                            + " clicks: " + bidLanscapePoint.getClicks() + " cost: "
                            + bidLanscapePoint.getCost().getMicroAmount() + " marginalCpc: "
                            + bidLanscapePoint.getMarginalCpc().getMicroAmount() + " impressions: "
                            + bidLanscapePoint.getImpressions() + "}");
                      }
                      System.out.println(" was found.");
                    }
                  }
                } else {
                  System.out.println("No bid landscapes were found.");
                }

                /**
                 * Get targeting ideas...
                 */
                  // Get the TargetingIdeaService.
                  TargetingIdeaServiceInterface targetingIdeaService =
                      user.getService(AdWordsService.V201003.TARGETING_IDEA_SERVICE);

                  // Create seed keyword.
                  Keyword keyword = new Keyword();
                  keyword.setText("estes park hotels");
                  keyword.setMatchType(KeywordMatchType.EXACT);

                  // Create selector.
                  TargetingIdeaSelector selectorA = new TargetingIdeaSelector();
                  selectorA.setRequestType(RequestType.IDEAS);
                  selectorA.setIdeaType(IdeaType.KEYWORD);
                  selectorA.setRequestedAttributeTypes(new AttributeType[] {AttributeType.KEYWORD,
                      AttributeType.AVERAGE_TARGETED_MONTHLY_SEARCHES});

                  // Set selector paging (required for targeting idea serivce).
                  Paging paging = new Paging();
                  paging.setStartIndex(0);
                  paging.setNumberResults(10);
                  selectorA.setPaging(paging);

                  // Create related to keyword search parameter.
                  RelatedToKeywordSearchParameter relatedToKeywordSearchParameter =
                      new RelatedToKeywordSearchParameter();
                  relatedToKeywordSearchParameter.setKeywords(new Keyword[] {keyword});

                  // Create keyword match type search parameter to ensure unique results.
                  KeywordMatchTypeSearchParameter keywordMatchTypeSearchParameter =
                      new KeywordMatchTypeSearchParameter();
                  keywordMatchTypeSearchParameter.setKeywordMatchTypes(
                      new KeywordMatchType[] {KeywordMatchType.BROAD});

                  selectorA.setSearchParameters(
                      new SearchParameter[] {relatedToKeywordSearchParameter, keywordMatchTypeSearchParameter});

                  // Get related keywords.
                  TargetingIdeaPage page = targetingIdeaService.get(selectorA);

                  // Display related keywords.
                  if (page.getEntries() != null && page.getEntries().length > 0) {
                    for (TargetingIdea targetingIdea : page.getEntries()) {
                      Map<AttributeType, Attribute> data = MapUtils.toMap(targetingIdea.getData());
                      keyword = ((KeywordAttribute) data.get(AttributeType.KEYWORD)).getValue();
                      Long averageMonthlySearches =
                          ((LongAttribute) data.get(AttributeType.AVERAGE_TARGETED_MONTHLY_SEARCHES)).getValue();
                      System.out.println("Keyword with text '" + keyword.getText() + "', match type '"
                          + keyword.getMatchType() + "', and average monthly search volume '"
                          + averageMonthlySearches + "' was found.");
                    }
                  } else {
                    System.out.println("No related keywords were found.");
                  }


            } catch (Exception e) {
                e.printStackTrace();
              }
        }

        public static long getAdGroupId(AdWordsUser user) throws ServiceException, ApiException, RemoteException
        {
              // Get the AdGroupService.
              AdGroupServiceInterface adGroupService =
                  user.getService(AdWordsService.V201003.ADGROUP_SERVICE);

              long campaignId = getCampaignId(user);

              // Create ad group.
              AdGroup adGroup = new AdGroup();
              adGroup.setName("RMNP Test Ad Group #" + System.currentTimeMillis());
              adGroup.setStatus(AdGroupStatus.ENABLED);
              adGroup.setCampaignId(campaignId);

              // Create ad group bid.
              ManualCPCAdGroupBids adGroupBids = new ManualCPCAdGroupBids();
              adGroupBids.setKeywordMaxCpc(new Bid(new Money(null, 10000000L)));
              adGroup.setBids(adGroupBids);

              // Create operations.
              AdGroupOperation operation = new AdGroupOperation();
              operation.setOperand(adGroup);
              operation.setOperator(Operator.ADD);

              AdGroupOperation[] operations = new AdGroupOperation[]{operation};

              // Add ad group.
              AdGroupReturnValue result = adGroupService.mutate(operations);
              AdGroup[] groups = result.getValue();
              if (groups.length>0)
                  return groups[0].getId();
              else
                  return -1;

        }

        public static long getCriterionId(AdWordsUser user, long adGroupId) throws ServiceException, ApiException, RemoteException
        {
              // Get the AdGroupCriterionService.
              AdGroupCriterionServiceInterface adGroupCriterionService =
                  user.getService(AdWordsService.V201003.ADGROUP_CRITERION_SERVICE);

              // Create keyword.
              Keyword keyword = new Keyword();
              keyword.setText("estes park hotels");
              keyword.setMatchType(KeywordMatchType.EXACT);

              // Create biddable ad group criterion.
              BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
              keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
              keywordBiddableAdGroupCriterion.setCriterion(keyword);

              // Create placement.
              Placement placement = new Placement();
              placement.setUrl("http://rockymountainnationalpark.com/pages/hiking.html");

              // Create biddable ad group criterion for placement.
              BiddableAdGroupCriterion placementBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
              placementBiddableAdGroupCriterion.setAdGroupId(adGroupId);
              placementBiddableAdGroupCriterion.setCriterion(placement);

              // Create operations.
              AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
              keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
              keywordAdGroupCriterionOperation.setOperator(Operator.ADD);

              AdGroupCriterionOperation placementAdGroupCriterionOperation =
                  new AdGroupCriterionOperation();
              placementAdGroupCriterionOperation.setOperand(placementBiddableAdGroupCriterion);
              placementAdGroupCriterionOperation.setOperator(Operator.ADD);

              AdGroupCriterionOperation[] operations =
                  new AdGroupCriterionOperation[] {keywordAdGroupCriterionOperation,
                      placementAdGroupCriterionOperation};

              // Add ad group criteria.
              AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
              AdGroupCriterion[] crits = result.getValue();

              return crits[0].getCriterion().getId();

        }

        public static long getCampaignId(AdWordsUser user) throws ServiceException, ApiException, RemoteException
        {
              // Get the CampaignService.
              CampaignServiceInterface campaignService =
                  user.getService(AdWordsService.V201003.CAMPAIGN_SERVICE);

              // Create campaign.
              Campaign campaign = new Campaign();
              campaign.setName("RMNP Test Campaign #" + System.currentTimeMillis());
              campaign.setStatus(CampaignStatus.PAUSED);
              campaign.setBiddingStrategy(new ManualCPC());

              // Create budget.
              Budget budget = new Budget();
              budget.setPeriod(BudgetBudgetPeriod.DAILY);
              budget.setAmount(new Money(null, 500000000L));
              budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.ACCELERATED);
              campaign.setBudget(budget);

              // Create operations.
              CampaignOperation operation = new CampaignOperation();
              operation.setOperand(campaign);
              operation.setOperator(Operator.ADD);

              CampaignOperation[] operations = new CampaignOperation[] {operation};

              // Add campaign.
              CampaignReturnValue result = campaignService.mutate(operations);
              Campaign[] createdCampaigns = result.getValue();
              if (createdCampaigns.length>0)
              return createdCampaigns[0].getId();
              else
                  return -1;
        }

    }