/// <summary> /// Adds the campaign targeting criteria to a campaign. /// </summary> /// <param name="user">The user.</param> /// <param name="campaignId">The campaign id.</param> /// <returns>The campaign criteria id.</returns> public long AddCampaignTargetingCriteria(AdWordsUser user, long campaignId) { // Get the CampaignCriterionService. CampaignCriterionService campaignCriterionService = (CampaignCriterionService) user.GetService( AdWordsService.v201306.CampaignCriterionService); // Create language criteria. // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html // for a detailed list of language codes. Language language1 = new Language(); language1.id = 1002; // French CampaignCriterion languageCriterion1 = new CampaignCriterion(); languageCriterion1.campaignId = campaignId; languageCriterion1.criterion = language1; CampaignCriterion[] criteria = new CampaignCriterion[] {languageCriterion1}; List<CampaignCriterionOperation> operations = new List<CampaignCriterionOperation>(); foreach (CampaignCriterion criterion in criteria) { CampaignCriterionOperation operation = new CampaignCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = criterion; operations.Add(operation); } CampaignCriterionReturnValue retVal = campaignCriterionService.mutate(operations.ToArray()); return retVal.value[0].criterion.id; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the campaign whose bid should be modified. /// </param> /// <param name="bidModifier">The bid modifier.</param> public void Run(AdWordsUser user, long campaignId, double bidModifier) { // Get the CampaignCriterionService. CampaignCriterionService campaignCriterionService = (CampaignCriterionService) user.GetService( AdWordsService.v201306.CampaignCriterionService); // Create mobile platform. The ID can be found in the documentation. // https://developers.google.com/adwords/api/docs/appendix/platforms Platform mobile = new Platform(); mobile.id = 30001; // Create criterion with modified bid. CampaignCriterion criterion = new CampaignCriterion(); criterion.campaignId = campaignId; criterion.criterion = mobile; criterion.bidModifier = bidModifier; // Create SET operation. CampaignCriterionOperation operation = new CampaignCriterionOperation(); operation.@operator = Operator.SET; operation.operand = criterion; try { // Update campaign criteria. CampaignCriterionReturnValue result = campaignCriterionService.mutate( new CampaignCriterionOperation[] {operation}); // Display campaign criteria. if (result.value != null) { foreach (CampaignCriterion newCriterion in result.value) { Console.WriteLine("Campaign criterion with campaign id '{0}', criterion id '{1}', " + "and type '{2}' was modified with bid {3:F2}.", newCriterion.campaignId, newCriterion.criterion.id, newCriterion.criterion.type, newCriterion.bidModifier); } } else { Console.WriteLine("No campaign criteria were modified."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to set bid modifier for campaign.", ex); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="campaignId">Id of the campaign to which targeting criteria /// are added.</param> public void Run(AdWordsUser user, long campaignId) { // Get the CampaignCriterionService. CampaignCriterionService campaignCriterionService = (CampaignCriterionService) user.GetService( AdWordsService.v201306.CampaignCriterionService); // Create language criteria. // See http://code.google.com/apis/adwords/docs/appendix/languagecodes.html // for a detailed list of language codes. Language language1 = new Language(); language1.id = 1002; // French CampaignCriterion languageCriterion1 = new CampaignCriterion(); languageCriterion1.campaignId = campaignId; languageCriterion1.criterion = language1; Language language2 = new Language(); language2.id = 1005; // Japanese CampaignCriterion languageCriterion2 = new CampaignCriterion(); languageCriterion2.campaignId = campaignId; languageCriterion2.criterion = language2; // Create location criteria. // See http://code.google.com/apis/adwords/docs/appendix/countrycodes.html // for a detailed list of country codes. Location location1 = new Location(); location1.id = 2840; // USA CampaignCriterion locationCriterion1 = new CampaignCriterion(); locationCriterion1.campaignId = campaignId; locationCriterion1.criterion = location1; Location location2 = new Location(); location2.id = 2392; // Japan CampaignCriterion locationCriterion2 = new CampaignCriterion(); locationCriterion2.campaignId = campaignId; locationCriterion2.criterion = location2; // Add a negative campaign placement. NegativeCampaignCriterion negativeCriterion = new NegativeCampaignCriterion(); negativeCriterion.campaignId = campaignId; Placement placement = new Placement(); placement.url = "http://mars.google.com"; negativeCriterion.criterion = placement; CampaignCriterion[] criteria = new CampaignCriterion[] {languageCriterion1, languageCriterion2, locationCriterion1, locationCriterion2, negativeCriterion}; List<CampaignCriterionOperation> operations = new List<CampaignCriterionOperation>(); foreach (CampaignCriterion criterion in criteria) { CampaignCriterionOperation operation = new CampaignCriterionOperation(); operation.@operator = Operator.ADD; operation.operand = criterion; operations.Add(operation); } try { // Set the campaign targets. CampaignCriterionReturnValue retVal = campaignCriterionService.mutate(operations.ToArray()); if (retVal != null && retVal.value != null) { // Display campaign targets. foreach (CampaignCriterion criterion in retVal.value) { Console.WriteLine("Campaign criteria of type '{0}' was set to campaign with" + " id = '{1}'.", criterion.criterion.CriterionType, criterion.campaignId); } } } catch (Exception ex) { throw new System.ApplicationException("Failed to set Campaign criteria.", ex); } }