/// <summary>
        /// Creates two different feed items to enable two different ad customizations.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="feedResourceName">The resource name of the feed.</param>
        /// <param name="feedAttributes">The attributes of the feed.</param>
        /// <returns>The resource names of the feed items.</returns>
        private List <string> CreateFeedItems(GoogleAdsClient client, long customerId,
                                              string feedResourceName, Dictionary <string, FeedAttribute> feedAttributes)
        {
            // Get the FeedItemServiceClient.
            FeedItemServiceClient feedItemService =
                client.GetService(Services.V4.FeedItemService);

            List <FeedItemOperation> feedItemOperations = new List <FeedItemOperation>();

            DateTime marsDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

            feedItemOperations.Add(
                CreateFeedItemOperation("Mars", "$1234.56", marsDate.ToString("yyyyMMdd HHmmss"),
                                        feedResourceName, feedAttributes));

            DateTime venusDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);

            feedItemOperations.Add(
                CreateFeedItemOperation("Venus", "$1450.00", venusDate.ToString("yyyyMMdd HHmmss"),
                                        feedResourceName, feedAttributes));

            List <string>           feedItemResourceNames = new List <string>();
            MutateFeedItemsResponse response =
                feedItemService.MutateFeedItems(customerId.ToString(), feedItemOperations);

            Console.WriteLine($"Added {response.Results.Count} feed items:");

            foreach (MutateFeedItemResult result in response.Results)
            {
                string feedItemResourceName = result.ResourceName;
                feedItemResourceNames.Add(feedItemResourceName);
                Console.WriteLine($"Added feed item with resource name '{feedItemResourceName}'.");
            }
            return(feedItemResourceNames);
        }
        /// <summary>
        /// Creates feed items for a given feed.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <param name="feedResourceName">The feed resource for which feed items are created.
        /// </param>
        /// <param name="feedAttributes">The feed attributes.</param>
        /// <param name="dsaPageUrlLabel">The DSA page URL label.</param>
        private void CreateFeedItems(GoogleAdsClient client, long customerId,
                                     string feedResourceName,
                                     Dictionary <DsaPageFeedCriterionField, FeedAttribute> feedAttributes,
                                     string dsaPageUrlLabel)
        {
            // Get the FeedItemService.
            FeedItemServiceClient feedItemService = client.GetService(
                Services.V3.FeedItemService);

            List <string> urls = new List <string>
            {
                "http://www.example.com/discounts/rental-cars",
                "http://www.example.com/discounts/hotel-deals",
                "http://www.example.com/discounts/flight-deals"
            };

            List <FeedItemOperation> operations = new List <FeedItemOperation>();

            foreach (string url in urls)
            {
                // Create a feed item.
                FeedItem feedItem = new FeedItem()
                {
                    Feed            = feedResourceName,
                    AttributeValues =
                    {
                        // Create a url attribute.
                        new FeedItemAttributeValue()
                        {
                            FeedAttributeId = feedAttributes[DsaPageFeedCriterionField.PageUrl].Id,
                            StringValue     = url
                        },

                        // Creates a label attribute.
                        new FeedItemAttributeValue()
                        {
                            FeedAttributeId = feedAttributes[DsaPageFeedCriterionField.Label].Id,
                            StringValue     = dsaPageUrlLabel
                        }
                    }
                };

                operations.Add(new FeedItemOperation()
                {
                    Create = feedItem
                });
            }

            // Add the feed items.
            MutateFeedItemsResponse response =
                feedItemService.MutateFeedItems(customerId.ToString(), operations);

            // Display the results.
            foreach (MutateFeedItemResult result in response.Results)
            {
                Console.WriteLine($"Created feed items with resource " +
                                  $"name '{result.ResourceName}'.");
            }
        }
Example #3
0
 /// <summary>Snippet for GetFeedItem</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetFeedItemResourceNames()
 {
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     FeedItemName resourceName = FeedItemName.FromCustomerFeedItem("[CUSTOMER]", "[FEED_ITEM]");
     // Make the request
     FeedItem response = feedItemServiceClient.GetFeedItem(resourceName);
 }
Example #4
0
 /// <summary>Snippet for GetFeedItem</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetFeedItem()
 {
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     string resourceName = "customers/[CUSTOMER_ID]/feedItems/[FEED_ID]~[FEED_ITEM_ID]";
     // Make the request
     FeedItem response = feedItemServiceClient.GetFeedItem(resourceName);
 }
        /// <summary>Snippet for GetFeedItemAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task GetFeedItemResourceNamesAsync()
        {
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            FeedItemName resourceName = FeedItemName.FromCustomerFeedItem("[CUSTOMER]", "[FEED_ITEM]");
            // Make the request
            FeedItem response = await feedItemServiceClient.GetFeedItemAsync(resourceName);
        }
