| 9005 |
manish.sha |
1 |
// Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
2 |
//
|
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
|
5 |
// You may obtain a copy of the License at
|
|
|
6 |
//
|
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
8 |
//
|
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
12 |
// See the License for the specific language governing permissions and
|
|
|
13 |
// limitations under the License.
|
|
|
14 |
|
|
|
15 |
package adwords.axis.v201302.advancedoperations;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.cm.AttributeFieldMapping;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeed;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedOperation;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedReturnValue;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignFeedServiceInterface;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.cm.ConstantOperand;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.cm.ConstantOperandConstantType;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.cm.Feed;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedAttribute;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedAttributeType;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedItem;
|
|
|
29 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemAttributeValue;
|
|
|
30 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemOperation;
|
|
|
31 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemReturnValue;
|
|
|
32 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedItemServiceInterface;
|
|
|
33 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedMapping;
|
|
|
34 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingOperation;
|
|
|
35 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingReturnValue;
|
|
|
36 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedMappingServiceInterface;
|
|
|
37 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedOperation;
|
|
|
38 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedOrigin;
|
|
|
39 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedReturnValue;
|
|
|
40 |
import com.google.api.ads.adwords.axis.v201302.cm.FeedServiceInterface;
|
|
|
41 |
import com.google.api.ads.adwords.axis.v201302.cm.Function;
|
|
|
42 |
import com.google.api.ads.adwords.axis.v201302.cm.FunctionArgumentOperand;
|
|
|
43 |
import com.google.api.ads.adwords.axis.v201302.cm.FunctionOperator;
|
|
|
44 |
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
|
|
|
45 |
import com.google.api.ads.adwords.axis.v201302.cm.RequestContextOperand;
|
|
|
46 |
import com.google.api.ads.adwords.axis.v201302.cm.RequestContextOperandContextType;
|
|
|
47 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
48 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
49 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
50 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
51 |
|
|
|
52 |
import java.util.ArrayList;
|
|
|
53 |
import java.util.List;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* This example adds a sitelinks feed and associates it with a campaign.
|
|
|
57 |
*
|
|
|
58 |
* Credentials and properties in {@code fromFile()} are pulled from the "ads.properties" file. See
|
|
|
59 |
* README for more info.
|
|
|
60 |
*
|
|
|
61 |
* Tags: CampaignFeedService.mutate, FeedItemService.mutate, FeedMappingService.mutate
|
|
|
62 |
* Tags: FeedService.mutate
|
|
|
63 |
*
|
|
|
64 |
* Category: adx-exclude
|
|
|
65 |
*
|
|
|
66 |
* @author Kevin Winter
|
|
|
67 |
*/
|
|
|
68 |
public class AddSiteLinks {
|
|
|
69 |
|
|
|
70 |
public static void main(String[] args) throws Exception {
|
|
|
71 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
72 |
// and can be used in place of a service account.
|
|
|
73 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
74 |
.forApi(Api.ADWORDS)
|
|
|
75 |
.fromFile()
|
|
|
76 |
.build()
|
|
|
77 |
.generateCredential();
|
|
|
78 |
|
|
|
79 |
// Construct an AdWordsSession.
|
|
|
80 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
81 |
.fromFile()
|
|
|
82 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
83 |
.build();
|
|
|
84 |
|
|
|
85 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
86 |
|
|
|
87 |
// Campaign must be enhanced.
|
|
|
88 |
Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
|
|
|
89 |
|
|
|
90 |
runExample(adWordsServices, session, campaignId);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public static void runExample(
|
|
|
94 |
AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
|
|
|
95 |
SiteLinksDataHolder siteLinksData = new SiteLinksDataHolder();
|
|
|
96 |
createSiteLinksFeed(adWordsServices, session, siteLinksData);
|
|
|
97 |
createSiteLinksFeedItems(adWordsServices, session, siteLinksData);
|
|
|
98 |
createSiteLinksFeedMapping(adWordsServices, session, siteLinksData);
|
|
|
99 |
createSiteLinksCampaignFeed(adWordsServices, session, siteLinksData, campaignId);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private static void createSiteLinksFeed(
|
|
|
103 |
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
|
|
|
104 |
throws Exception {
|
|
|
105 |
// Get the FeedService.
|
|
|
106 |
FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
|
|
|
107 |
|
|
|
108 |
// Create attributes.
|
|
|
109 |
FeedAttribute textAttribute = new FeedAttribute();
|
|
|
110 |
textAttribute.setType(FeedAttributeType.STRING);
|
|
|
111 |
textAttribute.setName("Link Text");
|
|
|
112 |
FeedAttribute urlAttribute = new FeedAttribute();
|
|
|
113 |
urlAttribute.setType(FeedAttributeType.URL);
|
|
|
114 |
urlAttribute.setName("Link URL");
|
|
|
115 |
|
|
|
116 |
// Create the feed.
|
|
|
117 |
Feed siteLinksFeed = new Feed();
|
|
|
118 |
siteLinksFeed.setName("Feed For Site Links");
|
|
|
119 |
siteLinksFeed.setAttributes(new FeedAttribute[] {textAttribute, urlAttribute});
|
|
|
120 |
siteLinksFeed.setOrigin(FeedOrigin.USER);
|
|
|
121 |
|
|
|
122 |
// Create operation.
|
|
|
123 |
FeedOperation operation = new FeedOperation();
|
|
|
124 |
operation.setOperand(siteLinksFeed);
|
|
|
125 |
operation.setOperator(Operator.ADD);
|
|
|
126 |
|
|
|
127 |
// Add the feed.
|
|
|
128 |
FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});
|
|
|
129 |
|
|
|
130 |
Feed savedFeed = result.getValue()[0];
|
|
|
131 |
siteLinksData.siteLinksFeedId = savedFeed.getId();
|
|
|
132 |
FeedAttribute[] savedAttributes = savedFeed.getAttributes();
|
|
|
133 |
siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
|
|
|
134 |
siteLinksData.linkUrlFeedAttributeId = savedAttributes[1].getId();
|
|
|
135 |
System.out.printf("Feed with name %s and ID %d with linkTextAttributeId %d"
|
|
|
136 |
+ " and linkUrlAttributeId %s was created.\n", savedFeed.getName(), savedFeed.getId(),
|
|
|
137 |
savedAttributes[0].getId(), savedAttributes[1].getId());
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
private static void createSiteLinksFeedItems(
|
|
|
141 |
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
|
|
|
142 |
throws Exception {
|
|
|
143 |
// Get the FeedItemService.
|
|
|
144 |
FeedItemServiceInterface feedItemService =
|
|
|
145 |
adWordsServices.get(session, FeedItemServiceInterface.class);
|
|
|
146 |
|
|
|
147 |
// Create operations to add FeedItems.
|
|
|
148 |
FeedItemOperation home =
|
|
|
149 |
newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com");
|
|
|
150 |
FeedItemOperation stores =
|
|
|
151 |
newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores");
|
|
|
152 |
FeedItemOperation onSale =
|
|
|
153 |
newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale");
|
|
|
154 |
FeedItemOperation support =
|
|
|
155 |
newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support");
|
|
|
156 |
FeedItemOperation products =
|
|
|
157 |
newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods");
|
|
|
158 |
FeedItemOperation aboutUs =
|
|
|
159 |
newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about");
|
|
|
160 |
|
|
|
161 |
FeedItemOperation[] operations =
|
|
|
162 |
new FeedItemOperation[] {home, stores, onSale, support, products, aboutUs};
|
|
|
163 |
|
|
|
164 |
FeedItemReturnValue result = feedItemService.mutate(operations);
|
|
|
165 |
for (FeedItem item : result.getValue()) {
|
|
|
166 |
System.out.printf("FeedItem with feedItemId %d was added.\n", item.getFeedItemId());
|
|
|
167 |
siteLinksData.siteLinkFeedItemIds.add(item.getFeedItemId());
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
// See the Placeholder reference page for a list of all the placeholder types and fields.
|
|
|
172 |
// https://developers.google.com/adwords/api/docs/appendix/placeholders
|
|
|
173 |
private static final int PLACEHOLDER_SITELINKS = 1;
|
|
|
174 |
|
|
|
175 |
// See the Placeholder reference page for a list of all the placeholder types and fields.
|
|
|
176 |
// https://developers.google.com/adwords/api/docs/appendix/placeholders
|
|
|
177 |
private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
|
|
|
178 |
private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;
|
|
|
179 |
|
|
|
180 |
private static void createSiteLinksFeedMapping(
|
|
|
181 |
AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
|
|
|
182 |
throws Exception {
|
|
|
183 |
// Get the FeedItemService.
|
|
|
184 |
FeedMappingServiceInterface feedMappingService =
|
|
|
185 |
adWordsServices.get(session, FeedMappingServiceInterface.class);
|
|
|
186 |
|
|
|
187 |
// Map the FeedAttributeIds to the fieldId constants.
|
|
|
188 |
AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
|
|
|
189 |
linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
|
|
|
190 |
linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
|
|
|
191 |
AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();
|
|
|
192 |
linkUrlFieldMapping.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
|
|
|
193 |
linkUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_URL);
|
|
|
194 |
|
|
|
195 |
// Create the FieldMapping and operation.
|
|
|
196 |
FeedMapping feedMapping = new FeedMapping();
|
|
|
197 |
feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
|
|
|
198 |
feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
|
|
|
199 |
feedMapping.setAttributeFieldMappings(
|
|
|
200 |
new AttributeFieldMapping[] {linkTextFieldMapping, linkUrlFieldMapping});
|
|
|
201 |
FeedMappingOperation operation = new FeedMappingOperation();
|
|
|
202 |
operation.setOperand(feedMapping);
|
|
|
203 |
operation.setOperator(Operator.ADD);
|
|
|
204 |
|
|
|
205 |
// Save the field mapping.
|
|
|
206 |
FeedMappingReturnValue result =
|
|
|
207 |
feedMappingService.mutate(new FeedMappingOperation[] {operation});
|
|
|
208 |
for (FeedMapping savedFeedMapping : result.getValue()) {
|
|
|
209 |
System.out.printf(
|
|
|
210 |
"Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.\n",
|
|
|
211 |
savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
|
|
|
212 |
savedFeedMapping.getFeedId());
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
private static void createSiteLinksCampaignFeed(AdWordsServices adWordsServices,
|
|
|
217 |
AdWordsSession session, SiteLinksDataHolder siteLinksData, Long campaignId) throws Exception {
|
|
|
218 |
// Get the CampaignFeedService.
|
|
|
219 |
CampaignFeedServiceInterface campaignFeedService =
|
|
|
220 |
adWordsServices.get(session, CampaignFeedServiceInterface.class);
|
|
|
221 |
|
|
|
222 |
RequestContextOperand requestContextOperand = new RequestContextOperand();
|
|
|
223 |
requestContextOperand.setContextType(RequestContextOperandContextType.FEED_ITEM_ID);
|
|
|
224 |
|
|
|
225 |
Function function = new Function();
|
|
|
226 |
function.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
|
|
|
227 |
function.setOperator(FunctionOperator.IN);
|
|
|
228 |
|
|
|
229 |
List<FunctionArgumentOperand> operands = new ArrayList<FunctionArgumentOperand>();
|
|
|
230 |
for (long feedItemId : siteLinksData.siteLinkFeedItemIds) {
|
|
|
231 |
ConstantOperand constantOperand = new ConstantOperand();
|
|
|
232 |
constantOperand.setLongValue(feedItemId);
|
|
|
233 |
constantOperand.setType(ConstantOperandConstantType.LONG);
|
|
|
234 |
operands.add(constantOperand);
|
|
|
235 |
}
|
|
|
236 |
function.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));
|
|
|
237 |
CampaignFeed campaignFeed = new CampaignFeed();
|
|
|
238 |
campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
|
|
|
239 |
campaignFeed.setCampaignId(campaignId);
|
|
|
240 |
campaignFeed.setMatchingFunction(function);
|
|
|
241 |
// Specifying placeholder types on the CampaignFeed allows the same feed
|
|
|
242 |
// to be used for different placeholders in different Campaigns.
|
|
|
243 |
campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});
|
|
|
244 |
|
|
|
245 |
CampaignFeedOperation operation = new CampaignFeedOperation();
|
|
|
246 |
operation.setOperand(campaignFeed);
|
|
|
247 |
operation.setOperator(Operator.ADD);
|
|
|
248 |
CampaignFeedReturnValue result =
|
|
|
249 |
campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
|
|
|
250 |
for (CampaignFeed savedCampaignFeed : result.getValue()) {
|
|
|
251 |
System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
|
|
|
252 |
savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
private static FeedItemOperation newSiteLinkFeedItemAddOperation(
|
|
|
257 |
SiteLinksDataHolder siteLinksData, String text, String url) {
|
|
|
258 |
// Create the FeedItemAttributeValues for our text values.
|
|
|
259 |
FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
|
|
|
260 |
linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
|
|
|
261 |
linkTextAttributeValue.setStringValue(text);
|
|
|
262 |
FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
|
|
|
263 |
linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
|
|
|
264 |
linkUrlAttributeValue.setStringValue(url);
|
|
|
265 |
|
|
|
266 |
// Create the feed item and operation.
|
|
|
267 |
FeedItem item = new FeedItem();
|
|
|
268 |
item.setFeedId(siteLinksData.siteLinksFeedId);
|
|
|
269 |
item.setAttributeValues(
|
|
|
270 |
new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
|
|
|
271 |
FeedItemOperation operation = new FeedItemOperation();
|
|
|
272 |
operation.setOperand(item);
|
|
|
273 |
operation.setOperator(Operator.ADD);
|
|
|
274 |
return operation;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
private static class SiteLinksDataHolder {
|
|
|
278 |
private Long siteLinksFeedId;
|
|
|
279 |
private Long linkTextFeedAttributeId;
|
|
|
280 |
private Long linkUrlFeedAttributeId;
|
|
|
281 |
private List<Long> siteLinkFeedItemIds = new ArrayList<Long>();
|
|
|
282 |
}
|
|
|
283 |
}
|