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.v201306.advancedoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.AttributeFieldMapping;
19
import com.google.api.ads.adwords.axis.v201306.cm.CampaignFeed;
20
import com.google.api.ads.adwords.axis.v201306.cm.CampaignFeedOperation;
21
import com.google.api.ads.adwords.axis.v201306.cm.CampaignFeedReturnValue;
22
import com.google.api.ads.adwords.axis.v201306.cm.CampaignFeedServiceInterface;
23
import com.google.api.ads.adwords.axis.v201306.cm.ConstantOperand;
24
import com.google.api.ads.adwords.axis.v201306.cm.ConstantOperandConstantType;
25
import com.google.api.ads.adwords.axis.v201306.cm.Feed;
26
import com.google.api.ads.adwords.axis.v201306.cm.FeedAttribute;
27
import com.google.api.ads.adwords.axis.v201306.cm.FeedAttributeType;
28
import com.google.api.ads.adwords.axis.v201306.cm.FeedItem;
29
import com.google.api.ads.adwords.axis.v201306.cm.FeedItemAttributeValue;
30
import com.google.api.ads.adwords.axis.v201306.cm.FeedItemOperation;
31
import com.google.api.ads.adwords.axis.v201306.cm.FeedItemReturnValue;
32
import com.google.api.ads.adwords.axis.v201306.cm.FeedItemServiceInterface;
33
import com.google.api.ads.adwords.axis.v201306.cm.FeedMapping;
34
import com.google.api.ads.adwords.axis.v201306.cm.FeedMappingOperation;
35
import com.google.api.ads.adwords.axis.v201306.cm.FeedMappingReturnValue;
36
import com.google.api.ads.adwords.axis.v201306.cm.FeedMappingServiceInterface;
37
import com.google.api.ads.adwords.axis.v201306.cm.FeedOperation;
38
import com.google.api.ads.adwords.axis.v201306.cm.FeedOrigin;
39
import com.google.api.ads.adwords.axis.v201306.cm.FeedReturnValue;
40
import com.google.api.ads.adwords.axis.v201306.cm.FeedServiceInterface;
41
import com.google.api.ads.adwords.axis.v201306.cm.Function;
42
import com.google.api.ads.adwords.axis.v201306.cm.FunctionArgumentOperand;
43
import com.google.api.ads.adwords.axis.v201306.cm.FunctionOperand;
44
import com.google.api.ads.adwords.axis.v201306.cm.FunctionOperator;
45
import com.google.api.ads.adwords.axis.v201306.cm.Operator;
46
import com.google.api.ads.adwords.axis.v201306.cm.RequestContextOperand;
47
import com.google.api.ads.adwords.axis.v201306.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
    // Campaign must be enhanced.
89
    Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
90
 
91
    runExample(adWordsServices, session, campaignId);
92
  }
93
 
94
  public static void runExample(
95
      AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
96
    SiteLinksDataHolder siteLinksData = new SiteLinksDataHolder();
97
    createSiteLinksFeed(adWordsServices, session, siteLinksData);
98
    createSiteLinksFeedItems(adWordsServices, session, siteLinksData);
99
    createSiteLinksFeedMapping(adWordsServices, session, siteLinksData);
100
    createSiteLinksCampaignFeed(adWordsServices, session, siteLinksData, campaignId);
101
  }
102
 
