You are an advertiser using AdWords to drive traffic to your site. You wish to have your ads appear with sitelinks in order to allow users to get to their desired destination on your site as quickly as possible. Previously, you would use ad extensions to accomplish this. Through a Java code example, we now show you how to use the new Feed services to create sitelinks in ads for a campaign.
Customer scenario
Suppose you are an advertiser and you have created some ads where you want sitelinks to appear. Here are the sitelinks you want to show under your ad:
| Sitelink name | Sitelink URL |
|---|---|
| Home Page | http://www.example.com |
| Store Locator | http://www.example.com/locations |
| Sale Items | http://www.example.com/discounts |
| Support | http://www.example.com/support |
| Products | http://www.example.com/prods |
| About Us | http://www.example.com/about |
Once you have created campaigns, ad groups, and ads, you will need to call four other services to create and show sitelinks:
- FeedService
- This service is used to describe to AdWords the shape of your data (i.e. the name of your tabular data as well as the column names and types).
- FeedItemService
- This service is used to populate your Feed with data.
- FeedMappingService
- This service is used to specify how the Feed will be used. We show how to use a Feed for sitelinks below.
- CampaignFeedService
- This service uses your Feed for the ads in a campaign. Note: CampaignFeedService is available only to enhanced campaigns.
Java code example
The Java code example in this document demonstrates how to add a sitelinks feed and associate it with a campaign. The example, as written, relies on the Java client library. You can download a copy of the code and follow along; a complete printout of the code is also provided at the end of the document.
Adding sitelinks to a campaign consists of four primary steps:
- Create the sitelinks feed.
- Populate the feed.
- Map the feed and placeholder fields.
- Associate the feed to the campaign.
1. Create the sitelinks feed.
We use FeedService to describe the shape of the data to be uploaded. The Feed contains all the shape information about the data. The Feed is given a name and has a set of FeedAttributes (columns). Each FeedAttribute also has a name and a type.
createSiteLinksFeed() uses FeedService to add and establish the
sitelinks feed:
private static void createSiteLinksFeed(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedService.
FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
// Create attributes.
FeedAttribute textAttribute = new FeedAttribute();
textAttribute.setType(FeedAttributeType.STRING);
textAttribute.setName("Link Text");
FeedAttribute urlAttribute = new FeedAttribute();
urlAttribute.setType(FeedAttributeType.URL);
urlAttribute.setName("Link URL");
// Create the feed.
Feed siteLinksFeed = new Feed();
siteLinksFeed.setName("Feed For Sitelinks");
siteLinksFeed.setAttributes(new FeedAttribute[] {textAttribute, urlAttribute});
siteLinksFeed.setOrigin(FeedOrigin.USER);
// Create operation.
FeedOperation operation = new FeedOperation();
operation.setOperand(siteLinksFeed);
operation.setOperator(Operator.ADD);
// Add the feed.
FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});
Feed savedFeed = result.getValue()[0];
siteLinksData.siteLinksFeedId = savedFeed.getId();
FeedAttribute[] savedAttributes = savedFeed.getAttributes();
siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
siteLinksData.linkUrlFeedAttributeId = savedAttributes[1].getId();
System.out.printf("Feed with name %s and ID %d with linkTextAttributeId %d"
+ " and linkUrlAttributeId %s was created.\n", savedFeed.getName(), savedFeed.getId(),
savedAttributes[0].getId(), savedAttributes[1].getId());
}
In order to describe the tabular data that will be used for sitelinks, the
method creates a Feed with two FeedAttributes: Link Text and
Link URL with types STRING and URL,
respectively. We name the feed Feed For Sitelinks, and because the feed
data will originate from the advertiser, we set the type of feed to
FeedOrigin.USER.
A mutate operation is called on FeedService to create the feed. Upon successfully adding the feed, FeedService returns the feed ID as well as all the attribute IDs. We store these IDs for use with other services later.
2. Populate the feed.
Now that the shape of the data has been described to AdWords, the sitelink data can be populated using FeedItemService:
private static void createSiteLinksFeedItems(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedItemService.
FeedItemServiceInterface feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
// Create operations to add FeedItems.
FeedItemOperation home =
newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com");
FeedItemOperation stores =
newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores");
FeedItemOperation onSale =
newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale");
FeedItemOperation support =
newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support");
FeedItemOperation products =
newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods");
FeedItemOperation aboutUs =
newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about");
FeedItemOperation[] operations =
new FeedItemOperation[] {home, stores, onSale, support, products, aboutUs};
FeedItemReturnValue result = feedItemService.mutate(operations);
for (FeedItem item : result.getValue()) {
System.out.printf("FeedItem with feedItemId %d was added.\n", item.getFeedItemId());
siteLinksData.siteLinkFeedItemIds.add(item.getFeedItemId());
}
}
createSiteLinksFeedItems creates the individual items for the
feed. Each feed item is created by calling newSiteLinkFeedItemAddOperation;
the siteLinksData object is passed as an argument so that the feed
item has access to the feed attributes.
The feed items returned by the mutate call include IDs which we store in
siteLinkFeedItemsIds for later use.
Here is the newSiteLinkFeedItemAddOperation method for
completeness:
private static FeedItemOperation newSiteLinkFeedItemAddOperation(
SiteLinksDataHolder siteLinksData, String text, String url) {
// Create the FeedItemAttributeValues for our text values.
FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
linkTextAttributeValue.setStringValue(text);
FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
linkUrlAttributeValue.setStringValue(url);
// Create the feed item and operation.
FeedItem item = new FeedItem();
item.setFeedId(siteLinksData.siteLinksFeedId);
item.setAttributeValues(
new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
FeedItemOperation operation = new FeedItemOperation();
operation.setOperand(item);
operation.setOperator(Operator.ADD);
return operation;
}
Here we construct FeedItemAttributeValues with the passed parameters and the feed attribute IDs that were stored previously. These attribute values are then put in a FeedItem, wrapped within an ADD operation, and returned by the method.
3. Map the feed and placeholder fields.
The FeedMappingService is used to define how the feed will be used. In this case, we are using the feed for sitelinks.
// See https://developers.google.com/adwords/api/docs/appendix/placeholders
// for a list of all the placeholder types and fields.
private static final int PLACEHOLDER_SITELINKS = 1;
// See https://developers.google.com/adwords/api/docs/appendix/placeholders
// for a list of all the placeholder types and fields.
private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;
private static void createSiteLinksFeedMapping(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedItemService.
FeedMappingServiceInterface feedMappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
// Map the FeedAttributeIds to the fieldId constants.
AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();
linkUrlFieldMapping.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
linkUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_URL);
// Create the FieldMapping and operation.
FeedMapping feedMapping = new FeedMapping();
feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
feedMapping.setAttributeFieldMappings(
new AttributeFieldMapping[] {linkTextFieldMapping, linkUrlFieldMapping});
FeedMappingOperation operation = new FeedMappingOperation();
operation.setOperand(feedMapping);
operation.setOperator(Operator.ADD);
// Save the field mapping.
FeedMappingReturnValue result =
feedMappingService.mutate(new FeedMappingOperation[] {operation});
for (FeedMapping savedFeedMapping : result.getValue()) {
System.out.printf(
"Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.\n",
savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
savedFeedMapping.getFeedId());
}
}
This method configures the feed to be used for sitelinks. It does this by
setting a placeholder type on the FeedMapping to the constant
PLACEHOLDER_SITELINKS. The value for this constant is
1. The method also maps the FeedAttributes to the placeholder
fields that are needed for sitelinks: link text and link URL. This mapping tells
the serving system what feed attributes are used for the different aspects of
the sitelinks.
4. Associate the feed to the campaign.
At this point, the feed is ready to be used for sitelinks. The last step is to associate the feed with a campaign so that the sitelinks can be used when serving ads in the campaign. This is done via CampaignFeedService.
private static void createSiteLinksCampaignFeed(AdWordsServices adWordsServices,
AdWordsSession session, SiteLinksDataHolder siteLinksData, Long campaignId) throws Exception {
// Get the CampaignFeedService.
CampaignFeedServiceInterface campaignFeedService =
adWordsServices.get(session, CampaignFeedServiceInterface.class);
RequestContextOperand requestContextOperand = new RequestContextOperand();
requestContextOperand.setContextType(RequestContextOperandContextType.FEED_ITEM_ID);
Function function = new Function();
function.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
function.setOperator(FunctionOperator.IN);
List<FunctionArgumentOperand> operands = new ArrayList<FunctionArgumentOperand>();
for (long feedItemId : siteLinksData.siteLinkFeedItemIds) {
ConstantOperand constantOperand = new ConstantOperand();
constantOperand.setLongValue(feedItemId);
constantOperand.setType(ConstantOperandConstantType.LONG);
operands.add(constantOperand);
}
function.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));
CampaignFeed campaignFeed = new CampaignFeed();
campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
campaignFeed.setCampaignId(campaignId);
campaignFeed.setMatchingFunction(function);
// Specifying placeholder types on the CampaignFeed allows the same feed
// to be used for different placeholders in different Campaigns.
campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});
CampaignFeedOperation operation = new CampaignFeedOperation();
operation.setOperand(campaignFeed);
operation.setOperator(Operator.ADD);
CampaignFeedReturnValue result =
campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
for (CampaignFeed savedCampaignFeed : result.getValue()) {
System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
}
}
This method creates an association between a campaign and a feed. There are two pieces of this data that are configured with this association: the matching function and the placeholder types.
You may be wondering why the placeholder type (sitelinks) needs to be configured for the CampaignFeed. In the FeedMapping call, we configured the feed to be used for sitelinks. Configuring the placeholder types in CampaignFeedService provides the flexibility to create multiple feed mappings for the same feed. This allows the feed to be used for different placeholders in different campaigns.
Setting a matching function in the CampaignFeed tells the serving system which feed items can be used as sitelinks. In this example, we used FEED_ITEM_ID as a RequestContextOperand in our matching Function; however, we could have just as easily used another RequestContextOperand type, or even matched using a FeedAttributeOperand such as FeedAttributeId.
Complete source code
Here is the complete printout of the code that creates a sitelinks feed for a campaign.
package adwords.axis.v201302.adextensions;
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201302.cm.AttributeFieldMapping;
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeed;
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedOperation;
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedReturnValue;
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedServiceInterface;
import com.google.api.ads.adwords.axis.v201302.cm.ConstantOperand;
import com.google.api.ads.adwords.axis.v201302.cm.ConstantOperandConstantType;
import com.google.api.ads.adwords.axis.v201302.cm.Feed;
import com.google.api.ads.adwords.axis.v201302.cm.FeedAttribute;
import com.google.api.ads.adwords.axis.v201302.cm.FeedAttributeType;
import com.google.api.ads.adwords.axis.v201302.cm.FeedItem;
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemAttributeValue;
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemOperation;
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemReturnValue;
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemServiceInterface;
import com.google.api.ads.adwords.axis.v201302.cm.FeedMapping;
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingOperation;
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingReturnValue;
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingServiceInterface;
import com.google.api.ads.adwords.axis.v201302.cm.FeedOperation;
import com.google.api.ads.adwords.axis.v201302.cm.FeedOrigin;
import com.google.api.ads.adwords.axis.v201302.cm.FeedReturnValue;
import com.google.api.ads.adwords.axis.v201302.cm.FeedServiceInterface;
import com.google.api.ads.adwords.axis.v201302.cm.Function;
import com.google.api.ads.adwords.axis.v201302.cm.FunctionArgumentOperand;
import com.google.api.ads.adwords.axis.v201302.cm.FunctionOperator;
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
import com.google.api.ads.adwords.axis.v201302.cm.RequestContextOperand;
import com.google.api.ads.adwords.axis.v201302.cm.RequestContextOperandContextType;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;
import java.util.ArrayList;
import java.util.List;
/**
* This example adds a sitelinks feed and associates it with a campaign.
*
* Credentials and properties in {@code fromFile()} are pulled from the "ads.properties" file. See
* README for more info.
*
* Tags: CampaignFeedService.mutate, FeedItemService.mutate, FeedMappingService.mutate
* Tags: FeedService.mutate
*
* @author [email protected] (Kevin Winter)
*/
public class AddSiteLinks {
public static void main(String[] args) throws Exception {
// Get a ClientLogin AuthToken.
String clientLoginToken = new ClientLoginTokens.Builder()
.forApi(ClientLoginTokens.Api.ADWORDS)
.fromFile()
.build()
.requestToken();
// Construct an AdWordsSession.
AdWordsSession session =
new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();
AdWordsServices adWordsServices = new AdWordsServices();
// Campaign must be enhanced.
Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
runExample(adWordsServices, session, campaignId);
}
public static void runExample(
AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
SiteLinksDataHolder siteLinksData = new SiteLinksDataHolder();
createSiteLinksFeed(adWordsServices, session, siteLinksData);
createSiteLinksFeedItems(adWordsServices, session, siteLinksData);
createSiteLinksFeedMapping(adWordsServices, session, siteLinksData);
createSiteLinksCampaignFeed(adWordsServices, session, siteLinksData, campaignId);
}
private static void createSiteLinksFeed(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedService.
FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
// Create attributes.
FeedAttribute textAttribute = new FeedAttribute();
textAttribute.setType(FeedAttributeType.STRING);
textAttribute.setName("Link Text");
FeedAttribute urlAttribute = new FeedAttribute();
urlAttribute.setType(FeedAttributeType.URL);
urlAttribute.setName("Link URL");
// Create the feed.
Feed siteLinksFeed = new Feed();
siteLinksFeed.setName("Feed For Sitelinks");
siteLinksFeed.setAttributes(new FeedAttribute[] {textAttribute, urlAttribute});
siteLinksFeed.setOrigin(FeedOrigin.USER);
// Create operation.
FeedOperation operation = new FeedOperation();
operation.setOperand(siteLinksFeed);
operation.setOperator(Operator.ADD);
// Add the feed.
FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});
Feed savedFeed = result.getValue()[0];
siteLinksData.siteLinksFeedId = savedFeed.getId();
FeedAttribute[] savedAttributes = savedFeed.getAttributes();
siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
siteLinksData.linkUrlFeedAttributeId = savedAttributes[1].getId();
System.out.printf("Feed with name %s and ID %d with linkTextAttributeId %d"
+ " and linkUrlAttributeId %s was created.\n", savedFeed.getName(), savedFeed.getId(),
savedAttributes[0].getId(), savedAttributes[1].getId());
}
private static void createSiteLinksFeedItems(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedItemService.
FeedItemServiceInterface feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
// Create operations to add FeedItems.
FeedItemOperation home =
newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com");
FeedItemOperation stores =
newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores");
FeedItemOperation onSale =
newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale");
FeedItemOperation support =
newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support");
FeedItemOperation products =
newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods");
FeedItemOperation aboutUs =
newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about");
FeedItemOperation[] operations =
new FeedItemOperation[] {home, stores, onSale, support, products, aboutUs};
FeedItemReturnValue result = feedItemService.mutate(operations);
for (FeedItem item : result.getValue()) {
System.out.printf("FeedItem with feedItemId %d was added.\n", item.getFeedItemId());
siteLinksData.siteLinkFeedItemIds.add(item.getFeedItemId());
}
}
// See https://developers.google.com/adwords/api/docs/appendix/placeholders
// for a list of all the placeholder types and fields.
private static final int PLACEHOLDER_SITELINKS = 1;
// See https://developers.google.com/adwords/api/docs/appendix/placeholders
// for a list of all the placeholder types and fields.
private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;
private static void createSiteLinksFeedMapping(
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
throws Exception {
// Get the FeedItemService.
FeedMappingServiceInterface feedMappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
// Map the FeedAttributeIds to the fieldId constants.
AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();
linkUrlFieldMapping.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
linkUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_URL);
// Create the FieldMapping and operation.
FeedMapping feedMapping = new FeedMapping();
feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
feedMapping.setAttributeFieldMappings(
new AttributeFieldMapping[] {linkTextFieldMapping, linkUrlFieldMapping});
FeedMappingOperation operation = new FeedMappingOperation();
operation.setOperand(feedMapping);
operation.setOperator(Operator.ADD);
// Save the field mapping.
FeedMappingReturnValue result =
feedMappingService.mutate(new FeedMappingOperation[] {operation});
for (FeedMapping savedFeedMapping : result.getValue()) {
System.out.printf(
"Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.\n",
savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
savedFeedMapping.getFeedId());
}
}
private static void createSiteLinksCampaignFeed(AdWordsServices adWordsServices,
AdWordsSession session, SiteLinksDataHolder siteLinksData, Long campaignId) throws Exception {
// Get the CampaignFeedService.
CampaignFeedServiceInterface campaignFeedService =
adWordsServices.get(session, CampaignFeedServiceInterface.class);
RequestContextOperand requestContextOperand = new RequestContextOperand();
requestContextOperand.setContextType(RequestContextOperandContextType.FEED_ITEM_ID);
Function function = new Function();
function.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
function.setOperator(FunctionOperator.IN);
List<FunctionArgumentOperand> operands = new ArrayList<FunctionArgumentOperand>();
for (long feedItemId : siteLinksData.siteLinkFeedItemIds) {
ConstantOperand constantOperand = new ConstantOperand();
constantOperand.setLongValue(feedItemId);
constantOperand.setType(ConstantOperandConstantType.LONG);
operands.add(constantOperand);
}
function.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));
CampaignFeed campaignFeed = new CampaignFeed();
campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
campaignFeed.setCampaignId(campaignId);
campaignFeed.setMatchingFunction(function);
// Specifying placeholder types on the CampaignFeed allows the same feed
// to be used for different placeholders in different Campaigns.
campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});
CampaignFeedOperation operation = new CampaignFeedOperation();
operation.setOperand(campaignFeed);
operation.setOperator(Operator.ADD);
CampaignFeedReturnValue result =
campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
for (CampaignFeed savedCampaignFeed : result.getValue()) {
System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
}
}
private static FeedItemOperation newSiteLinkFeedItemAddOperation(
SiteLinksDataHolder siteLinksData, String text, String url) {
// Create the FeedItemAttributeValues for our text values.
FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
linkTextAttributeValue.setStringValue(text);
FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
linkUrlAttributeValue.setStringValue(url);
// Create the feed item and operation.
FeedItem item = new FeedItem();
item.setFeedId(siteLinksData.siteLinksFeedId);
item.setAttributeValues(
new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
FeedItemOperation operation = new FeedItemOperation();
operation.setOperand(item);
operation.setOperator(Operator.ADD);
return operation;
}
private static class SiteLinksDataHolder {
private Long siteLinksFeedId;
private Long linkTextFeedAttributeId;
private Long linkUrlFeedAttributeId;
private List<Long> siteLinkFeedItemIds = new ArrayList<Long>();
}
}