Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.v201309.advancedoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.AttributeFieldMapping;
19
import com.google.api.ads.adwords.axis.v201309.cm.CampaignFeed;
20
import com.google.api.ads.adwords.axis.v201309.cm.CampaignFeedOperation;
21
import com.google.api.ads.adwords.axis.v201309.cm.CampaignFeedReturnValue;
22
import com.google.api.ads.adwords.axis.v201309.cm.CampaignFeedServiceInterface;
23
import com.google.api.ads.adwords.axis.v201309.cm.ConstantOperand;
24
import com.google.api.ads.adwords.axis.v201309.cm.ConstantOperandConstantType;
25
import com.google.api.ads.adwords.axis.v201309.cm.Feed;
26
import com.google.api.ads.adwords.axis.v201309.cm.FeedAttribute;
27
import com.google.api.ads.adwords.axis.v201309.cm.FeedAttributeType;
28
import com.google.api.ads.adwords.axis.v201309.cm.FeedItem;
29
import com.google.api.ads.adwords.axis.v201309.cm.FeedItemAttributeValue;
30
import com.google.api.ads.adwords.axis.v201309.cm.FeedItemOperation;
31
import com.google.api.ads.adwords.axis.v201309.cm.FeedItemReturnValue;
32
import com.google.api.ads.adwords.axis.v201309.cm.FeedItemServiceInterface;
33
import com.google.api.ads.adwords.axis.v201309.cm.FeedMapping;
34
import com.google.api.ads.adwords.axis.v201309.cm.FeedMappingOperation;
35
import com.google.api.ads.adwords.axis.v201309.cm.FeedMappingReturnValue;
36
import com.google.api.ads.adwords.axis.v201309.cm.FeedMappingServiceInterface;
37
import com.google.api.ads.adwords.axis.v201309.cm.FeedOperation;
38
import com.google.api.ads.adwords.axis.v201309.cm.FeedOrigin;
39
import com.google.api.ads.adwords.axis.v201309.cm.FeedReturnValue;
40
import com.google.api.ads.adwords.axis.v201309.cm.FeedServiceInterface;
41
import com.google.api.ads.adwords.axis.v201309.cm.Function;
42
import com.google.api.ads.adwords.axis.v201309.cm.FunctionArgumentOperand;
43
import com.google.api.ads.adwords.axis.v201309.cm.FunctionOperand;
44
import com.google.api.ads.adwords.axis.v201309.cm.FunctionOperator;
45
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
46
import com.google.api.ads.adwords.axis.v201309.cm.RequestContextOperand;
47
import com.google.api.ads.adwords.axis.v201309.cm.RequestContextOperandContextType;
48
import com.google.api.ads.adwords.lib.client.AdWordsSession;
49
import com.google.api.ads.common.lib.auth.OfflineCredentials;
50
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
51
import com.google.api.client.auth.oauth2.Credential;
52
 
53
import java.util.ArrayList;
54
import java.util.List;
55
 
56
/**
57
 * This example adds a sitelinks feed and associates it with a campaign.
58
 *
59
 * Credentials and properties in {@code fromFile()} are pulled from the "ads.properties" file. See
60
 * README for more info.
61
 *
62
 * Tags: CampaignFeedService.mutate, FeedItemService.mutate, FeedMappingService.mutate
63
 * Tags: FeedService.mutate
64
 *
65
 * Category: adx-exclude
66
 *
67
 * @author Kevin Winter
68
 */
69
public class AddSiteLinks {
70
 
71
  public static void main(String[] args) throws Exception {
72
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
73
    // and can be used in place of a service account.
74
    Credential oAuth2Credential = new OfflineCredentials.Builder()
75
        .forApi(Api.ADWORDS)
76
        .fromFile()
77
        .build()
78
        .generateCredential();
79
 
80
    // Construct an AdWordsSession.
81
    AdWordsSession session = new AdWordsSession.Builder()
82
        .fromFile()
83
        .withOAuth2Credential(oAuth2Credential)
84
        .build();
85
 
86
    AdWordsServices adWordsServices = new AdWordsServices();
87
 
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 feedItemFunction = new Function();
226
    feedItemFunction.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
227
    feedItemFunction.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
    feedItemFunction.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));
237
 
238
    // Optional: to target to a platform, define a function and 'AND' it with
239
    // the feed item ID link:
240
    RequestContextOperand platformRequestContextOperand = new RequestContextOperand();
241
    platformRequestContextOperand.setContextType(RequestContextOperandContextType.DEVICE_PLATFORM);
242
 
243
    ConstantOperand platformOperand = new ConstantOperand();
244
    platformOperand.setStringValue("Mobile");
245
    platformOperand.setType(ConstantOperandConstantType.STRING);
246
 
247
    Function platformFunction = new Function();
248
    platformFunction.setLhsOperand(new FunctionArgumentOperand[] {platformRequestContextOperand});
249
    platformFunction.setOperator(FunctionOperator.EQUALS);
250
    platformFunction.setRhsOperand(new FunctionArgumentOperand[] {platformOperand});
251
 
252
    // Combine the two functions using an AND operation.
253
    FunctionOperand feedItemFunctionOperand = new FunctionOperand();
254
    feedItemFunctionOperand.setValue(feedItemFunction);
255
 
256
    FunctionOperand platformFunctionOperand = new FunctionOperand();
257
    platformFunctionOperand.setValue(platformFunction);
258
 
259
    Function combinedFunction = new Function();
260
    combinedFunction.setOperator(FunctionOperator.AND);
261
    combinedFunction.setLhsOperand(new FunctionArgumentOperand[] {
262
        feedItemFunctionOperand, platformFunctionOperand});
263
 
264
    CampaignFeed campaignFeed = new CampaignFeed();
265
    campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
266
    campaignFeed.setCampaignId(campaignId);
267
    campaignFeed.setMatchingFunction(combinedFunction);
268
    // Specifying placeholder types on the CampaignFeed allows the same feed
269
    // to be used for different placeholders in different Campaigns.
270
    campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});
271
 
272
    CampaignFeedOperation operation = new CampaignFeedOperation();
273
    operation.setOperand(campaignFeed);
274
    operation.setOperator(Operator.ADD);
275
    CampaignFeedReturnValue result =
276
        campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
277
    for (CampaignFeed savedCampaignFeed : result.getValue()) {
278
      System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
279
          savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
280
    }
281
  }
282
 
283
  private static FeedItemOperation newSiteLinkFeedItemAddOperation(
284
      SiteLinksDataHolder siteLinksData, String text, String url) {
285
    // Create the FeedItemAttributeValues for our text values.
286
    FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
287
    linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
288
    linkTextAttributeValue.setStringValue(text);
289
    FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
290
    linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
291
    linkUrlAttributeValue.setStringValue(url);
292
 
293
    // Create the feed item and operation.
294
    FeedItem item = new FeedItem();
295
    item.setFeedId(siteLinksData.siteLinksFeedId);
296
    item.setAttributeValues(
297
        new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
298
    FeedItemOperation operation = new FeedItemOperation();
299
    operation.setOperand(item);
300
    operation.setOperator(Operator.ADD);
301
    return operation;
302
  }
303
 
304
  private static class SiteLinksDataHolder {
305
    private Long siteLinksFeedId;
306
    private Long linkTextFeedAttributeId;
307
    private Long linkUrlFeedAttributeId;
308
    private List<Long> siteLinkFeedItemIds = new ArrayList<Long>();
309
  }
310
}