Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.CampaignCriterionPage;
20
import com.google.api.ads.adwords.axis.v201302.cm.CampaignCriterionServiceInterface;
21
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
22
import com.google.api.ads.adwords.axis.v201302.cm.Predicate;
23
import com.google.api.ads.adwords.axis.v201302.cm.PredicateOperator;
24
import com.google.api.ads.adwords.axis.v201302.cm.Selector;
25
import com.google.api.ads.adwords.lib.client.AdWordsSession;
26
import com.google.api.ads.common.lib.auth.OfflineCredentials;
27
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
28
import com.google.api.client.auth.oauth2.Credential;
29
 
30
/**
31
 * This example illustrates how to retrieve all the campaign criteria. To add
32
 * campaign criteria, run AddCampaignTargetingCriteria.java.
33
 *
34
 * Credentials and properties in {@code fromFile()} are pulled from the
35
 * "ads.properties" file. See README for more info.
36
 *
37
 * Tags: CampaignCriterionService.get
38
 *
39
 * @author Kevin Winter
40
 */
41
public class GetCampaignTargetingCriteria {
42
 
43
  private static final int PAGE_SIZE = 100;
44
 
45
  public static void main(String[] args) throws Exception {
46
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
47
    // and can be used in place of a service account.
48
    Credential oAuth2Credential = new OfflineCredentials.Builder()
49
        .forApi(Api.ADWORDS)
50
        .fromFile()
51
        .build()
52
        .generateCredential();
53
 
54
    // Construct an AdWordsSession.
55
    AdWordsSession session = new AdWordsSession.Builder()
56
        .fromFile()
57
        .withOAuth2Credential(oAuth2Credential)
58
        .build();
59
 
60
    AdWordsServices adWordsServices = new AdWordsServices();
61
 
62
    runExample(adWordsServices, session);
63
  }
64
 
65
  public static void runExample(
66
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
67
    // Get the CampaignService.
68
    CampaignCriterionServiceInterface campaignCriterionService =
69
        adWordsServices.get(session, CampaignCriterionServiceInterface.class);
70
 
71
    int offset = 0;
72
 
73
    // Create selector.
74
    Selector selector = new Selector();
75
    selector.setFields(new String[] {"CampaignId", "Id", "CriteriaType", "PlatformName",
76
        "LanguageName", "LocationName", "KeywordText"});
77
    selector.setPredicates(new Predicate[] {new Predicate("CriteriaType", PredicateOperator.IN,
78
        new String[] {"KEYWORD", "LANGUAGE", "LOCATION", "PLATFORM"})});
79
    selector.setPaging(new Paging(offset, PAGE_SIZE));
80
 
81
    CampaignCriterionPage page = null;
82
    do {
83
      page = campaignCriterionService.get(selector);
84
 
85
      if (page.getEntries() != null) {
86
        // Display campaigns.
87
        for (CampaignCriterion campaignCriterion : page.getEntries()) {
88
          System.out.printf("Campaign criterion with campaign id '%s', criterion id '%s', "
89
              + "and type '%s' was found.\n", campaignCriterion.getCampaignId(), campaignCriterion
90
              .getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
91
        }
92
        Thread.sleep(1000);
93
      } else {
94
        System.out.println("No campaign criteria were found.");
95
      }
96
      offset += PAGE_SIZE;
97
      selector.getPaging().setStartIndex(offset);
98
    } while (offset < page.getTotalNumEntries());
99
  }
100
}