| 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.v201302.basicoperations;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.cm.AdServingOptimizationStatus;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.cm.BiddingStrategyConfiguration;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.cm.BiddingStrategyType;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.cm.Budget;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.cm.BudgetBudgetDeliveryMethod;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.cm.BudgetBudgetPeriod;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.cm.BudgetOperation;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.cm.BudgetServiceInterface;
|
|
|
26 |
import com.google.api.ads.adwords.axis.v201302.cm.Campaign;
|
|
|
27 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignOperation;
|
|
|
28 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignReturnValue;
|
|
|
29 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignServiceInterface;
|
|
|
30 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignStatus;
|
|
|
31 |
import com.google.api.ads.adwords.axis.v201302.cm.FrequencyCap;
|
|
|
32 |
import com.google.api.ads.adwords.axis.v201302.cm.GeoTargetTypeSetting;
|
|
|
33 |
import com.google.api.ads.adwords.axis.v201302.cm.GeoTargetTypeSettingPositiveGeoTargetType;
|
|
|
34 |
import com.google.api.ads.adwords.axis.v201302.cm.KeywordMatchSetting;
|
|
|
35 |
import com.google.api.ads.adwords.axis.v201302.cm.Level;
|
|
|
36 |
import com.google.api.ads.adwords.axis.v201302.cm.ManualCpcBiddingScheme;
|
|
|
37 |
import com.google.api.ads.adwords.axis.v201302.cm.Money;
|
|
|
38 |
import com.google.api.ads.adwords.axis.v201302.cm.NetworkSetting;
|
|
|
39 |
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
|
|
|
40 |
import com.google.api.ads.adwords.axis.v201302.cm.Setting;
|
|
|
41 |
import com.google.api.ads.adwords.axis.v201302.cm.TimeUnit;
|
|
|
42 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
43 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
44 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
45 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
46 |
|
|
|
47 |
import org.joda.time.DateTime;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* This example adds campaigns.
|
|
|
51 |
*
|
|
|
52 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
53 |
* "ads.properties" file. See README for more info.
|
|
|
54 |
*
|
|
|
55 |
* Tags: CampaignService.mutate, BudgetService.mutate
|
|
|
56 |
*
|
|
|
57 |
* Category: adx-exclude
|
|
|
58 |
*
|
|
|
59 |
* @author Adam Rogal
|
|
|
60 |
*/
|
|
|
61 |
public class AddCampaigns {
|
|
|
62 |
|
|
|
63 |
public static void main(String[] args) throws Exception {
|
|
|
64 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
65 |
// and can be used in place of a service account.
|
|
|
66 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
67 |
.forApi(Api.ADWORDS)
|
|
|
68 |
.fromFile()
|
|
|
69 |
.build()
|
|
|
70 |
.generateCredential();
|
|
|
71 |
|
|
|
72 |
// Construct an AdWordsSession.
|
|
|
73 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
74 |
.fromFile()
|
|
|
75 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
76 |
.build();
|
|
|
77 |
|
|
|
78 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
79 |
|
|
|
80 |
runExample(adWordsServices, session);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public static void runExample(
|
|
|
84 |
AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
|
|
|
85 |
// Get the BudgetService.
|
|
|
86 |
BudgetServiceInterface budgetService =
|
|
|
87 |
adWordsServices.get(session, BudgetServiceInterface.class);
|
|
|
88 |
|
|
|
89 |
// Create a budget, which can be shared by multiple campaigns.
|
|
|
90 |
Budget sharedBudget = new Budget();
|
|
|
91 |
sharedBudget.setName("Interplanetary Cruise #" + System.currentTimeMillis());
|
|
|
92 |
Money budgetAmount = new Money();
|
|
|
93 |
budgetAmount.setMicroAmount(50000000L);
|
|
|
94 |
sharedBudget.setAmount(budgetAmount);
|
|
|
95 |
sharedBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
|
|
|
96 |
sharedBudget.setPeriod(BudgetBudgetPeriod.DAILY);
|
|
|
97 |
|
|
|
98 |
BudgetOperation budgetOperation = new BudgetOperation();
|
|
|
99 |
budgetOperation.setOperand(sharedBudget);
|
|
|
100 |
budgetOperation.setOperator(Operator.ADD);
|
|
|
101 |
|
|
|
102 |
// Add the budget
|
|
|
103 |
Long budgetId =
|
|
|
104 |
budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
|
|
|
105 |
|
|
|
106 |
// Get the CampaignService.
|
|
|
107 |
CampaignServiceInterface campaignService =
|
|
|
108 |
adWordsServices.get(session, CampaignServiceInterface.class);
|
|
|
109 |
|
|
|
110 |
// Create campaign.
|
|
|
111 |
Campaign campaign = new Campaign();
|
|
|
112 |
campaign.setName("Interplanetary Cruise #" + System.currentTimeMillis());
|
|
|
113 |
campaign.setStatus(CampaignStatus.PAUSED);
|
|
|
114 |
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
|
|
|
115 |
biddingStrategyConfiguration.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
|
|
|
116 |
|
|
|
117 |
// You can optionally provide a bidding scheme in place of the type.
|
|
|
118 |
ManualCpcBiddingScheme cpcBiddingScheme = new ManualCpcBiddingScheme();
|
|
|
119 |
cpcBiddingScheme.setEnhancedCpcEnabled(false);
|
|
|
120 |
biddingStrategyConfiguration.setBiddingScheme(cpcBiddingScheme);
|
|
|
121 |
|
|
|
122 |
campaign.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
|
|
|
123 |
|
|
|
124 |
// You can optionally provide these field(s).
|
|
|
125 |
campaign.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));
|
|
|
126 |
campaign.setStartDate(new DateTime().plusDays(30).toString("yyyyMMdd"));
|
|
|
127 |
campaign.setAdServingOptimizationStatus(AdServingOptimizationStatus.ROTATE);
|
|
|
128 |
campaign.setFrequencyCap(new FrequencyCap(5L, TimeUnit.DAY, Level.ADGROUP));
|
|
|
129 |
|
|
|
130 |
// Only the budgetId should be sent, all other fields will be ignored by CampaignService.
|
|
|
131 |
Budget budget = new Budget();
|
|
|
132 |
budget.setBudgetId(budgetId);
|
|
|
133 |
campaign.setBudget(budget);
|
|
|
134 |
|
|
|
135 |
// Set the campaign network options to Search and Search Network.
|
|
|
136 |
NetworkSetting networkSetting = new NetworkSetting();
|
|
|
137 |
networkSetting.setTargetGoogleSearch(true);
|
|
|
138 |
networkSetting.setTargetSearchNetwork(true);
|
|
|
139 |
networkSetting.setTargetContentNetwork(false);
|
|
|
140 |
networkSetting.setTargetPartnerSearchNetwork(false);
|
|
|
141 |
campaign.setNetworkSetting(networkSetting);
|
|
|
142 |
|
|
|
143 |
// Set options that are not required.
|
|
|
144 |
GeoTargetTypeSetting geoTarget = new GeoTargetTypeSetting();
|
|
|
145 |
geoTarget.setPositiveGeoTargetType(GeoTargetTypeSettingPositiveGeoTargetType.DONT_CARE);
|
|
|
146 |
KeywordMatchSetting keywordMatch = new KeywordMatchSetting();
|
|
|
147 |
keywordMatch.setOptIn(Boolean.FALSE);
|
|
|
148 |
campaign.setSettings(new Setting[] {geoTarget, keywordMatch});
|
|
|
149 |
|
|
|
150 |
// You can create multiple campaigns in a single request.
|
|
|
151 |
Campaign campaign2 = new Campaign();
|
|
|
152 |
campaign2.setName("Interplanetary Cruise banner #" + System.currentTimeMillis());
|
|
|
153 |
campaign2.setStatus(CampaignStatus.PAUSED);
|
|
|
154 |
BiddingStrategyConfiguration biddingStrategyConfiguration2 = new BiddingStrategyConfiguration();
|
|
|
155 |
biddingStrategyConfiguration2.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
|
|
|
156 |
campaign2.setBiddingStrategyConfiguration(biddingStrategyConfiguration2);
|
|
|
157 |
|
|
|
158 |
Budget budget2 = new Budget();
|
|
|
159 |
budget2.setBudgetId(budgetId);
|
|
|
160 |
campaign2.setBudget(budget2);
|
|
|
161 |
|
|
|
162 |
NetworkSetting networkSetting2 = new NetworkSetting();
|
|
|
163 |
networkSetting2.setTargetGoogleSearch(false);
|
|
|
164 |
networkSetting2.setTargetSearchNetwork(false);
|
|
|
165 |
networkSetting2.setTargetContentNetwork(true);
|
|
|
166 |
networkSetting2.setTargetPartnerSearchNetwork(false);
|
|
|
167 |
campaign2.setNetworkSetting(networkSetting2);
|
|
|
168 |
|
|
|
169 |
KeywordMatchSetting keywordMatch2 = new KeywordMatchSetting();
|
|
|
170 |
keywordMatch2.setOptIn(Boolean.FALSE);
|
|
|
171 |
campaign2.setSettings(new Setting[] {keywordMatch2});
|
|
|
172 |
|
|
|
173 |
// Create operations.
|
|
|
174 |
CampaignOperation operation = new CampaignOperation();
|
|
|
175 |
operation.setOperand(campaign);
|
|
|
176 |
operation.setOperator(Operator.ADD);
|
|
|
177 |
CampaignOperation operation2 = new CampaignOperation();
|
|
|
178 |
operation2.setOperand(campaign2);
|
|
|
179 |
operation2.setOperator(Operator.ADD);
|
|
|
180 |
|
|
|
181 |
CampaignOperation[] operations = new CampaignOperation[] {operation, operation2};
|
|
|
182 |
|
|
|
183 |
// Add campaigns.
|
|
|
184 |
CampaignReturnValue result = campaignService.mutate(operations);
|
|
|
185 |
|
|
|
186 |
// Display campaigns.
|
|
|
187 |
for (Campaign campaignResult : result.getValue()) {
|
|
|
188 |
System.out.println("Campaign with name \"" + campaignResult.getName() + "\" and id \""
|
|
|
189 |
+ campaignResult.getId() + "\" was added.");
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
}
|