/// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    /// <param name="adGroupId">Id of the ad group that contains the keyword.
    /// </param>
    /// <param name="keywordId">Id of the keyword to be updated.</param>
    public void Run(AdWordsUser user, long adGroupId, long keywordId) {
      // Get the AdGroupCriterionService.
      AdGroupCriterionService adGroupCriterionService =
          (AdGroupCriterionService) user.GetService(AdWordsService.v201509.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 e) {
        throw new System.ApplicationException("Failed to update keyword.", e);
      }
    }
Beispiel #2
0
        /// <summary>
        /// Creates the criterion for product partition.
        /// </summary>
        /// <param name="partitionId">The product partition ID.</param>
        /// <param name="parentPartitionId">The proudct partition ID for parent node.</param>
        /// <param name="caseValue">The case value.</param>
        /// <param name="isUnit">True, if the node is UNIT node, false otherwise.</param>
        /// <param name="isExcluded">True, if the node is EXCLUDE node, false otherwise.</param>
        /// <param name="bid">The bid to be set on a node, if it is UNIT.</param>
        /// <returns>An ad group criterion node for the product partition.</returns>
        internal static AdGroupCriterion CreateCriterionForProductPartition(long partitionId,
                                                                            long parentPartitionId, ProductDimension caseValue, bool isUnit, bool isExcluded,
                                                                            long bid)
        {
            AdGroupCriterion adGroupCriterion;
            ProductPartition partition = new ProductPartition()
            {
                id = partitionId,
                parentCriterionId = parentPartitionId,
                caseValue         = caseValue,
                partitionType     = isUnit ? ProductPartitionType.UNIT : ProductPartitionType.SUBDIVISION
            };

            if (isExcluded)
            {
                NegativeAdGroupCriterion negative = new NegativeAdGroupCriterion();
                adGroupCriterion = negative;
            }
            else
            {
                BiddableAdGroupCriterion biddable = new BiddableAdGroupCriterion();
                biddable.userStatus = UserStatus.ENABLED;

                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
                if (isUnit && bid != 0)
                {
                    CpcBid cpcBid = new CpcBid()
                    {
                        bid = new Money()
                        {
                            microAmount = bid
                        },
                        cpcBidSource = BidSource.CRITERION
                    };
                    biddingConfig.bids = new Bids[] { cpcBid };
                }
                biddable.biddingStrategyConfiguration = biddingConfig;
                adGroupCriterion = biddable;
            }
            adGroupCriterion.criterion = partition;
            return(adGroupCriterion);
        }
            /// <summary>
            /// Creates the unit.
            /// </summary>
            /// <param name="parent">The node that should be this node's parent.
            /// </param>
            /// <param name="value">The value being paritioned on.</param>
            /// <param name="bidAmount">The amount to bid for matching products,
            /// in micros.</param>
            /// <param name="isNegative">True, if this is negative criterion, false
            /// otherwise.</param>
            /// <returns>A new unit node.</returns>
            public ProductPartition CreateUnit(ProductPartition parent, ProductDimension value,
                                               long bidAmount, bool isNegative)
            {
                ProductPartition unit = new ProductPartition();

                unit.partitionType = ProductPartitionType.UNIT;

                // The root node has neither a parent nor a value.
                if (parent != null)
                {
                    unit.parentCriterionId = parent.id;
                    unit.caseValue         = value;
                }

                AdGroupCriterion criterion;

                if (isNegative)
                {
                    criterion = new NegativeAdGroupCriterion();
                }
                else
                {
                    BiddingStrategyConfiguration biddingStrategyConfiguration =
                        new BiddingStrategyConfiguration();

                    CpcBid cpcBid = new CpcBid();
                    cpcBid.bid                        = new Money();
                    cpcBid.bid.microAmount            = bidAmount;
                    biddingStrategyConfiguration.bids = new Bids[] { cpcBid };

                    criterion = new BiddableAdGroupCriterion();
                    (criterion as BiddableAdGroupCriterion).biddingStrategyConfiguration =
                        biddingStrategyConfiguration;
                }

                criterion.adGroupId = this.adGroupId;
                criterion.criterion = unit;

                this.CreateAddOperation(criterion);

                return(unit);
            }
