public IHttpActionResult CreateAds([FromBody] AdWordsContentLo adWordsContent) { var retVal = ExpandedTextAds.CreateTextAds(new AdWordsUser(), adWordsContent); AdGroupAdwords.SetAdGroupStatus(new AdWordsUser(), adWordsContent.AdGroupLo.AdGroupId, AdGroupStatus.ENABLED); return(Ok(retVal)); }
public void AddAds() { // Assert CampaignLo campaignDto = new CampaignLo { Name = DateTime.Now.ToString(), StartDate = DateTime.Now, EndDate = DateTime.Now.AddYears(1), Budget = new BudgetLo { Name = DateTime.Now.ToString(), MicroAmount = 5000000 } }; var campaign = Campaigns.CreateCampaign(new AdWordsUser(), campaignDto); AdGroupLo adGroupDto = new AdGroupLo { CampaignId = campaign.value[0].id, Name = DateTime.Now.ToString(), KeyWords = "hej, med dig" }; var adGroup = AdGroupAdwords.CreateAdGroup(new AdWordsUser(), adGroupDto); AdWordsContentLo adsContent = new AdWordsContentLo { AdGroupLo = new AdGroupLo { AdGroupId = adGroup.value[0].id, Name = adGroup.value[0].name }, ContentProducts = new List <ProductItemLo> { new ProductItemLo { AdContent = new AdContentLo { HeadLinePart1 = "Overskrift1", HeadLinePart2 = "Overskrift2", Description = "Beksrivelse" }, FinalUrl = new string[] { "http://nolleren.org/test" } } } }; // Act var result = ExpandedTextAds.CreateTextAds(new AdWordsUser(), adsContent); // Assert Assert.AreEqual(adGroup.value[0].id, result.value[0].adGroupId); Assert.AreNotEqual(0, result.value.Length); }
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); } }