Example #6
0
        /// <summary>Snippet for GetFeedItemAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task GetFeedItemAsync()
        {
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            string resourceName = "customers/[CUSTOMER]/feedItems/[FEED_ITEM]";
            // Make the request
            FeedItem response = await feedItemServiceClient.GetFeedItemAsync(resourceName);
        }
Example #7
0
 /// <summary>Snippet for GetFeedItem</summary>
 public void GetFeedItem()
 {
     // Snippet: GetFeedItem(string, CallSettings)
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     string resourceName = "customers/[CUSTOMER]/feedItems/[FEED_ITEM]";
     // Make the request
     FeedItem response = feedItemServiceClient.GetFeedItem(resourceName);
     // End snippet
 }
Example #8
0
        /// <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>Snippet for GetFeedItem</summary>
 public void GetFeedItemResourceNames()
 {
     // Snippet: GetFeedItem(FeedItemName, CallSettings)
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     FeedItemName resourceName = FeedItemName.FromCustomerFeedFeedItem("[CUSTOMER_ID]", "[FEED_ID]", "[FEED_ITEM_ID]");
     // Make the request
     FeedItem response = feedItemServiceClient.GetFeedItem(resourceName);
     // End snippet
 }
 /// <summary>Snippet for GetFeedItem</summary>
 /// <remarks>
 /// This snippet has been automatically generated for illustrative purposes only.
 /// It may require modifications to work in your environment.
 /// </remarks>
 public void GetFeedItemRequestObject()
 {
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     GetFeedItemRequest request = new GetFeedItemRequest
     {
         ResourceNameAsFeedItemName = FeedItemName.FromCustomerFeedFeedItem("[CUSTOMER_ID]", "[FEED_ID]", "[FEED_ITEM_ID]"),
     };
     // Make the request
     FeedItem response = feedItemServiceClient.GetFeedItem(request);
 }
        /// <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);
 }
        /// <summary>Snippet for GetFeedItemAsync</summary>
        public async Task GetFeedItemAsync()
        {
            // Snippet: GetFeedItemAsync(string, CallSettings)
            // Additional: GetFeedItemAsync(string, CancellationToken)
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            string resourceName = "customers/[CUSTOMER_ID]/feedItems/[FEED_ID]~[FEED_ITEM_ID]";
            // Make the request
            FeedItem response = await feedItemServiceClient.GetFeedItemAsync(resourceName);

            // End snippet
        }
        /// <summary>Snippet for GetFeedItemAsync</summary>
        public async Task GetFeedItemResourceNamesAsync()
        {
            // Snippet: GetFeedItemAsync(FeedItemName, CallSettings)
            // Additional: GetFeedItemAsync(FeedItemName, CancellationToken)
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            FeedItemName resourceName = FeedItemName.FromCustomerFeedFeedItem("[CUSTOMER_ID]", "[FEED_ID]", "[FEED_ITEM_ID]");
            // Make the request
            FeedItem response = await feedItemServiceClient.GetFeedItemAsync(resourceName);

            // End snippet
        }
Example #15
0
        /// <summary>Snippet for GetFeedItemAsync</summary>
        public async Task GetFeedItemRequestObjectAsync()
        {
            // Snippet: GetFeedItemAsync(GetFeedItemRequest, CallSettings)
            // Additional: GetFeedItemAsync(GetFeedItemRequest, CancellationToken)
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            GetFeedItemRequest request = new GetFeedItemRequest
            {
                ResourceNameAsFeedItemName = FeedItemName.FromCustomerFeedItem("[CUSTOMER]", "[FEED_ITEM]"),
            };
            // Make the request
            FeedItem response = await feedItemServiceClient.GetFeedItemAsync(request);

            // End snippet
        }
        /// <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>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 MutateFeedItemsRequestObject()
 {
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     MutateFeedItemsRequest request = new MutateFeedItemsRequest
     {
         CustomerId = "",
         Operations =
         {
             new FeedItemOperation(),
         },
         PartialFailure = false,
         ValidateOnly   = false,
     };
     // Make the request
     MutateFeedItemsResponse response = feedItemServiceClient.MutateFeedItems(request);
 }
