Google.Api.Ads.AdWords.Examples.CSharp.v201306.UpdateKeyword.Run C# (CSharp) Method

Run() public method

Runs the code example.
public Run ( Google.Api.Ads.AdWords.Lib.AdWordsUser user, long adGroupId, long keywordId ) : void
user Google.Api.Ads.AdWords.Lib.AdWordsUser The AdWords user.
adGroupId long Id of the ad group that contains the keyword. ///
keywordId long Id of the keyword to be updated.
return void
        public void Run(AdWordsUser user, long adGroupId, long keywordId)
        {
            // Get the AdGroupCriterionService.
              AdGroupCriterionService adGroupCriterionService =
              (AdGroupCriterionService) user.GetService(AdWordsService.v201306.AdGroupCriterionService);

              // Since we are not updating any keyword-specific fields, it is enough to
              // create a criterion object.
              Criterion criterion = new Criterion();
              criterion.id = keywordId;

              // Create ad group criterion.
              BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
              biddableAdGroupCriterion.adGroupId = adGroupId;
              biddableAdGroupCriterion.criterion = criterion;

              // Create the bids.
              BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
              CpcBid cpcBid = new CpcBid();
              cpcBid.bid = new Money();
              cpcBid.bid.microAmount = 1000000;
              biddingConfig.bids = new Bids[] {cpcBid};

              biddableAdGroupCriterion.biddingStrategyConfiguration = biddingConfig;

              // Create the operation.
              AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
              operation.@operator = Operator.SET;
              operation.operand = biddableAdGroupCriterion;

              try {
            // Update the keyword.
            AdGroupCriterionReturnValue retVal =
            adGroupCriterionService.mutate(new AdGroupCriterionOperation[] {operation});

            // Display the results.
            if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
              AdGroupCriterion adGroupCriterion = retVal.value[0];
              long bidAmount = 0;
              foreach (Bids bids in (adGroupCriterion as BiddableAdGroupCriterion).
              biddingStrategyConfiguration.bids) {
            if (bids is CpcBid) {
              bidAmount = (bids as CpcBid).bid.microAmount;
              break;
            }
              }

              Console.WriteLine("Keyword with ad group id = '{0}', id = '{1}' was updated with " +
              "bid amount = '{2}' micros.", adGroupCriterion.adGroupId,
              adGroupCriterion.criterion.id, bidAmount);
            } else {
              Console.WriteLine("No keyword was updated.");
            }
              } catch (Exception ex) {
            throw new System.ApplicationException("Failed to update keyword.", ex);
              }
        }

Usage Example

 /// <summary>
 /// Main method, to run this code example as a standalone application.
 /// </summary>
 /// <param name="args">The command line arguments.</param>
 public static void Main(string[] args)
 {
     UpdateKeyword codeExample = new UpdateKeyword();
       Console.WriteLine(codeExample.Description);
       try {
     long adGroupId = long.Parse("INSERT_ADGROUP_ID_HERE");
     long keywordId = long.Parse("INSERT_KEYWORD_ID_HERE");
     codeExample.Run(new AdWordsUser(), adGroupId, keywordId);
       } catch (Exception ex) {
     Console.WriteLine("An exception occurred while running this code example. {0}",
     ExampleUtilities.FormatException(ex));
       }
 }
UpdateKeyword