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