Google Ads API is returning to beta status. Please read our blog post for more details.

Example: Updating flight prices

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:

  1. Get the mapping from placeholder to feed attribute ID using the utility method.
  2. Use the mapping and placeholder field to get the ID of the FeedItemAttributeValue.
  3. Get the FeedItem containing the FeedItemAttributeValue that will be updated.
  4. Create a FeedItemAttributeValue using the feed_attribute_id obtained previously and the updated value of the FeedItemAttributeValue.
  5. Construct 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 fields that are to be updated.
  6. Create a new FeedItemOperation as an update operation with an update_mask of the set fields of the newly created FeedItem.
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()
    .getValue();
  // 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(Int64Value.of(attributeId))
          .setStringValue(StringValue.of(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:

  1. Get the ID of the attribute you want to remove.
  2. Retrieve the FeedItem you will be updating.
  3. Create a new FeedItemAttributeValue with the attribute ID you retrieved earlier. You don't have to set the value for the FeedItemAttributeValue because the FeedItemAttributeValue will be removed.
  4. Get the index of the attribute that will be removed.
  5. Remove the attribute value from the FeedItem and perform the update operation on the FeedItem 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().getValue();

  // 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(Int64Value.of(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();
}