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.reporting;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.Campaign;
19
import com.google.api.ads.adwords.axis.v201302.cm.CampaignPage;
20
import com.google.api.ads.adwords.axis.v201302.cm.CampaignServiceInterface;
21
import com.google.api.ads.adwords.axis.v201302.cm.DateRange;
22
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
23
import com.google.api.ads.adwords.axis.v201302.cm.Predicate;
24
import com.google.api.ads.adwords.axis.v201302.cm.PredicateOperator;
25
import com.google.api.ads.adwords.axis.v201302.cm.Selector;
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 org.joda.time.DateTime;
32
 
33
/**
34
 * This example gets stats for all campaigns with an impression in the last
35
 * week. To add a campaign, run AddCampaigns.java.
36
 *
37
 * @author Kevin Winter
38
 */
39
public class GetCampaignStats {
40
  private static final int PAGE_SIZE = 100;
41
 
42
  public static void main(String[] args) throws Exception {
43
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
44
    // and can be used in place of a service account.
45
    Credential oAuth2Credential = new OfflineCredentials.Builder()
46
        .forApi(Api.ADWORDS)
47
        .fromFile()
48
        .build()
49
        .generateCredential();
50
 
51
    // Construct an AdWordsSession.
52
    AdWordsSession session = new AdWordsSession.Builder()
53
        .fromFile()
54
        .withOAuth2Credential(oAuth2Credential)
55
        .build();
56
 
57
    AdWordsServices adWordsServices = new AdWordsServices();
58
 
59
    runExample(adWordsServices, session);
60
  }
61
 
62
  public static void runExample(
63
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
64
    // Get the CampaignService.
65
    CampaignServiceInterface campaignService =
66
        adWordsServices.get(session, CampaignServiceInterface.class);
67
 
68
    int offset = 0;
69
 
70
    // Create selector.
71
    Selector selector = new Selector();
72
    selector.setFields(new String[] {"Id", "Name", "Impressions", "Clicks", "Cost", "Ctr"});
73
    selector.setPredicates(new Predicate[] {new Predicate("Impressions",
74
        PredicateOperator.GREATER_THAN, new String[] {"0"})});
75
    String oneWeekAgo = new DateTime().minusDays(7).toString("yyyyMMdd");
76
    String yesterday = new DateTime().minusDays(1).toString("yyyyMMdd");
77
    selector.setDateRange(new DateRange(oneWeekAgo, yesterday));
78
    selector.setPaging(new Paging(offset, PAGE_SIZE));
79
 
80
    CampaignPage page = null;
81
    do {
82
      // Get all campaigns.
83
      page = campaignService.get(selector);
84
 
85
      // Display campaigns.
86
      if (page.getEntries() != null) {
87
        for (Campaign campaign : page.getEntries()) {
88
          System.out.printf("Campaign with id \"%d\" and name \"%s\" had the following stats "
89
              + "during the last week.\n", campaign.getId(), campaign.getName());
90
          System.out.printf("  Impressions: %d\n", campaign.getCampaignStats().getImpressions());
91
          System.out.printf("  Clicks: %d\n", campaign.getCampaignStats().getClicks());
92
          System.out.printf("  Cost: %.02f\n", campaign.getCampaignStats().getCost()
93
              .getMicroAmount() / 1000000.0);
94
          System.out.printf("  CTR: %.02f %%\n", campaign.getCampaignStats().getCtr() * 100.0);
95
        }
96
      } else {
97
        System.out.println("No matching campaigns were found.");
98
      }
99
 
100
      offset += PAGE_SIZE;
101
      selector.getPaging().setStartIndex(offset);
102
    } while (offset < page.getTotalNumEntries());
103
  }
104
}