103
  private static void createSiteLinksFeed(
104
      AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
105
      throws Exception {
106
    // Get the FeedService.
107
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
108
 
109
    // Create attributes.
110
    FeedAttribute textAttribute = new FeedAttribute();
111
    textAttribute.setType(FeedAttributeType.STRING);
112
    textAttribute.setName("Link Text");
113
    FeedAttribute urlAttribute = new FeedAttribute();
114
    urlAttribute.setType(FeedAttributeType.URL);
115
    urlAttribute.setName("Link URL");
116
 
117
    // Create the feed.
118
    Feed siteLinksFeed = new Feed();
119
    siteLinksFeed.setName("Feed For Site Links");
120
    siteLinksFeed.setAttributes(new FeedAttribute[] {textAttribute, urlAttribute});
121
    siteLinksFeed.setOrigin(FeedOrigin.USER);
122
 
123
    // Create operation.
124
    FeedOperation operation = new FeedOperation();
125
    operation.setOperand(siteLinksFeed);
126
    operation.setOperator(Operator.ADD);
127
 
128
    // Add the feed.
129
    FeedReturnValue result = feedService.mutate(new FeedOperation[] {operation});
130
 
131
    Feed savedFeed = result.getValue()[0];
132
    siteLinksData.siteLinksFeedId = savedFeed.getId();
133
    FeedAttribute[] savedAttributes = savedFeed.getAttributes();
134
    siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
135
    siteLinksData.linkUrlFeedAttributeId = savedAttributes[1].getId();
136
    System.out.printf("Feed with name %s and ID %d with linkTextAttributeId %d"
137
        + " and linkUrlAttributeId %s was created.\n", savedFeed.getName(), savedFeed.getId(),
138
        savedAttributes[0].getId(), savedAttributes[1].getId());
139
  }
140
 
141
  private static void createSiteLinksFeedItems(
142
      AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
143
      throws Exception {
144
    // Get the FeedItemService.
145
    FeedItemServiceInterface feedItemService =
146
        adWordsServices.get(session, FeedItemServiceInterface.class);
147
 
148
    // Create operations to add FeedItems.
149
    FeedItemOperation home =
150
        newSiteLinkFeedItemAddOperation(siteLinksData, "Home", "http://www.example.com");
151
    FeedItemOperation stores =
152
        newSiteLinkFeedItemAddOperation(siteLinksData, "Stores", "http://www.example.com/stores");
153
    FeedItemOperation onSale =
154
        newSiteLinkFeedItemAddOperation(siteLinksData, "On Sale", "http://www.example.com/sale");
155
    FeedItemOperation support =
156
        newSiteLinkFeedItemAddOperation(siteLinksData, "Support", "http://www.example.com/support");
157
    FeedItemOperation products =
158
        newSiteLinkFeedItemAddOperation(siteLinksData, "Products", "http://www.example.com/prods");
159
    FeedItemOperation aboutUs =
160
        newSiteLinkFeedItemAddOperation(siteLinksData, "About Us", "http://www.example.com/about");
161
 
162
    FeedItemOperation[] operations =
163
        new FeedItemOperation[] {home, stores, onSale, support, products, aboutUs};
164
 
165
    FeedItemReturnValue result = feedItemService.mutate(operations);
166
    for (FeedItem item : result.getValue()) {
167
      System.out.printf("FeedItem with feedItemId %d was added.\n", item.getFeedItemId());
168
      siteLinksData.siteLinkFeedItemIds.add(item.getFeedItemId());
169
    }
170
  }
171
 
172
  // See the Placeholder reference page for a list of all the placeholder types and fields.
173
  // https://developers.google.com/adwords/api/docs/appendix/placeholders
174
  private static final int PLACEHOLDER_SITELINKS = 1;
175
 
176
  // See the Placeholder reference page for a list of all the placeholder types and fields.
177
  // https://developers.google.com/adwords/api/docs/appendix/placeholders
178
  private static final int PLACEHOLDER_FIELD_SITELINK_LINK_TEXT = 1;
179
  private static final int PLACEHOLDER_FIELD_SITELINK_URL = 2;
180
 
181
  private static void createSiteLinksFeedMapping(
182
      AdWordsServices adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData)
183
      throws Exception {
184
    // Get the FeedItemService.
185
    FeedMappingServiceInterface feedMappingService =
186
        adWordsServices.get(session, FeedMappingServiceInterface.class);
187
 
188
    // Map the FeedAttributeIds to the fieldId constants.
189
    AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
190
    linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
191
    linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
192
    AttributeFieldMapping linkUrlFieldMapping = new AttributeFieldMapping();
193
    linkUrlFieldMapping.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
194
    linkUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_URL);
195
 
196
    // Create the FieldMapping and operation.
197
    FeedMapping feedMapping = new FeedMapping();
198
    feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
199
    feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
200
    feedMapping.setAttributeFieldMappings(
201
        new AttributeFieldMapping[] {linkTextFieldMapping, linkUrlFieldMapping});
202
    FeedMappingOperation operation = new FeedMappingOperation();
203
    operation.setOperand(feedMapping);
204
    operation.setOperator(Operator.ADD);
205
 
206
    // Save the field mapping.
207
    FeedMappingReturnValue result =
208
        feedMappingService.mutate(new FeedMappingOperation[] {operation});
209
    for (FeedMapping savedFeedMapping : result.getValue()) {
210
      System.out.printf(
211
          "Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.\n",
212
          savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(),
213
          savedFeedMapping.getFeedId());
214
    }
215
  }
216
 
217
  private static void createSiteLinksCampaignFeed(AdWordsServices adWordsServices,
218
      AdWordsSession session, SiteLinksDataHolder siteLinksData, Long campaignId) throws Exception {
219
    // Get the CampaignFeedService.
220
    CampaignFeedServiceInterface campaignFeedService =
221
        adWordsServices.get(session, CampaignFeedServiceInterface.class);
222
 
223
    RequestContextOperand requestContextOperand = new RequestContextOperand();
224
    requestContextOperand.setContextType(RequestContextOperandContextType.FEED_ITEM_ID);
225
 
226
    Function feedItemFunction = new Function();
227
    feedItemFunction.setLhsOperand(new FunctionArgumentOperand[] {requestContextOperand});
228
    feedItemFunction.setOperator(FunctionOperator.IN);
229
 
230
    List<FunctionArgumentOperand> operands = new ArrayList<FunctionArgumentOperand>();
231
    for (long feedItemId : siteLinksData.siteLinkFeedItemIds) {
232
      ConstantOperand constantOperand = new ConstantOperand();
233
      constantOperand.setLongValue(feedItemId);
234
      constantOperand.setType(ConstantOperandConstantType.LONG);
235
      operands.add(constantOperand);
236
    }
237
    feedItemFunction.setRhsOperand(operands.toArray(new FunctionArgumentOperand[operands.size()]));
238
 
239
    // Optional: to target to a platform, define a function and 'AND' it with
240
    // the feed item ID link:
241
    RequestContextOperand platformRequestContextOperand = new RequestContextOperand();
242
    platformRequestContextOperand.setContextType(RequestContextOperandContextType.DEVICE_PLATFORM);
243
 
244
    ConstantOperand platformOperand = new ConstantOperand();
245
    platformOperand.setStringValue("Mobile");
246
    platformOperand.setType(ConstantOperandConstantType.STRING);
247
 
248
    Function platformFunction = new Function();
249
    platformFunction.setLhsOperand(new FunctionArgumentOperand[] {platformRequestContextOperand});
250
    platformFunction.setOperator(FunctionOperator.EQUALS);
251
    platformFunction.setRhsOperand(new FunctionArgumentOperand[] {platformOperand});
252
 
253
    // Combine the two functions using an AND operation.
254
    FunctionOperand feedItemFunctionOperand = new FunctionOperand();
255
    feedItemFunctionOperand.setValue(feedItemFunction);
256
 
257
    FunctionOperand platformFunctionOperand = new FunctionOperand();
258
    platformFunctionOperand.setValue(platformFunction);
259
 
260
    Function combinedFunction = new Function();
261
    combinedFunction.setOperator(FunctionOperator.AND);
262
    combinedFunction.setLhsOperand(new FunctionArgumentOperand[] {
263
        feedItemFunctionOperand, platformFunctionOperand});
264
 
265
    CampaignFeed campaignFeed = new CampaignFeed();
266
    campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
267
    campaignFeed.setCampaignId(campaignId);
268
    campaignFeed.setMatchingFunction(combinedFunction);
269
    // Specifying placeholder types on the CampaignFeed allows the same feed
270
    // to be used for different placeholders in different Campaigns.
271
    campaignFeed.setPlaceholderTypes(new int[] {PLACEHOLDER_SITELINKS});
272
 
273
    CampaignFeedOperation operation = new CampaignFeedOperation();
274
    operation.setOperand(campaignFeed);
275
    operation.setOperator(Operator.ADD);
276
    CampaignFeedReturnValue result =
277
        campaignFeedService.mutate(new CampaignFeedOperation[] {operation});
278
    for (CampaignFeed savedCampaignFeed : result.getValue()) {
279
      System.out.printf("Campaign with ID %d was associated with feed with ID %d.\n",
280
          savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
281
    }
282
  }
283
 
284
  private static FeedItemOperation newSiteLinkFeedItemAddOperation(
285
      SiteLinksDataHolder siteLinksData, String text, String url) {
286
    // Create the FeedItemAttributeValues for our text values.
287
    FeedItemAttributeValue linkTextAttributeValue = new FeedItemAttributeValue();
288
    linkTextAttributeValue.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
289
    linkTextAttributeValue.setStringValue(text);
290
    FeedItemAttributeValue linkUrlAttributeValue = new FeedItemAttributeValue();
291
    linkUrlAttributeValue.setFeedAttributeId(siteLinksData.linkUrlFeedAttributeId);
292
    linkUrlAttributeValue.setStringValue(url);
293
 
294
    // Create the feed item and operation.
295
    FeedItem item = new FeedItem();
296
    item.setFeedId(siteLinksData.siteLinksFeedId);
297
    item.setAttributeValues(
298
        new FeedItemAttributeValue[] {linkTextAttributeValue, linkUrlAttributeValue});
299
    FeedItemOperation operation = new FeedItemOperation();
300
    operation.setOperand(item);
301
    operation.setOperator(Operator.ADD);
302
    return operation;
303
  }
304
 
305
  private static class SiteLinksDataHolder {
306
    private Long siteLinksFeedId;
307
    private Long linkTextFeedAttributeId;
308
    private Long linkUrlFeedAttributeId;
309
    private List<Long> siteLinkFeedItemIds = new ArrayList<Long>();
310
  }
311
}