/// <summary> /// Creates the DSA URL add operation. /// </summary> /// <param name="details">The page feed details.</param> /// <param name="url">The DSA page feed URL.</param> /// <param name="label">DSA page feed label.</param> /// <returns>The DSA URL add operation.</returns> private static FeedItemOperation CreateDsaUrlAddOperation(DSAFeedDetails details, string url, string label) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue urlAttributeValue = new FeedItemAttributeValue(); urlAttributeValue.feedAttributeId = details.urlAttributeId; // See https://support.google.com/adwords/answer/7166527 for page feed URL recommendations // and rules. urlAttributeValue.stringValues = new string[] { url }; FeedItemAttributeValue labelAttributeValue = new FeedItemAttributeValue(); labelAttributeValue.feedAttributeId = details.labelAttributeId; labelAttributeValue.stringValues = new string[] { label }; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = details.feedId; item.attributeValues = new FeedItemAttributeValue[] { urlAttributeValue, labelAttributeValue }; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return(operation); }
private static FeedItemOperation newSiteLinkFeedItemAddOperation( SiteLinksDataHolder siteLinksData, String text, String url) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue(); linkTextAttributeValue.feedAttributeId = siteLinksData.LinkTextFeedAttributeId; linkTextAttributeValue.stringValue = text; FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue(); linkUrlAttributeValue.feedAttributeId = siteLinksData.LinkUrlFeedAttributeId; linkUrlAttributeValue.stringValue = url; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = siteLinksData.SiteLinksFeedId; item.attributeValues = new FeedItemAttributeValue[] { linkTextAttributeValue, linkUrlAttributeValue }; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return(operation); }
/// <summary> /// Creates the DSA URL add operation. /// </summary> /// <param name="details">The page feed details.</param> /// <param name="url">The DSA page feed URL.</param> /// <param name="label">DSA page feed label.</param> /// <returns>The DSA URL add operation.</returns> private static FeedItemOperation CreateDsaUrlAddOperation(DSAFeedDetails details, string url, string label) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue urlAttributeValue = new FeedItemAttributeValue(); urlAttributeValue.feedAttributeId = details.urlAttributeId; // Optional: Add the {feeditem} valuetrack parameter to track which page feed items lead // to each click. url = url + "?id={feeditem}"; urlAttributeValue.stringValues = new string[] { url }; FeedItemAttributeValue labelAttributeValue = new FeedItemAttributeValue(); labelAttributeValue.feedAttributeId = details.labelAttributeId; labelAttributeValue.stringValues = new string[] { label }; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = details.feedId; item.attributeValues = new FeedItemAttributeValue[] { urlAttributeValue, labelAttributeValue }; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return(operation); }
/// <summary> /// Deletes the old feed items for which extension settings have been /// created. /// </summary> /// <param name="user">The user that owns the feed items.</param> /// <param name="feedItemIds">IDs of the feed items to be removed.</param> /// <param name="feedId">ID of the feed that holds the feed items.</param> private void DeleteOldFeedItems(AdWordsUser user, List <long> feedItemIds, long feedId) { if (feedItemIds.Count == 0) { return; } List <FeedItemOperation> operations = new List <FeedItemOperation>(); foreach (long feedItemId in feedItemIds) { FeedItemOperation operation = new FeedItemOperation() { @operator = Operator.REMOVE, operand = new FeedItem() { feedItemId = feedItemId, feedId = feedId } }; operations.Add(operation); } using (FeedItemService feedItemService = (FeedItemService)user.GetService( AdWordsService.v201802.FeedItemService)) { feedItemService.mutate(operations.ToArray()); return; } }
/// <summary> /// Update Site Link using Feed Item Service /// </summary> /// <param name="user">Adwords User Object</param> /// <param name="feedItem">Feed Item on which Operation to be performed</param> /// <returns>FeedItem returned after performing SET Operation</returns> public FeedItem UpdateSiteLinkUsingFeedItemService(AdWordsUser user, FeedItem feedItem) { FeedItemService feedItemService = (FeedItemService)user.GetService(AdWordsService.v201809.FeedItemService); FeedItemOperation feedItemOperation = new FeedItemOperation(); feedItemOperation.operand = feedItem; feedItemOperation.@operator = Operator.SET; try { FeedItemReturnValue feedItemReturnValue = feedItemService.mutate(new[] { feedItemOperation }); if (feedItemReturnValue.value != null && feedItemReturnValue.value.Length > 0) { FeedItem modifiedFeedItem = feedItemReturnValue.value[0]; Logger.Log(Logger.LogType.INFO, "Modification Successful for Feed(ID): " + feedItem.feedId + " with FeedItem(ID): " + feedItem.feedItemId); return(modifiedFeedItem); } else { Logger.Log(Logger.LogType.WARNING, "Nothing Modified for Feed(ID): " + feedItem.feedId + " with FeedItem(ID): " + feedItem.feedItemId); return(null); } } catch (AdWordsApiException ex) { Logger.Log(Logger.LogType.EXCEPTION, Environment.NewLine + ex.Message + Environment.NewLine + ex.InnerException); return(null); } }
private static FeedItemOperation newSitelinkFeedItemAddOperation( SitelinksDataHolder sitelinksData, String text, String finalUrl, string line2, string line3, long?locationId) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue(); linkTextAttributeValue.feedAttributeId = sitelinksData.LinkTextFeedAttributeId; linkTextAttributeValue.stringValue = text; FeedItemAttributeValue linkFinalUrlAttributeValue = new FeedItemAttributeValue(); linkFinalUrlAttributeValue.feedAttributeId = sitelinksData.LinkFinalUrlFeedAttributeId; linkFinalUrlAttributeValue.stringValues = new string[] { finalUrl }; FeedItemAttributeValue line2AttributeValue = new FeedItemAttributeValue(); line2AttributeValue.feedAttributeId = sitelinksData.Line2FeedAttributeId; line2AttributeValue.stringValue = line2; FeedItemAttributeValue line3AttributeValue = new FeedItemAttributeValue(); line3AttributeValue.feedAttributeId = sitelinksData.Line3FeedAttributeId; line3AttributeValue.stringValue = line3; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = sitelinksData.FeedId; // OPTIONAL: Use geographical targeting on a feed item. // The IDs can be found in the documentation or retrieved with the // LocationCriterionService. if (locationId != null) { item.geoTargeting = new Location() { id = locationId.Value, }; // OPTIONAL: Restrict targeting only to people physically within the location. item.geoTargetingRestriction = new FeedItemGeoRestriction() { geoRestriction = GeoRestriction.LOCATION_OF_PRESENCE }; } item.attributeValues = new FeedItemAttributeValue[] { linkTextAttributeValue, linkFinalUrlAttributeValue, line2AttributeValue, line3AttributeValue }; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return(operation); }
/// <summary> /// Updates attribute value of the feed item. In order to update a FeedItemAttributeValue /// you must update the FeedItem. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="feedId">ID of the feed containing the feed item to be updated.</param> /// <param name="feedItemId">ID of the feed item to be updated.</param> /// <param name="flightPlaceholderField">the placeholder type for the attribute to be /// updated.</param> /// <param name="attributeValue">String value with which to update the /// FeedAttributeValue.</param> private void UpdateFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderField, string attributeValue) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService(Services.V2.FeedItemService); // Gets the feed resource name. string feedResourceName = ResourceNames.Feed(customerId, feedId); // Gets a map of the placeholder values and feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> feedAttributes = GetFeed(client, customerId, feedResourceName); // Gets the ID of the attribute to update. This is needed to specify which // FeedItemAttributeValue will be updated in the given FeedItem. FlightPlaceholderField placeholderField = (FlightPlaceholderField)Enum.Parse( typeof(FlightPlaceholderField), flightPlaceholderField); long attributeId = feedAttributes[placeholderField].Id.Value; // Gets the feed item resource name. string feedItemResourceName = ResourceNames.FeedItem(customerId, feedId, feedItemId); // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedItemResourceName); // Creates the updated FeedItemAttributeValue. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = attributeId, StringValue = attributeValue }; // Creates a new FeedItem from the existing FeedItem. Any FeedItemAttributeValues that are // not included in the updated FeedItem will be removed from the FeedItem, which is why you // must create the FeedItem from the existing FeedItem and set the field(s) that will be // updated. feedItem.AttributeValues[GetAttributeIndex(feedItem, feedItemAttributeValue)] = feedItemAttributeValue; // Creates the operation. FeedItemOperation operation = new FeedItemOperation() { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Updated feed item with resource name '{result.ResourceName}'."); } }
/// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads API client.</param> /// <param name="customerId">The Google Ads customer ID for which the call is made.</param> /// <param name="feedId">The Feed ID to which the feed item belongs.</param> /// <param name="feedItemId">The ID of the feed item to be updated.</param> /// <param name="flightPlaceholderFieldName"> /// The flight placeholder field name for the attribute to be removed. /// </param> public void Run(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderFieldName) { // Creates the Feed Item service client. FeedItemServiceClient feedItemServiceClient = client.GetService( Services.V10.FeedItemService); try { // Gets a map of the placeholder values to feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> placeholdersToFeedAttributesMap = GetFeed(client, customerId, feedId); // Removes the attribute from the feed item. FlightPlaceholderField flightPlaceholderField = (FlightPlaceholderField)Enum.Parse(typeof(FlightPlaceholderField), flightPlaceholderFieldName, true); FeedItem feedItem = RemoveAttributeValueFromFeedItem(client, customerId, feedId, feedItemId, placeholdersToFeedAttributesMap, flightPlaceholderField); // [START remove_flights_feed_item_attribute_value_1] Creates the operation. FeedItemOperation operation = new FeedItemOperation { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item and prints the results. MutateFeedItemsResponse response = feedItemServiceClient.MutateFeedItems (customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine("Updated feed item with resource name " + $"'{result.ResourceName}'."); } // [END remove_flights_feed_item_attribute_value_1] } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } catch (Exception e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); throw; } }
/// <summary>Snippet for MutateFeedItems</summary> /// <remarks> /// This snippet has been automatically generated for illustrative purposes only. /// It may require modifications to work in your environment. /// </remarks> public void MutateFeedItems() { // Create client FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create(); // Initialize request argument(s) string customerId = ""; IEnumerable <FeedItemOperation> operations = new FeedItemOperation[] { new FeedItemOperation(), }; // Make the request MutateFeedItemsResponse response = feedItemServiceClient.MutateFeedItems(customerId, operations); }
private static void CreateSitelinksFeedItems(AdWordsUser user, SitelinksDataHolder siteLinksData) { using (FeedItemService feedItemService = (FeedItemService)user.GetService(AdWordsService.v201809.FeedItemService)) { // Create operations to add FeedItems. FeedItemOperation home = NewSitelinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com", "Home line 2", "Home line 3"); FeedItemOperation stores = NewSitelinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores", "Stores line 2", "Stores line 3"); FeedItemOperation onSale = NewSitelinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale", "On Sale line 2", "On Sale line 3"); FeedItemOperation support = NewSitelinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support", "Support line 2", "Support line 3"); FeedItemOperation products = NewSitelinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods", "Products line 2", "Products line 3"); // This site link is using geographical targeting to use LOCATION_OF_PRESENCE. FeedItemOperation aboutUs = NewSitelinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about", "About Us line 2", "About Us line 3", true); FeedItemOperation[] operations = new FeedItemOperation[] { home, stores, onSale, support, products, aboutUs }; FeedItemReturnValue result = feedItemService.mutate(operations); foreach (FeedItem item in result.value) { Console.WriteLine("FeedItem with feedItemId {0} was added.", item.feedItemId); siteLinksData.FeedItemIds.Add(item.feedItemId); } // Target the "aboutUs" sitelink to geographically target California. // See https://developers.google.com/adwords/api/docs/appendix/geotargeting for // location criteria for supported locations. RestrictFeedItemToGeoTarget(user, result.value[5], 21137); } }
/// <summary> /// Creates the page URLs in the DSA page feed. /// </summary> /// <param name="user">The AdWords user.</param> /// <param name="feedDetails">The feed details.</param> /// <param name="labelName">The pagefeed url label.</param> private static void CreateFeedItems(AdWordsUser user, DSAFeedDetails feedDetails, string labelName) { using (FeedItemService feedItemService = (FeedItemService)user.GetService( AdWordsService.v201802.FeedItemService)) { FeedItemOperation[] operations = new FeedItemOperation[] { CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/rental-cars", labelName), CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/hotel-deals", labelName), CreateDsaUrlAddOperation(feedDetails, "http://www.example.com/discounts/flight-deals", labelName), }; feedItemService.mutate(operations); } }
/// <summary>Snippet for MutateFeedItemsAsync</summary> public async Task MutateFeedItemsAsync() { // Snippet: MutateFeedItemsAsync(string, IEnumerable<FeedItemOperation>, CallSettings) // Additional: MutateFeedItemsAsync(string, IEnumerable<FeedItemOperation>, CancellationToken) // Create client FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync(); // Initialize request argument(s) string customerId = ""; IEnumerable <FeedItemOperation> operations = new FeedItemOperation[] { new FeedItemOperation(), }; // Make the request MutateFeedItemsResponse response = await feedItemServiceClient.MutateFeedItemsAsync(customerId, operations); // End snippet }
/// <summary> /// Helper method to construct a single FeedAttribute. /// </summary> /// <param name="feed">The feed for which the operation will be created.</param> /// <param name="text">The link text for the feed item.</param> /// <param name="finalUrl">The final URL for the feed item.</param> /// <param name="line1">Line 1 of the feed item.</param> /// <param name="line2">Line 2 of the feed item.</param> /// <returns>The newly created FeedAttribute instance.</returns> private FeedItemOperation NewFeedItemOperation(Feed feed, string text, string finalUrl, string line1, string line2) { // The attribute IDs come back in the same order that they were added. FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feed.Attributes[0].Id, StringValue = text }; FeedItemAttributeValue finalUrlAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feed.Attributes[1].Id, StringValues = { finalUrl } }; FeedItemAttributeValue line1AttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feed.Attributes[2].Id, StringValue = line1 }; FeedItemAttributeValue line2AttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feed.Attributes[3].Id, StringValue = line2 }; FeedItem feedItem = new FeedItem() { Feed = feed.ResourceName, AttributeValues = { linkTextAttributeValue, finalUrlAttributeValue, line1AttributeValue, line2AttributeValue } }; FeedItemOperation feedItemOperation = new FeedItemOperation() { Create = feedItem }; return(feedItemOperation); }
private static void createSitelinksFeedItems(AdWordsUser user, SitelinksDataHolder siteLinksData) { using (FeedItemService feedItemService = (FeedItemService)user.GetService(AdWordsService.v201802.FeedItemService)) { // Create operations to add FeedItems. FeedItemOperation home = newSitelinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com", "Home line 2", "Home line 3"); FeedItemOperation stores = newSitelinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores", "Stores line 2", "Stores line 3"); FeedItemOperation onSale = newSitelinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale", "On Sale line 2", "On Sale line 3"); FeedItemOperation support = newSitelinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support", "Support line 2", "Support line 3"); FeedItemOperation products = newSitelinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods", "Products line 2", "Products line 3"); // This site link is using geographical targeting by specifying the // criterion ID for California. FeedItemOperation aboutUs = newSitelinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about", "About Us line 2", "About Us line 3", 21137); FeedItemOperation[] operations = new FeedItemOperation[] { home, stores, onSale, support, products, aboutUs }; FeedItemReturnValue result = feedItemService.mutate(operations); foreach (FeedItem item in result.value) { Console.WriteLine("FeedItem with feedItemId {0} was added.", item.feedItemId); siteLinksData.FeedItemIds.Add(item.feedItemId); } } }
/// <summary> /// Creates a FeedItemOperation that will create a FeedItem with the /// specified values and ad group target when sent to /// FeedItemService.mutate. /// </summary> /// <param name="adCustomizerFeed">The ad customizer feed.</param> /// <param name="name">The value for the name attribute of the FeedItem. /// </param> /// <param name="price">The value for the price attribute of the FeedItem. /// </param> /// <param name="date">The value for the date attribute of the FeedItem. /// </param> /// <param name="adGroupId">The ID of the ad group to target with the /// FeedItem.</param> /// <returns>A new FeedItemOperation for adding a FeedItem.</returns> private static FeedItemOperation CreateFeedItemAddOperation(AdCustomizerFeed adCustomizerFeed, string name, string price, String date, long adGroupId) { FeedItem feedItem = new FeedItem(); feedItem.feedId = adCustomizerFeed.feedId; List <FeedItemAttributeValue> attributeValues = new List <FeedItemAttributeValue>(); // FeedAttributes appear in the same order as they were created // - Name, Price, Date. See CreateCustomizerFeed method for details. FeedItemAttributeValue nameAttributeValue = new FeedItemAttributeValue(); nameAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[0].id; nameAttributeValue.stringValue = name; attributeValues.Add(nameAttributeValue); FeedItemAttributeValue priceAttributeValue = new FeedItemAttributeValue(); priceAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[1].id; priceAttributeValue.stringValue = price; attributeValues.Add(priceAttributeValue); FeedItemAttributeValue dateAttributeValue = new FeedItemAttributeValue(); dateAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[2].id; dateAttributeValue.stringValue = date; attributeValues.Add(dateAttributeValue); feedItem.attributeValues = attributeValues.ToArray(); feedItem.adGroupTargeting = new FeedItemAdGroupTargeting(); feedItem.adGroupTargeting.TargetingAdGroupId = adGroupId; FeedItemOperation feedItemOperation = new FeedItemOperation(); feedItemOperation.operand = feedItem; feedItemOperation.@operator = Operator.ADD; return(feedItemOperation); }
/// <summary> /// Creates a FeedItemOperation that will create a FeedItem with the /// specified values and ad group target when sent to /// FeedItemService.mutate. /// </summary> /// <param name="name">The value for the name attribute of the FeedItem. /// </param> /// <param name="price">The value for the price attribute of the FeedItem. /// </param> /// <param name="date">The value for the date attribute of the FeedItem. /// </param> /// <param name="adGroupId">The ID of the ad group to target with the /// FeedItem.</param> /// <param name="dataHolder">The data holder that contains metadata about /// the customizer Feed.</param> /// <returns>A new FeedItemOperation for adding a FeedItem.</returns> private static FeedItemOperation CreateFeedItemAddOperation(string name, string price, String date, long adGroupId, CustomizersDataHolder dataHolder) { FeedItem feedItem = new FeedItem(); feedItem.feedId = dataHolder.FeedId; List <FeedItemAttributeValue> attributeValues = new List <FeedItemAttributeValue>(); FeedItemAttributeValue nameAttributeValue = new FeedItemAttributeValue(); nameAttributeValue.feedAttributeId = dataHolder.NameFeedAttributeId; nameAttributeValue.stringValue = name; attributeValues.Add(nameAttributeValue); FeedItemAttributeValue priceAttributeValue = new FeedItemAttributeValue(); priceAttributeValue.feedAttributeId = dataHolder.PriceFeedAttributeId; priceAttributeValue.stringValue = price; attributeValues.Add(priceAttributeValue); FeedItemAttributeValue dateAttributeValue = new FeedItemAttributeValue(); dateAttributeValue.feedAttributeId = dataHolder.DateFeedAttributeId; dateAttributeValue.stringValue = date; attributeValues.Add(dateAttributeValue); feedItem.attributeValues = attributeValues.ToArray(); feedItem.adGroupTargeting = new FeedItemAdGroupTargeting(); feedItem.adGroupTargeting.TargetingAdGroupId = adGroupId; FeedItemOperation feedItemOperation = new FeedItemOperation(); feedItemOperation.operand = feedItem; feedItemOperation.@operator = Operator.ADD; return(feedItemOperation); }
private static void createSiteLinksFeedItems( AdWordsUser user, SiteLinksDataHolder siteLinksData) { // Get the FeedItemService. FeedItemService feedItemService = (FeedItemService)user.GetService(AdWordsService.v201406.FeedItemService); // Create operations to add FeedItems. FeedItemOperation home = newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com"); FeedItemOperation stores = newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores"); FeedItemOperation onSale = newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale"); FeedItemOperation support = newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support"); FeedItemOperation products = newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods"); FeedItemOperation aboutUs = newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about"); FeedItemOperation[] operations = new FeedItemOperation[] { home, stores, onSale, support, products, aboutUs }; FeedItemReturnValue result = feedItemService.mutate(operations); foreach (FeedItem item in result.value) { Console.WriteLine("FeedItem with feedItemId {0} was added.", item.feedItemId); siteLinksData.SiteLinkFeedItemIds.Add(item.feedItemId); } }
private static FeedItemOperation newSitelinkFeedItemAddOperation( SitelinksDataHolder sitelinksData, String text, String finalUrl, long? locationId) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue(); linkTextAttributeValue.feedAttributeId = sitelinksData.LinkTextFeedAttributeId; linkTextAttributeValue.stringValue = text; FeedItemAttributeValue linkFinalUrlAttributeValue = new FeedItemAttributeValue(); linkFinalUrlAttributeValue.feedAttributeId = sitelinksData.LinkFinalUrlFeedAttributeId; linkFinalUrlAttributeValue.stringValues = new string[] { finalUrl }; // Create the feed item and operation. FeedItem item = new FeedItem(); item.feedId = sitelinksData.FeedId; // OPTIONAL: Use geographical targeting on a feed item. // The IDs can be found in the documentation or retrieved with the // LocationCriterionService. if (locationId != null) { item.geoTargeting = new Location() { id = locationId.Value }; } item.attributeValues = new FeedItemAttributeValue[] {linkTextAttributeValue, linkFinalUrlAttributeValue}; FeedItemOperation operation = new FeedItemOperation(); operation.operand = item; operation.@operator = Operator.ADD; return operation; }
private static void createSitelinksFeedItems( AdWordsUser user, SitelinksDataHolder siteLinksData) { // Get the FeedItemService. FeedItemService feedItemService = (FeedItemService) user.GetService(AdWordsService.v201509.FeedItemService); // Create operations to add FeedItems. FeedItemOperation home = newSitelinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com"); FeedItemOperation stores = newSitelinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores"); FeedItemOperation onSale = newSitelinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale"); FeedItemOperation support = newSitelinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support"); FeedItemOperation products = newSitelinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods"); // This site link is using geographical targeting by specifying the // criterion ID for California. FeedItemOperation aboutUs = newSitelinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about", 21137); FeedItemOperation[] operations = new FeedItemOperation[] {home, stores, onSale, support, products, aboutUs}; FeedItemReturnValue result = feedItemService.mutate(operations); foreach (FeedItem item in result.value) { Console.WriteLine("FeedItem with feedItemId {0} was added.", item.feedItemId); siteLinksData.FeedItemIds.Add(item.feedItemId); } }
/// <summary> /// Deletes the old feed items for which extension settings have been /// created. /// </summary> /// <param name="user">The user that owns the feed items.</param> /// <param name="feedItemIds">IDs of the feed items to be removed.</param> /// <param name="feedId">ID of the feed that holds the feed items.</param> private void DeleteOldFeedItems(AdWordsUser user, List<long> feedItemIds, long feedId) { if (feedItemIds.Count == 0) { return; } List<FeedItemOperation> operations = new List<FeedItemOperation>(); foreach (long feedItemId in feedItemIds) { FeedItemOperation operation = new FeedItemOperation() { @operator = Operator.REMOVE, operand = new FeedItem() { feedItemId = feedItemId, feedId = feedId } }; operations.Add(operation); } FeedItemService feedItemService = (FeedItemService) user.GetService( AdWordsService.v201509.FeedItemService); feedItemService.mutate(operations.ToArray()); return; }
/// <summary> /// Updates attribute value of the feed item. In order to update a FeedItemAttributeValue /// you must update the FeedItem. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The customer ID for which the call is made.</param> /// <param name="feedId">ID of the feed containing the feed item to be updated.</param> /// <param name="feedItemId">ID of the feed item to be updated.</param> /// <param name="flightPlaceholderFieldName">the placeholder type for the attribute to be /// updated.</param> /// <param name="attributeValue">String value with which to update the /// FeedAttributeValue.</param> private void UpdateFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, string flightPlaceholderFieldName, string attributeValue) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService(Services.V5.FeedItemService); // Gets the feed resource name. string feedResourceName = ResourceNames.Feed(customerId, feedId); // Gets a map of the placeholder values and feed attributes. Dictionary <FlightPlaceholderField, FeedAttribute> feedAttributes = GetFeed(client, customerId, feedResourceName); // Gets the ID of the attribute to update. This is needed to specify which // FeedItemAttributeValue will be updated in the given FeedItem. FlightPlaceholderField placeholderField = (FlightPlaceholderField)Enum.Parse( typeof(FlightPlaceholderField), flightPlaceholderFieldName); long attributeId = feedAttributes[placeholderField].Id.Value; // Gets the feed item resource name. string feedItemResourceName = ResourceNames.FeedItem(customerId, feedId, feedItemId); // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedItemResourceName); // Creates the updated FeedItemAttributeValue. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = attributeId, StringValue = attributeValue }; // Creates a new FeedItem from the existing FeedItem. Any FeedItemAttributeValues that // are not included in the updated FeedItem will be removed from the FeedItem, which is // why you must create the FeedItem from the existing FeedItem and set the field(s) // that will be updated. int attributeIndex = feedItem.AttributeValues .Select((item, index) => new { item, index }) .Where(itemIndexPair => itemIndexPair.item.FeedAttributeId.Value == feedItemAttributeValue.FeedAttributeId.Value) .Select(itemIndexPair => itemIndexPair.index + 1) .FirstOrDefault() - 1; if (attributeIndex == -1) { throw new ArgumentException("No matching feed attribute found for " + $"value '{feedItemAttributeValue}'."); } feedItem.AttributeValues[attributeIndex] = feedItemAttributeValue; // Creates the operation. FeedItemOperation operation = new FeedItemOperation() { Update = feedItem, UpdateMask = FieldMasks.AllSetFieldsOf(feedItem) }; // Updates the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Updated feed item with resource name '{result.ResourceName}'."); } }
/// <summary> /// Creates a FeedItemOperation that will create a FeedItem with the /// specified values and ad group target when sent to /// FeedItemService.mutate. /// </summary> /// <param name="adCustomizerFeed">The ad customizer feed.</param> /// <param name="name">The value for the name attribute of the FeedItem. /// </param> /// <param name="price">The value for the price attribute of the FeedItem. /// </param> /// <param name="date">The value for the date attribute of the FeedItem. /// </param> /// <param name="adGroupId">The ID of the ad group to target with the /// FeedItem.</param> /// <returns>A new FeedItemOperation for adding a FeedItem.</returns> private static FeedItemOperation CreateFeedItemAddOperation(AdCustomizerFeed adCustomizerFeed, string name, string price, String date, long adGroupId) { FeedItem feedItem = new FeedItem(); feedItem.feedId = adCustomizerFeed.feedId; List<FeedItemAttributeValue> attributeValues = new List<FeedItemAttributeValue>(); // FeedAttributes appear in the same order as they were created // - Name, Price, Date. See CreateCustomizerFeed method for details. FeedItemAttributeValue nameAttributeValue = new FeedItemAttributeValue(); nameAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[0].id; nameAttributeValue.stringValue = name; attributeValues.Add(nameAttributeValue); FeedItemAttributeValue priceAttributeValue = new FeedItemAttributeValue(); priceAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[1].id; priceAttributeValue.stringValue = price; attributeValues.Add(priceAttributeValue); FeedItemAttributeValue dateAttributeValue = new FeedItemAttributeValue(); dateAttributeValue.feedAttributeId = adCustomizerFeed.feedAttributes[2].id; dateAttributeValue.stringValue = date; attributeValues.Add(dateAttributeValue); feedItem.attributeValues = attributeValues.ToArray(); feedItem.adGroupTargeting = new FeedItemAdGroupTargeting(); feedItem.adGroupTargeting.TargetingAdGroupId = adGroupId; FeedItemOperation feedItemOperation = new FeedItemOperation(); feedItemOperation.operand = feedItem; feedItemOperation.@operator = Operator.ADD; return feedItemOperation; }
/// <summary> /// Adds a new item to the feed. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID for which the flights feed is /// added.</param> /// <param name="feedAttributes">The feed attributes.</param> /// <param name="feedResourceName">The resource name of the feed.</param> private void CreateFeedItem(GoogleAdsClient client, long customerId, Dictionary <FlightPlaceholderField, FeedAttribute> feedAttributes, string feedResourceName) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService( Services.V5.FeedItemService); // Creates the flight description feed attribute value. FeedItemAttributeValue flightDescription = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[FlightPlaceholderField.FlightDescription].Id, StringValue = "Earth to Mars" }; // Creates the destination ID feed attribute value. FeedItemAttributeValue destinationId = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[FlightPlaceholderField.DestinationId].Id, StringValue = "Mars" }; // Creates the flight price feed attribute value. FeedItemAttributeValue flightPrice = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[FlightPlaceholderField.FlightPrice].Id, StringValue = "499.99 USD" }; // Creates the flight sale price feed attribute value. FeedItemAttributeValue flightSalePrice = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[FlightPlaceholderField.FlightSalePrice].Id, StringValue = "299.99 USD" }; // Creates the final URLs feed attribute value. FeedItemAttributeValue finalUrls = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[FlightPlaceholderField.FinalUrls].Id, StringValues = { "http://www.example.com/flights/" } }; // Creates the FeedItem, specifying the Feed ID and the attributes created above. FeedItem feedItem = new FeedItem() { Feed = feedResourceName, AttributeValues = { flightDescription, destinationId, flightPrice, flightSalePrice, finalUrls } }; // Creates an operation to add the FeedItem. FeedItemOperation operation = new FeedItemOperation() { Create = feedItem }; // Adds the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new FeedItemOperation[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Created feed item with resource name '{result.ResourceName}'."); } }
/// <summary> /// Adds a new item to the feed. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID for which the real estate feed is /// added.</param> /// <param name="feedAttributes">The feed attributes.</param> /// <param name="feedResourceName">The resource name of the feed.</param> private void CreateFeedItem(GoogleAdsClient client, long customerId, Dictionary <RealEstatePlaceholderField, FeedAttribute> feedAttributes, string feedResourceName) { // Get the FeedItemServiceClient. FeedItemServiceClient feedItemService = client.GetService( Services.V3.FeedItemService); // Creates the listing ID feed attribute value. FeedItemAttributeValue listingId = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[RealEstatePlaceholderField.ListingId].Id, StringValue = "ABC123DEF" }; // Creates the listing name feed attribute value. FeedItemAttributeValue listingName = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[RealEstatePlaceholderField.ListingName].Id, StringValue = "Two bedroom with magnificent views" }; // Creates the final URLs feed attribute value. FeedItemAttributeValue finalUrls = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[RealEstatePlaceholderField.FinalUrls].Id, StringValue = "http://www.example.com/listings/" }; // Creates the image URL feed attribute value. FeedItemAttributeValue imageUrl = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[RealEstatePlaceholderField.ImageUrl].Id, StringValue = "http://www.example.com/listings/images?listing_id=ABC123DEF" }; // Creates the contextual keywords feed attribute value. FeedItemAttributeValue contextualKeywords = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes[RealEstatePlaceholderField.ContextualKeywords].Id, StringValues = { "beach community", "ocean view", "two bedroom", } }; // Creates the FeedItem, specifying the Feed ID and the attributes created above. FeedItem feedItem = new FeedItem() { Feed = feedResourceName, AttributeValues = { listingId, listingName, finalUrls, imageUrl, contextualKeywords } }; // Creates an operation to add the FeedItem. FeedItemOperation operation = new FeedItemOperation() { Create = feedItem }; // Adds the feed item. MutateFeedItemsResponse response = feedItemService.MutateFeedItems(customerId.ToString(), new FeedItemOperation[] { operation }); foreach (MutateFeedItemResult result in response.Results) { Console.WriteLine($"Created feed item with resource name " + $"'{result.ResourceName}'."); } }