/// <summary> /// Gets the ID of the attribute. This is needed to specify which FeedItemAttributeValue /// will be updated in the given FeedItem. /// </summary> /// <param name="feedItem">The FeedItem that will be updated.</param> /// <param name="newFeedItemAttributeValue">The new FeedItemAttributeValue that will be /// updated.</param> /// <returns>The index of the attribute.</returns> private int GetAttributeIndex(FeedItem feedItem, FeedItemAttributeValue newFeedItemAttributeValue) { int attributeIndex = -1; // Loops through attribute values to find the index of the FeedItemAttributeValue // to update. foreach (FeedItemAttributeValue feedItemAttributeValue in feedItem.AttributeValues) { attributeIndex = (attributeIndex == -1) ? attributeIndex + 1 : 0; // Checks if the current feedItemAttributeValue is the one we are updating if (feedItemAttributeValue.FeedAttributeId == newFeedItemAttributeValue.FeedAttributeId) { break; } } // Throws an exception if the attribute value is not found. if (attributeIndex == -1) { new ArgumentException($"No matching feed attribute for feed item attribute " + $"value: {newFeedItemAttributeValue}"); } return(attributeIndex); }
/// <summary> /// Helper function to create a FeedItemOperation. /// </summary> /// <param name="name">The value of the Name attribute.</param> /// <param name="price">The value of the Price attribute.</param> /// <param name="date">The value of the Date attribute.</param> /// <param name="feedResourceName">The resource name of the feed.</param> /// <param name="feedAttributes">The attributes to be set on the feed.</param> /// <returns>A FeedItemOperation to create a feed item.</returns> private FeedItemOperation CreateFeedItemOperation(string name, string price, string date, string feedResourceName, Dictionary <string, FeedAttribute> feedAttributes) { FeedItemAttributeValue nameAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes["Name"].Id, StringValue = name }; FeedItemAttributeValue priceAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes["Price"].Id, StringValue = price }; FeedItemAttributeValue dateAttributeValue = new FeedItemAttributeValue() { FeedAttributeId = feedAttributes["Date"].Id, StringValue = date }; FeedItem feedItem = new FeedItem() { Feed = feedResourceName, AttributeValues = { nameAttributeValue, priceAttributeValue, dateAttributeValue } }; return(new FeedItemOperation() { Create = feedItem }); }
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> /// 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 { feedAttributeId = details.urlAttributeId, // See https://support.google.com/adwords/answer/7166527 for page feed URL recommendations // and rules. stringValues = new string[] { url } }; FeedItemAttributeValue labelAttributeValue = new FeedItemAttributeValue { feedAttributeId = details.labelAttributeId, stringValues = new string[] { label } }; // Create the feed item and operation. FeedItem item = new FeedItem { feedId = details.feedId, attributeValues = new FeedItemAttributeValue[] { urlAttributeValue, labelAttributeValue } }; FeedItemOperation operation = new FeedItemOperation { operand = item, @operator = Operator.ADD }; return(operation); }
private static FeedItemOperation NewSitelinkFeedItemAddOperation( SitelinksDataHolder sitelinksData, string text, string finalUrl, string line2, string line3, bool restrictToLop) { // Create the FeedItemAttributeValues for our text values. FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue() { feedAttributeId = sitelinksData.LinkTextFeedAttributeId, stringValue = text }; FeedItemAttributeValue linkFinalUrlAttributeValue = new FeedItemAttributeValue() { feedAttributeId = sitelinksData.LinkFinalUrlFeedAttributeId, stringValues = new string[] { finalUrl } }; FeedItemAttributeValue line2AttributeValue = new FeedItemAttributeValue() { feedAttributeId = sitelinksData.Line2FeedAttributeId, stringValue = line2 }; FeedItemAttributeValue line3AttributeValue = new FeedItemAttributeValue() { feedAttributeId = sitelinksData.Line3FeedAttributeId, stringValue = line3 }; // Create the feed item and operation. FeedItem item = new FeedItem() { feedId = sitelinksData.FeedId, attributeValues = new FeedItemAttributeValue[] { linkTextAttributeValue, linkFinalUrlAttributeValue, line2AttributeValue, line3AttributeValue } }; // OPTIONAL: Restrict targeting only to people physically within the location. if (restrictToLop) { item.geoTargetingRestriction = new FeedItemGeoRestriction() { geoRestriction = GeoRestriction.LOCATION_OF_PRESENCE }; } return(new FeedItemOperation() { operand = item, @operator = Operator.ADD }); }
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> ///Creates FeedItem Object from SiteLinkModel Object /// </summary> /// <param name="siteLinkModelObject">SiteLinkModelObject</param> /// <returns>Feed Item</returns> public FeedItem CreateFeedItem(SiteLinkModel siteLinkModelObject) { FeedItem feedItem = new FeedItem(); feedItem.feedId = siteLinkModelObject.FeedId; feedItem.feedItemId = siteLinkModelObject.FeedItemId; //Feed Attribute ID for Text is 1 FeedItemAttributeValue text = new FeedItemAttributeValue(); text.feedAttributeId = 1; text.stringValue = siteLinkModelObject.Text; //Feed Attribute ID for Description Line#1 is 3 FeedItemAttributeValue descriptionLine1 = new FeedItemAttributeValue(); descriptionLine1.feedAttributeId = 3; descriptionLine1.stringValue = siteLinkModelObject.DescriptionLine1; //Feed Attribute ID for Description Line2 is 4 FeedItemAttributeValue descriptionLine2 = new FeedItemAttributeValue(); descriptionLine2.feedAttributeId = 4; descriptionLine2.stringValue = siteLinkModelObject.DescriptionLine2; //Feed Attribute ID for Final URL is 5 FeedItemAttributeValue finalUrl = new FeedItemAttributeValue(); finalUrl.feedAttributeId = 5; finalUrl.stringValues = new string[] { siteLinkModelObject.Url }; feedItem.attributeValues = new FeedItemAttributeValue[] { text, descriptionLine1, descriptionLine2, finalUrl }; return(feedItem); }
/// <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); }
/// <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> /// Removes an attribute value from the specified feed item. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID that has the flights feed.</param> /// <param name="feedId">The feed ID that contains the target feed item.</param> /// <param name="feedItemId">The feed item ID that will be updated.</param> /// <param name="placeholdersToFeedAttributesMap">A mapping of FlightPlaceholderFields to /// FeedAttributes.</param> /// <param name="flightPlaceholderFieldName">The attributed field name to remove.</param> /// <returns>The modified feed item.</returns> /// <exception cref="ArgumentException">If the specified attribute was not found in the /// feed item.</exception> private FeedItem RemoveAttributeValueFromFeedItem(GoogleAdsClient client, long customerId, long feedId, long feedItemId, Dictionary <FlightPlaceholderField, FeedAttribute> placeholdersToFeedAttributesMap, FlightPlaceholderField flightPlaceholderFieldName) { // Gets the ID of the FeedAttribute for the placeholder field. long attributeId = placeholdersToFeedAttributesMap[flightPlaceholderFieldName].Id.Value; // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = GetFeedItem(client, customerId, feedId, feedItemId); //Creates the FeedItemAttributeValue that will be updated. FeedItemAttributeValue feedItemAttributeValue = new FeedItemAttributeValue { FeedAttributeId = attributeId }; // Gets the index of the attribute value that will be removed. // int attributeIndex = GetAttributeIndex(feedItem, feedItemAttributeValue); 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}'."); } // Returns the feed item with the removed FeedItemAttributeValue. 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 removed. feedItem.AttributeValues.RemoveAt(attributeIndex); return(feedItem); }
/// <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); }
/// <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> /// 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> /// 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}'."); } }
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; }
/// <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}'."); } }