| 9005 |
manish.sha |
1 |
// Copyright 2012 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.basicoperations;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterion;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionOperation;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionReturnValue;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionServiceInterface;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201306.cm.BiddableAdGroupCriterion;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201306.cm.BiddingStrategyConfiguration;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201306.cm.Bids;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201306.cm.CpcBid;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201306.cm.Criterion;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201306.cm.Money;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201306.cm.Operator;
|
|
|
29 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
30 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
31 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
32 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* This example updates the bid of a keyword. To add a keyword, run
|
|
|
36 |
* AddKeywords.java.
|
|
|
37 |
*
|
|
|
38 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
39 |
* "ads.properties" file. See README for more info.
|
|
|
40 |
*
|
|
|
41 |
* Tags: AdGroupCriterionService.mutate
|
|
|
42 |
*
|
|
|
43 |
* Category: adx-exclude
|
|
|
44 |
*
|
|
|
45 |
* @author Kevin Winter
|
|
|
46 |
*/
|
|
|
47 |
public class UpdateKeyword {
|
|
|
48 |
|
|
|
49 |
public static void main(String[] args) throws Exception {
|
|
|
50 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
51 |
// and can be used in place of a service account.
|
|
|
52 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
53 |
.forApi(Api.ADWORDS)
|
|
|
54 |
.fromFile()
|
|
|
55 |
.build()
|
|
|
56 |
.generateCredential();
|
|
|
57 |
|
|
|
58 |
// Construct an AdWordsSession.
|
|
|
59 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
60 |
.fromFile()
|
|
|
61 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
62 |
.build();
|
|
|
63 |
|
|
|
64 |
long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
|
|
|
65 |
long keywordId = Long.parseLong("INSERT_KEYWORD_ID_HERE");
|
|
|
66 |
|
|
|
67 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
68 |
|
|
|
69 |
runExample(adWordsServices, session, adGroupId, keywordId);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public static void runExample(
|
|
|
73 |
AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId, Long keywordId)
|
|
|
74 |
throws Exception {
|
|
|
75 |
// Get the AdGroupCriterionService.
|
|
|
76 |
AdGroupCriterionServiceInterface adGroupCriterionService =
|
|
|
77 |
adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
|
|
|
78 |
|
|
|
79 |
// Create ad group criterion with updated bid.
|
|
|
80 |
Criterion criterion = new Criterion();
|
|
|
81 |
criterion.setId(keywordId);
|
|
|
82 |
|
|
|
83 |
BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
|
|
|
84 |
biddableAdGroupCriterion.setAdGroupId(adGroupId);
|
|
|
85 |
biddableAdGroupCriterion.setCriterion(criterion);
|
|
|
86 |
|
|
|
87 |
// Create bids.
|
|
|
88 |
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
|
|
|
89 |
CpcBid bid = new CpcBid();
|
|
|
90 |
bid.setBid(new Money(null, 10000000L));
|
|
|
91 |
biddingStrategyConfiguration.setBids(new Bids[] {bid});
|
|
|
92 |
biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
|
|
|
93 |
|
|
|
94 |
// Create operations.
|
|
|
95 |
AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
|
|
|
96 |
operation.setOperand(biddableAdGroupCriterion);
|
|
|
97 |
operation.setOperator(Operator.SET);
|
|
|
98 |
|
|
|
99 |
AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};
|
|
|
100 |
|
|
|
101 |
// Update ad group criteria.
|
|
|
102 |
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
|
|
|
103 |
|
|
|
104 |
// Display ad group criteria.
|
|
|
105 |
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
|
|
|
106 |
if (adGroupCriterionResult instanceof BiddableAdGroupCriterion) {
|
|
|
107 |
biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
|
|
|
108 |
System.out.println(
|
|
|
109 |
"Ad group criterion with ad group id \""
|
|
|
110 |
+ biddableAdGroupCriterion.getAdGroupId() + "\", criterion id \""
|
|
|
111 |
+ biddableAdGroupCriterion.getCriterion().getId() + "\", type \""
|
|
|
112 |
+ biddableAdGroupCriterion.getCriterion().getCriterionType()
|
|
|
113 |
+ "\", and bid \""
|
|
|
114 |
+ ((CpcBid) biddableAdGroupCriterion.getBiddingStrategyConfiguration()
|
|
|
115 |
.getBids()[0]).getBid().getMicroAmount() + "\" was updated.");
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|