/// <summary> /// Creates the Product Ad. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">The ad group ID.</param> /// <returns>The Product Ad.</returns> private static AdGroupAd CreateProductAd(AdWordsUser user, long adGroupId) { AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService( AdWordsService.v201710.AdGroupAdService); // Create product ad. ProductAd productAd = new ProductAd(); // Create ad group ad. AdGroupAd adGroupAd = new AdGroupAd(); adGroupAd.adGroupId = adGroupId; adGroupAd.ad = productAd; // Create operation. AdGroupAdOperation operation = new AdGroupAdOperation(); operation.operand = adGroupAd; operation.@operator = Operator.ADD; // Make the mutate request. AdGroupAdReturnValue retval = adGroupAdService.mutate( new AdGroupAdOperation[] { operation }); adGroupAdService.Close(); return(retval.value[0]); }
/// <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) { using (AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(AdWordsService.v201802.AdGroupAdService)) { List <AdGroupAdOperation> operations = new List <AdGroupAdOperation>(); for (int i = 0; i < NUMBER_OF_ADS; i++) { // Create the expanded text ad. ExpandedTextAd expandedTextAd = new ExpandedTextAd { headlinePart1 = "Cruise #" + i.ToString() + " to Mars", headlinePart2 = "Best Space Cruise Line", description = "Buy your tickets now!", finalUrls = new string[] { "http://www.example.com/" + i } }; AdGroupAd expandedTextAdGroupAd = new AdGroupAd { adGroupId = adGroupId, ad = expandedTextAd, // Optional: Set the status. status = AdGroupAdStatus.PAUSED }; // Create the operation. AdGroupAdOperation operation = new AdGroupAdOperation { @operator = Operator.ADD, operand = expandedTextAdGroupAd }; operations.Add(operation); } AdGroupAdReturnValue retVal = null; try { // Create the ads. retVal = adGroupAdService.mutate(operations.ToArray()); // Display the results. if (retVal != null && retVal.value != null) { foreach (AdGroupAd adGroupAd in retVal.value) { ExpandedTextAd newAd = adGroupAd.ad as ExpandedTextAd; Console.WriteLine("Expanded text ad with ID '{0}' and headline '{1} - {2}' " + "was added.", newAd.id, newAd.headlinePart1, newAd.headlinePart2); } } else { Console.WriteLine("No expanded text ads were created."); } adGroupAdService.Close(); } catch (Exception e) { throw new System.ApplicationException("Failed to create expanded text ad.", e); } } }
/// <summary> /// Creates the Showcase ad. /// </summary> /// <param name="user">The AdWords user for which the ad is created.</param> /// <param name="adGroup">The ad group in which the ad is created.</param> /// <returns>The newly created Showcase ad.</returns> private static AdGroupAd CreateShowcaseAd(AdWordsUser user, AdGroup adGroup) { AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService( AdWordsService.v201710.AdGroupAdService); // Create the Showcase ad. ShowcaseAd showcaseAd = new ShowcaseAd(); // Required: set the ad's name, final URLs and display URL. showcaseAd.name = "Showcase ad " + ExampleUtilities.GetShortRandomString(); showcaseAd.finalUrls = new string[] { "http://example.com/showcase" }; showcaseAd.displayUrl = "example.com"; // Required: Set the ad's expanded image. Image expandedImage = new Image(); expandedImage.mediaId = UploadImage(user, "https://goo.gl/IfVlpF"); showcaseAd.expandedImage = expandedImage; // Optional: Set the collapsed image. Image collapsedImage = new Image(); collapsedImage.mediaId = UploadImage(user, "https://goo.gl/NqTxAE"); showcaseAd.collapsedImage = collapsedImage; // Create ad group ad. AdGroupAd adGroupAd = new AdGroupAd(); adGroupAd.adGroupId = adGroup.id; adGroupAd.ad = showcaseAd; // Create operation. AdGroupAdOperation operation = new AdGroupAdOperation(); operation.operand = adGroupAd; operation.@operator = Operator.ADD; // Make the mutate request. AdGroupAdReturnValue retval = adGroupAdService.mutate( new AdGroupAdOperation[] { operation }); adGroupAdService.Close(); return(retval.value[0]); }
public static AdGroupAdReturnValue CreateTextAds(AdWordsUser user, AdWordsContentLo adWordsContent) { using (AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(AdWordsService.v201710.AdGroupAdService)) { List <AdGroupAdOperation> operations = new List <AdGroupAdOperation>(); for (int i = 0; i < adWordsContent.ContentProducts.Count; i++) { // Create the expanded text ad. ExpandedTextAd expandedTextAd = new ExpandedTextAd(); expandedTextAd.headlinePart1 = adWordsContent.ContentProducts[i].AdContent.HeadLinePart1; expandedTextAd.headlinePart2 = adWordsContent.ContentProducts[i].AdContent.HeadLinePart2; expandedTextAd.path1 = adWordsContent.ContentProducts[i].AdContent.Path1 != "" ? adWordsContent.ContentProducts[i].AdContent.Path1 : ""; expandedTextAd.path2 = adWordsContent.ContentProducts[i].AdContent.Path2 != "" ? adWordsContent.ContentProducts[i].AdContent.Path2 : ""; expandedTextAd.description = adWordsContent.ContentProducts[i].AdContent.Description; expandedTextAd.finalUrls = adWordsContent.ContentProducts[i].FinalUrl; AdGroupAd expandedTextAdGroupAd = new AdGroupAd(); expandedTextAdGroupAd.adGroupId = adWordsContent.AdGroupLo.AdGroupId; expandedTextAdGroupAd.ad = expandedTextAd; // Optional: Set the status. expandedTextAdGroupAd.status = AdGroupAdStatus.ENABLED; // Create the operation. AdGroupAdOperation operation = new AdGroupAdOperation(); operation.@operator = Operator.ADD; operation.operand = expandedTextAdGroupAd; operations.Add(operation); } AdGroupAdReturnValue retVal = null; try { // Create the ads. retVal = adGroupAdService.mutate(operations.ToArray()); adGroupAdService.Close(); } 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 ex) { throw new System.ApplicationException("Failed to create expanded text ad.", ex); } return(retVal); } }