Feed Items

Use the FeedItemService to populate your existing Feed with FeedItem objects, which are analogous to database rows. Each FeedItem is a single row, and contains multiple FeedItemAttributeValues. Each FeedItemAttributeValue is data that corresponds to a specific feed attribute (column) in the feed (table) for this feed item (row). When populating feed items, essentially we are just adding rows with data to our existing table.

We will need the attribute IDs that we fetched from the feed that was created in the last step. Each FeedItemAttributeValue within the FeedItem needs to specify an attribute ID to tell the system which attribute (column) it corresponds to, and each FeedItem can have no more than one FeedItemAttributeValue with a given attribute ID.

Make sure you map the right feed attribute IDs to the right data. It can get a little tricky to keep track of, so creating a structure to store feed attribute IDs with some kind of identifier is often useful.

We are fetching the feed item IDs from the response at the end for use in a later step, but if you don't want to match on specific feed items, you can omit that step.

Ruby

client = Google::Ads::GoogleAds::GoogleAdsClient.new

data = {
  feed: feed_resource_name,
  # The attribute IDs come back in the same order that they were added.
  link_text_attribute_id: attribute_ids[0],
  final_url_attribute_id: attribute_ids[1],
  line_1_attribute_id: attribute_ids[2],
  line_2_attribute_id: attribute_ids[3],
}

def new_feed_item_operation(client, data, text, final_url, line_1, line_2)
  feed_item = client.resource.feed_item do |fi|
    fi.feed = data[:feed]

    fi.attribute_values << client.resource.feed_item_attribute_value do |av|
      av.feed_attribute_id = data[:link_text_attribute_id]
      av.string_value = text
    end
    fi.attribute_values << client.resource.feed_item_attribute_value do |av|
      av.feed_attribute_id = data[:final_url_attribute_id]
      av.string_values << final_url
    end
    fi.attribute_values << client.resource.feed_item_attribute_value do |av|
      av.feed_attribute_id = data[:line_1_attribute_id]
      av.string_value = line_1
    end
    fi.attribute_values << client.resource.feed_item_attribute_value do |av|
      av.feed_attribute_id = data[:line_2_attribute_id]
      av.string_value = line_2
    end
  end

  client.operation.create_resource.feed_item(feed_item)
end

operations = []
operations << new_feed_item_operation(client, data, "Home", "http://www.example.com", "Home line 1", "Home line 2")
operations << new_feed_item_operation(client, data, "Stores", "http://www.example.com/stores", "Stores line 1", "Stores line 2")
operations << new_feed_item_operation(client, data, "On Sale", "http://www.example.com/sale", "On Sale line 1", "On Sale line 2")
operations << new_feed_item_operation(client, data, "Support", "http://www.example.com/support", "Support line 1", "Support line 2")
operations << new_feed_item_operation(client, data, "Products", "http://www.example.com/catalogue", "Products line 1", "Products line 2")
operations << new_feed_item_operation(client, data, "About Us", "http://www.example.com/about", "About Us line 1", "About Us line 2")

response = client.service.feed_item.mutate_feed_items(customer_id, operations)

# We will need the resource name of the feed item to use in targeting.
feed_items = response.results.map {|result| result.resource_name}
# We may also need the feed item ID if we are going to use it in a mapping function.
# For feed items, the ID is the last part of the resource name, after the '~'.
feed_item_ids = feed_items.map do {|feed_item| resource_name.split('~').last}