Example #18
0
        /// <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 items belong.</param>
        /// <param name="feedItemIds">The IDs of the feed items to remove.</param>
        public void Run(GoogleAdsClient client, long customerId, long feedId, long[] feedItemIds)
        {
            FeedItemServiceClient feedItemServiceClient = client.GetService(
                Services.V6.FeedItemService);

            // Creates the remove operations.
            List <FeedItemOperation> operations = new List <FeedItemOperation>();

            foreach (long feedItemId in feedItemIds)
            {
                // Constructs the feed item resource name.
                string feedItemResourceName =
                    ResourceNames.FeedItem(customerId, feedId, feedItemId);

                // Constructs an operation that will remove the feed item based on the resource
                // name and adds it to the collection of operations.
                operations.Add(new FeedItemOperation
                {
                    Remove = feedItemResourceName
                });
            }

            try
            {
                // Issues a mutate request to remove the feed items.
                MutateFeedItemsResponse response =
                    feedItemServiceClient.MutateFeedItems(customerId.ToString(), operations);

                //  Prints the resource names of the removed feed items.
                foreach (MutateFeedItemResult removedFeedItem in response.Results)
                {
                    Console.WriteLine("Removed feed item with resource name " +
                                      $"'{removedFeedItem.ResourceName}'");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
        /// <summary>Snippet for MutateFeedItemsAsync</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task MutateFeedItemsRequestObjectAsync()
        {
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            MutateFeedItemsRequest request = new MutateFeedItemsRequest
            {
                CustomerId = "",
                Operations =
                {
                    new FeedItemOperation(),
                },
                PartialFailure      = false,
                ValidateOnly        = false,
                ResponseContentType = ResponseContentTypeEnum.Types.ResponseContentType.Unspecified,
            };
            // Make the request
            MutateFeedItemsResponse response = await feedItemServiceClient.MutateFeedItemsAsync(request);
        }
 /// <summary>Snippet for MutateFeedItems</summary>
 public void MutateFeedItemsRequestObject()
 {
     // Snippet: MutateFeedItems(MutateFeedItemsRequest, CallSettings)
     // Create client
     FeedItemServiceClient feedItemServiceClient = FeedItemServiceClient.Create();
     // Initialize request argument(s)
     MutateFeedItemsRequest request = new MutateFeedItemsRequest
     {
         CustomerId = "",
         Operations =
         {
             new FeedItemOperation(),
         },
         PartialFailure      = false,
         ValidateOnly        = false,
         ResponseContentType = ResponseContentTypeEnum.Types.ResponseContentType.Unspecified,
     };
     // Make the request
     MutateFeedItemsResponse response = feedItemServiceClient.MutateFeedItems(request);
     // End snippet
 }
        /// <summary>Snippet for MutateFeedItemsAsync</summary>
        public async Task MutateFeedItemsRequestObjectAsync()
        {
            // Snippet: MutateFeedItemsAsync(MutateFeedItemsRequest, CallSettings)
            // Additional: MutateFeedItemsAsync(MutateFeedItemsRequest, CancellationToken)
            // Create client
            FeedItemServiceClient feedItemServiceClient = await FeedItemServiceClient.CreateAsync();

            // Initialize request argument(s)
            MutateFeedItemsRequest request = new MutateFeedItemsRequest
            {
                CustomerId = "",
                Operations =
                {
                    new FeedItemOperation(),
                },
                PartialFailure = false,
                ValidateOnly   = false,
            };
            // Make the request
            MutateFeedItemsResponse response = await feedItemServiceClient.MutateFeedItemsAsync(request);

            // End snippet
        }
Example #22
0
        /// <summary>
        /// Creates feed items, which fill out the feed table with data.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The customer ID for which the call is made.</param>
        /// <param name="feed">The feed for which the operation will be created.</param>
        /// <returns>A list of string Feed Item Resource Names.</returns>
        private List <string> CreateFeedItems(GoogleAdsClient client, long customerId, Feed feed)
        {
            FeedItemServiceClient feedItemService = client.GetService(Services.V4.FeedItemService);

            List <FeedItemOperation> operations = new List <FeedItemOperation>
            {
                NewFeedItemOperation(feed, "Home", "http://www.example.com",
                                     "Home line 1", "Home line 2"),
                NewFeedItemOperation(feed, "Stores",
                                     "http://www.example.com/stores", "Stores line 1", "Stores line 2"),
                NewFeedItemOperation(feed, "On Sale",
                                     "http://www.example.com/sale", "On Sale line 1", "On Sale line 2"),
                NewFeedItemOperation(feed, "Support",
                                     "http://www.example.com/support", "Support line 1", "Support line 2"),
                NewFeedItemOperation(feed, "Products",
                                     "http://www.example.com/catalogue",
                                     "Products line 1", "Products line 2"),
                NewFeedItemOperation(feed, "About Us", "http://www.example.com/about",
                                     "About Us line 1", "About Us line 2")
            };

            MutateFeedItemsResponse response =
                feedItemService.MutateFeedItems(customerId.ToString(), operations);

            // We will need the resource name of each feed item to use in targeting.
            List <string> feedItemResourceNames = new List <string>();

            Console.WriteLine("Created the following feed items:");
            foreach (MutateFeedItemResult feedItemResult in response.Results)
            {
                Console.WriteLine($"\t{feedItemResult.ResourceName}");

                feedItemResourceNames.Add(feedItemResult.ResourceName);
            }

            return(feedItemResourceNames);
        }
        /// <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}'.");
            }
        }
Example #24
0
        /// <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}'.");
            }
        }
        /// <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}'.");
            }
        }