| 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.targeting;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignCriterion;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignCriterionOperation;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignCriterionReturnValue;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201302.cm.CampaignCriterionServiceInterface;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201302.cm.Criterion;
|
|
|
23 |
import com.google.api.ads.adwords.axis.v201302.cm.Language;
|
|
|
24 |
import com.google.api.ads.adwords.axis.v201302.cm.Location;
|
|
|
25 |
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
|
|
|
26 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
27 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
28 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
29 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
30 |
|
|
|
31 |
import java.util.ArrayList;
|
|
|
32 |
import java.util.List;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* This example adds various types of targeting criteria to a campaign. To get
|
|
|
36 |
* campaigns, run GetCampaigns.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: CampaignCriterionService.mutate
|
|
|
42 |
*
|
|
|
43 |
* @author Kevin Winter
|
|
|
44 |
*/
|
|
|
45 |
public class AddCampaignTargetingCriteria {
|
|
|
46 |
|
|
|
47 |
public static void main(String[] args) throws Exception {
|
|
|
48 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
49 |
// and can be used in place of a service account.
|
|
|
50 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
51 |
.forApi(Api.ADWORDS)
|
|
|
52 |
.fromFile()
|
|
|
53 |
.build()
|
|
|
54 |
.generateCredential();
|
|
|
55 |
|
|
|
56 |
// Construct an AdWordsSession.
|
|
|
57 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
58 |
.fromFile()
|
|
|
59 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
60 |
.build();
|
|
|
61 |
|
|
|
62 |
Long campaignId = Long.valueOf("INSERT_CAMPAIGN_ID_HERE");
|
|
|
63 |
|
|
|
64 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
65 |
|
|
|
66 |
runExample(adWordsServices, session, campaignId);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public static void runExample(
|
|
|
70 |
AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
|
|
|
71 |
// Get the CampaignService.
|
|
|
72 |
CampaignCriterionServiceInterface campaignCriterionService =
|
|
|
73 |
adWordsServices.get(session, CampaignCriterionServiceInterface.class);
|
|
|
74 |
|
|
|
75 |
// Create locations. The IDs can be found in the documentation or
|
|
|
76 |
// retrieved with the LocationCriterionService.
|
|
|
77 |
Location california = new Location();
|
|
|
78 |
california.setId(21137L);
|
|
|
79 |
Location mexico = new Location();
|
|
|
80 |
mexico.setId(2484L);
|
|
|
81 |
|
|
|
82 |
// Create languages. The IDs can be found in the documentation or
|
|
|
83 |
// retrieved with the ConstantDataService.
|
|
|
84 |
Language english = new Language();
|
|
|
85 |
english.setId(1000L);
|
|
|
86 |
Language spanish = new Language();
|
|
|
87 |
spanish.setId(1003L);
|
|
|
88 |
|
|
|
89 |
List<CampaignCriterionOperation> operations = new ArrayList<CampaignCriterionOperation>();
|
|
|
90 |
for (Criterion criterion : new Criterion[] {california, mexico, english, spanish}) {
|
|
|
91 |
CampaignCriterionOperation operation = new CampaignCriterionOperation();
|
|
|
92 |
CampaignCriterion campaignCriterion = new CampaignCriterion();
|
|
|
93 |
campaignCriterion.setCampaignId(campaignId);
|
|
|
94 |
campaignCriterion.setCriterion(criterion);
|
|
|
95 |
operation.setOperand(campaignCriterion);
|
|
|
96 |
operation.setOperator(Operator.ADD);
|
|
|
97 |
operations.add(operation);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
CampaignCriterionReturnValue result =
|
|
|
101 |
campaignCriterionService.mutate(operations
|
|
|
102 |
.toArray(new CampaignCriterionOperation[operations.size()]));
|
|
|
103 |
|
|
|
104 |
// Display campaigns.
|
|
|
105 |
for (CampaignCriterion campaignCriterion : result.getValue()) {
|
|
|
106 |
System.out.printf("Campaign criterion with campaign id '%s', criterion id '%s', "
|
|
|
107 |
+ "and type '%s' was added.\n", campaignCriterion.getCampaignId(), campaignCriterion
|
|
|
108 |
.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}
|