| 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 java.util.ArrayList;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.Map;
|
|
|
21 |
|
|
|
22 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterion;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionOperation;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionReturnValue;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionServiceInterface;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201309.cm.BiddingStrategyConfiguration;
|
|
|
29 |
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
|
|
|
30 |
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
|
|
|
31 |
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
|
|
|
32 |
import com.google.api.ads.adwords.axis.v201309.cm.KeywordMatchType;
|
|
|
33 |
import com.google.api.ads.adwords.axis.v201309.cm.Money;
|
|
|
34 |
import com.google.api.ads.adwords.axis.v201309.cm.NegativeAdGroupCriterion;
|
|
|
35 |
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
|
|
|
36 |
import com.google.api.ads.adwords.axis.v201309.cm.UserStatus;
|
|
|
37 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
38 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
39 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
40 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* This example adds keywords to an ad group. To get ad groups, run
|
|
|
44 |
* AddAdGroup.java
|
|
|
45 |
*
|
|
|
46 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
47 |
* "ads.properties" file. See README for more info.
|
|
|
48 |
*
|
|
|
49 |
* Tags: AdGroupCriterionService.mutate
|
|
|
50 |
*
|
|
|
51 |
* Category: adx-exclude
|
|
|
52 |
*
|
|
|
53 |
* @author Kevin Winter
|
|
|
54 |
*/
|
|
|
55 |
public class AddKeywords {
|
|
|
56 |
|
|
|
57 |
public static List<Map<String, String>> runExample(long adGroupId, List<Map<String, String>> keywordDataList) throws Exception {
|
|
|
58 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
59 |
// and can be used in place of a service account.
|
|
|
60 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
61 |
.forApi(Api.ADWORDS)
|
|
|
62 |
.fromFile()
|
|
|
63 |
.build()
|
|
|
64 |
.generateCredential();
|
|
|
65 |
|
|
|
66 |
// Construct an AdWordsSession.
|
|
|
67 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
68 |
.fromFile()
|
|
|
69 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
70 |
.build();
|
|
|
71 |
|
|
|
72 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
73 |
|
|
|
74 |
// Get the AdGroupCriterionService.
|
|
|
75 |
AdGroupCriterionServiceInterface adGroupCriterionService =
|
|
|
76 |
adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
|
|
|
77 |
|
|
|
78 |
BiddableAdGroupCriterion[] keyWordbiddableAdGroupCriterion = new BiddableAdGroupCriterion[keywordDataList.size()];
|
|
|
79 |
AdGroupCriterionOperation[] criterionOperations = new AdGroupCriterionOperation[keywordDataList.size()];
|
|
|
80 |
for(int i=0;i<keywordDataList.size();i++){
|
|
|
81 |
Map<String,String> keywordMap = keywordDataList.get(i);
|
|
|
82 |
|
|
|
83 |
Keyword keyword = new Keyword();
|
|
|
84 |
keyword.setText(keywordMap.get("Text"));
|
|
|
85 |
keyword.setMatchType(KeywordMatchType.fromString(keywordMap.get("Type")));
|
|
|
86 |
|
|
|
87 |
keyWordbiddableAdGroupCriterion[i] =new BiddableAdGroupCriterion();
|
|
|
88 |
keyWordbiddableAdGroupCriterion[i].setAdGroupId(adGroupId);
|
|
|
89 |
keyWordbiddableAdGroupCriterion[i].setCriterion(keyword);
|
|
|
90 |
|
|
|
91 |
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
|
|
|
92 |
CpcBid bid = new CpcBid();
|
|
|
93 |
bid.setBid(new Money(null, (long)Double.parseDouble(keywordMap.get("bidamount"))));
|
|
|
94 |
biddingStrategyConfiguration.setBids(new Bids[] {bid});
|
|
|
95 |
keyWordbiddableAdGroupCriterion[i].setBiddingStrategyConfiguration(biddingStrategyConfiguration);
|
|
|
96 |
|
|
|
97 |
criterionOperations[i] = new AdGroupCriterionOperation();
|
|
|
98 |
criterionOperations[i].setOperand(keyWordbiddableAdGroupCriterion[i]);
|
|
|
99 |
criterionOperations[i].setOperator(Operator.ADD);
|
|
|
100 |
}
|
|
|
101 |
/*// Create keywords.
|
|
|
102 |
Keyword keyword1 = new Keyword();
|
|
|
103 |
keyword1.setText("mars");
|
|
|
104 |
keyword1.setMatchType(KeywordMatchType.BROAD);
|
|
|
105 |
Keyword keyword2 = new Keyword();
|
|
|
106 |
keyword2.setText("pluto");
|
|
|
107 |
keyword2.setMatchType(KeywordMatchType.EXACT);
|
|
|
108 |
|
|
|
109 |
// Create biddable ad group criterion.
|
|
|
110 |
BiddableAdGroupCriterion keywordBiddableAdGroupCriterion1 = new BiddableAdGroupCriterion();
|
|
|
111 |
keywordBiddableAdGroupCriterion1.setAdGroupId(adGroupId);
|
|
|
112 |
keywordBiddableAdGroupCriterion1.setCriterion(keyword1);
|
|
|
113 |
|
|
|
114 |
// You can optionally provide these field(s).
|
|
|
115 |
keywordBiddableAdGroupCriterion1.setUserStatus(UserStatus.PAUSED);
|
|
|
116 |
keywordBiddableAdGroupCriterion1.setDestinationUrl("http://example.com/mars");
|
|
|
117 |
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
|
|
|
118 |
CpcBid bid = new CpcBid();
|
|
|
119 |
bid.setBid(new Money(null, 10000000L));
|
|
|
120 |
biddingStrategyConfiguration.setBids(new Bids[] {bid});
|
|
|
121 |
keywordBiddableAdGroupCriterion1.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
|
|
|
122 |
|
|
|
123 |
NegativeAdGroupCriterion keywordNegativeAdGroupCriterion2 = new NegativeAdGroupCriterion();
|
|
|
124 |
keywordNegativeAdGroupCriterion2.setAdGroupId(adGroupId);
|
|
|
125 |
keywordNegativeAdGroupCriterion2.setCriterion(keyword2);
|
|
|
126 |
|
|
|
127 |
// Create operations.
|
|
|
128 |
AdGroupCriterionOperation keywordAdGroupCriterionOperation1 = new AdGroupCriterionOperation();
|
|
|
129 |
keywordAdGroupCriterionOperation1.setOperand(keywordBiddableAdGroupCriterion1);
|
|
|
130 |
keywordAdGroupCriterionOperation1.setOperator(Operator.ADD);
|
|
|
131 |
AdGroupCriterionOperation keywordAdGroupCriterionOperation2 = new AdGroupCriterionOperation();
|
|
|
132 |
keywordAdGroupCriterionOperation2.setOperand(keywordNegativeAdGroupCriterion2);
|
|
|
133 |
keywordAdGroupCriterionOperation2.setOperator(Operator.ADD);
|
|
|
134 |
|
|
|
135 |
AdGroupCriterionOperation[] operations =
|
|
|
136 |
new AdGroupCriterionOperation[] {keywordAdGroupCriterionOperation1,
|
|
|
137 |
keywordAdGroupCriterionOperation2};*/
|
|
|
138 |
|
|
|
139 |
// Add keywords.
|
|
|
140 |
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(criterionOperations);
|
|
|
141 |
|
|
|
142 |
List<Map<String, String>> keywordDataMainList = new ArrayList<Map<String,String>>();
|
|
|
143 |
// Display results.
|
|
|
144 |
for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
|
|
|
145 |
System.out.println("Keyword ad group criterion with ad group id \""
|
|
|
146 |
+ adGroupCriterionResult.getAdGroupId() + "\", criterion id \""
|
|
|
147 |
+ adGroupCriterionResult.getCriterion().getId() + "\", text \""
|
|
|
148 |
+ ((Keyword) adGroupCriterionResult.getCriterion()).getText() + "\" and match type \""
|
|
|
149 |
+ ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType() + "\" was added.");
|
|
|
150 |
BiddableAdGroupCriterion biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
|
|
|
151 |
Keyword keyword = (Keyword) adGroupCriterionResult.getCriterion();
|
|
|
152 |
Bids bid = biddableAdGroupCriterion.getBiddingStrategyConfiguration().getBids(0);
|
|
|
153 |
Map<String,String> keywordMap = new HashMap<String,String>();
|
|
|
154 |
keywordMap.put("AdgroupId", adGroupCriterionResult.getAdGroupId()+"");
|
|
|
155 |
keywordMap.put("Text",keyword.getText());
|
|
|
156 |
keywordMap.put("CriterionId", keyword.getId()+"");
|
|
|
157 |
keywordMap.put("Type", keyword.getMatchType().getValue());
|
|
|
158 |
keywordMap.put("BidAmount", ((CpcBid)bid).getBid().getMicroAmount()+"");
|
|
|
159 |
keywordDataMainList.add(keywordMap);
|
|
|
160 |
}
|
|
|
161 |
return keywordDataMainList;
|
|
|
162 |
}
|
|
|
163 |
}
|