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.optimization;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.Language;
19
import com.google.api.ads.adwords.axis.v201302.cm.Paging;
20
import com.google.api.ads.adwords.axis.v201302.o.Attribute;
21
import com.google.api.ads.adwords.axis.v201302.o.AttributeType;
22
import com.google.api.ads.adwords.axis.v201302.o.IdeaType;
23
import com.google.api.ads.adwords.axis.v201302.o.IntegerSetAttribute;
24
import com.google.api.ads.adwords.axis.v201302.o.LanguageSearchParameter;
25
import com.google.api.ads.adwords.axis.v201302.o.LongAttribute;
26
import com.google.api.ads.adwords.axis.v201302.o.RelatedToQuerySearchParameter;
27
import com.google.api.ads.adwords.axis.v201302.o.RequestType;
28
import com.google.api.ads.adwords.axis.v201302.o.SearchParameter;
29
import com.google.api.ads.adwords.axis.v201302.o.StringAttribute;
30
import com.google.api.ads.adwords.axis.v201302.o.TargetingIdea;
31
import com.google.api.ads.adwords.axis.v201302.o.TargetingIdeaPage;
32
import com.google.api.ads.adwords.axis.v201302.o.TargetingIdeaSelector;
33
import com.google.api.ads.adwords.axis.v201302.o.TargetingIdeaServiceInterface;
34
import com.google.api.ads.adwords.lib.client.AdWordsSession;
35
import com.google.api.ads.common.lib.auth.OfflineCredentials;
36
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
37
import com.google.api.ads.common.lib.utils.Maps;
38
import com.google.api.client.auth.oauth2.Credential;
39
import com.google.common.base.Joiner;
40
import com.google.common.primitives.Ints;
41
 
42
import java.util.Map;
43
 
44
/**
45
 * This example gets keywords related to a seed keyword.
46
 *
47
 * Credentials and properties in {@code fromFile()} are pulled from the
48
 * "ads.properties" file. See README for more info.
49
 *
50
 * Tags: TargetingIdeaService.get
51
 *
52
 * Category: adx-exclude
53
 *
54
 * @author Kevin Winter
55
 */
56
public class GetKeywordIdeas {
57
 
58
  public static void main(String[] args) throws Exception {
59
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
60
    // and can be used in place of a service account.
61
    Credential oAuth2Credential = new OfflineCredentials.Builder()
62
        .forApi(Api.ADWORDS)
63
        .fromFile()
64
        .build()
65
        .generateCredential();
66
 
67
    // Construct an AdWordsSession.
68
    AdWordsSession session = new AdWordsSession.Builder()
69
        .fromFile()
70
        .withOAuth2Credential(oAuth2Credential)
71
        .build();
72
 
73
    AdWordsServices adWordsServices = new AdWordsServices();
74
 
75
    runExample(adWordsServices, session);
76
  }
77
 
78
  public static void runExample(
79
      AdWordsServices adWordsServices, AdWordsSession session) throws Exception {
80
    // Get the TargetingIdeaService.
81
    TargetingIdeaServiceInterface targetingIdeaService =
82
        adWordsServices.get(session, TargetingIdeaServiceInterface.class);
83
 
84
    // Create selector.
85
    TargetingIdeaSelector selector = new TargetingIdeaSelector();
86
    selector.setRequestType(RequestType.IDEAS);
87
    selector.setIdeaType(IdeaType.KEYWORD);
88
    selector.setRequestedAttributeTypes(new AttributeType[] {
89
        AttributeType.KEYWORD_TEXT,
90
        AttributeType.SEARCH_VOLUME,
91
        AttributeType.CATEGORY_PRODUCTS_AND_SERVICES});
92
 
93
    // Set selector paging (required for targeting idea service).
94
    Paging paging = new Paging();
95
    paging.setStartIndex(0);
96
    paging.setNumberResults(10);
97
    selector.setPaging(paging);
98
 
99
    // Create related to query search parameter.
100
    RelatedToQuerySearchParameter relatedToQuerySearchParameter =
101
        new RelatedToQuerySearchParameter();
102
    relatedToQuerySearchParameter.setQueries(new String[] {"mars cruise"});
103
 
104
    // Language setting (optional).
105
    // The ID can be found in the documentation:
106
    //   https://developers.google.com/adwords/api/docs/appendix/languagecodes
107
    // Note: As of v201302, only a single language parameter is allowed.
108
    LanguageSearchParameter languageParameter = new LanguageSearchParameter();
109
    Language english = new Language();
110
    english.setId(1000L);
111
    languageParameter.setLanguages(new Language[] {english});
112
 
113
    selector.setSearchParameters(
114
        new SearchParameter[] {relatedToQuerySearchParameter, languageParameter});
115
 
116
    // Get related keywords.
117
    TargetingIdeaPage page = targetingIdeaService.get(selector);
118
 
119
    // Display related keywords.
120
    if (page.getEntries() != null && page.getEntries().length > 0) {
121
      for (TargetingIdea targetingIdea : page.getEntries()) {
122
        Map<AttributeType, Attribute> data = Maps.toMap(targetingIdea.getData());
123
        StringAttribute keyword = (StringAttribute) data.get(AttributeType.KEYWORD_TEXT);
124
 
125
        IntegerSetAttribute categories =
126
            (IntegerSetAttribute) data.get(AttributeType.CATEGORY_PRODUCTS_AND_SERVICES);
127
        String categoriesString = "(none)";
128
        if (categories != null && categories.getValue() != null) {
129
          categoriesString = Joiner.on(", ").join(Ints.asList(categories.getValue()));
130
        }
131
        Long averageMonthlySearches =
132
            ((LongAttribute) data.get(AttributeType.SEARCH_VOLUME))
133
                .getValue();
134
        System.out.println("Keyword with text '" + keyword.getValue()
135
            + "', and average monthly search volume '" + averageMonthlySearches
136
            + "' was found with categories: " + categoriesString);
137
      }
138
    } else {
139
      System.out.println("No related keywords were found.");
140
    }
141
  }
142
}