要向广告系列添加附加链接,您需要完成以下几个步骤:1.创建附加链接Feed。2.填充Feed。3.映射Feed和占位符字段。4.将Feed与广告系列关联起来。1. 创建附加链接Feed。我们使用FeedService来描述要上传的数据的具体形态。Feed包含关于数据的全部形态信息。Feed具有指定名称,并包括一组FeedAttribute(列)。每个FeedAttribute还具有自己的名称和类型。注意:每个账户极限使用20个Feed。建议您为每个附近信息类型使用一个Feed。createSiteLinksFeed()使用FeedService来添加和创建附加链接Feed:private static void createSiteLinksFeed(AdWordsServices adWordsServices,AdWordsSession session,SiteLinksDataHolder siteLinksData)throws Exception {// 获取FeedService。FeedServiceInterface FeedService = adWordsServices.get(session,FeedServiceInterface.class);// 创建属性。FeedAttribute textAttribute = new FeedAttribute();textAttribute.setType(FeedAttributeType.STRING);textAttribute.setName("Link Text");FeedAttribute urlAttribute = new FeedAttribute();urlAttribute.setType(FeedAttributeType.URL);urlAttribute.setName("Link URL");FeedAttribute line1Attribute = new FeedAttribute();line1Attribute.setType(FeedAttributeType.STRING);line1Attribute.setName("Line 1 Description");FeedAttribute line2Attribute = new FeedAttribute();line2Attribute.setType(FeedAttributeType.STRING);line2Attribute.setName("Line 2 Description");// 创建Feed。Feed siteLinksFeed = new Feed();siteLinksFeed.setName("Feed For Sitelinks");siteLinksFeed.setAttributes( new FeedAttribute[] {textAttribute,urlAttribute,line1Attribute,line2Attribute});siteLinksFeed.setOrigin(FeedOrigin.USER);// 创建运算。FeedOperation operation = new FeedOperation();peration.setOperand(siteLinksFeed);operation.setOperator(Operator.ADD);// 添加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();siteLinksData.line1FeedAttributeId = savedAttributes[2].getId();siteLinksData.line2FeedAttributeId = savedAttributes[3].getId();System.out.printf("Feed with name '%s' and ID %d with linkTextAttributeId %d"+ " and linkUrlAttributeId %d and line1AttributeId %d"+ " and line2AttributeId %d was created.\n",savedFeed.getName(),savedFeed.getId(),savedAttributes[0].getId(),savedAttributes[1].getId(),savedAttributes[2].getId(),savedAttributes[3].getId()); }为描述用于附加链接的表格数据,该方法会创建一个Feed,此Feed包含2个FeedAttribute:Link Text和Link URL,其类型分别为STRING和URL。我们将此Feed命名为Feed For Sitelinks,由于Feed数据来源于广告客户,因此我们将Feed类型设置为FeedOrigin.USER。系统会在FeedService上调用Mutate操作,从而创建Feed。成功添加Feed后,FeedService将返回Feed ID以及所有属性ID。我们存储这些ID用于之后的其他服务。2. 填充Feed。向AdWords描述完数据形态后,可以使用FeedItemService填充附加链接数据:private static void createSiteLinksFeedItems(AdWordsServices adWordsServices,SiteLinksDataHolder siteLinksData)throws Exception {// 获取FeedItemService。FeedItemServiceInterface FeedItemService =adWordsServices.get(session,FeedItemServiceInterface.class);// 创建运算以添加FeedItem。FeedItemOperation home = newSiteLinkFeedItemAddOperation(siteLinksData,"Home","http://www.example.com","Home line 1","Home line 2");FeedItemOperation stores = newSiteLinkFeedItemAddOperation(siteLinksData,"Stores","http://www.example.com/stores","Stores line 1","Stores line 2");FeedItemOperation onSale = newSiteLinkFeedItemAddOperation(siteLinksData,"On Sale","http://www.example.com/sale","On Sale line 1","On Sale line 2");FeedItemOperation support = newSiteLinkFeedItemAddOperation(siteLinksData,"Support","http://www.example.com/support","Support line 1","Support line 2");FeedItemOperation products = newSiteLinkFeedItemAddOperation(siteLinksData,"Products","http://www.example.com/prods","Products line 1","Products line 2");FeedItemOperation aboutUs = newSiteLinkFeedItemAddOperation(siteLinksData,"About Us","http://www.example.com/about","About Us line 1","About Us line 2");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为Feed创建各个项目。每个Feed项均以调用newSiteLinkFeedItemAddOperation的方式创建;系统将siteLinksData对象作为参数传递,使Feed项能够访问Feed属性。Mutate调用返回的Feed项包含我们刚才存储在siteLinkFeedItemsIds中(以便之后使用)的ID。createSiteLinksFeed()使用FeedService来添加和创建附加链接Feed:private static void createSiteLinksFeed(AdWordsServices adWordsServices,FeedServiceInterface.class);// 创建属性。 FeedAttribute textAttribute = new FeedAttribute(); textAttribute.setType(FeedAttributeType.STRING); textAttribute.setName("Link Text"); FeedAttribute urlAttribute = new FeedAttribute(); urlAttribute.setType(FeedAttributeType.URL); urlAttribute.setName("Link URL"); FeedAttribute line1Attribute = new FeedAttribute(); line1Attribute.setType(FeedAttributeType.STRING); line1Attribute.setName("Line 1 Description"); FeedAttribute line2Attribute = new FeedAttribute(); line2Attribute.setType(FeedAttributeType.STRING); line2Attribute.setName("Line 2 Description"); // 创建Feed。 Feed siteLinksFeed = new Feed(); siteLinksFeed.setName("Feed For Sitelinks"); siteLinksFeed.setAttributes( new FeedAttribute[] {textAttribute,line2Attribute}); siteLinksFeed.setOrigin(FeedOrigin.USER); // 创建运算。 FeedOperation operation = new FeedOperation(); operation.setOperand(siteLinksFeed); operation.setOperator(Operator.ADD); // 添加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(); siteLinksData.line1FeedAttributeId = savedAttributes[2].getId(); siteLinksData.line2FeedAttributeId = savedAttributes[3].getId(); System.out.printf("Feed with name '%s' and ID %d with linkTextAttributeId %d" + " and linkUrlAttributeId %d and line1AttributeId %d" + " and line2AttributeId %d was created.\n",savedAttributes[3].getId()); }为描述用于附加链接的表格数据,该方法会创建一个Feed,此Feed包含2个FeedAttribute:Link Text和Link URL,其类型分别为STRING和URL。我们将此Feed命名为Feed For Sitelinks,由于Feed数据来源于广告客户,因此我们将Feed类型设置为FeedOrigin.USER。系统会在FeedService上调用Mutate操作,从而创建Feed。成功添加Feed后,FeedService将返回Feed ID以及所有属性ID。我们存储这些ID用于之后的其他服务。2. 填充Feed。向AdWords描述完数据形态后,可以使用FeedItemService填充附加链接数据: private static void createSiteLinksFeedItems( AdWordsServices adWordsServices,SiteLinksDataHolder siteLinksData) throws Exception { // 获取FeedItemService。 FeedItemServiceInterface FeedItemService = adWordsServices.get(session,FeedItemServiceInterface.class); // 创建运算以添加FeedItem。 FeedItemOperation home = newSiteLinkFeedItemAddOperation(siteLinksData,"Home line 2"); FeedItemOperation stores = newSiteLinkFeedItemAddOperation(siteLinksData,"Stores line 2"); FeedItemOperation onSale = newSiteLinkFeedItemAddOperation(siteLinksData,"On Sale line 2"); FeedItemOperation support = newSiteLinkFeedItemAddOperation(siteLinksData,"Support line 2"); FeedItemOperation products = newSiteLinkFeedItemAddOperation(siteLinksData,"Products line 2"); FeedItemOperation aboutUs = newSiteLinkFeedItemAddOperation(siteLinksData,"About Us line 2"); FeedItemOperation[] operations = new FeedItemOperation[] {home,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为Feed创建各个项目。每个Feed项均以调用newSiteLinkFeedItemAddOperation的方式创建;系统将siteLinksData对象作为参数传递,使Feed项能够访问Feed属性。Mutate调用返回的Feed项包含我们刚才存储在siteLinkFeedItemsIds中(以便之后使用)的ID。完整的newSiteLinkFeedItemAddOperation方法如下: private static FeedItemOperation newSiteLinkFeedItemAddOperation( SiteLinksDataHolder siteLinksData,String text,String url,String line1,String line2) { // 为文本值创建FeedItemAttributeValue。 FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue(); linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId); linkTextAttributeValue.setStringValue(text); FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue(); linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId); linkUrlAttributeValue.setStringValue(url); FeedItemAttributeValue line1TextAttributeValue = new FeedItemAttributeValue(); line1TextAttributeValue.setFeedAttributeId(siteLinksData.line1FeedAttributeId); line1TextAttributeValue.setStringValue(line1); FeedItemAttributeValue line2TextAttributeValue = new FeedItemAttributeValue(); line2TextAttributeValue.setFeedAttributeId(siteLinksData.line2FeedAttributeId); line2TextAttributeValue.setStringValue(line2); // 创建Feed项和运算。 FeedItem item = new FeedItem(); item.setFeedId(siteLinksData.siteLinksFeedId); item.setAttributeValues( new FeedItemAttributeValue[] {linkTextAttributeValue,linkUrlAttributeValue,line1TextAttributeValue,line2TextAttributeValue});3. 映射Feed和占位符字段。FeedMappingService用于指定Feed的使用方法。在本例中,我们将Feed用于附加链接。 // 查看包含所有占位符类型和字段的列表。 private static final int PLACEHOLDER_SITELINKS = 1; // 查看包含所有占位符类型和字段的列表。 private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1; private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2; private static final int PLACEHOLDER_FIELD_LINE_1_TEXT = 3; private static final int PLACEHOLDER_FIELD_LINE_2_TEXT = 4; private static void createSiteLinksFeedMapping( AdWordsServices adWordsServices,SiteLinksDataHolder siteLinksData) throws Exception { // 获取FeedItemService。 FeedMappingServiceInterface FeedMappingService = adWordsServices.get(session,FeedMappingServiceInterface.class); // 将FeedAttributeId映射到fieldId常量。 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); AttributeFieldMapping line1FieldMapping = new AttributeFieldMapping(); line1FieldMapping.setFeedAttributeId(siteLinksData.line1FeedAttributeId); line1FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_1_TEXT); AttributeFieldMapping line2FieldMapping = new AttributeFieldMapping(); line2FieldMapping.setFeedAttributeId(siteLinksData.line2FeedAttributeId); line2FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_2_TEXT); // 创建FieldMapping和运算。 FeedMapping FeedMapping = new FeedMapping(); FeedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS); FeedMapping.setFeedId(siteLinksData.siteLinksFeedId); FeedMapping.setAttributeFieldMappings( new AttributeFieldMapping[] {linkTextFieldMapping,linkUrlFieldMapping,line1FieldMapping,line2FieldMapping}); FeedMappingOperation operation = new FeedMappingOperation(); operation.setOperand(FeedMapping); operation.setOperator(Operator.ADD); // 保存字段映射。 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()); } }此方法将Feed配置为用于附加链接。具体配置方法是将FeedMapping上的一个占位符类型设置为常量PLACEHOLDER_SITELINKS。此常量的值为1。此方法还会将FeedAttribute映射到附加链接所需的占位符字段:link text和link URL。此映射关系可以告诉投放系统,哪些Feed属性被用在附加链接的不同方面。4. 将Feed与广告系列或广告组关联起来。此时,我们创建的Feed已经可以用于附加链接了。最后一步是将Feed与广告系列(CampaignFeedService)或广告组(AdGroupFeedService)关联起来,以便投放广告时使用附加链接。是否将Feed与广告系列或广告组关联起来,这取决于映射所需的控制级别。如果某个Feed适用于整个广告系列,则该Feed应该映射到广告系列一级。但是,如果一个广告系列包含多个可以使用的Feed,那么就应该在广告组一级映射Feed。private static void createSiteLinksCampaignFeed(AdWordsServices adWordsServices,SiteLinksDataHolder siteLinksData,Long campaignId) throws Exception { // 获取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); // 在CampaignFeed上指定占位符类型可允许相同的Feed // 针对不同广告系列的不同占位符使用。 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()); } }此方法在广告系列和Feed间建立了关联。此数据中有两个部分配置了此关联:匹配函数和占位符类型。您可能想知道,为什么占位符类型(sitelinks)需要针对CampaignFeed进行配置。在FeedMapping调用中,我们会将Feed配置为用于附加链接。在CampaignFeedService中配置占位符类型后,您可以为同一Feed灵活创建多个Feed映射。这样一来,该Feed就能针对不同广告系列中的不同占位符使用。在CampaignFeed中设置匹配函数可告诉投放系统,哪些Feed项能够用作附加链接。在本例中,我们使用了Feed_ITEM_ID作为匹配函数中的RequestContextOperand;但我们也可以使用另一种简便的方法,即:使用其他RequestContextOperand类型,或者使用FeedAttributeOperand(例如FeedAttributeId)实现匹配。对广告组也可以使用相同的流程。您可以使用AdGroupFeedService将Feed与广告组关联起来。唯一一个比较大的区别是,您需要使用AdGroupFeed和AdGroupFeedPage。KeyMob移动聚合平台倾向于为开发者服务,提供主要针对开发者方便的如交叉推广,互换广告。支持移动广告平台和手机广告,涵盖包括Android、IOS和WindowsPhone在内的所有主流操作系统。