/// <summary> /// Creates a new <see cref="AdGroupCriterion"/> configured for an /// <code>ADD</code> operation. /// </summary> /// <param name="node">The node whose criterion should be added.</param> /// <param name="adGroupId">The ad group ID of the criterion.</param> /// <param name="idGenerator">The temporary ID generator for new nodes.</param> /// <returns>An <see cref="AdGroupCriterion"/> object for <code>ADD</code> /// operation.</returns> internal static AdGroupCriterion CreateCriterionForAdd(ProductPartitionNode node, long adGroupId, TemporaryIdGenerator idGenerator) { PreconditionUtilities.CheckNotNull(node, ShoppingMessages.NodeCannotBeNull); AdGroupCriterion adGroupCriterion; if (node.IsExcludedUnit) { adGroupCriterion = new NegativeAdGroupCriterion(); } else { adGroupCriterion = new BiddableAdGroupCriterion() { biddingStrategyConfiguration = node.GetBiddingConfig() }; } adGroupCriterion.adGroupId = adGroupId; adGroupCriterion.criterion = node.GetCriterion(); adGroupCriterion.criterion.id = node.ProductPartitionId; if (node.Parent != null) { (adGroupCriterion.criterion as ProductPartition).parentCriterionId = node.Parent.ProductPartitionId; } return adGroupCriterion; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService( AdWordsService.v201509.AdGroupCriterionService); List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>(); foreach (string keywordText in KEYWORDS) { // Create the keyword. Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; // Create the biddable ad group criterion. BiddableAdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion(); keywordCriterion.adGroupId = adGroupId; keywordCriterion.criterion = keyword; // Optional: Set the user status. keywordCriterion.userStatus = UserStatus.PAUSED; // Optional: Set the keyword destination url. keywordCriterion.finalUrls = new UrlList() { urls = new string[] { "http://example.com/mars/cruise/?kw=" + HttpUtility.UrlEncode(keywordText) } }; // Create the operations. AdGroupCriterionOperation operation = new AdGroupCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = keywordCriterion; operations.Add(operation); } try { // Create the keywords. AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (retVal != null && retVal.value != null) { foreach (AdGroupCriterion adGroupCriterion in retVal.value) { // If you are adding multiple type of criteria, then you may need to // check for // // if (adGroupCriterion is Keyword) { ... } // // to identify the criterion type. Console.WriteLine("Keyword with ad group id = '{0}', keyword id = '{1}', text = " + "'{2}' and match type = '{3}' was created.", adGroupCriterion.adGroupId, adGroupCriterion.criterion.id, (adGroupCriterion.criterion as Keyword).text, (adGroupCriterion.criterion as Keyword).matchType); } } else { Console.WriteLine("No keywords were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create keywords.", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which criteria are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201509.AdGroupCriterionService); // Create biddable ad group criterion for gender Gender genderTarget = new Gender(); // Criterion Id for male. The IDs can be found here // https://developers.google.com/adwords/api/docs/appendix/genders genderTarget.id = 10; BiddableAdGroupCriterion genderBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); genderBiddableAdGroupCriterion.adGroupId = adGroupId; genderBiddableAdGroupCriterion.criterion = genderTarget; // Create negative ad group criterion for age range AgeRange ageRangeNegative = new AgeRange(); // Criterion Id for age 18 to 24. The IDs can be found here // https://developers.google.com/adwords/api/docs/appendix/ages ageRangeNegative.id = 503001; NegativeAdGroupCriterion ageRangeNegativeAdGroupCriterion = new NegativeAdGroupCriterion(); ageRangeNegativeAdGroupCriterion.adGroupId = adGroupId; ageRangeNegativeAdGroupCriterion.criterion = ageRangeNegative; // Create operations. AdGroupCriterionOperation genderBiddableAdGroupCriterionOperation = new AdGroupCriterionOperation(); genderBiddableAdGroupCriterionOperation.operand = genderBiddableAdGroupCriterion; genderBiddableAdGroupCriterionOperation.@operator = Operator.ADD; AdGroupCriterionOperation ageRangeNegativeAdGroupCriterionOperation = new AdGroupCriterionOperation(); ageRangeNegativeAdGroupCriterionOperation.operand = ageRangeNegativeAdGroupCriterion; ageRangeNegativeAdGroupCriterionOperation.@operator = Operator.ADD; AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { genderBiddableAdGroupCriterionOperation, ageRangeNegativeAdGroupCriterionOperation}; try { // Add ad group criteria. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations); // Display ad group criteria. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { Console.WriteLine("Ad group criterion with ad group id \"{0}\", criterion id " + "\"{1}\", and type \"{2}\" was added.", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, adGroupCriterionResult.criterion.CriterionType); } } else { Console.WriteLine("No ad group criteria were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create ad group criteria.", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which placements are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201509.AdGroupCriterionService); // Create the placement. Placement placement1 = new Placement(); placement1.url = "http://mars.google.com"; // Create biddable ad group criterion. AdGroupCriterion placementCriterion1 = new BiddableAdGroupCriterion(); placementCriterion1.adGroupId = adGroupId; placementCriterion1.criterion = placement1; // Create the placement. Placement placement2 = new Placement(); placement2.url = "http://venus.google.com"; // Create biddable ad group criterion. AdGroupCriterion placementCriterion2 = new BiddableAdGroupCriterion(); placementCriterion2.adGroupId = adGroupId; placementCriterion2.criterion = placement2; // Create the operations. AdGroupCriterionOperation placementOperation1 = new AdGroupCriterionOperation(); placementOperation1.@operator = Operator.ADD; placementOperation1.operand = placementCriterion1; AdGroupCriterionOperation placementOperation2 = new AdGroupCriterionOperation(); placementOperation2.@operator = Operator.ADD; placementOperation2.operand = placementCriterion2; try { // Create the placements. AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] {placementOperation1, placementOperation2}); // Display the results. if (retVal != null && retVal.value != null) { foreach (AdGroupCriterion adGroupCriterion in retVal.value) { // If you are adding multiple type of criteria, then you may need to // check for // // if (adGroupCriterion is Placement) { ... } // // to identify the criterion type. Console.WriteLine("Placement with ad group id = '{0}, placement id = '{1}, url = " + "'{2}' was created.", adGroupCriterion.adGroupId, adGroupCriterion.criterion.id, (adGroupCriterion.criterion as Placement).url); } } else { Console.WriteLine("No placements were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create placements.", e); } }
/// <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); } }
/// <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 removed.</param> public void Run(AdWordsUser user, long adGroupId, long keywordId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService( AdWordsService.v201509.AdGroupCriterionService); // Create base class criterion to avoid setting keyword-specific fields. Criterion criterion = new Criterion(); criterion.id = keywordId; // Create the ad group criterion. BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion(); adGroupCriterion.adGroupId = adGroupId; adGroupCriterion.criterion = criterion; // Create the operation. AdGroupCriterionOperation operation = new AdGroupCriterionOperation(); operation.operand = adGroupCriterion; operation.@operator = Operator.REMOVE; try { // Remove the keyword. AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] {operation}); // Display the results. if (retVal != null && retVal.value != null && retVal.value.Length > 0) { AdGroupCriterion removedKeyword = retVal.value[0]; Console.WriteLine("Keyword with ad group id = \"{0}\" and id = \"{1}\" was removed.", removedKeyword.adGroupId, removedKeyword.criterion.id); } else { Console.WriteLine("No keywords were removed."); } } catch (Exception e) { throw new System.ApplicationException("Failed to remove keyword.", e); } }
/// <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> /// 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); } } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201509.AdGroupCriterionService); // Set partial failure mode for the service. adGroupCriterionService.RequestHeader.partialFailure = true; List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>(); // Create the keywords. string[] keywords = new String[] {"mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise"}; foreach (String keywordText in keywords) { Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; // Create biddable ad group criterion. BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); keywordBiddableAdGroupCriterion.adGroupId = adGroupId; keywordBiddableAdGroupCriterion.criterion = keyword; // Create the operation. AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation(); keywordAdGroupCriterionOperation.operand = keywordBiddableAdGroupCriterion; keywordAdGroupCriterionOperation.@operator = Operator.ADD; operations.Add(keywordAdGroupCriterionOperation); } try { // Create the keywords. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { if (adGroupCriterionResult.criterion != null) { Console.WriteLine("Keyword with ad group id '{0}', criterion id '{1}', and " + "text '{2}' was added.\n", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, ((Keyword) adGroupCriterionResult.criterion).text); } } } else { Console.WriteLine("No keywords were added."); } // Display the partial failure errors. if (result != null && result.partialFailureErrors != null) { foreach (ApiError apiError in result.partialFailureErrors) { int operationIndex = ErrorUtilities.GetOperationIndex(apiError.fieldPath); if (operationIndex != -1) { AdGroupCriterion adGroupCriterion = operations[operationIndex].operand; Console.WriteLine("Keyword with ad group id '{0}' and text '{1}' " + "triggered a failure for the following reason: '{2}'.\n", adGroupCriterion.adGroupId, ((Keyword) adGroupCriterion.criterion).text, apiError.errorString); } else { Console.WriteLine("A failure for the following reason: '{0}' has occurred.\n", apiError.errorString); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to add keywords in partial failure mode.", e); } }
internal override void ProcessMappingsFromRowValues(RowValues values) { BiddableAdGroupCriterion = new BiddableAdGroupCriterion { CriterionBid = new FixedBid { Type = "FixedBid" }, Criterion = new Product { Type = "Product" }, Type = "BiddableAdGroupCriterion" }; values.ConvertToEntity(this, Mappings); }
/// <summary> /// Sets the Add action for a new AdGroupCriterion corresponding to the specified ProductCondition, /// and adds it to the helper's list of AdGroupCriterionAction. /// </summary> /// <param name="parent">The parent of the product partition unit that you want to add.</param> /// <param name="condition">The condition or product filter for the new product partition.</param> /// <param name="bidAmount">The bid amount for the new product partition.</param> /// <param name="isNegative">Indicates whether or not to add a NegativeAdGroupCriterion. /// The default value is false, in which case a BiddableAdGroupCriterion will be added.</param> /// <returns>The ad group criterion that was added to the list of PartitionActions.</returns> public AdGroupCriterion AddUnit( AdGroupCriterion parent, ProductCondition condition, double bidAmount, bool isNegative ) { AdGroupCriterion adGroupCriterion; if (isNegative) { adGroupCriterion = new NegativeAdGroupCriterion(); } else { adGroupCriterion = new BiddableAdGroupCriterion() { CriterionBid = new FixedBid() { Bid = new Bid() { Amount = bidAmount } } }; } adGroupCriterion.Criterion = new ProductPartition() { // If the root node is a unit, it would not have a parent ParentCriterionId = parent != null ? parent.Id : null, Condition = condition, PartitionType = ProductPartitionType.Unit }; adGroupCriterion.AdGroupId = this.adGroupId; var partitionAction = new AdGroupCriterionAction() { Action = ItemAction.Add, AdGroupCriterion = adGroupCriterion }; this.partitionActions.Add(partitionAction); return adGroupCriterion; }
public async override Task RunAsync(AuthorizationData authorizationData) { try { CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper(this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>(authorizationData); // Get a list of all Bing Merchant Center stores associated with your CustomerId IList <BMCStore> stores = (await CampaignManagementExampleHelper.GetBMCStoresByCustomerIdAsync())?.BMCStores; if (stores == null) { OutputStatusMessage( string.Format("You do not have any BMC stores registered for CustomerId {0}.\n", authorizationData.CustomerId) ); return; } #region ManageCampaign /* Add a new Bing Shopping campaign that will be associated with a ProductScope criterion. * - Set the CampaignType element of the Campaign to Shopping. * - Create a ShoppingSetting instance and set its Priority (0, 1, or 2), SalesCountryCode, and StoreId elements. * Add this shopping setting to the Settings list of the Campaign. */ var campaigns = new[] { new Campaign { CampaignType = CampaignType.Shopping, Settings = new[] { new ShoppingSetting() { Priority = 0, SalesCountryCode = "US", StoreId = (int)stores[0].Id } }, Name = "Bing Shopping Campaign " + DateTime.UtcNow, Description = "Bing Shopping Campaign Example.", // You must choose to set either the shared budget ID or daily amount. // You can set one or the other, but you may not set both. BudgetId = null, DailyBudget = 50, BudgetType = BudgetLimitType.DailyBudgetStandard, TimeZone = "PacificTimeUSCanadaTijuana", // Used with CustomParameters defined in lower level entities such as ad group criterion. TrackingUrlTemplate = "http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}" } }; AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync(authorizationData.AccountId, campaigns); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); long campaignId = (long)campaignIds[0]; /* Optionally, you can create a ProductScope criterion that will be associated with your Bing Shopping campaign. * Use the product scope criterion to include a subset of your product catalog, for example a specific brand, * category, or product type. A campaign can only be associated with one ProductScope, which contains a list * of up to 7 ProductCondition. You'll also be able to specify more specific product conditions for each ad group. */ var campaignCriterions = new BiddableCampaignCriterion[] { new BiddableCampaignCriterion() { CampaignId = campaignId, CriterionBid = null, // Not applicable for product scope Criterion = new ProductScope() { Conditions = new ProductCondition[] { new ProductCondition { Operand = "Condition", Attribute = "New" }, new ProductCondition { Operand = "CustomLabel0", Attribute = "MerchantDefinedCustomLabel" }, } }, } }; var addCampaignCriterionsResponse = await(CampaignManagementExampleHelper.AddCampaignCriterionsAsync( campaignCriterions, CampaignCriterionType.ProductScope) ); #endregion ManageCampaign #region ManageAdGroup // Create the ad group that will have the product partitions. var adGroups = new[] { new AdGroup { Name = "Product Categories", StartDate = null, EndDate = new Microsoft.BingAds.V12.CampaignManagement.Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, Language = "English", CpcBid = new Bid { Amount = 0.090 } } }; AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync((long)campaignId, adGroups, null); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); long adGroupId = (long)adGroupIds[0]; #region BidAllProducts var helper = new PartitionActionHelper(adGroupId); var root = helper.AddUnit( null, new ProductCondition { Operand = "All", Attribute = null }, 0.35, false ); OutputStatusMessage("Applying only the root as a Unit with a bid . . . \n"); var applyProductPartitionActionsResponse = await CampaignManagementExampleHelper.ApplyProductPartitionActionsAsync(helper.PartitionActions); var adGroupCriterions = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, adGroupId, AdGroupCriterionType.ProductPartition ); OutputStatusMessage("The ad group's product partition only has a tree root node: \n"); OutputProductPartitions(adGroupCriterions?.AdGroupCriterions); /* * Let's update the bid of the root Unit we just added. */ BiddableAdGroupCriterion updatedRoot = new BiddableAdGroupCriterion { Id = applyProductPartitionActionsResponse.AdGroupCriterionIds[0], CriterionBid = new FixedBid { Amount = 0.45 } }; helper = new PartitionActionHelper(adGroupId); helper.UpdatePartition(updatedRoot); OutputStatusMessage("Updating the bid for the tree root node . . . \n"); await CampaignManagementExampleHelper.ApplyProductPartitionActionsAsync(helper.PartitionActions); adGroupCriterions = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, adGroupId, AdGroupCriterionType.ProductPartition ); OutputStatusMessage("Updated the bid for the tree root node: \n"); OutputProductPartitions(adGroupCriterions?.AdGroupCriterions); #endregion BidAllProducts #region InitializeTree /* * Now we will overwrite any existing tree root, and build a product partition group tree structure in multiple steps. * You could build the entire tree in a single call since there are less than 5,000 nodes; however, * we will build it in steps to demonstrate how to use the results from ApplyProductPartitionActions to update the tree. * * For a list of validation rules, see the Product Ads technical guide: * https://docs.microsoft.com/en-us/bingads/guides/product-ads */ helper = new PartitionActionHelper(adGroupId); /* * Check whether a root node exists already. */ adGroupCriterions = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, adGroupId, AdGroupCriterionType.ProductPartition ); var existingRoot = GetRootNode(adGroupCriterions?.AdGroupCriterions); if (existingRoot != null) { helper.DeletePartition(existingRoot); } root = helper.AddSubdivision( null, new ProductCondition { Operand = "All", Attribute = null } ); /* * The direct children of any node must have the same Operand. * For this example we will use CategoryL1 nodes as children of the root. * For a list of valid CategoryL1 through CategoryL5 values, see the Bing Category Taxonomy: * http://go.microsoft.com/fwlink?LinkId=507666 */ var animalsSubdivision = helper.AddSubdivision( root, new ProductCondition { Operand = "CategoryL1", Attribute = "Animals & Pet Supplies" } ); /* * If you use a CategoryL2 node, it must be a descendant (child or later) of a CategoryL1 node. * In other words you cannot have a CategoryL2 node as parent of a CategoryL1 node. * For this example we will a CategoryL2 node as child of the CategoryL1 Animals & Pet Supplies node. */ var petSuppliesSubdivision = helper.AddSubdivision( animalsSubdivision, new ProductCondition { Operand = "CategoryL2", Attribute = "Pet Supplies" } ); var brandA = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand A" }, 0.35, false ); /* * If you won't bid on Brand B, set the helper method's bidAmount to '0' and isNegative to true. * The helper method will create a NegativeAdGroupCriterion and apply the condition. */ var brandB = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand B" }, 0, true ); var otherBrands = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = null }, 0.35, false ); var otherPetSupplies = helper.AddUnit( animalsSubdivision, new ProductCondition { Operand = "CategoryL2", Attribute = null }, 0.35, false ); var electronics = helper.AddUnit( root, new ProductCondition { Operand = "CategoryL1", Attribute = "Electronics" }, 0.35, false ); var otherCategoryL1 = helper.AddUnit( root, new ProductCondition { Operand = "CategoryL1", Attribute = null }, 0.35, false ); OutputStatusMessage("Applying product partitions to the ad group . . . \n"); applyProductPartitionActionsResponse = await CampaignManagementExampleHelper.ApplyProductPartitionActionsAsync(helper.PartitionActions); // To retrieve product partitions after they have been applied, call GetAdGroupCriterionsByIds. // The product partition with ParentCriterionId set to null is the root node. adGroupCriterions = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, adGroupId, AdGroupCriterionType.ProductPartition ); /* * The product partition group tree now has 9 nodes. * * All other (Root Node) | +-- Animals & Pet Supplies (CategoryL1) | | | +-- Pet Supplies (CategoryL2) | | | | | +-- Brand A | | | | | +-- Brand B | | | | | +-- All other (Brand) | | | +-- All other (CategoryL2) | +-- Electronics (CategoryL1) | +-- All other (CategoryL1) | */ OutputStatusMessage("The product partition group tree now has 9 nodes: \n"); OutputProductPartitions(adGroupCriterions?.AdGroupCriterions); #endregion InitializeTree #region UpdateTree /* * Let's replace the Electronics (CategoryL1) node created above with an Electronics (CategoryL1) node that * has children i.e. Brand C (Brand), Brand D (Brand), and All other (Brand) as follows: * * Electronics (CategoryL1) | +-- Brand C (Brand) | +-- Brand D (Brand) | +-- All other (Brand) | */ helper = new PartitionActionHelper(adGroupId); /* * To replace a node we must know its Id and its ParentCriterionId. In this case the parent of the node * we are replacing is All other (Root Node), and was created at Index 1 of the previous ApplyProductPartitionActions call. * The node that we are replacing is Electronics (CategoryL1), and was created at Index 8. */ var rootId = applyProductPartitionActionsResponse.AdGroupCriterionIds[1]; electronics.Id = applyProductPartitionActionsResponse.AdGroupCriterionIds[8]; helper.DeletePartition(electronics); var parent = new BiddableAdGroupCriterion() { Id = rootId }; var electronicsSubdivision = helper.AddSubdivision( parent, new ProductCondition { Operand = "CategoryL1", Attribute = "Electronics" } ); var brandC = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand C" }, 0.35, false ); var brandD = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand D" }, 0.35, false ); var otherElectronicsBrands = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = null }, 0.35, false ); OutputStatusMessage( "Updating the product partition group to refine Electronics (CategoryL1) with 3 child nodes . . . \n" ); applyProductPartitionActionsResponse = await CampaignManagementExampleHelper.ApplyProductPartitionActionsAsync(helper.PartitionActions); adGroupCriterions = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, adGroupId, AdGroupCriterionType.ProductPartition ); /* * The product partition group tree now has 12 nodes, including the children of Electronics (CategoryL1): * * All other (Root Node) | +-- Animals & Pet Supplies (CategoryL1) | | | +-- Pet Supplies (CategoryL2) | | | | | +-- Brand A | | | | | +-- Brand B | | | | | +-- All other (Brand) | | | +-- All other (CategoryL2) | +-- Electronics (CategoryL1) | | | +-- Brand C (Brand) | | | +-- Brand D (Brand) | | | +-- All other (Brand) | +-- All other (CategoryL1) | */ OutputStatusMessage( "The product partition group tree now has 12 nodes, including the children of Electronics (CategoryL1): \n" ); OutputProductPartitions(adGroupCriterions?.AdGroupCriterions); #endregion UpdateTree #endregion ManageAdGroup #region ManageAds /* * Create a product ad. You must add at least one product ad to the ad group. * The product ad identifier can be used for reporting analytics. * Use Merchant Promotions if you want tags to appear at the bottom of your product ad * as "special offer" links, helping to increase customer engagement. For details * on Merchant Promotions see https://help.bingads.microsoft.com/#apex/3/en/56805/0. */ var ads = new Ad[] { new ProductAd { } }; AddAdsResponse addAdsResponse = await CampaignManagementExampleHelper.AddAdsAsync((long)adGroupIds[0], ads); long?[] adIds = addAdsResponse.AdIds.ToArray(); BatchError[] adErrors = addAdsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adErrors); #endregion ManageAds #region CleanUp /* Delete the campaign, ad group, criterion, and ad that were previously added. * You should remove this region if you want to view the added entities in the * Bing Ads web application or another tool. */ await CampaignManagementExampleHelper.DeleteCampaignsAsync(authorizationData.AccountId, new[] { campaignId }); OutputStatusMessage(string.Format("Deleted Campaign Id {0}\n", campaignId)); #endregion CleanUp } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group that contains the placement. /// </param> /// <param name="placementId">Id of the placement to be updated.</param> public void Run(AdWordsUser user, long adGroupId, long placementId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201409.AdGroupCriterionService); // Since we are not updating any placement-specific fields, it is enough to // create a criterion object. Criterion criterion = new Criterion(); criterion.id = placementId; // Create ad group criterion. BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion(); biddableAdGroupCriterion.adGroupId = adGroupId; biddableAdGroupCriterion.criterion = criterion; // Create the bids. BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration(); CpmBid cpmBid = new CpmBid(); cpmBid.bid = new Money(); cpmBid.bid.microAmount = 1000000; biddingConfig.bids = new Bids[] { cpmBid }; biddableAdGroupCriterion.biddingStrategyConfiguration = biddingConfig; // Create the operation. AdGroupCriterionOperation operation = new AdGroupCriterionOperation(); operation.@operator = Operator.SET; operation.operand = biddableAdGroupCriterion; try { // Update the placement. 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 CpmBid) { bidAmount = (bids as CpmBid).bid.microAmount; break; } } Console.WriteLine("Placement with ad group id = '{0}', id = '{1}' was updated with " + "bid amount = '{2}' micros.", adGroupCriterion.adGroupId, adGroupCriterion.criterion.id, bidAmount); } else { Console.WriteLine("No placement was updated."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to update placement.", ex); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { ApiEnvironment environment = ((OAuthDesktopMobileAuthCodeGrant)authorizationData.Authentication).Environment; AdInsightExampleHelper AdInsightExampleHelper = new AdInsightExampleHelper( OutputStatusMessageDefault: this.OutputStatusMessage); AdInsightExampleHelper.AdInsightService = new ServiceClient <IAdInsightService>( authorizationData: authorizationData, environment: environment); CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper( OutputStatusMessageDefault: this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>( authorizationData: authorizationData, environment: environment); // To get started with dynamic search ads, first you'll need to add a new Campaign // with its type set to DynamicSearchAds. When you create the campaign, you'll need to // include a DynamicSearchAdsSetting that specifies the target website domain and language. var campaigns = new[] { new Campaign { BudgetType = Microsoft.BingAds.V12.CampaignManagement.BudgetLimitType.DailyBudgetStandard, DailyBudget = 50, CampaignType = CampaignType.DynamicSearchAds, Description = "Red shoes line.", Languages = new string[] { "All" }, Name = "Women's Shoes " + DateTime.UtcNow, TimeZone = "PacificTimeUSCanadaTijuana", Settings = new [] { new DynamicSearchAdsSetting { DomainName = "contoso.com", Language = "English" } }, }, }; OutputStatusMessage("-----\nAddCampaigns:"); AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync( accountId : authorizationData.AccountId, campaigns : campaigns, includeDynamicSearchAdsSource : true); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); OutputStatusMessage("CampaignIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); // Create a new ad group within the dynamic search ads campaign. var adGroups = new[] { new AdGroup { Name = "Women's Red Shoe Sale", StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, CpcBid = new Bid { Amount = 0.09 }, } }; OutputStatusMessage("-----\nAddAdGroups:"); AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync( campaignId : (long)campaignIds[0], adGroups : adGroups, returnInheritedBidStrategyTypes : false); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); OutputStatusMessage("AdGroupIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); // You can add one or more Webpage criteria to each ad group that helps determine // whether or not to serve dynamic search ads. var adGroupCriterions = new List <AdGroupCriterion>(); var adGroupWebpagePositivePageContent = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = "flowers", Operand = WebpageConditionOperand.PageContent, } }, CriterionName = "Ad Group Webpage Positive Page Content Criterion" }, }, }; adGroupCriterions.Add(adGroupWebpagePositivePageContent); // To discover the categories that you can use for Webpage criteria (positive or negative), // use the GetDomainCategories operation with the Ad Insight service. OutputStatusMessage("-----\nGetDomainCategories:"); var getDomainCategoriesResponse = await AdInsightExampleHelper.GetDomainCategoriesAsync( categoryName : null, domainName : DOMAIN_NAME, language : LANGUAGE); var categories = getDomainCategoriesResponse.Categories; OutputStatusMessage("Categories:"); AdInsightExampleHelper.OutputArrayOfDomainCategory(categories); // If any categories are available let's use one as a condition. if (categories.Count > 0) { var adGroupWebpagePositiveCategory = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = categories[0].CategoryName, Operand = WebpageConditionOperand.Category, } }, CriterionName = "Ad Group Webpage Positive Category Criterion" }, } }; adGroupCriterions.Add(adGroupWebpagePositiveCategory); } // If you want to exclude certain portions of your website, you can add negative Webpage // criterion at the campaign and ad group level. var adGroupWebpageNegativeUrl = new NegativeAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new Webpage { Parameter = new WebpageParameter { // You can choose whether you want the criterion argument to match partial URLs, // page content, page title, or categories that Bing thinks applies to your website. Conditions = new[] { new WebpageCondition { Argument = DOMAIN_NAME, Operand = WebpageConditionOperand.Url, } }, // If you do not specify any name, then it will be set to a concatenated list of conditions. CriterionName = null } }, }; adGroupCriterions.Add(adGroupWebpageNegativeUrl); OutputStatusMessage("-----\nAddAdGroupCriterions:"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupCriterions); AddAdGroupCriterionsResponse addAdGroupCriterionsResponse = await CampaignManagementExampleHelper.AddAdGroupCriterionsAsync( adGroupCriterions : adGroupCriterions, criterionType : AdGroupCriterionType.Webpage); long?[] adGroupCriterionIds = addAdGroupCriterionsResponse.AdGroupCriterionIds.ToArray(); OutputStatusMessage("AdGroupCriterionIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupCriterionIds); BatchErrorCollection[] adGroupCriterionErrors = addAdGroupCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("NestedPartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(adGroupCriterionErrors); // The negative Webpage criterion at the campaign level applies to all ad groups // within the campaign; however, if you define ad group level negative Webpage criterion, // the campaign criterion is ignored for that ad group. var campaignCriterions = new List <CampaignCriterion>(); var campaignWebpageNegative = new NegativeCampaignCriterion { CampaignId = (long)campaignIds[0], Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = DOMAIN_NAME + "\\seattle", Operand = WebpageConditionOperand.Url, } }, CriterionName = "Campaign Negative Webpage Url Criterion" } } }; campaignCriterions.Add(campaignWebpageNegative); OutputStatusMessage("-----\nAddCampaignCriterions:"); CampaignManagementExampleHelper.OutputArrayOfCampaignCriterion(campaignCriterions); AddCampaignCriterionsResponse addCampaignCriterionsResponse = await CampaignManagementExampleHelper.AddCampaignCriterionsAsync( campaignCriterions : campaignCriterions, criterionType : CampaignCriterionType.Webpage); long?[] campaignCriterionIds = addCampaignCriterionsResponse.CampaignCriterionIds.ToArray(); OutputStatusMessage("CampaignCriterionIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(campaignCriterionIds); BatchErrorCollection[] campaignCriterionErrors = addCampaignCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("NestedPartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(campaignCriterionErrors); // Finally you must add at least one DynamicSearchAd into the ad group. The ad title and display URL // are generated automatically based on the website domain and language that you want to target. var ads = new Ad[] { new DynamicSearchAd { Text = "Find New Customers & Increase Sales! Start Advertising on Contoso Today.", Path1 = "seattle", Path2 = "shoe sale", // You cannot set FinalUrls for dynamic search ads. // The Final URL will be a dynamically selected landing page. // The final URL is distinct from the path that customers will see and click on in your ad. FinalUrls = null, }, }; OutputStatusMessage("-----\nAddAds:"); AddAdsResponse addAdsResponse = await CampaignManagementExampleHelper.AddAdsAsync( adGroupId : (long)adGroupIds[0], ads : ads); long?[] adIds = addAdsResponse.AdIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] adErrors = addAdsResponse.PartialErrors.ToArray(); OutputStatusMessage("AdIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(adIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(adErrors); // Delete the campaign and everything it contains e.g., ad groups and ads. OutputStatusMessage("-----\nDeleteCampaigns:"); await CampaignManagementExampleHelper.DeleteCampaignsAsync( accountId : authorizationData.AccountId, campaignIds : new[] { (long)campaignIds[0] }); OutputStatusMessage(string.Format("Deleted Campaign Id {0}", campaignIds[0])); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201406.AdGroupCriterionService); // Set partial failure mode for the service. adGroupCriterionService.RequestHeader.partialFailure = true; List <AdGroupCriterionOperation> operations = new List <AdGroupCriterionOperation>(); // Create the placements. string[] urls = new String[] { "http://mars.google.com", "http:/mars.google.com", "mars.google.com" }; foreach (String url in urls) { Placement placement = new Placement(); placement.url = url; // Create biddable ad group criterion. BiddableAdGroupCriterion placementBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); placementBiddableAdGroupCriterion.adGroupId = adGroupId; placementBiddableAdGroupCriterion.criterion = placement; // Create the operation. AdGroupCriterionOperation placementAdGroupCriterionOperation = new AdGroupCriterionOperation(); placementAdGroupCriterionOperation.operand = placementBiddableAdGroupCriterion; placementAdGroupCriterionOperation.@operator = Operator.ADD; operations.Add(placementAdGroupCriterionOperation); } try { // Create the placements. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { if (adGroupCriterionResult.criterion != null) { Console.WriteLine("Placement with ad group id '{0}', and criterion " + "id '{1}', and url '{2}' was added.\n", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, ((Placement)adGroupCriterionResult.criterion).url); } } } else { Console.WriteLine("No placements were added."); } // Display the partial failure errors. if (result != null && result.partialFailureErrors != null) { foreach (ApiError apiError in result.partialFailureErrors) { int operationIndex = ErrorUtilities.GetOperationIndex(apiError.fieldPath); if (operationIndex != -1) { AdGroupCriterion adGroupCriterion = operations[operationIndex].operand; Console.WriteLine("Placement with ad group id '{0}' and url '{1}' " + "triggered a failure for the following reason: '{2}'.\n", adGroupCriterion.adGroupId, ((Placement)adGroupCriterion.criterion).url, apiError.errorString); } else { Console.WriteLine("A failure for the following reason: '{0}' has occurred.\n", apiError.errorString); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to add placements in partial failure mode.", e); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { ApiEnvironment environment = ((OAuthDesktopMobileAuthCodeGrant)authorizationData.Authentication).Environment; CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper(this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>(authorizationData, environment); // Setup an Audience campaign with one ad group and a responsive ad. var campaigns = new[] { new Campaign { // CampaignType must be set for Audience campaigns CampaignType = CampaignType.Audience, // Languages must be set for Audience campaigns Languages = new string[] { "All" }, Name = "Women's Shoes " + DateTime.UtcNow, Description = "Red shoes line.", DailyBudget = 50, BudgetType = BudgetLimitType.DailyBudgetStandard, TimeZone = "PacificTimeUSCanadaTijuana", }, }; var adGroups = new[] { new AdGroup { Name = "Women's Red Shoe Sale", StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, CpcBid = new Bid { Amount = 0.09 }, // Language cannot be set for ad groups in Audience campaigns Language = null, // Network cannot be set for ad groups in Audience campaigns Network = null, Settings = new[] { new TargetSetting { // By including the corresponding TargetSettingDetail, // this example sets the "target and bid" option for // CompanyName, Industry, and JobFunction. We will only deliver ads to // people who meet at least one of your criteria. // By default the "bid only" option is set for Audience, Age, and Gender. // We will deliver ads to all audiences, ages, and genders, if they meet // your company name, industry, or job function criteria. Details = new [] { new TargetSettingDetail { CriterionTypeGroup = CriterionTypeGroup.CompanyName, TargetAndBid = true }, new TargetSettingDetail { CriterionTypeGroup = CriterionTypeGroup.Industry, TargetAndBid = true }, new TargetSettingDetail { CriterionTypeGroup = CriterionTypeGroup.JobFunction, TargetAndBid = true }, } } }, } }; var ads = new Ad[] { new ResponsiveAd { // Not applicable for responsive ads AdFormatPreference = null, BusinessName = "Contoso", CallToAction = CallToAction.AddToCart, // Not applicable for responsive ads DevicePreference = null, EditorialStatus = null, FinalAppUrls = null, FinalMobileUrls = new[] { "http://mobile.contoso.com/womenshoesale" }, FinalUrls = new[] { "http://www.contoso.com/womenshoesale" }, ForwardCompatibilityMap = null, Headline = "Fast & Easy Setup", Id = null, LandscapeImageMediaId = LandscapeImageMediaId, LandscapeLogoMediaId = LandscapeLogoMediaId, LongHeadline = "Find New Customers & Increase Sales!", SquareImageMediaId = SquareImageMediaId, SquareLogoMediaId = SquareLogoMediaId, Status = null, Text = "Find New Customers & Increase Sales! Start Advertising on Contoso Today.", TrackingUrlTemplate = null, Type = null, UrlCustomParameters = null, } }; // Add the campaign, ad group, and ad AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync( authorizationData.AccountId, campaigns, false); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync( (long)campaignIds[0], adGroups, false); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); AddAdsResponse addAdsResponse = await CampaignManagementExampleHelper.AddAdsAsync((long)adGroupIds[0], ads); long?[] adIds = addAdsResponse.AdIds.ToArray(); BatchError[] adErrors = addAdsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adErrors); // Whether or not the "target and bid" option has been set for a given // criterion type group, you can set bid adjustments for specific criteria. var adGroupCriterions = new List <AdGroupCriterion>(); var adGroupCompanyNameCriterion = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new ProfileCriterion { ProfileId = 808251207, // Microsoft ProfileType = ProfileType.CompanyName }, }; adGroupCriterions.Add(adGroupCompanyNameCriterion); var adGroupIndustryCriterion = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new ProfileCriterion { ProfileId = 807658654, // Computer & Network Security ProfileType = ProfileType.Industry }, }; adGroupCriterions.Add(adGroupIndustryCriterion); var adGroupJobFunctionCriterion = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new ProfileCriterion { ProfileId = 807658477, // Engineering ProfileType = ProfileType.JobFunction }, }; adGroupCriterions.Add(adGroupJobFunctionCriterion); // Exclude ages twenty-five through thirty-four. var adGroupNegativeAgeCriterion = new NegativeAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new AgeCriterion { AgeRange = AgeRange.TwentyFiveToThirtyFour }, }; adGroupCriterions.Add(adGroupNegativeAgeCriterion); OutputStatusMessage("Adding Ad Group Criteria . . . \n"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupCriterions); AddAdGroupCriterionsResponse addAdGroupCriterionsResponse = await CampaignManagementExampleHelper.AddAdGroupCriterionsAsync( adGroupCriterions, AdGroupCriterionType.Targets); long?[] adGroupCriterionIds = addAdGroupCriterionsResponse.AdGroupCriterionIds.ToArray(); OutputStatusMessage("New Ad Group Criterion Ids:\n"); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupCriterionIds); BatchErrorCollection[] adGroupCriterionErrors = addAdGroupCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("\nAddAdGroupCriterions Errors:\n"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(adGroupCriterionErrors); // Delete the campaign, ad group, criteria, and ad that were previously added. // You should remove this line if you want to view the added entities in the // Bing Ads web application or another tool. await CampaignManagementExampleHelper.DeleteCampaignsAsync(authorizationData.AccountId, new[] { (long)campaignIds[0] }); OutputStatusMessage(string.Format("\nDeleted Campaign Id {0}\n", campaignIds[0])); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201603.AdGroupCriterionService); // Set partial failure mode for the service. adGroupCriterionService.RequestHeader.partialFailure = true; List <AdGroupCriterionOperation> operations = new List <AdGroupCriterionOperation>(); // Create the keywords. string[] keywords = new String[] { "mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise" }; foreach (String keywordText in keywords) { Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; // Create biddable ad group criterion. BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); keywordBiddableAdGroupCriterion.adGroupId = adGroupId; keywordBiddableAdGroupCriterion.criterion = keyword; // Create the operation. AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation(); keywordAdGroupCriterionOperation.operand = keywordBiddableAdGroupCriterion; keywordAdGroupCriterionOperation.@operator = Operator.ADD; operations.Add(keywordAdGroupCriterionOperation); } try { // Create the keywords. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { if (adGroupCriterionResult.criterion != null) { Console.WriteLine("Keyword with ad group id '{0}', criterion id '{1}', and " + "text '{2}' was added.\n", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, ((Keyword)adGroupCriterionResult.criterion).text); } } } else { Console.WriteLine("No keywords were added."); } // Display the partial failure errors. if (result != null && result.partialFailureErrors != null) { foreach (ApiError apiError in result.partialFailureErrors) { int operationIndex = ErrorUtilities.GetOperationIndex(apiError.fieldPath); if (operationIndex != -1) { AdGroupCriterion adGroupCriterion = operations[operationIndex].operand; Console.WriteLine("Keyword with ad group id '{0}' and text '{1}' " + "triggered a failure for the following reason: '{2}'.\n", adGroupCriterion.adGroupId, ((Keyword)adGroupCriterion.criterion).text, apiError.errorString); } else { Console.WriteLine("A failure for the following reason: '{0}' has occurred.\n", apiError.errorString); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to add keywords in partial failure mode.", e); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { ApiEnvironment environment = ((OAuthDesktopMobileAuthCodeGrant)authorizationData.Authentication).Environment; CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper( OutputStatusMessageDefault: this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>( authorizationData: authorizationData, environment: environment); // Before you can track conversions or target audiences using a remarketing list // you need to create a UET tag, and then add the UET tag tracking code to every page of your website. // For more information, please see Universal Event Tracking at https://go.microsoft.com/fwlink/?linkid=829965. // First you should call the GetUetTagsByIds operation to check whether a tag has already been created. // You can leave the TagIds element null or empty to request all UET tags available for the customer. OutputStatusMessage("-----\nGetUetTagsByIds:"); var uetTags = (await CampaignManagementExampleHelper.GetUetTagsByIdsAsync( tagIds: null))?.UetTags; // If you do not already have a UET tag that can be used, or if you need another UET tag, // call the AddUetTags service operation to create a new UET tag. If the call is successful, // the tracking script that you should add to your website is included in a corresponding // UetTag within the response message. if (uetTags == null || uetTags.Count < 1) { var uetTag = new UetTag { Description = "My First Uet Tag", Name = "New Uet Tag", }; OutputStatusMessage("-----\nAddUetTags:"); uetTags = (await CampaignManagementExampleHelper.AddUetTagsAsync( uetTags: new[] { uetTag })).UetTags; } if (uetTags == null || uetTags.Count < 1) { OutputStatusMessage( string.Format("You do not have any UET tags registered for CustomerId {0}.", authorizationData.CustomerId) ); return; } OutputStatusMessage("List of all UET Tags:"); CampaignManagementExampleHelper.OutputArrayOfUetTag(uetTags); // After you retreive the tracking script from the AddUetTags or GetUetTagsByIds operation, // the next step is to add the UET tag tracking code to your website. // We will use the same UET tag for the remainder of this example. var tagId = uetTags[0].Id; // Add remarketing lists that depend on the UET Tag Id retreived above. var addAudiences = new[] { new RemarketingList { Description = "New list with CustomEventsRule", MembershipDuration = 30, Name = "Remarketing List with CustomEventsRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // (Category Equals video) and (Action Equals play) and (Label Equals trailer) // and (Value Equals 5) Rule = new CustomEventsRule { // The type of user interaction you want to track. Action = "play", ActionOperator = StringOperator.Equals, // The category of event you want to track. Category = "video", CategoryOperator = StringOperator.Equals, // The name of the element that caused the action. Label = "trailer", LabelOperator = StringOperator.Equals, // A numerical value associated with that event. // Could be length of the video played etc. Value = 5.00m, ValueOperator = NumberOperator.Equals, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // ((Url Contains X) and (ReferrerUrl DoesNotContain Z)) or ((Url DoesNotBeginWith Y)) // or ((ReferrerUrl Equals Z)) Rule = new PageVisitorsRule { RuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsWhoDidNotVisitAnotherPageRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsWhoDidNotVisitAnotherPageRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // (((Url Contains X) and (ReferrerUrl DoesNotContain Z)) or ((Url DoesNotBeginWith Y)) // or ((ReferrerUrl Equals Z))) // and not (((Url BeginsWith A) and (ReferrerUrl BeginsWith B)) or ((Url Contains C))) Rule = new PageVisitorsWhoDidNotVisitAnotherPageRule { ExcludeRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.BeginsWith, Value = "A" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.BeginsWith, Value = "B" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "C" }, } }, }, IncludeRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsWhoVisitedAnotherPageRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsWhoVisitedAnotherPageRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // (((Url Contains X) and (ReferrerUrl NotEquals Z)) or ((Url DoesNotBeginWith Y)) or // ((ReferrerUrl Equals Z))) // and (((Url BeginsWith A) and (ReferrerUrl BeginsWith B)) or ((Url Contains C))) Rule = new PageVisitorsWhoVisitedAnotherPageRule { AnotherRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.BeginsWith, Value = "A" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.BeginsWith, Value = "B" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "C" }, } }, }, RuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, }; // RemarketingList extends the Audience base class. // We manage remarketing lists with Audience operations. OutputStatusMessage("-----\nAddAudiences:"); var addAudiencesResponse = await CampaignManagementExampleHelper.AddAudiencesAsync( audiences : addAudiences); long?[] audienceIds = addAudiencesResponse.AudienceIds.ToArray(); BatchError[] audienceErrors = addAudiencesResponse.PartialErrors.ToArray(); OutputStatusMessage("AudienceIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(audienceIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(audienceErrors); // Add an ad group in a campaign. The ad group will later be associated with remarketing lists. var campaigns = new[] { new Campaign { BudgetType = BudgetLimitType.DailyBudgetStandard, DailyBudget = 50, Languages = new string[] { "All" }, Name = "Women's Shoes " + DateTime.UtcNow, TimeZone = "PacificTimeUSCanadaTijuana", }, }; OutputStatusMessage("-----\nAddCampaigns:"); AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync( accountId : authorizationData.AccountId, campaigns : campaigns); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); OutputStatusMessage("CampaignIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); var adGroups = new[] { new AdGroup { Name = "Women's Red Shoe Sale", StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, CpcBid = new Bid { Amount = 0.09 }, // Applicable for all remarketing lists that are associated with this ad group. TargetAndBid indicates // that you want to show ads only to people included in the remarketing list, with the option to change // the bid amount. Ads in this ad group will only show to people included in the remarketing list. Settings = new[] { new TargetSetting { Details = new [] { new TargetSettingDetail { CriterionTypeGroup = CriterionTypeGroup.Audience, TargetAndBid = true } } } }, } }; OutputStatusMessage("-----\nAddAdGroups:"); AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync( campaignId : (long)campaignIds[0], adGroups : adGroups, returnInheritedBidStrategyTypes : false); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); OutputStatusMessage("AdGroupIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); OutputStatusMessage("PartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); // Associate all of the remarketing lists created above with the new ad group. var adGroupRemarketingListAssociations = new List <AdGroupCriterion>(); foreach (var audienceId in audienceIds) { if (audienceId != null) { var biddableAdGroupCriterion = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new AudienceCriterion { AudienceId = audienceId, AudienceType = AudienceType.RemarketingList, }, CriterionBid = new BidMultiplier { Multiplier = 20.00, }, Status = AdGroupCriterionStatus.Active, }; adGroupRemarketingListAssociations.Add(biddableAdGroupCriterion); } } OutputStatusMessage("-----\nAddAdGroupCriterions:"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupRemarketingListAssociations); AddAdGroupCriterionsResponse addAdGroupCriterionsResponse = await CampaignManagementExampleHelper.AddAdGroupCriterionsAsync( adGroupCriterions : adGroupRemarketingListAssociations, criterionType : AdGroupCriterionType.Audience); long?[] nullableAdGroupCriterionIds = addAdGroupCriterionsResponse.AdGroupCriterionIds.ToArray(); OutputStatusMessage("AdGroupCriterionIds:"); CampaignManagementExampleHelper.OutputArrayOfLong(nullableAdGroupCriterionIds); BatchErrorCollection[] adGroupCriterionErrors = addAdGroupCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("NestedPartialErrors:"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(adGroupCriterionErrors); // Delete the campaign and everything it contains e.g., ad groups and ads. OutputStatusMessage("-----\nDeleteCampaigns:"); await CampaignManagementExampleHelper.DeleteCampaignsAsync( accountId : authorizationData.AccountId, campaignIds : new[] { (long)campaignIds[0] }); OutputStatusMessage(string.Format("Deleted Campaign Id {0}", campaignIds[0])); // Delete the remarketing lists. OutputStatusMessage("-----\nDeleteAudiences:"); await CampaignManagementExampleHelper.DeleteAudiencesAsync( audienceIds : new[] { (long)audienceIds[0] }); OutputStatusMessage(string.Format("Deleted Audience Id {0}", audienceIds[0])); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V13.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V13.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V13.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
private static void CsvToBidding(RowValues values, BulkAdGroupProductPartition entity) { //string exclude; values.TryGetValue(StringTable.IsExcluded, out string exclude); exclude = exclude.GetValueOrEmptyString().ToLower(); bool isExcluded; switch (exclude) { case "yes": isExcluded = true; break; case "true": isExcluded = true; break; case "no": isExcluded = false; break; case "false": isExcluded = false; break; default: throw new InvalidOperationException( string.Format("\"{0}\" can only be set to TRUE|FALSE in {1}", StringTable.IsExcluded, typeof(BulkAdGroupProductPartition).Name)); } if (isExcluded) { entity.AdGroupCriterion = new NegativeAdGroupCriterion { Criterion = new ProductPartition { Condition = new ProductCondition(), Type = typeof(ProductPartition).Name, }, Type = typeof(NegativeAdGroupCriterion).Name, }; } else { var biddableAdGroupCriterion = new BiddableAdGroupCriterion { Criterion = new ProductPartition { Condition = new ProductCondition(), Type = typeof(ProductPartition).Name, }, Type = typeof(BiddableAdGroupCriterion).Name, }; values.TryGetValue(StringTable.Bid, out string bidStr); values.TryGetValue(StringTable.BidAdjustment, out string bidAdjustmentStr); double?bid = bidStr.ParseOptional <double>(); double?bidAdjustment = bidAdjustmentStr.ParseOptional <double>(); if (bid != null) { biddableAdGroupCriterion.CriterionBid = new FixedBid { Amount = bid.Value, Type = typeof(FixedBid).Name, }; } else if (bidAdjustment != null) { biddableAdGroupCriterion.CriterionBid = new BidMultiplier { Multiplier = bidAdjustment.Value, Type = typeof(FixedBid).Name, }; } else { biddableAdGroupCriterion.CriterionBid = new FixedBid { Type = typeof(FixedBid).Name, }; } entity.AdGroupCriterion = biddableAdGroupCriterion; } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { Service = new ServiceClient<ICampaignManagementService>(authorizationData); // Get a list of all Bing Merchant Center stores associated with your CustomerId IList<BMCStore> stores = await GetBMCStoresByCustomerIdAsync(); if (stores == null) { OutputStatusMessage( String.Format("You do not have any BMC stores registered for CustomerId {0}.\n", authorizationData.CustomerId) ); return; } #region ManageCampaign /* Add a new Bing Shopping campaign that will be associated with a ProductScope criterion. * - Set the CampaignType element of the Campaign to Shopping. * - Create a ShoppingSetting instance and set its Priority (0, 1, or 2), SalesCountryCode, and StoreId elements. * Add this shopping setting to the Settings list of the Campaign. */ var campaign = new Campaign { CampaignType = CampaignType.Shopping, Settings = new[] { new ShoppingSetting() { Priority = 0, SalesCountryCode = "US", StoreId = (int)stores[0].Id } }, Name = "Bing Shopping Campaign " + DateTime.UtcNow, Description = "Bing Shopping Campaign Example.", BudgetType = BudgetLimitType.MonthlyBudgetSpendUntilDepleted, MonthlyBudget = 1000.00, TimeZone = "PacificTimeUSCanadaTijuana", DaylightSaving = true, }; var campaignIds = await AddCampaignsAsync(authorizationData.AccountId, new[] { campaign }); OutputCampaignIdentifiers(campaignIds); /* Optionally, you can create a ProductScope criterion that will be associated with your Bing Shopping campaign. * Use the product scope criterion to include a subset of your product catalog, for example a specific brand, * category, or product type. A campaign can only be associated with one ProductScope, which contains a list * of up to 7 ProductCondition. You'll also be able to specify more specific product conditions for each ad group. */ var campaignCriterions = new CampaignCriterion[] { new CampaignCriterion() { CampaignId = campaignIds[0], BidAdjustment = null, // Reserved for future use Criterion = new ProductScope() { Conditions = new ProductCondition[] { new ProductCondition { Operand = "Condition", Attribute = "New" }, new ProductCondition { Operand = "CustomLabel0", Attribute = "MerchantDefinedCustomLabel" }, } }, } }; var addCampaignCriterionsResponse = await (AddCampaignCriterionsAsync( campaignCriterions, CampaignCriterionType.ProductScope) ); #endregion ManageCampaign #region ManageAdGroup // Specify one or more ad groups. var adGroup = new AdGroup { Name = "Product Categories", AdDistribution = AdDistribution.Search, BiddingModel = BiddingModel.Keyword, PricingModel = PricingModel.Cpc, StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = 2016 }, Language = "English" }; var adGroupIds = (long[])await AddAdGroupsAsync(campaignIds[0], new[] { adGroup }); OutputAdGroupIdentifiers(adGroupIds); #region BidAllProducts var helper = new PartitionActionHelper(adGroupIds[0]); var root = helper.AddUnit( null, new ProductCondition { Operand = "All", Attribute = null }, 0.35, false ); OutputStatusMessage("Applying only the root as a Unit with a bid . . . \n"); var applyProductPartitionActionsResponse = await ApplyProductPartitionActionsAsync(helper.PartitionActions); var adGroupCriterions = await GetAdGroupCriterionsByAdGroupIdAsync( adGroupIds[0], CriterionType.ProductPartition ); OutputStatusMessage("The ad group's product partition only has a tree root node: \n"); OutputProductPartitions(adGroupCriterions); /* * Let's update the bid of the root Unit we just added. */ BiddableAdGroupCriterion updatedRoot = new BiddableAdGroupCriterion { Id = applyProductPartitionActionsResponse.AdGroupCriterionIds[0], CriterionBid = new FixedBid { Bid = new Bid { Amount = 0.45 } } }; helper = new PartitionActionHelper(adGroupIds[0]); helper.UpdatePartition(updatedRoot); OutputStatusMessage("Updating the bid for the tree root node . . . \n"); await ApplyProductPartitionActionsAsync(helper.PartitionActions); adGroupCriterions = await GetAdGroupCriterionsByAdGroupIdAsync( adGroupIds[0], CriterionType.ProductPartition ); OutputStatusMessage("Updated the bid for the tree root node: \n"); OutputProductPartitions(adGroupCriterions); #endregion BidAllProducts #region InitializeTree /* * Now we will overwrite any existing tree root, and build a product partition group tree structure in multiple steps. * You could build the entire tree in a single call since there are less than 5,000 nodes; however, * we will build it in steps to demonstrate how to use the results from ApplyProductPartitionActions to update the tree. * * For a list of validation rules, see the Bing Shopping Campaigns technical guide: * https://msdn.microsoft.com/en-US/library/bing-ads-campaign-management-bing-shopping-campaigns.aspx */ helper = new PartitionActionHelper(adGroupIds[0]); /* * Check whether a root node exists already. */ adGroupCriterions = await GetAdGroupCriterionsByAdGroupIdAsync( adGroupIds[0], CriterionType.ProductPartition ); var existingRoot = GetRootNode(adGroupCriterions); if (existingRoot != null) { helper.DeletePartition(existingRoot); } root = helper.AddSubdivision( null, new ProductCondition { Operand = "All", Attribute = null } ); /* * The direct children of any node must have the same Operand. * For this example we will use CategoryL1 nodes as children of the root. * For a list of valid CategoryL1 through CategoryL5 values, see the Bing Category Taxonomy: * http://advertise.bingads.microsoft.com/en-us/WWDocs/user/search/en-us/Bing_Category_Taxonomy.txt */ var animalsSubdivision = helper.AddSubdivision( root, new ProductCondition { Operand = "CategoryL1", Attribute = "Animals & Pet Supplies" } ); /* * If you use a CategoryL2 node, it must be a descendant (child or later) of a CategoryL1 node. * In other words you cannot have a CategoryL2 node as parent of a CategoryL1 node. * For this example we will a CategoryL2 node as child of the CategoryL1 Animals & Pet Supplies node. */ var petSuppliesSubdivision = helper.AddSubdivision( animalsSubdivision, new ProductCondition { Operand = "CategoryL2", Attribute = "Pet Supplies" } ); var brandA = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand A" }, 0.35, false ); /* * If you won't bid on Brand B, set the helper method's bidAmount to '0' and isNegative to true. * The helper method will create a NegativeAdGroupCriterion and apply the condition. */ var brandB = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand B" }, 0, true ); var otherBrands = helper.AddUnit( petSuppliesSubdivision, new ProductCondition { Operand = "Brand", Attribute = null }, 0.35, false ); var otherPetSupplies = helper.AddUnit( animalsSubdivision, new ProductCondition { Operand = "CategoryL2", Attribute = null }, 0.35, false ); var electronics = helper.AddUnit( root, new ProductCondition { Operand = "CategoryL1", Attribute = "Electronics" }, 0.35, false ); var otherCategoryL1 = helper.AddUnit( root, new ProductCondition { Operand = "CategoryL1", Attribute = null }, 0.35, false ); OutputStatusMessage("Applying product partitions to the ad group . . . \n"); applyProductPartitionActionsResponse = await ApplyProductPartitionActionsAsync(helper.PartitionActions); // To retrieve product partitions after they have been applied, call GetAdGroupCriterionsByAdGroupId. // The product partition with ParentCriterionId set to null is the root node. adGroupCriterions = await GetAdGroupCriterionsByAdGroupIdAsync( adGroupIds[0], CriterionType.ProductPartition ); /* * The product partition group tree now has 9 nodes. All other (Root Node) | +-- Animals & Pet Supplies (CategoryL1) | | | +-- Pet Supplies (CategoryL2) | | | | | +-- Brand A | | | | | +-- Brand B | | | | | +-- All other (Brand) | | | +-- All other (CategoryL2) | +-- Electronics (CategoryL1) | +-- All other (CategoryL1) */ OutputStatusMessage("The product partition group tree now has 9 nodes: \n"); OutputProductPartitions(adGroupCriterions); #endregion InitializeTree #region UpdateTree /* * Let's replace the Electronics (CategoryL1) node created above with an Electronics (CategoryL1) node that * has children i.e. Brand C (Brand), Brand D (Brand), and All other (Brand) as follows: Electronics (CategoryL1) | +-- Brand C (Brand) | +-- Brand D (Brand) | +-- All other (Brand) */ helper = new PartitionActionHelper(adGroupIds[0]); /* * To replace a node we must know its Id and its ParentCriterionId. In this case the parent of the node * we are replacing is All other (Root Node), and was created at Index 1 of the previous ApplyProductPartitionActions call. * The node that we are replacing is Electronics (CategoryL1), and was created at Index 8. */ var rootId = applyProductPartitionActionsResponse.AdGroupCriterionIds[1]; electronics.Id = applyProductPartitionActionsResponse.AdGroupCriterionIds[8]; helper.DeletePartition(electronics); var parent = new BiddableAdGroupCriterion() { Id = rootId }; var electronicsSubdivision = helper.AddSubdivision( parent, new ProductCondition { Operand = "CategoryL1", Attribute = "Electronics" } ); var brandC = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand C" }, 0.35, false ); var brandD = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = "Brand D" }, 0.35, false ); var otherElectronicsBrands = helper.AddUnit( electronicsSubdivision, new ProductCondition { Operand = "Brand", Attribute = null }, 0.35, false ); OutputStatusMessage( "Updating the product partition group to refine Electronics (CategoryL1) with 3 child nodes . . . \n" ); applyProductPartitionActionsResponse = await ApplyProductPartitionActionsAsync(helper.PartitionActions); adGroupCriterions = await GetAdGroupCriterionsByAdGroupIdAsync( adGroupIds[0], CriterionType.ProductPartition ); /* * The product partition group tree now has 12 nodes, including the children of Electronics (CategoryL1): All other (Root Node) | +-- Animals & Pet Supplies (CategoryL1) | | | +-- Pet Supplies (CategoryL2) | | | | | +-- Brand A | | | | | +-- Brand B | | | | | +-- All other (Brand) | | | +-- All other (CategoryL2) | +-- Electronics (CategoryL1) | | | +-- Brand C (Brand) | | | +-- Brand D (Brand) | | | +-- All other (Brand) | +-- All other (CategoryL1) */ OutputStatusMessage( "The product partition group tree now has 12 nodes, including the children of Electronics (CategoryL1): \n" ); OutputProductPartitions(adGroupCriterions); #endregion UpdateTree #endregion ManageAdGroup #region ManageAds /* * Create a product ad. You must add at least one ProductAd to the corresponding ad group. * A ProductAd is not used directly for delivered ad copy. Instead, the delivery engine generates * product ads from the product details that it finds in your Bing Merchant Center store's product catalog. * The primary purpose of the ProductAd object is to provide promotional text that the delivery engine * adds to the product ads that it generates. For example, if the promotional text is set to * “Free shipping on $99 purchases”, the delivery engine will set the product ad’s description to * “Free shipping on $99 purchases.” */ var ads = new Ad[] { new ProductAd { PromotionalText = "Free shipping on $99 purchases." }, }; AddAdsResponse addAdsResponse = await AddAdsAsync(adGroupIds[0], ads); OutputAdResults(ads, addAdsResponse.AdIds, addAdsResponse.PartialErrors); #endregion ManageAds #region CleanUp /* Delete the campaign, ad group, criterion, and ad that were previously added. * You should remove this region if you want to view the added entities in the * Bing Ads web application or another tool. */ DeleteCampaignsAsync(authorizationData.AccountId, new[] { campaignIds[0] }); OutputStatusMessage(String.Format("Deleted CampaignId {0}\n", campaignIds[0])); #endregion CleanUp } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException<Microsoft.BingAds.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException<Microsoft.BingAds.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException<Microsoft.BingAds.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
/// <summary> /// Sets the Add action for a new AdGroupCriterion corresponding to the specified ProductCondition, /// and adds it to the helper's list of AdGroupCriterionAction. /// </summary> /// <param name="parent">The parent of the product partition unit that you want to add.</param> /// <param name="condition">The condition or product filter for the new product partition.</param> /// <param name="bidAmount">The bid amount for the new product partition.</param> /// <param name="isNegative">Indicates whether or not to add a NegativeAdGroupCriterion. /// The default value is false, in which case a BiddableAdGroupCriterion will be added.</param> /// <returns>The ad group criterion that was added to the list of PartitionActions.</returns> public AdGroupCriterion AddUnit( AdGroupCriterion parent, ProductCondition condition, double bidAmount, bool isNegative ) { AdGroupCriterion adGroupCriterion; if (isNegative) { adGroupCriterion = new NegativeAdGroupCriterion(); } else { adGroupCriterion = new BiddableAdGroupCriterion() { CriterionBid = new FixedBid() { Amount = bidAmount }, // This destination URL is used if specified; otherwise, the destination URL is determined // by the corresponding value of the 'Link' that you specified for the product offer // in your Bing Merchant Center catalog. DestinationUrl = null, // You could use a tracking template which would override the campaign level // tracking template. Tracking templates defined for lower level entities // override those set for higher level entities. // In this example we are using the campaign level tracking template. TrackingUrlTemplate = null, // Set custom parameters that are specific to this criterion, // and can be used by the criterion, ad group, campaign, or account level tracking template. // In this example we are using the campaign level tracking template. UrlCustomParameters = new CustomParameters { Parameters = new[] { new CustomParameter() { Key = "promoCode", Value = "PROMO1" }, new CustomParameter() { Key = "season", Value = "summer" }, } } }; } adGroupCriterion.Criterion = new ProductPartition() { // If the root node is a unit, it would not have a parent ParentCriterionId = parent != null ? parent.Id : null, Condition = condition, PartitionType = ProductPartitionType.Unit }; adGroupCriterion.AdGroupId = this.adGroupId; var partitionAction = new AdGroupCriterionAction() { Action = ItemAction.Add, AdGroupCriterion = adGroupCriterion }; this.partitionActions.Add(partitionAction); return(adGroupCriterion); }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad groups to which placements are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the MutateJobService. MutateJobService mutateJobService = (MutateJobService)user.GetService( AdWordsService.v201502.MutateJobService); const int RETRY_INTERVAL = 30; const int RETRIES_COUNT = 30; const int PLACEMENT_NUMBER = 100; const string INDEX_REGEX = "operations\\[(\\d+)\\].operand"; List <Operation> operations = new List <Operation>(); // Create AdGroupCriterionOperation to add placements. for (int i = 0; i < PLACEMENT_NUMBER; i++) { Placement placement = new Placement(); placement.url = "http://mars.google.com/" + i.ToString(); BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion(); criterion.adGroupId = adGroupId; criterion.criterion = placement; AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation(); adGroupCriterionOperation.@operator = Operator.ADD; adGroupCriterionOperation.operand = criterion; operations.Add(adGroupCriterionOperation); } BulkMutateJobPolicy policy = new BulkMutateJobPolicy(); // You can specify up to 3 job IDs that must successfully complete before // this job can be processed. policy.prerequisiteJobIds = new long[] {}; SimpleMutateJob job = mutateJobService.mutate(operations.ToArray(), policy); // Wait for the job to complete. bool completed = false; int retryCount = 0; Console.WriteLine("Retrieving job status..."); while (completed == false && retryCount < RETRIES_COUNT) { BulkMutateJobSelector selector = new BulkMutateJobSelector(); selector.jobIds = new long[] { job.id }; try { Job[] allJobs = mutateJobService.get(selector); if (allJobs != null && allJobs.Length > 0) { job = (SimpleMutateJob)allJobs[0]; if (job.status == BasicJobStatus.COMPLETED || job.status == BasicJobStatus.FAILED) { completed = true; break; } else { Console.WriteLine("{0}: Current status is {1}, waiting {2} seconds to retry...", retryCount, job.status, RETRY_INTERVAL); Thread.Sleep(RETRY_INTERVAL * 1000); retryCount++; } } } catch (Exception e) { throw new System.ApplicationException(string.Format("Failed to fetch simple mutate " + "job with id = {0}.", job.id), e); } } if (job.status == BasicJobStatus.COMPLETED) { // Handle cases where the job completed. // Create the job selector. BulkMutateJobSelector selector = new BulkMutateJobSelector(); selector.jobIds = new long[] { job.id }; // Get the job results. JobResult jobResult = mutateJobService.getResult(selector); if (jobResult != null) { SimpleMutateResult results = (SimpleMutateResult)jobResult.Item; if (results != null) { // Display the results. if (results.results != null) { for (int i = 0; i < results.results.Length; i++) { Operand operand = results.results[i]; Console.WriteLine("Operation {0} - {1}", i, (operand.Item is PlaceHolder) ? "FAILED" : "SUCCEEDED"); } } // Display the errors. if (results.errors != null) { foreach (ApiError apiError in results.errors) { Match match = Regex.Match(apiError.fieldPath, INDEX_REGEX, RegexOptions.IgnoreCase); string index = (match.Success)? match.Groups[1].Value : "???"; Console.WriteLine("Operation index {0} failed due to reason: '{1}', " + "trigger: '{2}'", index, apiError.errorString, apiError.trigger); } } } } Console.WriteLine("Job completed successfully!"); } else if (job.status == BasicJobStatus.FAILED) { // Handle the cases where job failed. Console.WriteLine("Job failed with reason: " + job.failureReason); } else if (job.status == BasicJobStatus.PROCESSING || job.status == BasicJobStatus.PENDING) { // Handle the cases where job didn't complete after wait period. Console.WriteLine("Job did not complete in {0} secconds.", RETRY_INTERVAL * RETRIES_COUNT); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { AdInsightExampleHelper AdInsightExampleHelper = new AdInsightExampleHelper(this.OutputStatusMessage); AdInsightExampleHelper.AdInsightService = new ServiceClient <IAdInsightService>(authorizationData); CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper(this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>(authorizationData); // To get started with dynamic search ads, first you'll need to add a new Campaign // with its type set to DynamicSearchAds. When you create the campaign, you'll need to // include a DynamicSearchAdsSetting that specifies the target website domain and language. var campaigns = new[] { new Campaign { CampaignType = CampaignType.DynamicSearchAds, Settings = new [] { new DynamicSearchAdsSetting { DomainName = "contoso.com", Language = "English" } }, Name = "Women's Shoes " + DateTime.UtcNow, Description = "Red shoes line.", // You must choose to set either the shared budget ID or daily amount. // You can set one or the other, but you may not set both. BudgetId = null, DailyBudget = 50, BudgetType = Microsoft.BingAds.V12.CampaignManagement.BudgetLimitType.DailyBudgetStandard, // You can set your campaign bid strategy to Enhanced CPC (EnhancedCpcBiddingScheme) // and then, at any time, set an individual ad group bid strategy to // Manual CPC (ManualCpcBiddingScheme). BiddingScheme = new EnhancedCpcBiddingScheme { }, TimeZone = "PacificTimeUSCanadaTijuana", // Used with CustomParameters defined in lower level entities such as ads. TrackingUrlTemplate = "http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}" }, }; AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync(authorizationData.AccountId, campaigns); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); // Next, create a new AdGroup within the dynamic search ads campaign. var adGroups = new[] { new AdGroup { Name = "Women's Red Shoe Sale", StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, CpcBid = new Bid { Amount = 0.09 }, Language = "English", // For ad groups you can use either of the InheritFromParentBiddingScheme or ManualCpcBiddingScheme objects. // If you do not set this element, then InheritFromParentBiddingScheme is used by default. BiddingScheme = new ManualCpcBiddingScheme { }, } }; AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync((long)campaignIds[0], adGroups, null); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); // You can add one or more Webpage criterion to each ad group that helps determine // whether or not to serve dynamic search ads. var adGroupCriterions = new List <AdGroupCriterion>(); var adGroupWebpagePositivePageContent = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = "flowers", Operand = WebpageConditionOperand.PageContent, } }, CriterionName = "Ad Group Webpage Positive Page Content Criterion" }, }, // DestinationUrl and FinalUrls are not supported with Webpage criterion. // The Final URL is dynamically created at the ad level. DestinationUrl = null, FinalUrls = null, // You could use a tracking template which would override the campaign level // tracking template. Tracking templates defined for lower level entities // override those set for higher level entities. // In this example we are using the campaign level tracking template. TrackingUrlTemplate = null, // Set custom parameters that are specific to this webpage criterion, // and can be used by the criterion, ad group, campaign, or account level tracking template. // In this example we are using the campaign level tracking template. UrlCustomParameters = new CustomParameters { Parameters = new[] { new CustomParameter() { Key = "promoCode", Value = "PROMO1" }, new CustomParameter() { Key = "season", Value = "summer" }, } } }; adGroupCriterions.Add(adGroupWebpagePositivePageContent); // To discover the categories that you can use for Webpage criterion (positive or negative), // use the GetDomainCategories operation with the Ad Insight service. var getDomainCategoriesResponse = await AdInsightExampleHelper.GetDomainCategoriesAsync( null, DOMAIN_NAME, LANGUAGE); var categories = getDomainCategoriesResponse.Categories; // If any categories are available let's use one as a condition. if (categories.Count > 0) { var adGroupWebpagePositiveCategory = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], CriterionBid = new FixedBid { Amount = 0.50 }, Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = categories[0].CategoryName, Operand = WebpageConditionOperand.Category, } }, CriterionName = "Ad Group Webpage Positive Category Criterion" }, } }; adGroupCriterions.Add(adGroupWebpagePositiveCategory); } // If you want to exclude certain portions of your website, you can add negative Webpage // criterion at the campaign and ad group level. var adGroupWebpageNegativeUrl = new NegativeAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new Webpage { Parameter = new WebpageParameter { // You can choose whether you want the criterion argument to match partial URLs, // page content, page title, or categories that Bing thinks applies to your website. Conditions = new[] { new WebpageCondition { Argument = DOMAIN_NAME, Operand = WebpageConditionOperand.Url, } }, // If you do not specify any name, then it will be set to a concatenated list of conditions. CriterionName = null } }, }; adGroupCriterions.Add(adGroupWebpageNegativeUrl); OutputStatusMessage("Adding Ad Group Webpage Criterion . . . \n"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupCriterions); AddAdGroupCriterionsResponse addAdGroupCriterionsResponse = await CampaignManagementExampleHelper.AddAdGroupCriterionsAsync(adGroupCriterions, AdGroupCriterionType.Webpage); long?[] adGroupCriterionIds = addAdGroupCriterionsResponse.AdGroupCriterionIds.ToArray(); OutputStatusMessage("New Ad Group Criterion Ids:\n"); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupCriterionIds); BatchErrorCollection[] adGroupCriterionErrors = addAdGroupCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("\nAddAdGroupCriterions Errors:\n"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(adGroupCriterionErrors); // The negative Webpage criterion at the campaign level applies to all ad groups // within the campaign; however, if you define ad group level negative Webpage criterion, // the campaign criterion is ignored for that ad group. var campaignCriterions = new List <CampaignCriterion>(); var campaignWebpageNegative = new NegativeCampaignCriterion { CampaignId = (long)campaignIds[0], Criterion = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = DOMAIN_NAME + "\\seattle", Operand = WebpageConditionOperand.Url, } }, CriterionName = "Campaign Negative Webpage Url Criterion" } } }; campaignCriterions.Add(campaignWebpageNegative); OutputStatusMessage("Adding Campaign Webpage Criterion . . . \n"); CampaignManagementExampleHelper.OutputArrayOfCampaignCriterion(campaignCriterions); AddCampaignCriterionsResponse addCampaignCriterionsResponse = await CampaignManagementExampleHelper.AddCampaignCriterionsAsync(campaignCriterions, CampaignCriterionType.Webpage); long?[] campaignCriterionIds = addCampaignCriterionsResponse.CampaignCriterionIds.ToArray(); OutputStatusMessage("\nNew Campaign Criterion Ids:\n"); CampaignManagementExampleHelper.OutputArrayOfLong(campaignCriterionIds); BatchErrorCollection[] campaignCriterionErrors = addCampaignCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("\nAddCampaignCriterions Errors:\n"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(campaignCriterionErrors); // Finally you can add a DynamicSearchAd into the ad group. The ad title and display URL // are generated automatically based on the website domain and language that you want to target. var ads = new Ad[] { new DynamicSearchAd { Text = "Find New Customers & Increase Sales! Start Advertising on Contoso Today.", Path1 = "seattle", Path2 = "shoe sale", // You cannot set FinalUrls. The Final URL will be a dynamically selected landing page. // The final URL is distinct from the path that customers will see and click on in your ad. FinalUrls = null, // You could use a tracking template which would override the campaign level // tracking template. Tracking templates defined for lower level entities // override those set for higher level entities. // In this example we are using the campaign level tracking template. TrackingUrlTemplate = null, // Set custom parameters that are specific to this ad, // and can be used by the ad, webpage, ad group, campaign, or account level tracking template. // In this example we are using the campaign level tracking template. UrlCustomParameters = new CustomParameters { Parameters = new[] { new CustomParameter() { Key = "promoCode", Value = "PROMO1" }, new CustomParameter() { Key = "season", Value = "summer" }, } } }, }; AddAdsResponse addAdsResponse = await CampaignManagementExampleHelper.AddAdsAsync((long)adGroupIds[0], ads); long?[] adIds = addAdsResponse.AdIds.ToArray(); Microsoft.BingAds.V12.CampaignManagement.BatchError[] adErrors = addAdsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adErrors); // Retrieve the Webpage criterion for the campaign. var getCampaignCriterionsByIdsResponse = await CampaignManagementExampleHelper.GetCampaignCriterionsByIdsAsync( null, (long)campaignIds[0], CampaignCriterionType.Webpage ); OutputStatusMessage("Retrieving the Campaign Webpage Criterions that we added . . . \n"); campaignCriterions = getCampaignCriterionsByIdsResponse.CampaignCriterions.ToList(); CampaignManagementExampleHelper.OutputArrayOfCampaignCriterion(campaignCriterions); // Retrieve the Webpage criterion for the ad group and then test some update scenarios. var getAdGroupCriterionsByIdsResponse = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( null, (long)adGroupIds[0], AdGroupCriterionType.Webpage ); OutputStatusMessage("Retrieving the Ad Group Webpage Criterions that we added . . . \n"); adGroupCriterions = getAdGroupCriterionsByIdsResponse.AdGroupCriterions.ToList(); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupCriterions); // You can update the bid for BiddableAdGroupCriterion var updateBid = new FixedBid { Amount = 0.75 }; // You can update the Webpage criterion name but cannot update the conditions. // To update the conditions you must delete the criterion and add a new criterion. // This update attempt will return an error. var updateCriterionAttemptFailure = new Webpage { Parameter = new WebpageParameter { Conditions = new[] { new WebpageCondition { Argument = "Books", Operand = WebpageConditionOperand.PageContent, } }, CriterionName = "Update Attempt Failure" }, }; var updateCriterionAttemptSuccess = new Webpage { Parameter = new WebpageParameter { CriterionName = "Update Attempt Success" }, }; foreach (var adGroupCriterion in adGroupCriterions) { var biddableAdGroupCriterion = adGroupCriterion as BiddableAdGroupCriterion; if (biddableAdGroupCriterion != null) { ((BiddableAdGroupCriterion)(adGroupCriterion)).CriterionBid = updateBid; ((BiddableAdGroupCriterion)(adGroupCriterion)).Criterion = updateCriterionAttemptSuccess; } else { var negativeAdGroupCriterion = adGroupCriterion as NegativeAdGroupCriterion; if (negativeAdGroupCriterion != null) { ((NegativeAdGroupCriterion)(adGroupCriterion)).Criterion = updateCriterionAttemptFailure; } } } OutputStatusMessage("Updating Ad Group Webpage Criterion . . . \n"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(adGroupCriterions); UpdateAdGroupCriterionsResponse updateAdGroupCriterionsResponse = await CampaignManagementExampleHelper.UpdateAdGroupCriterionsAsync(adGroupCriterions, AdGroupCriterionType.Webpage); adGroupCriterionErrors = updateAdGroupCriterionsResponse.NestedPartialErrors.ToArray(); OutputStatusMessage("UpdateAdGroupCriterions Errors:\n"); CampaignManagementExampleHelper.OutputArrayOfBatchErrorCollection(adGroupCriterionErrors); // Delete the campaign, ad group, criterion, and ad that were previously added. // You should remove this operation if you want to view the added entities in the // Bing Ads web application or another tool. await CampaignManagementExampleHelper.DeleteCampaignsAsync(authorizationData.AccountId, new[] { (long)campaignIds[0] }); OutputStatusMessage(string.Format("\nDeleted Campaign Id {0}\n", campaignIds[0])); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
internal override void ProcessMappingsFromRowValues(RowValues values) { AdGroupCriterion = new BiddableAdGroupCriterion { Criterion = new Webpage() { Parameter = new WebpageParameter(), Type = typeof(Webpage).Name, }, CriterionBid = new FixedBid { Type = typeof(FixedBid).Name, }, Type = typeof(BiddableAdGroupCriterion).Name }; values.ConvertToEntity(this, Mappings); PerformanceData = PerformanceData.ReadFromRowValuesOrNull(values); }
/// <summary> /// Main method for the thread. /// </summary> /// <param name="obj">The thread parameter.</param> public void Run(object obj) { // Create the operations. List <AdGroupCriterionOperation> operations = new List <AdGroupCriterionOperation>(); for (int j = 0; j < NUM_KEYWORDS; j++) { // Create the keyword. Keyword keyword = new Keyword { text = "mars cruise thread " + threadIndex.ToString() + " seed " + j.ToString(), matchType = KeywordMatchType.BROAD }; // Create the biddable ad group criterion. AdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion { adGroupId = adGroupId, criterion = keyword }; // Create the operations. AdGroupCriterionOperation keywordOperation = new AdGroupCriterionOperation { @operator = Operator.ADD, operand = keywordCriterion }; operations.Add(keywordOperation); } // Get the AdGroupCriterionService. This should be done within the // thread, since a service can only handle one outgoing HTTP request // at a time. using (AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201806 .AdGroupCriterionService)) { adGroupCriterionService.RequestHeader.validateOnly = true; int retryCount = 0; const int NUM_RETRIES = 3; try { while (retryCount < NUM_RETRIES) { try { // Validate the keywords. adGroupCriterionService.mutate(operations.ToArray()); break; } catch (AdWordsApiException e) { // Handle API errors. ApiException innerException = e.ApiException as ApiException; if (innerException == null) { throw new Exception( "Failed to retrieve ApiError. See inner exception " + "for more details.", e); } foreach (ApiError apiError in innerException.errors) { if (!(apiError is RateExceededError)) { // Rethrow any errors other than RateExceededError. throw; } // Handle rate exceeded errors. RateExceededError rateExceededError = (RateExceededError)apiError; Console.WriteLine( "Got Rate exceeded error - rate name = '{0}', " + "scope = '{1}', retry After {2} seconds.", rateExceededError.rateScope, rateExceededError.rateName, rateExceededError.retryAfterSeconds); Thread.Sleep(rateExceededError.retryAfterSeconds * 1000); retryCount = retryCount + 1; } } finally { if (retryCount == NUM_RETRIES) { throw new Exception(string.Format( "Could not recover after making {0} attempts.", retryCount)); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to validate keywords.", e); } } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService( AdWordsService.v201601.AdGroupCriterionService); List <AdGroupCriterionOperation> operations = new List <AdGroupCriterionOperation>(); foreach (string keywordText in KEYWORDS) { // Create the keyword. Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; // Create the biddable ad group criterion. BiddableAdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion(); keywordCriterion.adGroupId = adGroupId; keywordCriterion.criterion = keyword; // Optional: Set the user status. keywordCriterion.userStatus = UserStatus.PAUSED; // Optional: Set the keyword destination url. keywordCriterion.finalUrls = new UrlList() { urls = new string[] { "http://example.com/mars/cruise/?kw=" + HttpUtility.UrlEncode(keywordText) } }; // Create the operations. AdGroupCriterionOperation operation = new AdGroupCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = keywordCriterion; operations.Add(operation); } try { // Create the keywords. AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (retVal != null && retVal.value != null) { foreach (AdGroupCriterion adGroupCriterion in retVal.value) { // If you are adding multiple type of criteria, then you may need to // check for // // if (adGroupCriterion is Keyword) { ... } // // to identify the criterion type. Console.WriteLine("Keyword with ad group id = '{0}', keyword id = '{1}', text = " + "'{2}' and match type = '{3}' was created.", adGroupCriterion.adGroupId, adGroupCriterion.criterion.id, (adGroupCriterion.criterion as Keyword).text, (adGroupCriterion.criterion as Keyword).matchType); } } else { Console.WriteLine("No keywords were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create keywords.", e); } }
public async override Task RunAsync(AuthorizationData authorizationData) { try { CampaignManagementExampleHelper CampaignManagementExampleHelper = new CampaignManagementExampleHelper(this.OutputStatusMessage); CampaignManagementExampleHelper.CampaignManagementService = new ServiceClient <ICampaignManagementService>(authorizationData); // Before you can track conversions or target audiences using a remarketing list, // you need to create a UET tag in Bing Ads (web application or API) and then // add the UET tag tracking code to every page of your website. For more information, please see // Universal Event Tracking at https://docs.microsoft.com/en-us/bingads/guides/universal-event-tracking. // First you should call the GetUetTagsByIds operation to check whether a tag has already been created. // You can leave the TagIds element null or empty to request all UET tags available for the customer. var uetTags = (await CampaignManagementExampleHelper.GetUetTagsByIdsAsync(null))?.UetTags; // If you do not already have a UET tag that can be used, or if you need another UET tag, // call the AddUetTags service operation to create a new UET tag. If the call is successful, // the tracking script that you should add to your website is included in a corresponding // UetTag within the response message. if (uetTags == null || uetTags.Count < 1) { var uetTag = new UetTag { Description = "My First Uet Tag", Name = "New Uet Tag", }; uetTags = (await CampaignManagementExampleHelper.AddUetTagsAsync(new[] { uetTag })).UetTags; } if (uetTags == null || uetTags.Count < 1) { OutputStatusMessage( string.Format("You do not have any UET tags registered for CustomerId {0}.\n", authorizationData.CustomerId) ); return; } OutputStatusMessage("List of all UET Tags:\n"); foreach (var uetTag in uetTags) { CampaignManagementExampleHelper.OutputUetTag(uetTag); } // After you retreive the tracking script from the AddUetTags or GetUetTagsByIds operation, // the next step is to add the UET tag tracking code to your website. We recommend that you, // or your website administrator, add it to your entire website in either the head or body sections. // If your website has a master page, then that is the best place to add it because you add it once // and it is included on all pages. For more information, please see // Universal Event Tracking at https://docs.microsoft.com/en-us/bingads/guides/universal-event-tracking. // We will use the same UET tag for the remainder of this example. var tagId = uetTags[0].Id; // Add a remarketing list that depend on the UET Tag Id retreived above. var addAudiences = new[] { new RemarketingList { Description = "New list with CustomEventsRule", MembershipDuration = 30, Name = "Remarketing List with CustomEventsRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, Rule = new CustomEventsRule { // The type of user interaction you want to track. Action = "play", ActionOperator = StringOperator.Contains, // The category of event you want to track. Category = "video", CategoryOperator = StringOperator.Contains, // The name of the element that caused the action. Label = "trailer", LabelOperator = StringOperator.Contains, // A numerical value associated with that event. // Could be length of the video played etc. Value = 5.00m, ValueOperator = NumberOperator.Equals, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // ((Url Contains X) and (ReferrerUrl DoesNotContain Z)) or ((Url DoesNotBeginWith Y)) // or ((ReferrerUrl Equals Z)) Rule = new PageVisitorsRule { RuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsWhoDidNotVisitAnotherPageRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsWhoDidNotVisitAnotherPageRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // (((Url Contains X) and (ReferrerUrl DoesNotContain Z)) or ((Url DoesNotBeginWith Y)) // or ((ReferrerUrl Equals Z))) // and not (((Url BeginsWith A) and (ReferrerUrl BeginsWith B)) or ((Url Contains C))) Rule = new PageVisitorsWhoDidNotVisitAnotherPageRule { ExcludeRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.BeginsWith, Value = "A" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.BeginsWith, Value = "B" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "C" }, } }, }, IncludeRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, new RemarketingList { Description = "New list with PageVisitorsWhoVisitedAnotherPageRule", MembershipDuration = 30, Name = "Remarketing List with PageVisitorsWhoVisitedAnotherPageRule " + DateTime.UtcNow, ParentId = authorizationData.AccountId, // The rule definition is translated to the following logical expression: // (((Url Contains X) and (ReferrerUrl NotEquals Z)) or ((Url DoesNotBeginWith Y)) or // ((ReferrerUrl Equals Z))) // and (((Url BeginsWith A) and (ReferrerUrl BeginsWith B)) or ((Url Contains C))) Rule = new PageVisitorsWhoVisitedAnotherPageRule { AnotherRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.BeginsWith, Value = "A" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.BeginsWith, Value = "B" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "C" }, } }, }, RuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.DoesNotBeginWith, Value = "Y" }, } }, new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.Equals, Value = "Z" }, } }, }, }, Scope = EntityScope.Account, TagId = tagId }, }; // RemarketingList extends the Audience base class. // We manage remarketing lists with Audience operations. var addAudiencesResponse = await CampaignManagementExampleHelper.AddAudiencesAsync(addAudiences); var audienceIds = addAudiencesResponse.AudienceIds; // You must already have at least one remarketing list for the remainder of this example. if (audienceIds.Count < 1) { return; } var updateAudiences = new[] { new RemarketingList { Id = audienceIds[0], // The ParentId cannot be updated, even if you update the rule type. // You can either send the same value or leave ParentId empty. ParentId = authorizationData.AccountId, Rule = new CustomEventsRule { // For both add and update remarketing list operations, you must include one or more // of the following events: // Action, Category, Label, or Value. // For example if you do not include Action during update, // any existing ActionOperator and Action settings will be deleted. Action = null, //ActionOperator = null, Category = "video", CategoryOperator = StringOperator.Equals, // You cannot update the operator unless you also include the expression. // The following attempt to update LabelOperator will result in an error. Label = null, LabelOperator = StringOperator.Equals, // You must specify the previous settings unless you want // them replaced during the update conversion goal operation. Value = 5.00m, ValueOperator = NumberOperator.Equals, }, // The Scope cannot be updated, even if you update the rule type. // You can either send the same value or leave Scope empty. Scope = EntityScope.Account, // You can update the tag as needed. In this example we will explicitly use the same UET tag. // To keep the UET tag unchanged, you can also leave this element nil or empty. TagId = tagId, }, new RemarketingList { // You can change the remarketing rule type e.g. in this example a remarketing list // with the PageVisitorsRule had been created above at index 1. // Now we are using the returned identifier at index 1 to update the type from // PageVisitorsRule to PageVisitorsWhoDidNotVisitAnotherPageRule. Id = audienceIds[1], Rule = new PageVisitorsWhoDidNotVisitAnotherPageRule { // If you want to keep any of the previous rule items, // then you must explicitly set them again during update. ExcludeRuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "C" }, } }, }, // If you leave the entire list of rule item groups null, // then previous settings will be retained. IncludeRuleItemGroups = null, }, }, new RemarketingList { Id = audienceIds[2], Rule = new PageVisitorsRule { // If you want to keep any of the previous rule items, // then you must explicitly set them again during update. RuleItemGroups = new [] { new RuleItemGroup { Items = new [] { new StringRuleItem { Operand = "Url", Operator = StringOperator.Contains, Value = "X" }, new StringRuleItem { Operand = "ReferrerUrl", Operator = StringOperator.DoesNotContain, Value = "Z" }, } }, }, }, }, new RemarketingList { Id = audienceIds[3], MembershipDuration = 20, // If not specified during update, the previous rule settings are retained. Rule = null, }, }; var updateAudiencesResponse = await CampaignManagementExampleHelper.UpdateAudiencesAsync(updateAudiences); OutputStatusMessage("Updated audiences. List of errors (if applicable):\n"); CampaignManagementExampleHelper.OutputArrayOfBatchError(updateAudiencesResponse.PartialErrors); // Add an ad group in a campaign. The ad group will later be associated with remarketing lists. var campaigns = new[] { new Campaign { Name = "Women's Shoes " + DateTime.UtcNow, Description = "Red shoes line.", // You must choose to set either the shared budget ID or daily amount. // You can set one or the other, but you may not set both. BudgetId = null, DailyBudget = 50, BudgetType = BudgetLimitType.DailyBudgetStandard, BiddingScheme = new EnhancedCpcBiddingScheme(), TimeZone = "PacificTimeUSCanadaTijuana", }, }; var adGroups = new[] { new AdGroup { Name = "Women's Red Shoe Sale", StartDate = null, EndDate = new Date { Month = 12, Day = 31, Year = DateTime.UtcNow.Year + 1 }, CpcBid = new Bid { Amount = 0.09 }, Language = "English", TrackingUrlTemplate = null, // Applicable for all remarketing lists that are associated with this ad group. TargetAndBid indicates // that you want to show ads only to people included in the remarketing list, with the option to change // the bid amount. Ads in this ad group will only show to people included in the remarketing list. Settings = new[] { new TargetSetting { // Each target setting detail is delimited by a semicolon (;) in the Bulk file Details = new [] { new TargetSettingDetail { CriterionTypeGroup = CriterionTypeGroup.Audience, TargetAndBid = true } } } }, } }; AddCampaignsResponse addCampaignsResponse = await CampaignManagementExampleHelper.AddCampaignsAsync(authorizationData.AccountId, campaigns); long?[] campaignIds = addCampaignsResponse.CampaignIds.ToArray(); BatchError[] campaignErrors = addCampaignsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(campaignIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(campaignErrors); AddAdGroupsResponse addAdGroupsResponse = await CampaignManagementExampleHelper.AddAdGroupsAsync((long)campaignIds[0], adGroups, null); long?[] adGroupIds = addAdGroupsResponse.AdGroupIds.ToArray(); BatchError[] adGroupErrors = addAdGroupsResponse.PartialErrors.ToArray(); CampaignManagementExampleHelper.OutputArrayOfLong(adGroupIds); CampaignManagementExampleHelper.OutputArrayOfBatchError(adGroupErrors); // If the campaign or ad group add operations failed then we cannot continue this example. if (adGroupIds == null || adGroupIds.Length < 1) { return; } var adGroupRemarketingListAssociations = new List <AdGroupCriterion>(); // This example associates all of the remarketing lists with the new ad group. var getAudienceIds = new List <long>(); foreach (var listId in audienceIds) { getAudienceIds.Add((long)listId); } var remarketingLists = (await CampaignManagementExampleHelper.GetAudiencesByIdsAsync( getAudienceIds, AudienceType.RemarketingList)).Audiences; foreach (var remarketingList in remarketingLists) { if (remarketingList.Id != null) { var biddableAdGroupCriterion = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new AudienceCriterion { AudienceId = (long)remarketingList.Id, AudienceType = AudienceType.RemarketingList, }, CriterionBid = new BidMultiplier { Multiplier = 20.00, }, Status = AdGroupCriterionStatus.Active, }; adGroupRemarketingListAssociations.Add(biddableAdGroupCriterion); OutputStatusMessage(string.Format("Associating the following remarketing list with AdGroup Id {0}.\n", (long)adGroupIds[0])); CampaignManagementExampleHelper.OutputRemarketingList((RemarketingList)remarketingList); } } var addAdGroupCriterionsResponse = await CampaignManagementExampleHelper.AddAdGroupCriterionsAsync( adGroupRemarketingListAssociations, AdGroupCriterionType.Audience); var adGroupCriterionIds = new List <long>(); foreach (long id in addAdGroupCriterionsResponse.AdGroupCriterionIds) { adGroupCriterionIds.Add(id); } var getAdGroupCriterionsByIdsResponse = await CampaignManagementExampleHelper.GetAdGroupCriterionsByIdsAsync( adGroupCriterionIds, (long)adGroupIds[0], AdGroupCriterionType.RemarketingList); foreach (var adGroupRemarketingListAssociation in getAdGroupCriterionsByIdsResponse.AdGroupCriterions) { OutputStatusMessage("The following ad group remarketing list association was added.\n"); CampaignManagementExampleHelper.OutputArrayOfAdGroupCriterion(new AdGroupCriterion[] { adGroupRemarketingListAssociation }); } // You can store the association IDs which can be used to update or delete associations later. var nullableAdGroupCriterionIds = addAdGroupCriterionsResponse.AdGroupCriterionIds; // If the associations were added and retrieved successfully let's practice updating and deleting one of them. if (nullableAdGroupCriterionIds != null && nullableAdGroupCriterionIds.Count > 0) { var updateAdGroupRemarketingListAssociation = new BiddableAdGroupCriterion { AdGroupId = (long)adGroupIds[0], Criterion = new AudienceCriterion { AudienceType = AudienceType.RemarketingList, }, CriterionBid = new BidMultiplier { Multiplier = 10.00, }, Id = nullableAdGroupCriterionIds[0], Status = AdGroupCriterionStatus.Active, }; var updateAdGroupCriterionsResponse = await CampaignManagementExampleHelper.UpdateAdGroupCriterionsAsync( new BiddableAdGroupCriterion[] { updateAdGroupRemarketingListAssociation }, AdGroupCriterionType.Audience ); var deleteAdGroupCriterionsResponse = await CampaignManagementExampleHelper.DeleteAdGroupCriterionsAsync( adGroupCriterionIds, (long)adGroupIds[0], AdGroupCriterionType.Audience ); } // Delete the campaign, ad group, and ad group remarketing list associations that were previously added. var deleteCampaignsResponse = (await CampaignManagementExampleHelper.DeleteCampaignsAsync(authorizationData.AccountId, new[] { (long)campaignIds[0] })); OutputStatusMessage(string.Format("Deleted Campaign Id {0}\n", campaignIds[0])); // Delete the remarketing list. var deleteAudiencesResponse = (await CampaignManagementExampleHelper.DeleteAudiencesAsync(new[] { (long)audienceIds[0] })); OutputStatusMessage(string.Format("Deleted Audience Id {0}\n", audienceIds[0])); } // Catch authentication exceptions catch (OAuthTokenRequestException ex) { OutputStatusMessage(string.Format("Couldn't get OAuth tokens. Error: {0}. Description: {1}", ex.Details.Error, ex.Details.Description)); } // Catch Campaign Management service exceptions catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.AdApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.Errors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.ApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (FaultException <Microsoft.BingAds.V12.CampaignManagement.EditorialApiFaultDetail> ex) { OutputStatusMessage(string.Join("; ", ex.Detail.OperationErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); OutputStatusMessage(string.Join("; ", ex.Detail.BatchErrors.Select(error => string.Format("{0}: {1}", error.Code, error.Message)))); } catch (Exception ex) { OutputStatusMessage(ex.Message); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which keywords are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201509.AdGroupCriterionService); // Set partial failure mode for the service. adGroupCriterionService.RequestHeader.partialFailure = true; List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>(); // Create the placements. string[] urls = new String[] {"http://mars.google.com", "http:/mars.google.com", "mars.google.com"}; foreach (String url in urls) { Placement placement = new Placement(); placement.url = url; // Create biddable ad group criterion. BiddableAdGroupCriterion placementBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); placementBiddableAdGroupCriterion.adGroupId = adGroupId; placementBiddableAdGroupCriterion.criterion = placement; // Create the operation. AdGroupCriterionOperation placementAdGroupCriterionOperation = new AdGroupCriterionOperation(); placementAdGroupCriterionOperation.operand = placementBiddableAdGroupCriterion; placementAdGroupCriterionOperation.@operator = Operator.ADD; operations.Add(placementAdGroupCriterionOperation); } try { // Create the placements. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.ToArray()); // Display the results. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { if (adGroupCriterionResult.criterion != null) { Console.WriteLine("Placement with ad group id '{0}', and criterion " + "id '{1}', and url '{2}' was added.\n", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, ((Placement) adGroupCriterionResult.criterion).url); } } } else { Console.WriteLine("No placements were added."); } // Display the partial failure errors. if (result != null && result.partialFailureErrors != null) { foreach (ApiError apiError in result.partialFailureErrors) { int operationIndex = ErrorUtilities.GetOperationIndex(apiError.fieldPath); if (operationIndex != -1) { AdGroupCriterion adGroupCriterion = operations[operationIndex].operand; Console.WriteLine("Placement with ad group id '{0}' and url '{1}' " + "triggered a failure for the following reason: '{2}'.\n", adGroupCriterion.adGroupId, ((Placement) adGroupCriterion.criterion).url, apiError.errorString); } else { Console.WriteLine("A failure for the following reason: '{0}' has occurred.\n", apiError.errorString); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to add placements in partial failure mode.", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the campaign to which experiments are /// added.</param> /// <param name="adGroupId">Id of the ad group to which experiments are /// added.</param> /// <param name="criterionId">Id of the criterion for which experiments /// are added.</param> public void Run(AdWordsUser user, long campaignId, long adGroupId, long criterionId) { // Get the ExperimentService. ExperimentService experimentService = (ExperimentService) user.GetService(AdWordsService.v201601.ExperimentService); // Get the AdGroupService. AdGroupService adGroupService = (AdGroupService) user.GetService(AdWordsService.v201601.AdGroupService); // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201601.AdGroupCriterionService); // Create the experiment. Experiment experiment = new Experiment(); experiment.campaignId = campaignId; experiment.name = "Interplanetary Cruise #" + ExampleUtilities.GetRandomString(); experiment.queryPercentage = 10; experiment.startDateTime = DateTime.Now.AddDays(1).ToString("yyyyMMdd HHmmss"); // Optional: Set the end date. experiment.endDateTime = DateTime.Now.AddDays(30).ToString("yyyyMMdd HHmmss"); // Optional: Set the status. experiment.status = ExperimentStatus.ENABLED; // Create the operation. ExperimentOperation experimentOperation = new ExperimentOperation(); experimentOperation.@operator = Operator.ADD; experimentOperation.operand = experiment; try { // Add the experiment. ExperimentReturnValue experimentRetVal = experimentService.mutate( new ExperimentOperation[] {experimentOperation}); // Display the results. if (experimentRetVal != null && experimentRetVal.value != null && experimentRetVal.value. Length > 0) { long experimentId = 0; Experiment newExperiment = experimentRetVal.value[0]; Console.WriteLine("Experiment with name = \"{0}\" and id = \"{1}\" was added.\n", newExperiment.name, newExperiment.id); experimentId = newExperiment.id; // Set ad group for the experiment. AdGroup adGroup = new AdGroup(); adGroup.id = adGroupId; // Create experiment bid multiplier rule that will modify ad group bid // for the experiment. ManualCPCAdGroupExperimentBidMultipliers adGroupBidMultiplier = new ManualCPCAdGroupExperimentBidMultipliers(); adGroupBidMultiplier.maxCpcMultiplier = new BidMultiplier(); adGroupBidMultiplier.maxCpcMultiplier.multiplier = 1.5; // Set experiment data to the ad group. AdGroupExperimentData adGroupExperimentData = new AdGroupExperimentData(); adGroupExperimentData.experimentId = experimentId; adGroupExperimentData.experimentDeltaStatus = ExperimentDeltaStatus.MODIFIED; adGroupExperimentData.experimentBidMultipliers = adGroupBidMultiplier; adGroup.experimentData = adGroupExperimentData; // Create the operation. AdGroupOperation adGroupOperation = new AdGroupOperation(); adGroupOperation.operand = adGroup; adGroupOperation.@operator = Operator.SET; // Update the ad group. AdGroupReturnValue adGroupRetVal = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation}); // Display the results. if (adGroupRetVal != null && adGroupRetVal.value != null && adGroupRetVal.value.Length > 0) { AdGroup updatedAdGroup = adGroupRetVal.value[0]; Console.WriteLine("Ad group with name = \"{0}\", id = \"{1}\" and status = \"{2}\" " + "was updated for the experiment.\n", updatedAdGroup.name, updatedAdGroup.id, updatedAdGroup.status); } else { Console.WriteLine("No ad groups were updated."); } // Set ad group criteria for the experiment. Criterion criterion = new Criterion(); criterion.id = criterionId; BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion(); adGroupCriterion.adGroupId = adGroupId; adGroupCriterion.criterion = criterion; // Create experiment bid multiplier rule that will modify criterion bid // for the experiment. ManualCPCAdGroupCriterionExperimentBidMultiplier bidMultiplier = new ManualCPCAdGroupCriterionExperimentBidMultiplier(); bidMultiplier.maxCpcMultiplier = new BidMultiplier(); bidMultiplier.maxCpcMultiplier.multiplier = 1.5; // Set experiment data to the criterion. BiddableAdGroupCriterionExperimentData adGroupCriterionExperimentData = new BiddableAdGroupCriterionExperimentData(); adGroupCriterionExperimentData.experimentId = experimentId; adGroupCriterionExperimentData.experimentDeltaStatus = ExperimentDeltaStatus.MODIFIED; adGroupCriterionExperimentData.experimentBidMultiplier = bidMultiplier; adGroupCriterion.experimentData = adGroupCriterionExperimentData; // Create the operation. AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation(); adGroupCriterionOperation.operand = adGroupCriterion; adGroupCriterionOperation.@operator = Operator.SET; // Update the ad group criteria. AdGroupCriterionReturnValue adGroupCriterionRetVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] {adGroupCriterionOperation}); // Display the results. if (adGroupCriterionRetVal != null && adGroupCriterionRetVal.value != null && adGroupCriterionRetVal.value.Length > 0) { AdGroupCriterion updatedAdGroupCriterion = adGroupCriterionRetVal.value[0]; Console.WriteLine("Ad group criterion with ad group id = \"{0}\", criterion id = " + "\"{1}\" and type = \"{2}\" was updated for the experiment.\n", updatedAdGroupCriterion.adGroupId, updatedAdGroupCriterion.criterion.id, updatedAdGroupCriterion.criterion.CriterionType); } else { Console.WriteLine("No ad group criteria were updated."); } } else { Console.WriteLine("No experiments were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to add experiment.", e); } }
/// <summary> /// Creates a subdivision node. /// </summary> /// <param name="parent">The node that should be this node's parent. /// </param> /// <param name="value">The value being paritioned on.</param> /// <returns>A new subdivision node.</returns> public ProductPartition CreateSubdivision(ProductPartition parent, ProductDimension value) { ProductPartition division = new ProductPartition(); division.partitionType = ProductPartitionType.SUBDIVISION; division.id = this.nextId--; // The root node has neither a parent nor a value. if (parent != null) { division.parentCriterionId = parent.id; division.caseValue = value; } BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion(); criterion.adGroupId = this.adGroupId; criterion.criterion = division; this.CreateAddOperation(criterion); return division; }
/// <summary> /// Sets the Add action for a new BiddableAdGroupCriterion corresponding to the specified ProductCondition, /// and adds it to the helper's list of BulkAdGroupProductPartition. /// </summary> /// <param name="parent">The parent of the product partition subdivision that you want to add.</param> /// <param name="condition">The condition or product filter for the new product partition.</param> /// <param name="clientId">The Client Id in the bulk upload file corresponding to the product partition.</param> /// <returns>The BulkAdGroupProductPartition that was added to the list of PartitionActions.</returns> public BulkAdGroupProductPartition AddSubdivision( BulkAdGroupProductPartition parent, ProductCondition condition, string clientId ) { var biddableAdGroupCriterion = new BiddableAdGroupCriterion() { Id = this.referenceId--, Criterion = new ProductPartition() { // If the root node is a unit, it would not have a parent ParentCriterionId = parent != null && parent.AdGroupCriterion != null ? parent.AdGroupCriterion.Id : null, Condition = condition, PartitionType = ProductPartitionType.Subdivision }, CriterionBid = null, AdGroupId = this.adGroupId }; var partitionAction = new BulkAdGroupProductPartition() { ClientId = clientId, AdGroupCriterion = biddableAdGroupCriterion }; this.partitionActions.Add(partitionAction); return partitionAction; }
/// <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; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad groups to which keywords are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the MutateJobService. MutateJobService mutateJobService = (MutateJobService) user.GetService( AdWordsService.v201509.MutateJobService); const int RETRY_INTERVAL = 30; const int RETRIES_COUNT = 30; const int KEYWORD_NUMBER = 100; const string INDEX_REGEX = "operations\\[(\\d+)\\].operand"; List<Operation> operations = new List<Operation>(); // Create AdGroupCriterionOperation to add keywords. for (int i = 0; i < KEYWORD_NUMBER; i++) { Keyword keyword = new Keyword(); keyword.text = string.Format("mars cruise {0}", i); keyword.matchType = KeywordMatchType.BROAD; BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion(); criterion.adGroupId = adGroupId; criterion.criterion = keyword; AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation(); adGroupCriterionOperation.@operator = Operator.ADD; adGroupCriterionOperation.operand = criterion; operations.Add(adGroupCriterionOperation); } BulkMutateJobPolicy policy = new BulkMutateJobPolicy(); // You can specify up to 3 job IDs that must successfully complete before // this job can be processed. policy.prerequisiteJobIds = new long[] {}; SimpleMutateJob job = mutateJobService.mutate(operations.ToArray(), policy); // Wait for the job to complete. bool completed = false; int retryCount = 0; Console.WriteLine("Retrieving job status..."); while (completed == false && retryCount < RETRIES_COUNT) { BulkMutateJobSelector selector = new BulkMutateJobSelector(); selector.jobIds = new long[] {job.id}; try { Job[] allJobs = mutateJobService.get(selector); if (allJobs != null && allJobs.Length > 0) { job = (SimpleMutateJob) allJobs[0]; if (job.status == BasicJobStatus.COMPLETED || job.status == BasicJobStatus.FAILED) { completed = true; break; } else { Console.WriteLine("{0}: Current status is {1}, waiting {2} seconds to retry...", retryCount, job.status, RETRY_INTERVAL); Thread.Sleep(RETRY_INTERVAL * 1000); retryCount++; } } } catch (Exception e) { throw new System.ApplicationException("Failed to fetch simple mutate job with " + "id = {0}.", e); } } if (job.status == BasicJobStatus.COMPLETED) { // Handle cases where the job completed. // Create the job selector. BulkMutateJobSelector selector = new BulkMutateJobSelector(); selector.jobIds = new long[] {job.id}; // Get the job results. JobResult jobResult = mutateJobService.getResult(selector); if (jobResult != null) { SimpleMutateResult results = (SimpleMutateResult) jobResult.Item; if (results != null) { // Display the results. if (results.results != null) { for (int i = 0; i < results.results.Length; i++) { Operand operand = results.results[i]; Console.WriteLine("Operation {0} - {1}", i, (operand.Item is PlaceHolder) ? "FAILED" : "SUCCEEDED"); } } // Display the errors. if (results.errors != null) { foreach (ApiError apiError in results.errors) { Match match = Regex.Match(apiError.fieldPath, INDEX_REGEX, RegexOptions.IgnoreCase); string index = (match.Success)? match.Groups[1].Value : "???"; Console.WriteLine("Operation index {0} failed due to reason: '{1}', " + "trigger: '{2}'", index, apiError.errorString, apiError.trigger); } } } } Console.WriteLine("Job completed successfully!"); } else if (job.status == BasicJobStatus.FAILED) { // Handle the cases where job failed. Console.WriteLine("Job failed with reason: " + job.failureReason); } else if (job.status == BasicJobStatus.PROCESSING || job.status == BasicJobStatus.PENDING) { // Handle the cases where job didn't complete after wait period. Console.WriteLine("Job did not complete in {0} secconds.", RETRY_INTERVAL * RETRIES_COUNT); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which criteria are /// added.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201609.AdGroupCriterionService); // Create biddable ad group criterion for gender Gender genderTarget = new Gender(); // Criterion Id for male. The IDs can be found here // https://developers.google.com/adwords/api/docs/appendix/genders genderTarget.id = 10; BiddableAdGroupCriterion genderBiddableAdGroupCriterion = new BiddableAdGroupCriterion(); genderBiddableAdGroupCriterion.adGroupId = adGroupId; genderBiddableAdGroupCriterion.criterion = genderTarget; // Create negative ad group criterion for age range AgeRange ageRangeNegative = new AgeRange(); // Criterion Id for age 18 to 24. The IDs can be found here // https://developers.google.com/adwords/api/docs/appendix/ages ageRangeNegative.id = 503001; NegativeAdGroupCriterion ageRangeNegativeAdGroupCriterion = new NegativeAdGroupCriterion(); ageRangeNegativeAdGroupCriterion.adGroupId = adGroupId; ageRangeNegativeAdGroupCriterion.criterion = ageRangeNegative; // Create operations. AdGroupCriterionOperation genderBiddableAdGroupCriterionOperation = new AdGroupCriterionOperation(); genderBiddableAdGroupCriterionOperation.operand = genderBiddableAdGroupCriterion; genderBiddableAdGroupCriterionOperation.@operator = Operator.ADD; AdGroupCriterionOperation ageRangeNegativeAdGroupCriterionOperation = new AdGroupCriterionOperation(); ageRangeNegativeAdGroupCriterionOperation.operand = ageRangeNegativeAdGroupCriterion; ageRangeNegativeAdGroupCriterionOperation.@operator = Operator.ADD; AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] { genderBiddableAdGroupCriterionOperation, ageRangeNegativeAdGroupCriterionOperation }; try { // Add ad group criteria. AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations); // Display ad group criteria. if (result != null && result.value != null) { foreach (AdGroupCriterion adGroupCriterionResult in result.value) { Console.WriteLine("Ad group criterion with ad group id \"{0}\", criterion id " + "\"{1}\", and type \"{2}\" was added.", adGroupCriterionResult.adGroupId, adGroupCriterionResult.criterion.id, adGroupCriterionResult.criterion.CriterionType); } } else { Console.WriteLine("No ad group criteria were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create ad group criteria.", e); } }
public static AdGroupCriterionReturnValue AddKeyWordsToAdGroup(AdWordsUser user, long adGroupId, string keyWords) { using (AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService( AdWordsService.v201710.AdGroupCriterionService)) { AdGroupCriterionReturnValue retVal = new AdGroupCriterionReturnValue(); List <AdGroupCriterionOperation> operations = new List <AdGroupCriterionOperation>(); string[] keyWordsArray = keyWords.Split(','); foreach (string keywordText in keyWordsArray) { // Create the keyword. Keyword keyword = new Keyword(); keyword.text = keywordText; keyword.matchType = KeywordMatchType.BROAD; // Create the biddable ad group criterion. BiddableAdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion(); keywordCriterion.adGroupId = adGroupId; keywordCriterion.criterion = keyword; // Optional: Set the user status. keywordCriterion.userStatus = UserStatus.ENABLED; // Create the operations. AdGroupCriterionOperation operation = new AdGroupCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = keywordCriterion; operations.Add(operation); } try { // Create the keywords. retVal = adGroupCriterionService.mutate( operations.ToArray()); } catch (AdWordsApiException e) { ApiException innerException = e.ApiException as ApiException; if (innerException == null) { throw new Exception("Failed to retrieve ApiError. See inner exception for more " + "details.", e); } // Examine each ApiError received from the server. foreach (ApiError apiError in innerException.errors) { int index = apiError.GetOperationIndex(); if (index == -1) { // This API error is not associated with an operand, so we cannot // recover from this error by removing one or more operations. // Rethrow the exception for manual inspection. throw; } // Handle policy violation errors. if (apiError is PolicyViolationError) { PolicyViolationError policyError = (PolicyViolationError)apiError; if (policyError.isExemptable) { // If the policy violation error is exemptable, add an exemption // request. List <ExemptionRequest> exemptionRequests = new List <ExemptionRequest>(); } else { // Policy violation error is not exemptable, remove this // operation from the list of operations. } } else { // This is not a policy violation error, remove this operation // from the list of operations. } } } catch (Exception e) { throw new System.ApplicationException("Failed to create keywords.", e); } return(retVal); } }
/// <summary> /// Set custom targeting for the page feed URLs based on a list of labels. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Ad group ID.</param> /// <param name="labelName">The label name.</param> /// <returns>The newly created webpage criterion.</returns> private static BiddableAdGroupCriterion AddDsaTargeting(AdWordsUser user, long adGroupId, string labelName) { using (AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201809 .AdGroupCriterionService)) { // Create a webpage criterion. Webpage webpage = new Webpage(); WebpageParameter parameter = new WebpageParameter { criterionName = "Test criterion" }; webpage.parameter = parameter; // Add a condition for label=specified_label_name. WebpageCondition condition = new WebpageCondition { operand = WebpageConditionOperand.CUSTOM_LABEL, argument = labelName }; parameter.conditions = new WebpageCondition[] { condition }; BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion { adGroupId = adGroupId, criterion = webpage }; // Set a custom bid for this criterion. BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration { bids = new Bids[] { new CpcBid() { bid = new Money() { microAmount = 1500000 } } } }; criterion.biddingStrategyConfiguration = biddingStrategyConfiguration; AdGroupCriterionOperation operation = new AdGroupCriterionOperation { operand = criterion, @operator = Operator.ADD }; try { AdGroupCriterionReturnValue retval = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] { operation }); BiddableAdGroupCriterion newCriterion = (BiddableAdGroupCriterion)retval.value[0]; Console.WriteLine( "Web page criterion with ID = {0} and status = {1} was created.", newCriterion.criterion.id, newCriterion.userStatus); return(newCriterion); } catch (Exception e) { throw new System.ApplicationException( "Failed to create webpage criterion for " + "custom page feed label.", e); } } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the campaign to which experiments are /// added.</param> /// <param name="adGroupId">Id of the ad group to which experiments are /// added.</param> /// <param name="criterionId">Id of the criterion for which experiments /// are added.</param> public void Run(AdWordsUser user, long campaignId, long adGroupId, long criterionId) { // Get the ExperimentService. ExperimentService experimentService = (ExperimentService)user.GetService(AdWordsService.v201502.ExperimentService); // Get the AdGroupService. AdGroupService adGroupService = (AdGroupService)user.GetService(AdWordsService.v201502.AdGroupService); // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201502.AdGroupCriterionService); // Create the experiment. Experiment experiment = new Experiment(); experiment.campaignId = campaignId; experiment.name = "Interplanetary Cruise #" + ExampleUtilities.GetRandomString(); experiment.queryPercentage = 10; experiment.startDateTime = DateTime.Now.AddDays(1).ToString("yyyyMMdd HHmmss"); // Optional: Set the end date. experiment.endDateTime = DateTime.Now.AddDays(30).ToString("yyyyMMdd HHmmss"); // Optional: Set the status. experiment.status = ExperimentStatus.ENABLED; // Create the operation. ExperimentOperation experimentOperation = new ExperimentOperation(); experimentOperation.@operator = Operator.ADD; experimentOperation.operand = experiment; try { // Add the experiment. ExperimentReturnValue experimentRetVal = experimentService.mutate( new ExperimentOperation[] { experimentOperation }); // Display the results. if (experimentRetVal != null && experimentRetVal.value != null && experimentRetVal.value. Length > 0) { long experimentId = 0; Experiment newExperiment = experimentRetVal.value[0]; Console.WriteLine("Experiment with name = \"{0}\" and id = \"{1}\" was added.\n", newExperiment.name, newExperiment.id); experimentId = newExperiment.id; // Set ad group for the experiment. AdGroup adGroup = new AdGroup(); adGroup.id = adGroupId; // Create experiment bid multiplier rule that will modify ad group bid // for the experiment. ManualCPCAdGroupExperimentBidMultipliers adGroupBidMultiplier = new ManualCPCAdGroupExperimentBidMultipliers(); adGroupBidMultiplier.maxCpcMultiplier = new BidMultiplier(); adGroupBidMultiplier.maxCpcMultiplier.multiplier = 1.5; // Set experiment data to the ad group. AdGroupExperimentData adGroupExperimentData = new AdGroupExperimentData(); adGroupExperimentData.experimentId = experimentId; adGroupExperimentData.experimentDeltaStatus = ExperimentDeltaStatus.MODIFIED; adGroupExperimentData.experimentBidMultipliers = adGroupBidMultiplier; adGroup.experimentData = adGroupExperimentData; // Create the operation. AdGroupOperation adGroupOperation = new AdGroupOperation(); adGroupOperation.operand = adGroup; adGroupOperation.@operator = Operator.SET; // Update the ad group. AdGroupReturnValue adGroupRetVal = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation }); // Display the results. if (adGroupRetVal != null && adGroupRetVal.value != null && adGroupRetVal.value.Length > 0) { AdGroup updatedAdGroup = adGroupRetVal.value[0]; Console.WriteLine("Ad group with name = \"{0}\", id = \"{1}\" and status = \"{2}\" " + "was updated for the experiment.\n", updatedAdGroup.name, updatedAdGroup.id, updatedAdGroup.status); } else { Console.WriteLine("No ad groups were updated."); } // Set ad group criteria for the experiment. Criterion criterion = new Criterion(); criterion.id = criterionId; BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion(); adGroupCriterion.adGroupId = adGroupId; adGroupCriterion.criterion = criterion; // Create experiment bid multiplier rule that will modify criterion bid // for the experiment. ManualCPCAdGroupCriterionExperimentBidMultiplier bidMultiplier = new ManualCPCAdGroupCriterionExperimentBidMultiplier(); bidMultiplier.maxCpcMultiplier = new BidMultiplier(); bidMultiplier.maxCpcMultiplier.multiplier = 1.5; // Set experiment data to the criterion. BiddableAdGroupCriterionExperimentData adGroupCriterionExperimentData = new BiddableAdGroupCriterionExperimentData(); adGroupCriterionExperimentData.experimentId = experimentId; adGroupCriterionExperimentData.experimentDeltaStatus = ExperimentDeltaStatus.MODIFIED; adGroupCriterionExperimentData.experimentBidMultiplier = bidMultiplier; adGroupCriterion.experimentData = adGroupCriterionExperimentData; // Create the operation. AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation(); adGroupCriterionOperation.operand = adGroupCriterion; adGroupCriterionOperation.@operator = Operator.SET; // Update the ad group criteria. AdGroupCriterionReturnValue adGroupCriterionRetVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] { adGroupCriterionOperation }); // Display the results. if (adGroupCriterionRetVal != null && adGroupCriterionRetVal.value != null && adGroupCriterionRetVal.value.Length > 0) { AdGroupCriterion updatedAdGroupCriterion = adGroupCriterionRetVal.value[0]; Console.WriteLine("Ad group criterion with ad group id = \"{0}\", criterion id = " + "\"{1}\" and type = \"{2}\" was updated for the experiment.\n", updatedAdGroupCriterion.adGroupId, updatedAdGroupCriterion.criterion.id, updatedAdGroupCriterion.criterion.CriterionType); } else { Console.WriteLine("No ad group criteria were updated."); } } else { Console.WriteLine("No experiments were added."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to add experiment.", ex); } }
/// <summary> /// Sets the Add action for a new AdGroupCriterion corresponding to the specified ProductCondition, /// and adds it to the helper's list of BulkAdGroupProductPartition. /// </summary> /// <param name="parent">The parent of the product partition unit that you want to add.</param> /// <param name="condition">The condition or product filter for the new product partition.</param> /// <param name="bidAmount">The bid amount for the new product partition.</param> /// <param name="isNegative">Indicates whether or not to add a NegativeAdGroupCriterion. /// The default value is false, in which case a BiddableAdGroupCriterion will be added.</param> /// <returns>The BulkAdGroupProductPartition that was added to the list of PartitionActions.</returns> public BulkAdGroupProductPartition AddUnit( BulkAdGroupProductPartition parent, ProductCondition condition, double bidAmount, bool isNegative, string clientId ) { AdGroupCriterion adGroupCriterion; if (isNegative) { adGroupCriterion = new NegativeAdGroupCriterion(); } else { adGroupCriterion = new BiddableAdGroupCriterion() { CriterionBid = new FixedBid() { Bid = new Bid() { Amount = bidAmount } }, DestinationUrl = "http://www.contoso.com/womenshoesale/?season=spring&promocode=PROMO123", }; } adGroupCriterion.Criterion = new ProductPartition() { // If the root node is a unit, it would not have a parent ParentCriterionId = parent != null && parent.AdGroupCriterion != null ? parent.AdGroupCriterion.Id : null, Condition = condition, PartitionType = ProductPartitionType.Unit }; adGroupCriterion.AdGroupId = this.adGroupId; var partitionAction = new BulkAdGroupProductPartition() { ClientId = clientId, AdGroupCriterion = adGroupCriterion }; this.partitionActions.Add(partitionAction); return partitionAction; }
/// <summary> /// /// </summary> /// <param name="UseSandbox"></param> /// <param name="Name"></param> /// <param name="Countries"></param> /// <param name="Languages"></param> /// <param name="BudgetPeriod"></param> /// <param name="BudgetAmount"></param> /// <param name="MaxAdGroups"></param> /// <param name="DisplayUrl"></param> /// <param name="destinationUrlPrefix"></param> public void CreateCampaignByKeywordDensityContentMatch(bool UseSandbox, Guid AffiliateSiteRefId, int BudgetPeriod, Moneyv200906 BudgetAmount, AdGroupCriterionMoneyv200906 KeywordMaxCpc, string DisplayUrl, int MaxAdGroups, int AdKeywordType, int MinKeywordDensity, int MaxKeywordDensity, int MinContentMatch, decimal maxApiUsageDollars) { // Create a user (reads headers from App.config file). AdWordsUser user = new AdWordsUser(); if (UseSandbox) user.UseSandbox(); // use sandbox AccountService accountService = (AccountService)user.GetService(ApiServices.v13.AccountService); string[] accounts = accountService.getClientAccounts(); try { #region Create Campaign PpcNetwork ppcNetwork = DataRepository.PpcNetworkProvider.GetByName("AdWords"); TList<PpcCampaign> Campaigns = DataRepository.PpcCampaignProvider.GetByPpcNetworkRefId(ppcNetwork.PpcNetworkRefId); foreach (PpcCampaign campaign in Campaigns) { // Target the campaign at CampaignServicev200906 campaignService = (com.google.api.adwords.v200906.CampaignService.CampaignService) user.GetService(ApiServices.v200906.CampaignService); // Create a new campaign with an ad group. First create a // campaign, so we can get its id. Campaignv200906 newCampaign = new Campaignv200906(); // The campaign name is optional. An error results if a campaign // of the same name already exists. newCampaign.name = campaign.CampaignName + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); // Set the campaign status to paused, we don't want to start // paying for this test. // Required: Set the campaign status. newCampaign.status = CampaignStatusv200906.ACTIVE; newCampaign.statusSpecified = true; // Required: Specify the currency and budget amount. Budget budget = new Budget(); BudgetAmount.microAmountSpecified = true; budget.amount = BudgetAmount; // Required: Specify the bidding strategy. newCampaign.biddingStrategy = new ManualCPC(); // Optional: Specify the budget period and delivery method. budget.periodSpecified = true; budget.period = BudgetBudgetPeriod.DAILY; budget.deliveryMethodSpecified = true; budget.deliveryMethod = BudgetBudgetDeliveryMethod.STANDARD; newCampaign.budget = budget; // Optional: Specify an endDate for the campaign. newCampaign.endDate = DateTime.Now.AddDays(campaign.DurationInDays).ToString("yyyyMMdd"); // Define an Add operation to add the campaign. CampaignOperation campaignOperation = new CampaignOperation(); campaignOperation.operatorSpecified = true; campaignOperation.@operator = CampaignOperatorv200906.ADD; campaignOperation.operand = newCampaign; try { CampaignReturnValue results = campaignService.mutate(new CampaignOperation[] { campaignOperation }); if (results != null && results.value != null && results.value.Length > 0) { newCampaign.id = results.value[0].id; newCampaign.idSpecified = true; Trace.TraceInformation( "New campaign with name = \"{0}\" and id = " + "\"{1}\" was created.", results.value[0].name, results.value[0].id); } } catch (Exception ex) { Trace.TraceError("Error:" + ex.Message); throw new Exception("Failed to create campaign. " + ex.Message); } #endregion #region Targeting CampaignTargetService service = (CampaignTargetService)user.GetService(ApiServices.v200906.CampaignTargetService); // Create a language target - for English language. LanguageTargetv200906 languageTarget = new LanguageTargetv200906(); languageTarget.languageCode = "en"; //TODO: Add as property LanguageTargetList languageTargetList = new LanguageTargetList(); languageTargetList.targets = new LanguageTargetv200906[] { languageTarget }; languageTargetList.campaignId = newCampaign.id; languageTargetList.campaignIdSpecified = true; // Create a country target - include US, exclude metrocode 743. CountryTargetv200906 countryTarget = new CountryTargetv200906(); countryTarget.countryCode = campaign.TargetCountry; countryTarget.excludedSpecified = true; countryTarget.excluded = false; MetroTargetv200906 metroTarget = new MetroTargetv200906(); metroTarget.excludedSpecified = true; metroTarget.excluded = true; metroTarget.metroCode = campaign.ExcludeMetroTarget; GeoTargetList geoTargetList = new GeoTargetList(); geoTargetList.targets = new GeoTargetv200906[] { countryTarget, metroTarget }; geoTargetList.campaignId = newCampaign.id; geoTargetList.campaignIdSpecified = true; // Create a network target - Google Search. NetworkTargetv200906 networkTarget1 = new NetworkTargetv200906(); networkTarget1.networkCoverageTypeSpecified = true; networkTarget1.networkCoverageType = NetworkCoverageTypev200906.GOOGLE_SEARCH; NetworkTargetv200906 networkTarget2 = new NetworkTargetv200906(); networkTarget2.networkCoverageTypeSpecified = true; networkTarget2.networkCoverageType = NetworkCoverageTypev200906.SEARCH_NETWORK; NetworkTargetList networkTargetList = new NetworkTargetList(); networkTargetList.targets = new NetworkTargetv200906[] { networkTarget1, networkTarget2 }; networkTargetList.campaignId = newCampaign.id; networkTargetList.campaignIdSpecified = true; TargetList[] targets = new TargetList[] { languageTargetList, geoTargetList, networkTargetList }; ArrayList campaignTargetOperations = new ArrayList(); foreach (TargetList target in targets) { CampaignTargetOperation ops = new CampaignTargetOperation(); ops.operatorSpecified = true; ops.@operator = CampaignTargetOperatorv200906.SET; ops.operand = target; campaignTargetOperations.Add(ops); } try { service.mutate((CampaignTargetOperation[]) campaignTargetOperations.ToArray(typeof(CampaignTargetOperation))); Trace.TraceInformation("Geo, language, and network targeting were " + "successfully added to campaign id = \"{0}\".", newCampaign.id); } catch (Exception ex) { Trace.TraceError("Failed to create campaign targeting. " + "Exception says \"{0}\"", ex.Message); } #endregion #region Create your Services //create your services List<SeedKeyword> keywords = new List<SeedKeyword>(); AdGroupAdServicev200906 adService = (AdGroupAdServicev200906)user.GetService(ApiServices.v200906.AdGroupAdService); KeywordToolService keywordToolService = (KeywordToolService)user.GetService(ApiServices.v13.KeywordToolService); AdGroupServicev200906 adgroupService = (AdGroupServicev200906)user.GetService(ApiServices.v200906.AdGroupService); TrafficEstimatorService trafficEstimatorService = (TrafficEstimatorService)user.GetService(ApiServices.v13.TrafficEstimatorService); #endregion #region Enumerate thru all the keywords by category AdGroupv200906 newAdGroup = null; foreach ( SiteCategory siteCategory in DataRepository.SiteCategoryProvider.GetByAffiliateSiteRefId(AffiliateSiteRefId)) { int adGroupCnt = 1; //enumerate thru all the keywords foreach ( KeywordUrLsDistinct keywordUrLsDistinct in DataRepository.KeywordUrLsDistinctProvider.GetBySiteCategoryRefId( siteCategory.SiteCategoryRefId)) { VList<KeywordDensity> keywordDensityList = DataRepository.KeywordDensityProvider.GetURLKeywordDensity(keywordUrLsDistinct.Url, MinKeywordDensity, MaxKeywordDensity). FindAllDistinct("SiteContent"); int GroupAdCount = 0; //check the avg keyword density if (keywordDensityList.Count >= MinContentMatch) { if (adGroupCnt == 1) { #region Ad AdGroup if (GroupAdCount >= MaxAdGroups) break; //Create an ad group by site category newAdGroup = new AdGroupv200906(); newAdGroup.name = siteCategory.Name; newAdGroup.campaignId = newCampaign.id; newAdGroup.campaignIdSpecified = true; //newAdGroup.campaignName = newCampaign.name; // Optional: set the status of adgroup. newAdGroup.statusSpecified = true; newAdGroup.status = AdGroupStatus.ENABLED; // Optional: Create a Manual CPC Bid. ManualCPCAdGroupBids bids = new ManualCPCAdGroupBids(); // Set the keyword content max cpc. bids.keywordContentMaxCpc = new Bid(); Money kwdContentMaxCpc = new Money(); kwdContentMaxCpc.microAmountSpecified = true; kwdContentMaxCpc.microAmount = KeywordMaxCpc.microAmount; bids.keywordContentMaxCpc.amount = kwdContentMaxCpc; // Set the keyword max cpc. bids.keywordMaxCpc = new Bid(); Money kwdMaxCpc = new Money(); kwdMaxCpc.microAmountSpecified = true; kwdMaxCpc.microAmount = KeywordMaxCpc.microAmount; bids.keywordMaxCpc.amount = kwdMaxCpc; // Set the manual bid to the adgroup. newAdGroup.bids = bids; AdGroupOperation adGroupOperation = new AdGroupOperation(); adGroupOperation.operatorSpecified = true; adGroupOperation.@operator = AddGroupOperatorv200906.ADD; adGroupOperation.operand = newAdGroup; try { AdGroupReturnValue results = adgroupService.mutate(new AdGroupOperation[] { adGroupOperation }); if (results != null && results.value != null && results.value.Length > 0) { newAdGroup.id = results.value[0].id; newAdGroup.idSpecified = true; Trace.TraceInformation( "New ad group with name = \"{0}\" and id = \"{1}\" was created.", results.value[0].name, results.value[0].id); } } catch (Exception ex) { Trace.TraceError("Failed to create ad group. Exception says \"{0}\"", ex.Message); } adGroupCnt++; #endregion } Trace.TraceInformation(keywordUrLsDistinct.Url); //Create an add for each product // // IMPORTANT: create an ad before adding keywords! Else the // minCpc will have a higher value. TList<PpcAdTemplate> ppcAdTemplateList = DataRepository.PpcAdTemplateProvider.GetByPpcCampaignRefId(campaign.PpcCampaignRefId); foreach (var ppcAdTemplate in ppcAdTemplateList) { TextAdv200906 newTextAd = new TextAdv200906(); Product prod = DataRepository.ProductProvider.GetByProductRefId( (Guid)keywordUrLsDistinct.ProductRefId); newTextAd.headline = StringUtils.ScrubProdName(prod.Name); while (newTextAd.headline.Length > 25) { // if one word longer than 25 chars if (newTextAd.headline.LastIndexOf(" ") < 0) continue; newTextAd.headline = newTextAd.headline.Substring(0, newTextAd.headline.LastIndexOf(" ")). Substring( 0, newTextAd.headline.Substring(0, newTextAd.headline.LastIndexOf(" ")) . LastIndexOf(" ")); } newTextAd.description1 = ppcAdTemplate.AdLine1.Replace(KeywordToken, keywordDensityList[0].Keywords).Replace(ProductNameToken, newTextAd.headline); newTextAd.description2 = ppcAdTemplate.AdLine2; //} newTextAd.displayUrl = DisplayUrl; newTextAd.url = keywordUrLsDistinct.Url; //don't add it yet, there is a check below to see if it meets criteria //SeedKeyword[] keywordsArray = new SeedKeyword[] { new SeedKeyword() }; //keywordsArray = keywords.ToArray(); // Associate this ad group with the newly created campaign. Send // the request to add the new ad group. try { //we found a keyword that meets criteria so ad the new Ad. AdGroupAd adGroupAd = new AdGroupAd(); adGroupAd.adGroupId = newAdGroup.id; adGroupAd.adGroupIdSpecified = true; adGroupAd.ad = newTextAd; AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation(); adGroupAdOperation.operatorSpecified = true; adGroupAdOperation.@operator = AddGroupAdOperatorv200906.ADD; adGroupAdOperation.operand = adGroupAd; AdGroupAdReturnValue result = null; try { result = adService.mutate(new AdGroupAdOperation[] { adGroupAdOperation }); if (result.value != null && result.value.Length > 0) { foreach (AdGroupAd tempAdGroupAd in result.value) { Trace.TraceInformation( String.Format( "New text ad with headline = \"{0}\" and id = \"{1}\" was created.", ((TextAdv200906)tempAdGroupAd.ad).headline, tempAdGroupAd.ad.id)); } } } catch (Exception ex) { Trace.TraceError("Failed to create Ad(s). Exception says \"{0}\"", ex.Message); } GroupAdCount++; Trace.TraceInformation("Text ad" + GroupAdCount + ": " + newTextAd.headline + " Text Line1:" + newTextAd.description1 + " Text Line2:" + newTextAd.description2); //Trace.TraceInformation("Text ad: " + newTextAd.headline + " Text Line1:" + newTextAd.description1 + " Text Line2:" + newTextAd.description2); } catch { //do nothing Trace.TraceError("***Text ad Failed:" + newTextAd.headline + " Text Line1:" + newTextAd.description1 + " Text Line2:" + newTextAd.description2); } } //Add the Product name as a whole phrase AdGroupCriterionServicev200906 criterionService = (AdGroupCriterionServicev200906) user.GetService(ApiServices.v200906.AdGroupCriterionService); foreach (KeywordDensity kd in keywordDensityList) { try { Keywordv200906 newKeyword = new Keywordv200906(); newKeyword.matchTypeSpecified = true; newKeyword.matchType = com.google.api.adwords.v200906.AdGroupCriterionService.KeywordMatchType. BROAD; newKeyword.text = kd.Keywords; BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion(); criterion.adGroupId = newAdGroup.id; criterion.adGroupIdSpecified = true; criterion.criterion = newKeyword; criterion.destinationUrl = kd.Url; //TODO: Use the Traffic Estimator to determine the // the maxCpc to use //newKeyword.maxCpc = KeywordMaxCpc; //newKeyword.maxCpcSpecified = true; var adGroupCriterionBids = new com.google.api.adwords.v200906.AdGroupCriterionService. ManualCPCAdGroupCriterionBids(); adGroupCriterionBids.maxCpc = new com.google.api.adwords.v200906.AdGroupCriterionService.Bid(); adGroupCriterionBids.maxCpc.amount = KeywordMaxCpc; criterion.bids = adGroupCriterionBids; AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation(); adGroupCriterionOperation.@operator = com.google.api.adwords.v200906.AdGroupCriterionService.Operator.ADD; adGroupCriterionOperation.operatorSpecified = true; adGroupCriterionOperation.operand = criterion; try { AdGroupCriterionReturnValue results = criterionService.mutate(new AdGroupCriterionOperation[] { adGroupCriterionOperation }); if (results != null && results.value != null && results.value.Length > 0) { Keywordv200906 result = results.value[0].criterion as Keywordv200906; Trace.TraceInformation( String.Format( "New keyword with text = \"{0}\" and id = \"{1}\" was created.", result.text, result.id)); } } catch (Exception ex) { Trace.TraceError( String.Format( "Failed to create keyword at Ad group level. Exception says \"{0}\"", ex.Message)); } } catch { //do nothing Trace.TraceError("***Add Criteria Failed: Keyword" + kd.Keywords); } } if (GroupAdCount >= MaxAdGroups) break; } } } #endregion } #region Check api usage // check api usage ApiUsage apiUsage = new ApiUsage(); APIQuotaValues aPIQuotaValues = apiUsage.GetApiUsage(UseSandbox); Trace.TraceInformation("FreeQuotaUsed:" + aPIQuotaValues.FreeQuotaUsed.ToString() + " FreeUnitsRemaining:" + aPIQuotaValues.FreeUnitsRemaining.ToString() + " SysDefinesQuotaCap:" + aPIQuotaValues.SysDefinesQuotaCap.ToString() + " TotalUsed:" + aPIQuotaValues.TotalUsed.ToString()); #endregion #region Log everything created //AdGroup[] adGroups = adgroupService.getAllAdGroups(campaignId); //foreach (AdGroup adGroup in adGroups) //{ // Trace.TraceInformation("Ad group: " + adGroup.name); // Ad[] ads = adService.getAllAds(new long[] { adGroup.id }); // foreach (Ad ad in ads) // { // if (ad is TextAd) // { // TextAd textAd = (TextAd)ad; // Trace.TraceInformation("Text ad: " + textAd.headline + " Text Line1:" + textAd.description1 + " Text Line2:" + textAd.description2); // } // } //} #endregion } catch (Exception ex) { Trace.TraceError("Error:" + ex.Message); throw ex; } }
/// <summary> /// Main method for the thread. /// </summary> /// <param name="obj">The thread parameter.</param> public void Run(Object obj) { // Create the operations. List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>(); for (int j = 0; j < NUM_KEYWORDS; j++) { // Create the keyword. Keyword keyword = new Keyword(); keyword.text = "mars cruise thread " + threadIndex.ToString() + " seed " + j.ToString(); keyword.matchType = KeywordMatchType.BROAD; // Create the biddable ad group criterion. AdGroupCriterion keywordCriterion = new BiddableAdGroupCriterion(); keywordCriterion.adGroupId = adGroupId; keywordCriterion.criterion = keyword; // Create the operations. AdGroupCriterionOperation keywordOperation = new AdGroupCriterionOperation(); keywordOperation.@operator = Operator.ADD; keywordOperation.operand = keywordCriterion; operations.Add(keywordOperation); } // Get the AdGroupCriterionService. This should be done within the // thread, since a service can only handle one outgoing HTTP request // at a time. AdGroupCriterionService service = (AdGroupCriterionService) user.GetService( AdWordsService.v201601.AdGroupCriterionService); service.RequestHeader.validateOnly = true; int retryCount = 0; const int NUM_RETRIES = 3; try { while (retryCount < NUM_RETRIES) { try { // Validate the keywords. AdGroupCriterionReturnValue retval = service.mutate(operations.ToArray()); break; } catch (AdWordsApiException e) { // Handle API errors. ApiException innerException = e.ApiException as ApiException; if (innerException == null) { throw new Exception("Failed to retrieve ApiError. See inner exception for more " + "details.", e); } foreach (ApiError apiError in innerException.errors) { if (!(apiError is RateExceededError)) { // Rethrow any errors other than RateExceededError. throw; } // Handle rate exceeded errors. RateExceededError rateExceededError = (RateExceededError) apiError; Console.WriteLine("Got Rate exceeded error - rate name = '{0}', scope = '{1}', " + "retry After {2} seconds.", rateExceededError.rateScope, rateExceededError.rateName, rateExceededError.retryAfterSeconds); Thread.Sleep(rateExceededError.retryAfterSeconds * 1000); retryCount = retryCount + 1; } } finally { if (retryCount == NUM_RETRIES) { throw new Exception(String.Format("Could not recover after making {0} attempts.", retryCount)); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to validate keywords.", e); } }
/// <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> /// Sets the Add action for a new BiddableAdGroupCriterion corresponding to the specified ProductCondition, /// and adds it to the helper's list of AdGroupCriterionAction. /// </summary> /// <param name="parent">The parent of the product partition subdivision that you want to add.</param> /// <param name="condition">The condition or product filter for the new product partition.</param> /// <returns>The ad group criterion that was added to the list of PartitionActions.</returns> public AdGroupCriterion AddSubdivision( AdGroupCriterion parent, ProductCondition condition ) { var biddableAdGroupCriterion = new BiddableAdGroupCriterion() { Id = this.referenceId--, Criterion = new ProductPartition() { // If the root node is a unit, it would not have a parent ParentCriterionId = parent != null ? parent.Id : null, Condition = condition, PartitionType = ProductPartitionType.Subdivision }, CriterionBid = null, AdGroupId = this.adGroupId }; var partitionAction = new AdGroupCriterionAction() { Action = ItemAction.Add, AdGroupCriterion = biddableAdGroupCriterion }; this.partitionActions.Add(partitionAction); return biddableAdGroupCriterion; }
/// <summary> /// Creates the placement. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">The adgroup id for which the placement is /// created.</param> /// <returns>The placement id.</returns> public long CreatePlacement(AdWordsUser user, long adGroupId) { AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService(AdWordsService.v201601.AdGroupCriterionService); Placement placement = new Placement(); placement.url = "http://mars.google.com"; AdGroupCriterion placementCriterion = new BiddableAdGroupCriterion(); placementCriterion.adGroupId = adGroupId; placementCriterion.criterion = placement; AdGroupCriterionOperation placementOperation = new AdGroupCriterionOperation(); placementOperation.@operator = Operator.ADD; placementOperation.operand = placementCriterion; AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] { placementOperation }); return retVal.value[0].criterion.id; }
/// <summary> /// Sets the Update action for the specified BiddableAdGroupCriterion, /// and adds it to the helper's list of AdGroupCriterionAction. /// You can only update the CriterionBid, DestinationUrl, Param1, Param2, and Param3 elements /// of the BiddableAdGroupCriterion. /// When working with product partitions, youu cannot update the Criterion (ProductPartition). /// To update a ProductPartition, you must delete the existing node (DeletePartition) and /// add a new one (AddUnit or AddSubdivision) during the same call to ApplyProductPartitionActions. /// </summary> /// <param name="biddableAdGroupCriterion">The biddable ad group criterion to update.</param> public void UpdatePartition(BiddableAdGroupCriterion biddableAdGroupCriterion) { biddableAdGroupCriterion.AdGroupId = this.adGroupId; var partitionAction = new AdGroupCriterionAction() { Action = ItemAction.Update, AdGroupCriterion = biddableAdGroupCriterion }; this.partitionActions.Add(partitionAction); return; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which placements are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService)user.GetService(AdWordsService.v201502.AdGroupCriterionService); // Create the placement. Placement placement1 = new Placement(); placement1.url = "http://mars.google.com"; // Create biddable ad group criterion. AdGroupCriterion placementCriterion1 = new BiddableAdGroupCriterion(); placementCriterion1.adGroupId = adGroupId; placementCriterion1.criterion = placement1; // Create the placement. Placement placement2 = new Placement(); placement2.url = "http://venus.google.com"; // Create biddable ad group criterion. AdGroupCriterion placementCriterion2 = new BiddableAdGroupCriterion(); placementCriterion2.adGroupId = adGroupId; placementCriterion2.criterion = placement2; // Create the operations. AdGroupCriterionOperation placementOperation1 = new AdGroupCriterionOperation(); placementOperation1.@operator = Operator.ADD; placementOperation1.operand = placementCriterion1; AdGroupCriterionOperation placementOperation2 = new AdGroupCriterionOperation(); placementOperation2.@operator = Operator.ADD; placementOperation2.operand = placementCriterion2; try { // Create the placements. AdGroupCriterionReturnValue retVal = adGroupCriterionService.mutate( new AdGroupCriterionOperation[] { placementOperation1, placementOperation2 }); // Display the results. if (retVal != null && retVal.value != null) { foreach (AdGroupCriterion adGroupCriterion in retVal.value) { // If you are adding multiple type of criteria, then you may need to // check for // // if (adGroupCriterion is Placement) { ... } // // to identify the criterion type. Console.WriteLine("Placement with ad group id = '{0}, placement id = '{1}, url = " + "'{2}' was created.", adGroupCriterion.adGroupId, adGroupCriterion.criterion.id, (adGroupCriterion.criterion as Placement).url); } } else { Console.WriteLine("No placements were added."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create placements.", e); } }