Subversion Repositories SmartDukaan

Rev

Rev 9018 | Go to most recent revision | 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.basicoperations;
16
 
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
 
22
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
23
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterion;
24
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionPage;
25
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionServiceInterface;
26
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
27
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
28
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
29
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
30
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
31
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
32
import com.google.api.ads.adwords.axis.v201309.cm.Predicate;
33
import com.google.api.ads.adwords.axis.v201309.cm.PredicateOperator;
34
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
35
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
36
import com.google.api.ads.adwords.lib.client.AdWordsSession;
37
import com.google.api.ads.common.lib.auth.OfflineCredentials;
38
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
39
import com.google.api.client.auth.oauth2.Credential;
40
 
41
/**
42
 * This example gets all ad group criteria in an account. To add keywords
43
 * criteria, run AddKeywords.java
44
 *
45
 * Credentials and properties in {@code fromFile()} are pulled from the
46
 * "ads.properties" file. See README for more info.
47
 *
48
 * Tags: AdGroupCriterionService.get
49
 *
50
 * Category: adx-exclude
51
 *
52
 * @author Kevin Winter
53
 */
54
public class GetKeywords {
55
/* 6796860944 |
56
| 6796861064 |
57
| 6796861184 |
58
| 6796861304 |
59
| 6796861424 |
60
| 6796861544 |
61
| 6803931224 |
62
| 6803931344 |
63
| 6803931464 |
64
| 6803931584 |
65
| 6803931704 |
66
| 4438409624 |
67
| 4439752304 |
68
| 4439752424 |
69
	*/
70
	private static final int PAGE_SIZE = 100;
71
 
72
	public static void main(String[] args) {
73
		try {
74
			List<Long> adgroupIds=new ArrayList<Long>();
75
			adgroupIds.add(6796860944L);
76
			adgroupIds.add(6796861064L);
77
			adgroupIds.add(6796861184L);
78
			adgroupIds.add(6796861304L);
79
			for(Long adgroupId : adgroupIds){
80
				List<Map<String, String>> adgroupAdKeywordDataMainList = runExample(adgroupId);
81
				if(adgroupAdKeywordDataMainList.size()>0){
82
					for(Map<String,String> adgroupAdKeywordDataMap : adgroupAdKeywordDataMainList){
83
						System.out.println(adgroupAdKeywordDataMap.get("AdgroupId")+" "
84
								+adgroupAdKeywordDataMap.get("Text")+" "
85
								+adgroupAdKeywordDataMap.get("CriterionId")+ " "
86
								+adgroupAdKeywordDataMap.get("Type")+" "
87
								+adgroupAdKeywordDataMap.get("BidAmount"));
88
					}
89
				}
90
			}
91
		} catch (Exception e) {
92
			// TODO Auto-generated catch block
93
			e.printStackTrace();
94
		}
95
	}
96
 
97
	public static List<Map<String, String>> runExample(Long adGroupId) throws Exception {
98
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
99
		// and can be used in place of a service account.
100
		Credential oAuth2Credential = new OfflineCredentials.Builder()
101
		.forApi(Api.ADWORDS)
102
		.fromFile()
103
		.build()
104
		.generateCredential();
105
 
106
		// Construct an AdWordsSession.
107
		AdWordsSession session = new AdWordsSession.Builder()
108
		.fromFile()
109
		.withOAuth2Credential(oAuth2Credential)
110
		.build();
111
 
112
		AdWordsServices adWordsServices = new AdWordsServices();
113
 
114
		// Get the AdGroupCriterionService.
115
		AdGroupCriterionServiceInterface adGroupCriterionService =
116
			adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
117
 
118
		int offset = 0;
119
		boolean morePages = true;
120
 
121
		// Create selector.
122
		Selector selector = new Selector();
123
		selector.setFields(new String[] {"Id", "AdGroupId", "MatchType", "KeywordText"});
124
		selector.setOrdering(new OrderBy[] {new OrderBy("AdGroupId", SortOrder.ASCENDING)});
125
		selector.setPaging(new Paging(offset, PAGE_SIZE));
126
 
127
		// Create predicates.
128
		Predicate adGroupIdPredicate =
129
			new Predicate("AdGroupId", PredicateOperator.IN, new String[] {adGroupId.toString()});
130
		Predicate criteriaTypePredicate =
131
			new Predicate("CriteriaType", PredicateOperator.EQUALS, new String[] {"KEYWORD"});
132
		selector.setPredicates(new Predicate[] {adGroupIdPredicate, criteriaTypePredicate});
133
 
134
		List<Map<String, String>> keywordDataMainList = new ArrayList<Map<String,String>>();
135
 
136
		while (morePages) {
137
			// Get all ad group criteria.
138
			AdGroupCriterionPage page = adGroupCriterionService.get(selector);
139
 
140
			// Display ad group criteria.
141
			if (page.getEntries() != null && page.getEntries().length > 0) {
142
				// Display results.
143
				for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
144
					System.out.println("Keyword ad group criterion with ad group id \""
145
							+ adGroupCriterionResult.getAdGroupId() + "\", criterion id \""
146
							+ adGroupCriterionResult.getCriterion().getId() + "\", text \""
147
							+ ((Keyword) adGroupCriterionResult.getCriterion()).getText()
148
							+ "\" and match type \""
149
							+ ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType() + "\" was found.");
150
 
151
						BiddableAdGroupCriterion biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
152
						Keyword keyword = (Keyword) adGroupCriterionResult.getCriterion();
153
						if(biddableAdGroupCriterion!=null){
154
							if(biddableAdGroupCriterion.getBiddingStrategyConfiguration()!=null){
155
								Bids bid = biddableAdGroupCriterion.getBiddingStrategyConfiguration().getBids(0);
156
 
157
								Map<String,String> keywordMap = new HashMap<String,String>();
158
								keywordMap.put("AdgroupId", adGroupCriterionResult.getAdGroupId()+"");
159
								keywordMap.put("Text",keyword.getText());
160
								keywordMap.put("CriterionId", keyword.getId()+"");
161
								keywordMap.put("Type", keyword.getMatchType().getValue());
162
								keywordMap.put("BidAmount", ((CpcBid)bid).getBid().getMicroAmount()+"");
163
								keywordDataMainList.add(keywordMap);
164
 
165
							}
166
					}
167
				}
168
			} else {
169
				System.out.println("No ad group criteria were found.");
170
			}
171
 
172
			offset += PAGE_SIZE;
173
			selector.getPaging().setStartIndex(offset);
174
			morePages = offset < page.getTotalNumEntries();
175
		}
176
		return keywordDataMainList;
177
	}
178
}