Beispiel #4
0
        /// <summary>
        /// Creates a test ad group for running further tests.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">The campaign id for which the adgroup is created.</param>
        /// <param name="adGroupType">The ad group type.</param>
        /// <param name="isCpmBid">True, if a ManualCPM bid is to be used.</param>
        /// <returns>The ad group ID.</returns>
        public long CreateAdGroup(AdWordsUser user, long campaignId, AdGroupType adGroupType,
                                  bool isCpmBid)
        {
            AdGroupService adGroupService =
                (AdGroupService)user.GetService(AdWordsService.v201806.AdGroupService);

            AdGroupOperation adGroupOperation = new AdGroupOperation();

            adGroupOperation.@operator          = Operator.ADD;
            adGroupOperation.operand            = new AdGroup();
            adGroupOperation.operand.campaignId = campaignId;
            adGroupOperation.operand.name       =
                string.Format("AdGroup {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
            adGroupOperation.operand.status = AdGroupStatus.ENABLED;

            if (adGroupType != AdGroupType.UNKNOWN)
            {
                adGroupOperation.operand.adGroupType = adGroupType;
            }

            if (isCpmBid)
            {
                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
                CpmBid cpmBid = new CpmBid();
                cpmBid.bid             = new Money();
                cpmBid.bid.microAmount = 10000000;
                biddingConfig.bids     = new Bids[] { cpmBid };
                adGroupOperation.operand.biddingStrategyConfiguration = biddingConfig;
            }
            else
            {
                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
                CpcBid cpcBid = new CpcBid();
                cpcBid.bid             = new Money();
                cpcBid.bid.microAmount = 10000000;
                biddingConfig.bids     = new Bids[] { cpcBid };
                adGroupOperation.operand.biddingStrategyConfiguration = biddingConfig;
            }
            AdGroupReturnValue retVal = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });

            return(retVal.value[0].id);
        }
        /// <summary>
        /// Gets the criterion-level bid, or null if no such bid exists.
        /// </summary>
        /// <param name="biddableCriterion">The biddable criterion.</param>
        /// <returns>The criterion-level bid, or null if no such bid exists.
        /// </returns>
        private static Money GetBid(BiddableAdGroupCriterion biddableCriterion)
        {
            BiddingStrategyConfiguration biddingConfig = biddableCriterion.biddingStrategyConfiguration;
            Money cpcBidAmount = null;

            if (biddingConfig != null && biddingConfig.bids != null)
            {
                foreach (Bids bid in biddingConfig.bids)
                {
                    if (bid is CpcBid)
                    {
                        CpcBid cpcBid = (CpcBid)bid;
                        if (cpcBid.cpcBidSource == BidSource.CRITERION)
                        {
                            cpcBidAmount = cpcBid.bid;
                            break;
                        }
                    }
                }
            }
            return(cpcBidAmount);
        }
