/// <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.v201806.AdGroupAdService)) { // Create a responsive search ad. ResponsiveSearchAd responsiveSearchAd = new ResponsiveSearchAd() { finalUrls = new string[] { "http://www.example.com/cruise" }, path1 = "all-inclusive", path2 = "deals", headlines = new AssetLink[] { new AssetLink() { asset = new TextAsset() { assetText = "Cruise to Mars #" + ExampleUtilities.GetShortRandomString(), }, // Set a pinning to always choose this asset for HEADLINE_1. // Pinning is optional; if no pinning is set, then headlines // and descriptions will be rotated and the ones that perform // best will be used more often. pinnedField = ServedAssetFieldType.HEADLINE_1 }, new AssetLink() { asset = new TextAsset() { assetText = "Best Space Cruise Line", } }, new AssetLink() { asset = new TextAsset() { assetText = "Experience the Stars", } }, }, descriptions = new AssetLink[] { new AssetLink() { asset = new TextAsset() { assetText = "Buy your tickets now", } }, new AssetLink() { asset = new TextAsset() { assetText = "Visit the Red Planet", } }, } }; // Create ad group ad. AdGroupAd adGroupAd = new AdGroupAd() { adGroupId = adGroupId, ad = responsiveSearchAd, // Optional: Set additional settings. status = AdGroupAdStatus.PAUSED }; // Create ad group ad operation and add it to the list. AdGroupAdOperation operation = new AdGroupAdOperation() { operand = adGroupAd, @operator = Operator.ADD }; try { // Add the responsive search ad on the server. AdGroupAdReturnValue retval = adGroupAdService.mutate( new AdGroupAdOperation[] { operation }); // Print out some information for the created ad. foreach (AdGroupAd newAdGroupAd in retval.value) { ResponsiveSearchAd newAd = (ResponsiveSearchAd)newAdGroupAd.ad; Console.WriteLine($"New responsive search ad with ID {newAd.id} was added."); Console.WriteLine("Headlines:"); foreach (AssetLink headline in newAd.headlines) { TextAsset textAsset = headline.asset as TextAsset; Console.WriteLine($" {textAsset.assetText}"); if (headline.pinnedFieldSpecified) { Console.WriteLine($" (pinned to {headline.pinnedField})"); } } Console.WriteLine("Descriptions:"); foreach (AssetLink description in newAd.descriptions) { TextAsset textAsset = description.asset as TextAsset; Console.WriteLine($" {textAsset.assetText}"); if (description.pinnedFieldSpecified) { Console.WriteLine($" (pinned to {description.pinnedField})"); } } } } catch (Exception e) { throw new System.ApplicationException("Failed to create responsive search ad.", e); } } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="adGroupId">Id of the ad group from which expanded text ads /// are retrieved.</param> public void Run(AdWordsUser user, long adGroupId) { using (AdGroupAdService adGroupAdService = (AdGroupAdService)user.GetService(AdWordsService.v201806.AdGroupAdService)) { // Create a selector to select all ads for the specified ad group. Selector selector = new Selector() { fields = new string[] { ResponsiveSearchAd.Fields.Id, AdGroupAd.Fields.Status, ResponsiveSearchAd.Fields.ResponsiveSearchAdHeadlines, ResponsiveSearchAd.Fields.ResponsiveSearchAdDescriptions }, ordering = new OrderBy[] { OrderBy.Asc(ResponsiveSearchAd.Fields.Id) }, predicates = new Predicate[] { // Restrict the fetch to only the selected ad group id. Predicate.Equals(AdGroupAd.Fields.AdGroupId, adGroupId), // Retrieve only responsive search ads. Predicate.Equals("AdType", AdType.RESPONSIVE_SEARCH_AD.ToString()), }, paging = Paging.Default }; AdGroupAdPage page = new AdGroupAdPage(); try { do { // Get the responsive search ads. page = adGroupAdService.get(selector); // Display the results. if (page != null && page.entries != null) { int i = selector.paging.startIndex; foreach (AdGroupAd adGroupAd in page.entries) { ResponsiveSearchAd ad = (ResponsiveSearchAd)adGroupAd.ad; Console.WriteLine($"{i + 1} New responsive search ad with ID {ad.id} and status " + $"{adGroupAd.status} was found."); Console.WriteLine("Headlines:"); foreach (AssetLink headline in ad.headlines) { TextAsset textAsset = headline.asset as TextAsset; Console.WriteLine($" {textAsset.assetText}"); if (headline.pinnedFieldSpecified) { Console.WriteLine($" (pinned to {headline.pinnedField})"); } } Console.WriteLine("Descriptions:"); foreach (AssetLink description in ad.descriptions) { TextAsset textAsset = description.asset as TextAsset; Console.WriteLine($" {textAsset.assetText}"); if (description.pinnedFieldSpecified) { Console.WriteLine($" (pinned to {description.pinnedField})"); } } i++; } } selector.paging.IncreaseOffset(); } while (selector.paging.startIndex < page.totalNumEntries); Console.WriteLine("Number of responsive search ads found: {0}", page.totalNumEntries); } catch (Exception e) { throw new System.ApplicationException("Failed to get responsive search ads.", e); } } }