| Line 18... |
Line 18... |
| 18 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroup;
|
18 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroup;
|
| 19 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupOperation;
|
19 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupOperation;
|
| 20 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupReturnValue;
|
20 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupReturnValue;
|
| 21 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
|
21 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
|
| 22 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupStatus;
|
22 |
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupStatus;
|
| - |
|
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.Money;
|
| 23 |
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
|
27 |
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
|
| 24 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
28 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
| 25 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
29 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
| 26 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
30 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
| 27 |
import com.google.api.client.auth.oauth2.Credential;
|
31 |
import com.google.api.client.auth.oauth2.Credential;
|
| Line 37... |
Line 41... |
| 37 |
*
|
41 |
*
|
| 38 |
* @author Kevin Winter
|
42 |
* @author Kevin Winter
|
| 39 |
*/
|
43 |
*/
|
| 40 |
public class UpdateAdGroup {
|
44 |
public class UpdateAdGroup {
|
| 41 |
|
45 |
|
| 42 |
public static void main(String[] args) throws Exception {
|
46 |
public static Long runExample(String name, String status, Long bidAmount,Long adGroupId) throws Exception {
|
| 43 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
47 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
| 44 |
// and can be used in place of a service account.
|
48 |
// and can be used in place of a service account.
|
| 45 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
49 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
| 46 |
.forApi(Api.ADWORDS)
|
50 |
.forApi(Api.ADWORDS)
|
| 47 |
.fromFile()
|
51 |
.fromFile()
|
| 48 |
.build()
|
52 |
.build()
|
| 49 |
.generateCredential();
|
53 |
.generateCredential();
|
| 50 |
|
54 |
|
| 51 |
// Construct an AdWordsSession.
|
55 |
// Construct an AdWordsSession.
|
| 52 |
AdWordsSession session = new AdWordsSession.Builder()
|
56 |
AdWordsSession session = new AdWordsSession.Builder()
|
| 53 |
.fromFile()
|
57 |
.fromFile()
|
| 54 |
.withOAuth2Credential(oAuth2Credential)
|
58 |
.withOAuth2Credential(oAuth2Credential)
|
| 55 |
.build();
|
59 |
.build();
|
| 56 |
|
- |
|
| 57 |
Long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
|
- |
|
| 58 |
|
60 |
|
| 59 |
AdWordsServices adWordsServices = new AdWordsServices();
|
61 |
AdWordsServices adWordsServices = new AdWordsServices();
|
| 60 |
|
- |
|
| 61 |
runExample(adWordsServices, session, adGroupId);
|
- |
|
| 62 |
}
|
- |
|
| 63 |
|
- |
|
| 64 |
public static void runExample(
|
- |
|
| 65 |
AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
|
- |
|
| 66 |
// Get the AdGroupService.
|
62 |
// Get the AdGroupService.
|
| 67 |
AdGroupServiceInterface adGroupService =
|
63 |
AdGroupServiceInterface adGroupService =
|
| 68 |
adWordsServices.get(session, AdGroupServiceInterface.class);
|
64 |
adWordsServices.get(session, AdGroupServiceInterface.class);
|
| 69 |
|
65 |
|
| 70 |
// Create ad group with updated status.
|
66 |
// Create ad group with updated status.
|
| 71 |
AdGroup adGroup = new AdGroup();
|
67 |
AdGroup adGroup = new AdGroup();
|
| 72 |
adGroup.setId(adGroupId);
|
68 |
adGroup.setId(adGroupId);
|
| - |
|
69 |
if(status!=null && !("").equalsIgnoreCase(status)){
|
| 73 |
adGroup.setStatus(AdGroupStatus.PAUSED);
|
70 |
adGroup.setStatus(AdGroupStatus.fromString(status));
|
| - |
|
71 |
}
|
| - |
|
72 |
if(name!=null && !("").equalsIgnoreCase(name)){
|
| - |
|
73 |
adGroup.setName(name);
|
| - |
|
74 |
}
|
| - |
|
75 |
|
| - |
|
76 |
if(bidAmount>-1l){
|
| - |
|
77 |
BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
|
| - |
|
78 |
CpcBid bid = new CpcBid();
|
| - |
|
79 |
bid.setBid(new Money(null, bidAmount));
|
| 74 |
|
80 |
|
| - |
|
81 |
biddingStrategyConfiguration.setBids(new Bids[] {bid});
|
| - |
|
82 |
adGroup.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
|
| - |
|
83 |
}
|
| 75 |
// Create operations.
|
84 |
// Create operations.
|
| 76 |
AdGroupOperation operation = new AdGroupOperation();
|
85 |
AdGroupOperation operation = new AdGroupOperation();
|
| 77 |
operation.setOperand(adGroup);
|
86 |
operation.setOperand(adGroup);
|
| 78 |
operation.setOperator(Operator.SET);
|
87 |
operation.setOperator(Operator.SET);
|
| 79 |
|
88 |
|
| 80 |
AdGroupOperation[] operations = new AdGroupOperation[] {operation};
|
89 |
AdGroupOperation[] operations = new AdGroupOperation[] {operation};
|
| 81 |
|
- |
|
| - |
|
90 |
if((status!=null && !("").equalsIgnoreCase(status)) || (name!=null && !("").equalsIgnoreCase(name)) || (bidAmount>-1l)){
|
| 82 |
// Update ad group.
|
91 |
// Update ad group.
|
| 83 |
AdGroupReturnValue result = adGroupService.mutate(operations);
|
92 |
AdGroupReturnValue result = adGroupService.mutate(operations);
|
| 84 |
|
93 |
|
| - |
|
94 |
Long updatedAdGroupId = 0L;
|
| 85 |
// Display ad groups.
|
95 |
// Display ad groups.
|
| 86 |
for (AdGroup adGroupResult : result.getValue()) {
|
96 |
for (AdGroup adGroupResult : result.getValue()) {
|
| 87 |
System.out.println("Ad group with name \"" + adGroupResult.getName() + "\", id \""
|
97 |
System.out.println("Ad group with name \"" + adGroupResult.getName() + "\", id \""
|
| 88 |
+ adGroupResult.getId() + "\", and status \"" + adGroupResult.getStatus()
|
98 |
+ adGroupResult.getId() + "\", and status \"" + adGroupResult.getStatus()
|
| 89 |
+ "\" was updated.");
|
99 |
+ "\" was updated.");
|
| - |
|
100 |
updatedAdGroupId = adGroupResult.getId();
|
| 90 |
}
|
101 |
}
|
| - |
|
102 |
return updatedAdGroupId;
|
| 91 |
}
|
103 |
}
|
| - |
|
104 |
return 0l;
|
| - |
|
105 |
}
|
| 92 |
}
|
106 |
}
|