/// <summary> /// Creates a test ThirdPartyRedirectAd for running further tests. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">The adgroup id for which the ad is created. /// </param> /// <returns>The text ad id.</returns> public long CreateThirdPartyRedirectAd(AdWordsUser user, long adGroupId) { AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(AdWordsService.v201806.AdGroupAdService); AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation(); adGroupAdOperation.@operator = Operator.ADD; adGroupAdOperation.operand = new AdGroupAd(); adGroupAdOperation.operand.adGroupId = adGroupId; // Create the third party redirect ad. ThirdPartyRedirectAd redirectAd = new ThirdPartyRedirectAd(); redirectAd.name = string.Format("Example third party ad #{0}", this.GetTimeStamp()); redirectAd.url = "http://www.example.com"; redirectAd.dimensions = new Dimensions(); redirectAd.dimensions.height = 250; redirectAd.dimensions.width = 300; // This field normally contains the javascript ad tag. redirectAd.snippet = "<img src=\"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg\"/>"; redirectAd.impressionBeaconUrl = "http://www.examples.com/beacon"; redirectAd.certifiedVendorFormatId = 119; redirectAd.isCookieTargeted = false; redirectAd.isUserInterestTargeted = false; redirectAd.isTagged = false; adGroupAdOperation.operand.ad = redirectAd; AdGroupAdReturnValue retVal = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation }); return(retVal.value[0].ad.id); }
/// <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.v201502.AdGroupAdService); // Create standard third party redirect ad. ThirdPartyRedirectAd standardAd = new ThirdPartyRedirectAd(); standardAd.name = String.Format("Example third party ad #{0}", ExampleUtilities.GetRandomString()); standardAd.finalUrls = new string[] { "http://www.example.com" }; standardAd.dimensions = new Dimensions(); standardAd.dimensions.height = 250; standardAd.dimensions.width = 300; standardAd.snippet = "<img src=\"http://goo.gl/HJM3L\"/>"; // DoubleClick Rich Media Expandable format ID. standardAd.certifiedVendorFormatId = 232; standardAd.isCookieTargeted = false; standardAd.isUserInterestTargeted = false; standardAd.isTagged = false; standardAd.richMediaAdType = RichMediaAdRichMediaAdType.STANDARD; // Expandable Ad properties. standardAd.expandingDirections = new ThirdPartyRedirectAdExpandingDirection[] { ThirdPartyRedirectAdExpandingDirection.EXPANDING_UP, ThirdPartyRedirectAdExpandingDirection.EXPANDING_DOWN }; standardAd.adAttributes = new RichMediaAdAdAttribute[] { RichMediaAdAdAttribute.ROLL_OVER_TO_EXPAND }; // Create in-stream third party redirect ad. ThirdPartyRedirectAd inStreamAd = new ThirdPartyRedirectAd(); inStreamAd.name = String.Format("Example third party ad #{0}", ExampleUtilities.GetRandomString()); inStreamAd.finalUrls = new string[] { "http://www.example.com" }; // Set the duration to 15 secs. inStreamAd.adDuration = 15000; inStreamAd.sourceUrl = "http://ad.doubleclick.net/pfadx/N270.126913.6102203221521/B3876671.21;dcadv=2215309;sz=0x0;ord=%5btimestamp%5d;dcmt=text/xml"; inStreamAd.certifiedVendorFormatId = 303; inStreamAd.richMediaAdType = RichMediaAdRichMediaAdType.IN_STREAM_VIDEO; List <AdGroupAdOperation> operations = new List <AdGroupAdOperation>(); foreach (ThirdPartyRedirectAd redirectAd in new ThirdPartyRedirectAd[] { standardAd, inStreamAd }) { // Set the ad group id. AdGroupAd adGroupAd = new AdGroupAd(); adGroupAd.adGroupId = adGroupId; adGroupAd.ad = redirectAd; // Create the operation. AdGroupAdOperation operation = new AdGroupAdOperation(); operation.@operator = Operator.ADD; operation.operand = adGroupAd; operations.Add(operation); } AdGroupAdReturnValue retVal = null; try { // Create the ads retVal = service.mutate(operations.ToArray()); if (retVal != null && retVal.value != null) { // If you are adding multiple type of Ads, then you may need to check // for // // if (adGroupAd.ad is ThirdPartyRedirectAd) { ... } // // to identify the ad type. foreach (AdGroupAd newAdGroupAd in retVal.value) { Console.WriteLine("New third party redirect ad with url = \"{0}\" and id = {1}" + " was created.", ((ThirdPartyRedirectAd)newAdGroupAd.ad).finalUrls[0], newAdGroupAd.ad.id); } } else { Console.WriteLine("No third party redirect ads were created."); } } catch (Exception ex) { throw new System.ApplicationException("Failed to create third party redirect 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 ads are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService) user.GetService(AdWordsService.v201509.AdGroupAdService); // Create standard third party redirect ad. ThirdPartyRedirectAd standardAd = new ThirdPartyRedirectAd(); standardAd.name = String.Format("Example third party ad #{0}", ExampleUtilities.GetRandomString()); standardAd.finalUrls = new string[] {"http://www.example.com"}; standardAd.dimensions = new Dimensions(); standardAd.dimensions.height = 250; standardAd.dimensions.width = 300; standardAd.snippet = "<img src=\"http://goo.gl/HJM3L\"/>"; // DoubleClick Rich Media Expandable format ID. standardAd.certifiedVendorFormatId = 232; standardAd.isCookieTargeted = false; standardAd.isUserInterestTargeted = false; standardAd.isTagged = false; standardAd.richMediaAdType = RichMediaAdRichMediaAdType.STANDARD; // Expandable Ad properties. standardAd.expandingDirections = new ThirdPartyRedirectAdExpandingDirection[] { ThirdPartyRedirectAdExpandingDirection.EXPANDING_UP, ThirdPartyRedirectAdExpandingDirection.EXPANDING_DOWN }; standardAd.adAttributes = new RichMediaAdAdAttribute[] { RichMediaAdAdAttribute.ROLL_OVER_TO_EXPAND}; // Create in-stream third party redirect ad. ThirdPartyRedirectAd inStreamAd = new ThirdPartyRedirectAd(); inStreamAd.name = String.Format("Example third party ad #{0}", ExampleUtilities.GetRandomString()); inStreamAd.finalUrls = new string[] {"http://www.example.com"}; // Set the duration to 15 secs. inStreamAd.adDuration = 15000; inStreamAd.sourceUrl = "http://ad.doubleclick.net/pfadx/N270.126913.6102203221521/B3876671.21;dcadv=2215309;sz=0x0;ord=%5btimestamp%5d;dcmt=text/xml"; inStreamAd.certifiedVendorFormatId = 303; inStreamAd.richMediaAdType = RichMediaAdRichMediaAdType.IN_STREAM_VIDEO; List<AdGroupAdOperation> operations = new List<AdGroupAdOperation>(); foreach (ThirdPartyRedirectAd redirectAd in new ThirdPartyRedirectAd[] {standardAd, inStreamAd}) { // Set the ad group id. AdGroupAd adGroupAd = new AdGroupAd(); adGroupAd.adGroupId = adGroupId; adGroupAd.ad = redirectAd; // Create the operation. AdGroupAdOperation operation = new AdGroupAdOperation(); operation.@operator = Operator.ADD; operation.operand = adGroupAd; operations.Add(operation); } AdGroupAdReturnValue retVal = null; try { // Create the ads retVal = service.mutate(operations.ToArray()); if (retVal != null && retVal.value != null) { // If you are adding multiple type of Ads, then you may need to check // for // // if (adGroupAd.ad is ThirdPartyRedirectAd) { ... } // // to identify the ad type. foreach (AdGroupAd newAdGroupAd in retVal.value) { Console.WriteLine("New third party redirect ad with url = \"{0}\" and id = {1}" + " was created.", ((ThirdPartyRedirectAd) newAdGroupAd.ad).finalUrls[0], newAdGroupAd.ad.id); } } else { Console.WriteLine("No third party redirect ads were created."); } } catch (Exception e) { throw new System.ApplicationException("Failed to create third party redirect 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 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 ads are added. /// </param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService) user.GetService(AdWordsService.v201509.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 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 from which third party /// redirect ads are retrieved.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService)user.GetService(AdWordsService.v201509.AdGroupAdService); // Create a selector. Selector selector = new Selector() { fields = new string[] { Ad.Fields.Id, AdGroupAd.Fields.Status, ThirdPartyRedirectAd.Fields.Url, ThirdPartyRedirectAd.Fields.DisplayUrl, ThirdPartyRedirectAd.Fields.RichMediaAdSnippet }, ordering = new OrderBy[] { OrderBy.Asc(Ad.Fields.Id) }, predicates = new Predicate[] { // Restrict the fetch to only the selected ad group id. Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId.ToString()), // Restrieve only third party redirect ads. Predicate.Equals("AdType", "THIRD_PARTY_REDIRECT_AD"), // By default disabled ads aren't returned by the selector. To return // them include the DISABLED status in the statuses field. Predicate.In(AdGroupAd.Fields.Status, new string[] { AdGroupAdStatus.ENABLED.ToString(), AdGroupAdStatus.PAUSED.ToString(), AdGroupAdStatus.DISABLED.ToString() }) }, paging = Paging.Default }; AdGroupAdPage page = new AdGroupAdPage(); try { do { // Get the third party redirect ads. page = service.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (AdGroupAd adGroupAd in page.entries) { ThirdPartyRedirectAd thirdPartyRedirectAd = (ThirdPartyRedirectAd)adGroupAd.ad; Console.WriteLine("{0}) Ad id is {1} and status is {2}", i, thirdPartyRedirectAd.id, adGroupAd.status); Console.WriteLine(" Url: {0}\n Display Url: {1}\n Snippet:{2}", thirdPartyRedirectAd.finalUrls[0], thirdPartyRedirectAd.displayUrl, thirdPartyRedirectAd.snippet); i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of third party redirect ads found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to get third party redirect ad(s).", e); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group from which third party /// redirect ads are retrieved.</param> public void Run(AdWordsUser user, long adGroupId) { // Get the AdGroupAdService. AdGroupAdService service = (AdGroupAdService)user.GetService(AdWordsService.v201409.AdGroupAdService); // Create a selector. Selector selector = new Selector(); selector.fields = new string[] { "Id", "Status", "Url", "DisplayUrl", "RichMediaAdSnippet" }; // Set the sort order. OrderBy orderBy = new OrderBy(); orderBy.field = "Id"; orderBy.sortOrder = SortOrder.ASCENDING; selector.ordering = new OrderBy[] { orderBy }; // Restrict the fetch to only the selected ad group id. Predicate adGroupPredicate = new Predicate(); adGroupPredicate.field = "AdGroupId"; adGroupPredicate.@operator = PredicateOperator.EQUALS; adGroupPredicate.values = new string[] { adGroupId.ToString() }; // Retrieve only third party redirect ads. Predicate typePredicate = new Predicate(); typePredicate.field = "AdType"; typePredicate.@operator = PredicateOperator.EQUALS; typePredicate.values = new string[] { "THIRD_PARTY_REDIRECT_AD" }; // By default disabled ads aren't returned by the selector. To return // them include the DISABLED status in the statuses field. Predicate statusPredicate = new Predicate(); statusPredicate.field = "Status"; statusPredicate.@operator = PredicateOperator.IN; statusPredicate.values = new string[] { AdGroupAdStatus.ENABLED.ToString(), AdGroupAdStatus.PAUSED.ToString(), AdGroupAdStatus.DISABLED.ToString() }; selector.predicates = new Predicate[] { adGroupPredicate, statusPredicate, typePredicate }; // Select the selector paging. selector.paging = new Paging(); int offset = 0; int pageSize = 500; AdGroupAdPage page = new AdGroupAdPage(); try { do { selector.paging.startIndex = offset; selector.paging.numberResults = pageSize; // Get the third party redirect ads. page = service.get(selector); // Display the results. if (page != null && page.entries != null) { int i = offset; foreach (AdGroupAd adGroupAd in page.entries) { ThirdPartyRedirectAd thirdPartyRedirectAd = (ThirdPartyRedirectAd)adGroupAd.ad; Console.WriteLine("{0}) Ad id is {1} and status is {2}", i, thirdPartyRedirectAd.id, adGroupAd.status); Console.WriteLine(" Url: {0}\n Display Url: {1}\n Snippet:{2}", thirdPartyRedirectAd.finalUrls[0], thirdPartyRedirectAd.displayUrl, thirdPartyRedirectAd.snippet); i++; } } offset += pageSize; } while (offset < page.totalNumEntries); Console.WriteLine("Number of third party redirect ads found: {0}", page.totalNumEntries); } catch (Exception ex) { throw new System.ApplicationException("Failed to get third party redirect ad(s).", ex); } }
/// <summary> /// Creates a test ThirdPartyRedirectAd for running further tests. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">The adgroup id for which the ad is created. /// </param> /// <param name="hasAdParam">True, if an ad param placeholder should be /// added.</param> /// <returns>The text ad id.</returns> public long CreateThirdPartyRedirectAd(AdWordsUser user, long adGroupId) { AdGroupAdService adGroupAdService = (AdGroupAdService) user.GetService(AdWordsService.v201601.AdGroupAdService); AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation(); adGroupAdOperation.@operator = Operator.ADD; adGroupAdOperation.operand = new AdGroupAd(); adGroupAdOperation.operand.adGroupId = adGroupId; // Create the third party redirect ad. ThirdPartyRedirectAd redirectAd = new ThirdPartyRedirectAd(); redirectAd.name = string.Format("Example third party ad #{0}", this.GetTimeStamp()); redirectAd.url = "http://www.example.com"; redirectAd.dimensions = new Dimensions(); redirectAd.dimensions.height = 250; redirectAd.dimensions.width = 300; // This field normally contains the javascript ad tag. redirectAd.snippet = "<img src=\"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg\"/>"; redirectAd.impressionBeaconUrl = "http://www.examples.com/beacon"; redirectAd.certifiedVendorFormatId = 119; redirectAd.isCookieTargeted = false; redirectAd.isUserInterestTargeted = false; redirectAd.isTagged = false; adGroupAdOperation.operand.ad = redirectAd; AdGroupAdReturnValue retVal = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation }); return retVal.value[0].ad.id; }