Assume you have a flight reservation site where you've set up Dynamic
Remarketing ads, and you want to update the sale price on some flights and
remove the sale price on others. To ensure that your ads display the correct
prices for each flight, you'll want to update the price on each flight's
corresponding FeedItem
.
Updating the price of a flight
This is similar to creating new FeedItem
objects. However, to update the
FeedItemAttributeValue
of flight price, you
must update the FeedItem
with the new FeedItemAttributeValue
. In order to do
so, you must retrieve the feed_attribute_id
using the
FlightPlaceholderField
, so that you can indicate which
FeedAttribute
. It's also important to note that you
must include all of the other existing FeedItemAttributeValue
objects that you
don't want to change in the FeedItem
. If you fail to do so, they will be
removed from the FeedItem
. Full list of FlightPlaceholderField
values.
The basic steps to modify each flight entry are:
- Get the mapping from placeholder to feed attribute ID using the utility method.
- Use the mapping and placeholder field to get the ID of the
FeedItemAttributeValue
. - Get the
FeedItem
containing theFeedItemAttributeValue
that will be updated. - Create a
FeedItemAttributeValue
using thefeed_attribute_id
obtained previously and the updated value of theFeedItemAttributeValue
. - Construct a new
FeedItem
from the existingFeedItem
. AnyFeedItemAttributeValues
that are not included in the updatedFeedItem
will be removed from theFeedItem
, which is why you must create theFeedItem
from the existingFeedItem
and set the fields that are to be updated. - Create a new
FeedItemOperation
as anupdate
operation with anupdate_mask
of the set fields of the newly createdFeedItem
.
private void updateFeedItem( GoogleAdsClient googleAdsClient, long customerId, long feedId, long feedItemId, String flightPlaceholderField, String attributeValue) { // Gets the feed resource name. String feedResourceName = ResourceNames.feed(customerId, feedId); // Gets a map of the placeholder values and feed attributes. Map<FlightPlaceholderField, FeedAttribute> feedAttributes = AddFlightsFeed.getFeed(googleAdsClient, customerId, feedResourceName); // Gets the ID of the attribute to update. This is needed to specify which // FeedItemAttributeValue will be updated in the given FeedItem. long attributeId = feedAttributes .get(FlightPlaceholderField.valueOf(flightPlaceholderField.toUpperCase())) .getId(); // 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(googleAdsClient, customerId, feedItemResourceName); // Creates the updated FeedItemAttributeValue. FeedItemAttributeValue feedItemAttributeValue = FeedItemAttributeValue.newBuilder() .setFeedAttributeId(attributeId) .setStringValue(attributeValue) .build(); // 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 = feedItem.toBuilder() // Sets the attribute value of the FeedItem given its index relative to other attributes // in the FeedItem. .setAttributeValues( // Gets the index of the attribute value that will be updated. getAttributeIndex(feedItem, feedItemAttributeValue), feedItemAttributeValue) .build(); // Creates the operation. FeedItemOperation operation = FeedItemOperation.newBuilder() .setUpdate(feedItem) .setUpdateMask(FieldMasks.allSetFieldsOf(feedItem)) .build(); // Creates the feed item service client. try (FeedItemServiceClient feedItemServiceClient = googleAdsClient.getLatestVersion().createFeedItemServiceClient()) { // Updates the feed item. MutateFeedItemsResponse response = feedItemServiceClient.mutateFeedItems( Long.toString(customerId), ImmutableList.of(operation)); for (MutateFeedItemResult result : response.getResultsList()) { System.out.printf("Updated feed item with resource name '%s'.%n", result.getResourceName()); } } }
Removing the sale price of a flight
Removing a FeedItemAttributeValue
from a FeedItem
requires basically the
same steps as updating a FeedItemAttributeValue
because, in either case, you
must perform an update FeedItemOperation
to update the FeedItem
. The only
difference is that instead of updating the FeedItemAttributeValue
from the
FeedItem
, you remove it.
The basic steps to remove an FeedItemAttributeValue
from a FeedItem
are:
- Get the ID of the attribute you want to remove.
- Retrieve the
FeedItem
you will be updating. - Create a new
FeedItemAttributeValue
with the attribute ID you retrieved earlier. You don't have to set the value for theFeedItemAttributeValue
because theFeedItemAttributeValue
will be removed. - Get the index of the attribute that will be removed.
- Remove the attribute value from the
FeedItem
and perform the update operation on theFeedItem
in the same way you would if you were updating the attribute value.
private FeedItem removeAttributeValueFromFeedItem( GoogleAdsClient googleAdsClient, long customerId, Map<FlightPlaceholderField, FeedAttribute> feedAttributes, String feedItemResourceName, FlightPlaceholderField flightPlaceholderField) { // Gets the ID of the FeedAttribute for the placeholder field and converts to an integer. long attributeId = feedAttributes.get(flightPlaceholderField).getId(); // Retrieves the feed item and its associated attributes based on its resource name. FeedItem feedItem = getFeedItem(googleAdsClient, customerId, feedItemResourceName); // Creates the FeedItemAttributeValue that will be updated. FeedItemAttributeValue feedItemAttributeValue = FeedItemAttributeValue.newBuilder().setFeedAttributeId(attributeId).build(); // Gets the index of the attribute value that will be removed. int attributeIndex = getAttributeIndex(feedItem, 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. return feedItem.toBuilder().removeAttributeValues(attributeIndex).build(); }