/// <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
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public async Task MutateFeedItemsAsync()
        {
            Mock <FeedItemService.FeedItemServiceClient> mockGrpcClient = new Mock <FeedItemService.FeedItemServiceClient>(MockBehavior.Strict);
            MutateFeedItemsRequest expectedRequest = new MutateFeedItemsRequest
            {
                CustomerId     = "customerId-1772061412",
                Operations     = { },
                PartialFailure = true,
                ValidateOnly   = false,
            };
            MutateFeedItemsResponse expectedResponse = new MutateFeedItemsResponse();

            mockGrpcClient.Setup(x => x.MutateFeedItemsAsync(expectedRequest, It.IsAny <CallOptions>()))
            .Returns(new Grpc.Core.AsyncUnaryCall <MutateFeedItemsResponse>(Task.FromResult(expectedResponse), null, null, null, null));
            FeedItemServiceClient client = new FeedItemServiceClientImpl(mockGrpcClient.Object, null);
            string customerId            = "customerId-1772061412";
            IEnumerable <FeedItemOperation> operations = new List <FeedItemOperation>();
            bool partialFailure = true;
            bool validateOnly   = false;
            MutateFeedItemsResponse response = await client.MutateFeedItemsAsync(customerId, operations, partialFailure, validateOnly);

            Assert.AreEqual(expectedResponse, response);
            mockGrpcClient.VerifyAll();
        }
        /// <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}'.");
            }
        }
Beispiel #6
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}'.");
            }
        }