/// <summary> /// Checks a list of keywords for policy violations, and add the errors to /// a list of traffic estimates. /// </summary> /// <param name="user">The user.</param> /// <param name="trafficEstimates">The list of keywords and their traffic /// estimates.</param> /// <param name="adGroupId">The ad group ID.</param> /// <remarks>Users can use the policy violation details to decide whether /// to pick a keyword and submit an exemption request, or skip the /// violating keyword and scout for other keywords that are policy /// compliant.</remarks> private void CheckPolicyViolations(AdWordsUser user, List<TrafficEstimate> trafficEstimates, long adGroupId) { // Get the AdGroupCriterionService. AdGroupCriterionService adGroupCriterionService = (AdGroupCriterionService) user.GetService( AdWordsService.v201603.AdGroupCriterionService); adGroupCriterionService.RequestHeader.validateOnly = true; for (int i = 0; i < trafficEstimates.Count; i += Settings.AGCS_KEYWORDS_LIST_SIZE) { List<AdGroupCriterionOperation> operations = new List<AdGroupCriterionOperation>(); for (int j = i; j < i + Settings.AGCS_KEYWORDS_LIST_SIZE && j < trafficEstimates.Count; j++) { AdGroupCriterionOperation operation = new AdGroupCriterionOperation() { @operator = Operator.ADD, operand = new BiddableAdGroupCriterion() { adGroupId = adGroupId, criterion = new Keyword() { text = trafficEstimates[i].Keyword.KeywordText, matchType = trafficEstimates[i].MatchType, }, userStatus = UserStatus.ENABLED } }; operations.Add(operation); } try { AdGroupCriterionReturnValue 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 = ErrorUtilities.GetOperationIndex(apiError.fieldPath); 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; trafficEstimates[i + index].Errors.Add(policyError); } } } } return; }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which ads are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService)user.GetService(AdWordsService.v201605.AdGroupAdService); // Create the text ad. TextAd textAd = new TextAd(); textAd.headline = "Luxury Cruise to Mars"; textAd.description1 = "Visit the Red Planet in style."; textAd.description2 = "Low-gravity fun for everyone!!"; textAd.displayUrl = "www.example.com"; textAd.finalUrls = new string[] { "http://www.example.com" }; AdGroupAd textadGroupAd = new AdGroupAd(); textadGroupAd.adGroupId = adGroupId; textadGroupAd.ad = textAd; // Create the operations. AdGroupAdOperation textAdOperation = new AdGroupAdOperation(); textAdOperation.@operator = Operator.ADD; textAdOperation.operand = textadGroupAd; try { AdGroupAdReturnValue retVal = null; // Setup two arrays, one to hold the list of all operations to be // validated, and another to hold the list of operations that cannot be // fixed after validation. List <AdGroupAdOperation> allOperations = new List <AdGroupAdOperation>(); List <AdGroupAdOperation> operationsToBeRemoved = new List <AdGroupAdOperation>(); allOperations.Add(textAdOperation); try { // Validate the operations. service.RequestHeader.validateOnly = true; retVal = service.mutate(allOperations.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 = ErrorUtilities.GetOperationIndex(apiError.fieldPath); 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>(); if (allOperations[index].exemptionRequests != null) { exemptionRequests.AddRange(allOperations[index].exemptionRequests); } ExemptionRequest exemptionRequest = new ExemptionRequest(); exemptionRequest.key = policyError.key; exemptionRequests.Add(exemptionRequest); allOperations[index].exemptionRequests = exemptionRequests.ToArray(); } else { // Policy violation error is not exemptable, remove this // operation from the list of operations. operationsToBeRemoved.Add(allOperations[index]); } } else { // This is not a policy violation error, remove this operation // from the list of operations. operationsToBeRemoved.Add(allOperations[index]); } } // Remove all operations that aren't exemptable. foreach (AdGroupAdOperation operation in operationsToBeRemoved) { allOperations.Remove(operation); } } if (allOperations.Count > 0) { // Perform the operations exemptible of a policy violation. service.RequestHeader.validateOnly = false; retVal = service.mutate(allOperations.ToArray()); // Display the results. if (retVal != null && retVal.value != null && retVal.value.Length > 0) { foreach (AdGroupAd newAdGroupAd in retVal.value) { Console.WriteLine("New ad with id = \"{0}\" and displayUrl = \"{1}\" was created.", newAdGroupAd.ad.id, newAdGroupAd.ad.displayUrl); } } else { Console.WriteLine("No ads were created."); } } else { Console.WriteLine("There are no ads to create after policy violation checks."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create ads.", 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.v201607.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); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group to which ads are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService)user.GetService(AdWordsService.v201402.AdGroupAdService); // Create the third party redirect ad that violates a policy. ThirdPartyRedirectAd redirectAd = new ThirdPartyRedirectAd(); redirectAd.name = "Policy violation demo ad " + ExampleUtilities.GetRandomString(); redirectAd.url = "gopher://gopher.google.com"; redirectAd.dimensions = new Dimensions(); redirectAd.dimensions.width = 300; redirectAd.dimensions.height = 250; redirectAd.snippet = "<img src=\"https://sandbox.google.com/sandboximages/image.jpg\"/>"; redirectAd.impressionBeaconUrl = "http://www.examples.com/beacon1"; redirectAd.certifiedVendorFormatId = 119; redirectAd.isCookieTargeted = false; redirectAd.isUserInterestTargeted = false; redirectAd.isTagged = false; AdGroupAd redirectAdGroupAd = new AdGroupAd(); redirectAdGroupAd.adGroupId = adGroupId; redirectAdGroupAd.ad = redirectAd; // Create the operations. AdGroupAdOperation redirectAdOperation = new AdGroupAdOperation(); redirectAdOperation.@operator = Operator.ADD; redirectAdOperation.operand = redirectAdGroupAd; try { AdGroupAdReturnValue retVal = null; // Setup two arrays, one to hold the list of all operations to be // validated, and another to hold the list of operations that cannot be // fixed after validation. List <AdGroupAdOperation> allOperations = new List <AdGroupAdOperation>(); List <AdGroupAdOperation> operationsToBeRemoved = new List <AdGroupAdOperation>(); allOperations.Add(redirectAdOperation); try { // Validate the operations. service.RequestHeader.validateOnly = true; retVal = service.mutate(allOperations.ToArray()); } catch (AdWordsApiException ex) { ApiException innerException = ex.ApiException as ApiException; if (innerException == null) { throw new Exception("Failed to retrieve ApiError. See inner exception for more " + "details.", ex); } // Examine each ApiError received from the server. foreach (ApiError apiError in innerException.errors) { int index = ErrorUtilities.GetOperationIndex(apiError.fieldPath); 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>(); if (allOperations[index].exemptionRequests != null) { exemptionRequests.AddRange(allOperations[index].exemptionRequests); } ExemptionRequest exemptionRequest = new ExemptionRequest(); exemptionRequest.key = policyError.key; exemptionRequests.Add(exemptionRequest); allOperations[index].exemptionRequests = exemptionRequests.ToArray(); } else { // Policy violation error is not exemptable, remove this // operation from the list of operations. operationsToBeRemoved.Add(allOperations[index]); } } else { // This is not a policy violation error, remove this operation // from the list of operations. operationsToBeRemoved.Add(allOperations[index]); } } // Remove all operations that aren't exemptable. foreach (AdGroupAdOperation operation in operationsToBeRemoved) { allOperations.Remove(operation); } } if (allOperations.Count > 0) { // Perform the operations exemptible of a policy violation. service.RequestHeader.validateOnly = false; retVal = service.mutate(allOperations.ToArray()); // Display the results. if (retVal != null && retVal.value != null && retVal.value.Length > 0) { foreach (AdGroupAd newAdGroupAd in retVal.value) { Console.WriteLine("New ad with id = \"{0}\" and displayUrl = \"{1}\" was created.", newAdGroupAd.ad.id, newAdGroupAd.ad.displayUrl); } } else { Console.WriteLine("No ads were created."); } } else { Console.WriteLine("There are no ads to create after policy violation checks."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to create ads.", ex); } }
/// <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.v201502.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); } }