Beispiel #6
0
        /// <summary>
        /// Constructs a the bidding configuration object corresponding to the node.
        /// </summary>
        /// <returns>The bidding configuration node.</returns>
        /// <remarks>A <code>null</code> may be returned if a bid is not allowed
        /// on this node, or if the bid is not specified.</remarks>
        internal BiddingStrategyConfiguration GetBiddingConfig()
        {
            BiddingStrategyConfiguration biddingConfig = null;

            if (this.CpcBidSpecified && this.IsUnit)
            {
                biddingConfig = new BiddingStrategyConfiguration();

                Money bidMoney = new Money()
                {
                    microAmount = this.CpcBid
                };

                CpcBid cpcBid = new CpcBid()
                {
                    bid          = bidMoney,
                    cpcBidSource = BidSource.CRITERION
                };

                biddingConfig.bids = new Bids[] { cpcBid };
            }
            return(biddingConfig);
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign to which ad groups are
        /// added.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the AdGroupService.
              AdGroupService adGroupService =
              (AdGroupService) user.GetService(AdWordsService.v201509.AdGroupService);

              List<AdGroupOperation> operations = new List<AdGroupOperation>();

              for (int i = 0; i < NUM_ITEMS; i++) {
            // Create the ad group.
            AdGroup adGroup = new AdGroup();
            adGroup.name = string.Format("Earth to Mars Cruises #{0}",
            ExampleUtilities.GetRandomString());
            adGroup.status = AdGroupStatus.ENABLED;
            adGroup.campaignId = campaignId;

            // Set the ad group bids.
            BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();

            CpcBid cpcBid = new CpcBid();
            cpcBid.bid = new Money();
            cpcBid.bid.microAmount = 10000000;

            biddingConfig.bids = new Bids[] {cpcBid};

            adGroup.biddingStrategyConfiguration = biddingConfig;

            // Optional: Set targeting restrictions.
            // Depending on the criterionTypeGroup value, most TargetingSettingDetail
            // only affect Display campaigns. However, the USER_INTEREST_AND_LIST value
            // works for RLSA campaigns - Search campaigns targeting using a
            // remarketing list.
            TargetingSetting targetingSetting = new TargetingSetting();

            // Restricting to serve ads that match your ad group placements.
            // This is equivalent to choosing "Target and bid" in the UI.
            TargetingSettingDetail placementDetail = new TargetingSettingDetail();
            placementDetail.criterionTypeGroup = CriterionTypeGroup.PLACEMENT;
            placementDetail.targetAll = false;

            // Using your ad group verticals only for bidding. This is equivalent
            // to choosing "Bid only" in the UI.
            TargetingSettingDetail verticalDetail = new TargetingSettingDetail();
            verticalDetail.criterionTypeGroup = CriterionTypeGroup.VERTICAL;
            verticalDetail.targetAll = true;

            targetingSetting.details = new TargetingSettingDetail[] {placementDetail, verticalDetail};

            adGroup.settings = new Setting[] {targetingSetting};

            // Create the operation.
            AdGroupOperation operation = new AdGroupOperation();
            operation.@operator = Operator.ADD;
            operation.operand = adGroup;

            operations.Add(operation);
              }

              try {
            // Create the ad group.
            AdGroupReturnValue retVal = adGroupService.mutate(operations.ToArray());

            // Display the results.
            if (retVal != null && retVal.value != null && retVal.value.Length > 0) {
              foreach (AdGroup newAdGroup in retVal.value) {
            Console.WriteLine("Ad group with id = '{0}' and name = '{1}' was created.",
                newAdGroup.id, newAdGroup.name);
              }
            } else {
              Console.WriteLine("No ad groups were created.");
            }
              } catch (Exception e) {
            throw new System.ApplicationException("Failed to create ad groups.", e);
              }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign to which ad groups are
        /// added.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            using (AdGroupService adGroupService =
                       (AdGroupService)user.GetService(AdWordsService.v201710.AdGroupService)) {
                List <AdGroupOperation> operations = new List <AdGroupOperation>();

                for (int i = 0; i < NUM_ITEMS; i++)
                {
                    // Create the ad group.
                    AdGroup adGroup = new AdGroup();
                    adGroup.name = string.Format("Earth to Mars Cruises #{0}",
                                                 ExampleUtilities.GetRandomString());
                    adGroup.status     = AdGroupStatus.ENABLED;
                    adGroup.campaignId = campaignId;

                    // Set the ad group bids.
                    BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();

                    CpcBid cpcBid = new CpcBid();
                    cpcBid.bid             = new Money();
                    cpcBid.bid.microAmount = 10000000;

                    biddingConfig.bids = new Bids[] { cpcBid };

                    adGroup.biddingStrategyConfiguration = biddingConfig;

                    // Optional: Set targeting restrictions.
                    // Depending on the criterionTypeGroup value, most TargetingSettingDetail
                    // only affect Display campaigns. However, the USER_INTEREST_AND_LIST value
                    // works for RLSA campaigns - Search campaigns targeting using a
                    // remarketing list.
                    TargetingSetting targetingSetting = new TargetingSetting();

                    // Restricting to serve ads that match your ad group placements.
                    // This is equivalent to choosing "Target and bid" in the UI.
                    TargetingSettingDetail placementDetail = new TargetingSettingDetail();
                    placementDetail.criterionTypeGroup = CriterionTypeGroup.PLACEMENT;
                    placementDetail.targetAll          = false;

                    // Using your ad group verticals only for bidding. This is equivalent
                    // to choosing "Bid only" in the UI.
                    TargetingSettingDetail verticalDetail = new TargetingSettingDetail();
                    verticalDetail.criterionTypeGroup = CriterionTypeGroup.VERTICAL;
                    verticalDetail.targetAll          = true;

                    targetingSetting.details = new TargetingSettingDetail[] {
                        placementDetail, verticalDetail
                    };

                    adGroup.settings = new Setting[] { targetingSetting };

                    // Set the rotation mode.
                    AdGroupAdRotationMode rotationMode = new AdGroupAdRotationMode();
                    rotationMode.adRotationMode   = AdRotationMode.OPTIMIZE;
                    adGroup.adGroupAdRotationMode = rotationMode;

                    // Create the operation.
                    AdGroupOperation operation = new AdGroupOperation();
                    operation.@operator = Operator.ADD;
                    operation.operand   = adGroup;

                    operations.Add(operation);
                }

                try {
                    // Create the ad group.
                    AdGroupReturnValue retVal = adGroupService.mutate(operations.ToArray());

                    // Display the results.
                    if (retVal != null && retVal.value != null && retVal.value.Length > 0)
                    {
                        foreach (AdGroup newAdGroup in retVal.value)
                        {
                            Console.WriteLine("Ad group with id = '{0}' and name = '{1}' was created.",
                                              newAdGroup.id, newAdGroup.name);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No ad groups were created.");
                    }
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to create ad groups.", e);
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group to be updated.</param>
        /// <param name="bidMicroAmount">The CPC bid amount in micros.</param>
        public void Run(AdWordsUser user, long adGroupId, long?bidMicroAmount)
        {
            using (AdGroupService adGroupService =
                       (AdGroupService)user.GetService(AdWordsService.v201710.AdGroupService)) {
                // Create an ad group with the specified ID.
                AdGroup adGroup = new AdGroup();
                adGroup.id = adGroupId;

                // Pause the ad group.
                adGroup.status = AdGroupStatus.PAUSED;

                // Update the CPC bid if specified.
                if (bidMicroAmount != null)
                {
                    BiddingStrategyConfiguration biddingStrategyConfiguration =
                        new BiddingStrategyConfiguration();
                    Money cpcBidMoney = new Money();
                    cpcBidMoney.microAmount = bidMicroAmount.Value;
                    CpcBid cpcBid = new CpcBid();
                    cpcBid.bid = cpcBidMoney;
                    biddingStrategyConfiguration.bids    = new Bids[] { cpcBid };
                    adGroup.biddingStrategyConfiguration = biddingStrategyConfiguration;
                }

                // Create the operation.
                AdGroupOperation operation = new AdGroupOperation();
                operation.@operator = Operator.SET;
                operation.operand   = adGroup;

                try {
                    // Update the ad group.
                    AdGroupReturnValue retVal = adGroupService.mutate(new AdGroupOperation[] { operation });

                    // Display the results.
                    if (retVal != null && retVal.value != null && retVal.value.Length > 0)
                    {
                        AdGroup adGroupResult = retVal.value[0];
                        BiddingStrategyConfiguration bsConfig = adGroupResult.biddingStrategyConfiguration;

                        // Find the CpcBid in the bidding strategy configuration's bids collection.
                        long cpcBidMicros = 0L;
                        if (bsConfig != null && bsConfig.bids != null)
                        {
                            foreach (Bids bid in bsConfig.bids)
                            {
                                if (bid is CpcBid)
                                {
                                    cpcBidMicros = ((CpcBid)bid).bid.microAmount;
                                    break;
                                }
                            }
                        }
                        Console.WriteLine("Ad group with ID {0} and name '{1}' updated to have status '{2}'" +
                                          " and CPC bid {3}", adGroupResult.id, adGroupResult.name,
                                          adGroupResult.status, cpcBidMicros);
                    }
                    else
                    {
                        Console.WriteLine("No ad groups were updated.");
                    }
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to update ad group.", e);
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Creates an ad group.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">The campaign ID.</param>
        /// <returns>the newly created ad group.</returns>
        private static AdGroup CreateAdGroup(AdWordsUser user, long campaignId)
        {
            using (AdGroupService adGroupService =
                       (AdGroupService)user.GetService(AdWordsService.v201802.AdGroupService))
            {
                // Create the ad group.
                AdGroup adGroup = new AdGroup
                {
                    // Required: Set the ad group's type to Dynamic Search Ads.
                    adGroupType = AdGroupType.SEARCH_DYNAMIC_ADS,

                    name = string.Format("Earth to Mars Cruises #{0}",
                                         ExampleUtilities.GetRandomString()),
                    campaignId = campaignId,
                    status     = AdGroupStatus.PAUSED,

                    // Recommended: Set a tracking URL template for your ad group if you want to use
                    // URL tracking software.
                    trackingUrlTemplate = "http://tracker.example.com/traveltracker/{escapedlpurl}"
                };

                // Set the ad group bids.
                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();

                CpcBid cpcBid = new CpcBid
                {
                    bid = new Money
                    {
                        microAmount = 3000000
                    }
                };

                biddingConfig.bids = new Bids[]
                {
                    cpcBid
                };

                adGroup.biddingStrategyConfiguration = biddingConfig;

                // Create the operation.
                AdGroupOperation operation = new AdGroupOperation
                {
                    @operator = Operator.ADD,
                    operand   = adGroup
                };

                try
                {
                    // Create the ad group.
                    AdGroupReturnValue retVal = adGroupService.mutate(new AdGroupOperation[]
                    {
                        operation
                    });

                    // Display the results.
                    AdGroup newAdGroup = retVal.value[0];
                    Console.WriteLine("Ad group with id = '{0}' and name = '{1}' was created.",
                                      newAdGroup.id, newAdGroup.name);
                    return(newAdGroup);
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to create ad group.", e);
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// Adds a web page criterion to target Dynamic Search Ads.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group ID.</param>
        /// <returns>The newly created web page criterion.</returns>
        private static BiddableAdGroupCriterion AddWebPageCriteria(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(AdWordsService.v201802
                                                                .AdGroupCriterionService))
            {
                // Create a webpage criterion for special offers for mars cruise.
                WebpageParameter param = new WebpageParameter
                {
                    criterionName = "Special offers for mars"
                };

                WebpageCondition urlCondition = new WebpageCondition
                {
                    operand  = WebpageConditionOperand.URL,
                    argument = "/marscruise/special"
                };

                WebpageCondition titleCondition = new WebpageCondition
                {
                    operand  = WebpageConditionOperand.PAGE_TITLE,
                    argument = "Special Offer"
                };

                param.conditions = new WebpageCondition[]
                {
                    urlCondition,
                    titleCondition
                };

                Webpage webpage = new Webpage
                {
                    parameter = param
                };

                // Create biddable ad group criterion.
                BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion
                {
                    adGroupId  = adGroupId,
                    criterion  = webpage,
                    userStatus = UserStatus.PAUSED
                };

                // Optional: set a custom bid.
                BiddingStrategyConfiguration biddingStrategyConfiguration =
                    new BiddingStrategyConfiguration();
                CpcBid bid = new CpcBid()
                {
                    bid = new Money()
                    {
                        microAmount = 10000000L
                    }
                };
                biddingStrategyConfiguration.bids = new Bids[]
                {
                    bid
                };
                biddableAdGroupCriterion.biddingStrategyConfiguration =
                    biddingStrategyConfiguration;

                // Create the operation.
                AdGroupCriterionOperation operation = new AdGroupCriterionOperation
                {
                    @operator = Operator.ADD,
                    operand   = biddableAdGroupCriterion
                };

                try
                {
                    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(
                        new AdGroupCriterionOperation[]
                    {
                        operation
                    });

                    BiddableAdGroupCriterion newCriterion =
                        (BiddableAdGroupCriterion)result.value[0];
                    Console.WriteLine(
                        "Webpage criterion with ID = '{0}' was added to ad group ID '{1}'.",
                        newCriterion.criterion.id, newCriterion.adGroupId);
                    return(newCriterion);
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to create webpage criterion.", e);
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">Id of the ad group that contains the keyword.
        /// </param>
        /// <param name="keywordId">Id of the keyword to be updated.</param>
        public void Run(AdWordsUser user, long adGroupId, long keywordId)
        {
            // Get the AdGroupCriterionService.
            AdGroupCriterionService adGroupCriterionService =
                (AdGroupCriterionService)user.GetService(AdWordsService.v201502.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);
            }
        }
Beispiel #13
0
        public static AdGroupReturnValue CreateAdGroup(AdWordsUser user, AdGroupLo adGroupLo)
        {
            using (AdGroupService adGroupService =
                       (AdGroupService)user.GetService(AdWordsService.v201710.AdGroupService))
            {
                List <AdGroupOperation> operations = new List <AdGroupOperation>();

                // Create the ad group.
                AdGroup adGroup = new AdGroup();
                adGroup.name       = adGroupLo.Name;
                adGroup.status     = AdGroupStatus.PAUSED;
                adGroup.campaignId = adGroupLo.CampaignId;

                // Set the ad group bids.
                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();

                CpcBid cpcBid = new CpcBid();
                cpcBid.bid             = new Money();
                cpcBid.bid.microAmount = 10000000;

                biddingConfig.bids = new Bids[] { cpcBid };

                adGroup.biddingStrategyConfiguration = biddingConfig;

                // Optional: Set targeting restrictions.
                // Depending on the criterionTypeGroup value, most TargetingSettingDetail
                // only affect Display campaigns. However, the USER_INTEREST_AND_LIST value
                // works for RLSA campaigns - Search campaigns targeting using a
                // remarketing list.
                TargetingSetting targetingSetting = new TargetingSetting();

                // Restricting to serve ads that match your ad group placements.
                // This is equivalent to choosing "Target and bid" in the UI.
                TargetingSettingDetail placementDetail = new TargetingSettingDetail();
                placementDetail.criterionTypeGroup = CriterionTypeGroup.PLACEMENT;
                placementDetail.targetAll          = false;

                // Using your ad group verticals only for bidding. This is equivalent
                // to choosing "Bid only" in the UI.
                TargetingSettingDetail verticalDetail = new TargetingSettingDetail();
                verticalDetail.criterionTypeGroup = CriterionTypeGroup.VERTICAL;
                verticalDetail.targetAll          = true;

                targetingSetting.details = new TargetingSettingDetail[] {
                    placementDetail, verticalDetail
                };

                adGroup.settings = new Setting[] { targetingSetting };

                // Set the rotation mode.
                AdGroupAdRotationMode rotationMode = new AdGroupAdRotationMode();
                rotationMode.adRotationMode   = AdRotationMode.OPTIMIZE;
                adGroup.adGroupAdRotationMode = rotationMode;

                // Create the operation.
                AdGroupOperation operation = new AdGroupOperation();
                operation.@operator = Operator.ADD;
                operation.operand   = adGroup;

                operations.Add(operation);


                AdGroupReturnValue returnDGroup;
                try
                {
                    // Create the ad group.
                    returnDGroup = adGroupService.mutate(operations.ToArray());
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to create ad group.", e);
                }
                return(returnDGroup);
            }
        }
        /// <summary>
        /// Creates the criterion for product partition.
        /// </summary>
        /// <param name="partitionId">The product partition ID.</param>
        /// <param name="parentPartitionId">The proudct partition ID for parent node.</param>
        /// <param name="caseValue">The case value.</param>
        /// <param name="isUnit">True, if the node is UNIT node, false otherwise.</param>
        /// <param name="isExcluded">True, if the node is EXCLUDE node, false otherwise.</param>
        /// <param name="bid">The bid to be set on a node, if it is UNIT.</param>
        /// <returns>An ad group criterion node for the product partition.</returns>
        internal static AdGroupCriterion CreateCriterionForProductPartition(long partitionId,
        long parentPartitionId, ProductDimension caseValue, bool isUnit, bool isExcluded,
        long bid)
        {
            AdGroupCriterion adGroupCriterion;
              ProductPartition partition = new ProductPartition() {
            id = partitionId,
            parentCriterionId = parentPartitionId,
            caseValue = caseValue,
            partitionType = isUnit ? ProductPartitionType.UNIT : ProductPartitionType.SUBDIVISION
              };

              if (isExcluded) {
            NegativeAdGroupCriterion negative = new NegativeAdGroupCriterion();
            adGroupCriterion = negative;
              } else {
            BiddableAdGroupCriterion biddable = new BiddableAdGroupCriterion();
            biddable.userStatus = UserStatus.ENABLED;

            BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
            if (isUnit && bid != 0) {
              CpcBid cpcBid = new CpcBid() {
            bid = new Money() {
              microAmount = bid
            },
            cpcBidSource = BidSource.CRITERION
              };
              biddingConfig.bids = new Bids[] { cpcBid };
            }
            biddable.biddingStrategyConfiguration = biddingConfig;
            adGroupCriterion = biddable;
              }
              adGroupCriterion.criterion = partition;
              return adGroupCriterion;
        }
        /// <summary>
        /// Creates a test adgroup for running further tests.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">The campaign id for which the adgroup is created.</param>
        /// <param name="isCpmBid">True, if a ManualCPM bid is to be used.</param>
        /// <returns>The adgroup id.</returns>
        public long CreateAdGroup(AdWordsUser user, long campaignId, bool isCpmBid)
        {
            AdGroupService adGroupService =
              (AdGroupService) user.GetService(AdWordsService.v201601.AdGroupService);

              AdGroupOperation adGroupOperation = new AdGroupOperation();
              adGroupOperation.@operator = Operator.ADD;
              adGroupOperation.operand = new AdGroup();
              adGroupOperation.operand.campaignId = campaignId;
              adGroupOperation.operand.name =
              string.Format("AdGroup {0}", DateTime.Now.ToString("yyyy-M-d H:m:s.ffffff"));
              adGroupOperation.operand.status = AdGroupStatus.ENABLED;

              if (isCpmBid) {
            BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
            CpmBid cpmBid = new CpmBid();
            cpmBid.bid = new Money();
            cpmBid.bid.microAmount = 10000000;
            biddingConfig.bids = new Bids[] { cpmBid };
            adGroupOperation.operand.biddingStrategyConfiguration = biddingConfig;
              } else {
            BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
            CpcBid cpcBid = new CpcBid();
            cpcBid.bid = new Money();
            cpcBid.bid.microAmount = 10000000;
            biddingConfig.bids = new Bids[] { cpcBid };
            adGroupOperation.operand.biddingStrategyConfiguration = biddingConfig;
              }
              AdGroupReturnValue retVal = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });
              return retVal.value[0].id;
        }
            /// <summary>
            /// Creates the unit.
            /// </summary>
            /// <param name="parent">The node that should be this node's parent.
            /// </param>
            /// <param name="value">The value being paritioned on.</param>
            /// <param name="bidAmount">The amount to bid for matching products,
            /// in micros.</param>
            /// <param name="isNegative">True, if this is negative criterion, false
            /// otherwise.</param>
            /// <returns>A new unit node.</returns>
            public ProductPartition CreateUnit(ProductPartition parent, ProductDimension value,
          long bidAmount, bool isNegative)
            {
                ProductPartition unit = new ProductPartition();
                unit.partitionType = ProductPartitionType.UNIT;

                // The root node has neither a parent nor a value.
                if (parent != null) {
                  unit.parentCriterionId = parent.id;
                  unit.caseValue = value;
                }

                AdGroupCriterion criterion;

                if (isNegative) {
                  criterion = new NegativeAdGroupCriterion();
                } else {
                  BiddingStrategyConfiguration biddingStrategyConfiguration =
                  new BiddingStrategyConfiguration();

                  CpcBid cpcBid = new CpcBid();
                  cpcBid.bid = new Money();
                  cpcBid.bid.microAmount = bidAmount;
                  biddingStrategyConfiguration.bids = new Bids[] { cpcBid };

                  criterion = new BiddableAdGroupCriterion();
                  (criterion as BiddableAdGroupCriterion).biddingStrategyConfiguration =
                  biddingStrategyConfiguration;
                }

                criterion.adGroupId = this.adGroupId;
                criterion.criterion = unit;

                this.CreateAddOperation(criterion);

                return unit;
            }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="campaignId">Id of the campaign to which ad groups are
        /// added.</param>
        public void Run(AdWordsUser user, long campaignId)
        {
            // Get the AdGroupService.
            AdGroupService adGroupService =
                (AdGroupService)user.GetService(AdWordsService.v201409.AdGroupService);

            List <AdGroupOperation> operations = new List <AdGroupOperation>();

            for (int i = 0; i < NUM_ITEMS; i++)
            {
                // Create the ad group.
                AdGroup adGroup = new AdGroup();
                adGroup.name = string.Format("Earth to Mars Cruises #{0}",
                                             ExampleUtilities.GetRandomString());
                adGroup.status     = AdGroupStatus.ENABLED;
                adGroup.campaignId = campaignId;

                // Set the ad group bids.
                BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();

                CpcBid cpcBid = new CpcBid();
                cpcBid.bid             = new Money();
                cpcBid.bid.microAmount = 10000000;

                biddingConfig.bids = new Bids[] { cpcBid };

                adGroup.biddingStrategyConfiguration = biddingConfig;

                // Optional: Set targeting restrictions.
                // These settings only affect serving for the Display Network.
                TargetingSetting targetingSetting = new TargetingSetting();

                TargetingSettingDetail placementDetail = new TargetingSettingDetail();
                placementDetail.criterionTypeGroup = CriterionTypeGroup.PLACEMENT;
                placementDetail.targetAll          = true;

                TargetingSettingDetail verticalDetail = new TargetingSettingDetail();
                verticalDetail.criterionTypeGroup = CriterionTypeGroup.VERTICAL;
                verticalDetail.targetAll          = false;

                targetingSetting.details = new TargetingSettingDetail[] { placementDetail, verticalDetail };

                adGroup.settings = new Setting[] { targetingSetting };

                // Create the operation.
                AdGroupOperation operation = new AdGroupOperation();
                operation.@operator = Operator.ADD;
                operation.operand   = adGroup;

                operations.Add(operation);
            }

            try {
                // Create the ad group.
                AdGroupReturnValue retVal = adGroupService.mutate(operations.ToArray());

                // Display the results.
                if (retVal != null && retVal.value != null && retVal.value.Length > 0)
                {
                    foreach (AdGroup newAdGroup in retVal.value)
                    {
                        Console.WriteLine("Ad group with id = '{0}' and name = '{1}' was created.",
                                          newAdGroup.id, newAdGroup.name);
                    }
                }
                else
                {
                    Console.WriteLine("No ad groups were created.");
                }
            } catch (Exception ex) {
                throw new System.ApplicationException("Failed to create ad groups.", ex);
            }
        }
    /// <summary>
    /// Constructs a the bidding configuration object corresponding to the node.
    /// </summary>
    /// <returns>The bidding configuration node.</returns>
    /// <remarks>A <code>null</code> may be returned if a bid is not allowed
    /// on this node, or if the bid is not specified.</remarks>
    internal BiddingStrategyConfiguration GetBiddingConfig() {
      BiddingStrategyConfiguration biddingConfig = null;
      if (this.CpcBidSpecified && this.IsUnit) {
        biddingConfig = new BiddingStrategyConfiguration();

        Money bidMoney = new Money() {
          microAmount = this.CpcBid
        };

        CpcBid cpcBid = new CpcBid() {
          bid = bidMoney,
          cpcBidSource = BidSource.CRITERION
        };

        biddingConfig.bids = new Bids[] { cpcBid };
      }
      return biddingConfig;
    }