I'm looking through the .net examples and the GetAdgroupCriterion example, 
while helpful, is pretty limited.
It only gets the text and status (not bid, qualityScore, etc.)
http://code.google.com/p/google-api-adwords-dotnet/source/browse/trunk/examples/adwords/csharp/v201109/BasicOperations/GetKeywords.cs

I'm getting errors because if I run this on the whole account (trying to 
get a full account download), the various kinds of criterion and bids, etc. 
all need to be addressed.
Any help pointing to a reference implementation much appreciated.
(Maybe more thorough examples in other libraries?)

For now, I'm chasing down very generic system exceptions thrown by the 
simple try/catch wrapper in the example.
For example, qualityScore comes down as null for deleted keywords (and 
that's true even if they are status active but in a deleted adgroup or 
campaign).  So we need to deal with that possibly being null.
etc.

Any comments or assistance much appreciated.

    private void GetKeywords(AdWordsUser user, string account, string 
reportDate, StreamWriter file)
    {
      int offset = 0;
      int pageSize = 500;
 
      // Service
      AdGroupCriterionService adGroupCriterionService = 
(AdGroupCriterionService)user.GetService(AdWordsService.v201109.AdGroupCriterionService);
 
      // Create a selector.
      Selector selector = new Selector();
      selector.fields = new string[] { "Id", "AdGroupId", "KeywordText", 
"CriteriaType", "CriterionUse", "QualityScore", "MaxCpc" };
 
      // Select only keywords.
      Predicate CriteriaTypePredicate = new Predicate();
      CriteriaTypePredicate.field = "CriteriaType";
      CriteriaTypePredicate.@operator = PredicateOperator.EQUALS;
      CriteriaTypePredicate.values = new string[] { "KEYWORD" };
 
      //Predicate IsNegativePredicate = new Predicate();
      //IsNegativePredicate.field = "IsNegative";
      //IsNegativePredicate.@operator = PredicateOperator.EQUALS;
      //IsNegativePredicate.values = new string[] { "FALSE" };
 
      // e.g.: not negatives ;-)
      Predicate CriterionUsePredicate = new Predicate();
      CriterionUsePredicate.field = "CriterionUse";
      CriterionUsePredicate.@operator = PredicateOperator.EQUALS;
      CriterionUsePredicate.values = new string[] { "BIDDABLE" };
 
      selector.predicates = new Predicate[] { CriteriaTypePredicate, 
CriterionUsePredicate };
 
      // Set the selector paging.
      selector.paging = new Paging();
 
      AdGroupCriterionPage page = new AdGroupCriterionPage();
 
      try
      {
        do
        {
          selector.paging.startIndex = offset;
          selector.paging.numberResults = pageSize;
 
          // Get the keywords.
          try
          {
            page = adGroupCriterionService.get(selector);
          }
          catch (Exception ex)
          {
            throw new System.ApplicationException("Failed to get keywords.", 
ex);
          }
 
          // Display the results.
          // Write results to file
          // Write Header row
          file.WriteLine(String.Join("\t", new string[] {
                  "ReportDate"
                  , "Adgroup_ID"
                  , "Keyword_ID"
                  , "MatchType"
                  , "Status"
                  , "ApprovalStatus"
                  , "Text"
                  , "QualityScore"
                  , "Bid"
              }));
          if (page != null && page.entries != null)
          {
            int i = offset;
            foreach (AdGroupCriterion adGroupCriterion in page.entries)
            {
              if (adGroupCriterion is BiddableAdGroupCriterion)
              {
                BiddableAdGroupCriterion biddableAdGroupCriterion = 
(BiddableAdGroupCriterion)adGroupCriterion;
 
                if (biddableAdGroupCriterion.criterion is Keyword)
                {
                  long bidMicroAmount = ((adGroupCriterion as 
BiddableAdGroupCriterion).bids as 
ManualCPCAdGroupCriterionBids).maxCpc.amount.microAmount;
                  string bid = (bidMicroAmount / _MICROS).ToString();
                  string qualityScore = (biddableAdGroupCriterion.qualityInfo 
== null) ? "0" : biddableAdGroupCriterion.qualityInfo.qualityScore.ToString();
 
                  Keyword keyword = (Keyword)biddableAdGroupCriterion.criterion;
                  file.WriteLine(String.Join("\t", new string[] {
                    reportDate
                    , biddableAdGroupCriterion.adGroupId.ToString()
                    , keyword.id.ToString()
                    , keyword.matchType.ToString()
                    , biddableAdGroupCriterion.userStatus.ToString()
                    , biddableAdGroupCriterion.approvalStatus.ToString()
                    , keyword.text.ToString()
                    , qualityScore
                    , bid
                  }));
                }
              }
              i++;
            }
          }
          offset += pageSize;
        } while (offset < page.totalNumEntries && offset < _MAXENTITIES);
        Console.WriteLine("--GetKeywords(): found: {0}, wrote: {1}", 
page.totalNumEntries, offset);
      }
      catch (Exception ex)
      {
        throw new System.ApplicationException("Failed to retrieve keywords.", 
ex);
      }
      file.Flush();
      file.Dispose();
      file.Close();
    }


-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
http://adwordsapi.blogspot.com
http://groups.google.com/group/adwords-api
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en

Reply via email to