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.v201309.optimization;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.Criterion;
19
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
20
import com.google.api.ads.adwords.axis.v201309.cm.KeywordMatchType;
21
import com.google.api.ads.adwords.axis.v201309.cm.Language;
22
import com.google.api.ads.adwords.axis.v201309.cm.Location;
23
import com.google.api.ads.adwords.axis.v201309.cm.Money;
24
import com.google.api.ads.adwords.axis.v201309.o.AdGroupEstimateRequest;
25
import com.google.api.ads.adwords.axis.v201309.o.CampaignEstimateRequest;
26
import com.google.api.ads.adwords.axis.v201309.o.KeywordEstimate;
27
import com.google.api.ads.adwords.axis.v201309.o.KeywordEstimateRequest;
28
import com.google.api.ads.adwords.axis.v201309.o.TrafficEstimatorResult;
29
import com.google.api.ads.adwords.axis.v201309.o.TrafficEstimatorSelector;
30
import com.google.api.ads.adwords.axis.v201309.o.TrafficEstimatorServiceInterface;
31
import com.google.api.ads.adwords.lib.client.AdWordsSession;
32
import com.google.api.ads.common.lib.auth.OfflineCredentials;
33
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
34
import com.google.api.client.auth.oauth2.Credential;
35
 
36
import java.util.ArrayList;
37
import java.util.List;
38
 
39
/**
40
 * This example gets keyword traffic estimates.
41
 *
42
 * Credentials and properties in {@code fromFile()} are pulled from the
43
 * "ads.properties" file. See README for more info.
44
 *
45
 * Tags: TrafficEstimatorService.get
46
 *
47
 * Category: adx-exclude
48
 *
49
 * @author Kevin Winter
50
 */
51
public class EstimateKeywordTraffic {
52
 
53
  public static void main(String[] args) throws Exception {
54
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
55
    // and can be used in place of a service account.
56
    Credential oAuth2Credential = new OfflineCredentials.Builder()
57
        .forApi(Api.ADWORDS)
58
        .fromFile()
59
        .build()
60
        .generateCredential();
61
 
62
    // Construct an AdWordsSession.
63
    AdWordsSession session = new AdWordsSession.Builder()
64
        .fromFile()
65
        .withOAuth2Credential(oAuth2Credential)
66
        .build();
67
 
68
    AdWordsServices adWordsServices = new AdWordsServices();
69
 
70
    runExample(adWordsServices, session);
71
  }
72
 
73
  public static void runExample(
74
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
75
    // Get the TrafficEstimatorService.
76
    TrafficEstimatorServiceInterface trafficEstimatorService =
77
        adWordsServices.get(session, TrafficEstimatorServiceInterface.class);
78
 
79
    // Create keywords. Up to 2000 keywords can be passed in a single request.
80
    List<Keyword> keywords = new ArrayList<Keyword>();
81
    keywords.add(new Keyword(null, null, null, "mars cruise", KeywordMatchType.BROAD));
82
    keywords.add(new Keyword(null, null, null, "cheap cruise", KeywordMatchType.PHRASE));
83
    keywords.add(new Keyword(null, null, null, "cruise", KeywordMatchType.EXACT));
84
 
85
    // Create a keyword estimate request for each keyword.
86
    List<KeywordEstimateRequest> keywordEstimateRequests = new ArrayList<KeywordEstimateRequest>();
87
    for (Keyword keyword : keywords) {
88
      KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
89
      keywordEstimateRequest.setKeyword(keyword);
90
      keywordEstimateRequests.add(keywordEstimateRequest);
91
    }
92
 
93
    // Add a negative keyword to the traffic estimate.
94
    KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();
95
    negativeKeywordEstimateRequest.setKeyword(new Keyword(null, null, null, "hiking tour",
96
        KeywordMatchType.BROAD));
97
    negativeKeywordEstimateRequest.setIsNegative(true);
98
    keywordEstimateRequests.add(negativeKeywordEstimateRequest);
99
 
100
    // Create ad group estimate requests.
101
    List<AdGroupEstimateRequest> adGroupEstimateRequests = new ArrayList<AdGroupEstimateRequest>();
102
    AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();
103
    adGroupEstimateRequest.setKeywordEstimateRequests(keywordEstimateRequests
104
        .toArray(new KeywordEstimateRequest[] {}));
105
    adGroupEstimateRequest.setMaxCpc(new Money(null, 1000000L));
106
    adGroupEstimateRequests.add(adGroupEstimateRequest);
107
 
108
    // Create campaign estimate requests.
109
    List<CampaignEstimateRequest> campaignEstimateRequests =
110
        new ArrayList<CampaignEstimateRequest>();
111
    CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();
112
    campaignEstimateRequest.setAdGroupEstimateRequests(adGroupEstimateRequests
113
        .toArray(new AdGroupEstimateRequest[] {}));
114
    Location unitedStates = new Location();
115
    unitedStates.setId(2840L);
116
    Language english = new Language();
117
    english.setId(1000L);
118
    campaignEstimateRequest.setCriteria(new Criterion[] {unitedStates, english});
119
    campaignEstimateRequests.add(campaignEstimateRequest);
120
 
121
    // Create selector.
122
    TrafficEstimatorSelector selector = new TrafficEstimatorSelector();
123
    selector.setCampaignEstimateRequests(campaignEstimateRequests
124
        .toArray(new CampaignEstimateRequest[] {}));
125
 
126
    // Get traffic estimates.
127
    TrafficEstimatorResult result = trafficEstimatorService.get(selector);
128
 
129
    // Display traffic estimates.
130
    if (result != null && result.getCampaignEstimates() != null) {
131
      KeywordEstimate[] keywordEstimates =
132
          result.getCampaignEstimates()[0].getAdGroupEstimates()[0].getKeywordEstimates();
133
      for (int i = 0; i < keywordEstimates.length; i++) {
134
        Keyword keyword = keywordEstimateRequests.get(i).getKeyword();
135
        KeywordEstimate keywordEstimate = keywordEstimates[i];
136
        if (Boolean.TRUE.equals(keywordEstimateRequests.get(i).getIsNegative())) {
137
          continue;
138
        }
139
 
140
        // Find the mean of the min and max values.
141
        double meanAverageCpc =
142
            (keywordEstimate.getMin().getAverageCpc().getMicroAmount() + keywordEstimate.getMax()
143
                .getAverageCpc().getMicroAmount()) / 2.0;
144
        double meanAveragePosition =
145
            (keywordEstimate.getMin().getAveragePosition() + keywordEstimate.getMax()
146
                .getAveragePosition()) / 2.0;
147
        double meanClicks =
148
            (keywordEstimate.getMin().getClicksPerDay() + keywordEstimate.getMax()
149
                .getClicksPerDay()) / 2.0;
150
        double meanTotalCost =
151
            (keywordEstimate.getMin().getTotalCost().getMicroAmount() + keywordEstimate.getMax()
152
                .getTotalCost().getMicroAmount()) / 2.0;
153
 
154
        System.out.println(String.format(
155
            "Results for the keyword with text '%s' and match type '%s':", keyword.getText(),
156
            keyword.getMatchType()));
157
        System.out.printf("\tEstimated average CPC: %.2f\n", meanAverageCpc);
158
        System.out.printf("\tEstimated ad position: %.2f\n", meanAveragePosition);
159
        System.out.printf("\tEstimated daily clicks: %.2f\n", meanClicks);
160
        System.out.printf("\tEstimated daily cost: %.2f\n\n", meanTotalCost);
161
      }
162
    } else {
163
      System.out.println("No traffic estimates were returned.");
164
    }
165
  }
166
}