Subversion Repositories SmartDukaan

Rev

Rev 9018 | Details | Compare with Previous | 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 {
9018 manish.sha 55
 
9005 manish.sha 56
	private static final int PAGE_SIZE = 100;
9441 manish.sha 57
 
9005 manish.sha 58
	public static void main(String[] args) {
59
		try {
9018 manish.sha 60
			runExample(6796861424L);
9005 manish.sha 61
		} catch (Exception e) {
62
			e.printStackTrace();
63
		}
64
	}
65
 
66
	public static List<Map<String, String>> runExample(Long adGroupId) throws Exception {
67
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
68
		// and can be used in place of a service account.
69
		Credential oAuth2Credential = new OfflineCredentials.Builder()
70
		.forApi(Api.ADWORDS)
71
		.fromFile()
72
		.build()
73
		.generateCredential();
74
 
75
		// Construct an AdWordsSession.
76
		AdWordsSession session = new AdWordsSession.Builder()
77
		.fromFile()
78
		.withOAuth2Credential(oAuth2Credential)
79
		.build();
80
 
81
		AdWordsServices adWordsServices = new AdWordsServices();
9441 manish.sha 82
 
9005 manish.sha 83
		// Get the AdGroupCriterionService.
84
		AdGroupCriterionServiceInterface adGroupCriterionService =
85
			adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
86
 
87
		int offset = 0;
88
		boolean morePages = true;
89
 
90
		// Create selector.
91
		Selector selector = new Selector();
9018 manish.sha 92
		selector.setFields(new String[] {"Id", "AdGroupId", "CpcBid" ,"MatchType","KeywordText"});
9005 manish.sha 93
		selector.setOrdering(new OrderBy[] {new OrderBy("AdGroupId", SortOrder.ASCENDING)});
94
		selector.setPaging(new Paging(offset, PAGE_SIZE));
95
 
96
		// Create predicates.
97
		Predicate adGroupIdPredicate =
98
			new Predicate("AdGroupId", PredicateOperator.IN, new String[] {adGroupId.toString()});
99
		Predicate criteriaTypePredicate =
100
			new Predicate("CriteriaType", PredicateOperator.EQUALS, new String[] {"KEYWORD"});
101
		selector.setPredicates(new Predicate[] {adGroupIdPredicate, criteriaTypePredicate});
9441 manish.sha 102
 
9005 manish.sha 103
		List<Map<String, String>> keywordDataMainList = new ArrayList<Map<String,String>>();
9441 manish.sha 104
 
9005 manish.sha 105
		while (morePages) {
106
			// Get all ad group criteria.
107
			AdGroupCriterionPage page = adGroupCriterionService.get(selector);
108
 
109
			// Display ad group criteria.
110
			if (page.getEntries() != null && page.getEntries().length > 0) {
111
				// Display results.
112
				for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
113
					System.out.println("Keyword ad group criterion with ad group id \""
114
							+ adGroupCriterionResult.getAdGroupId() + "\", criterion id \""
115
							+ adGroupCriterionResult.getCriterion().getId() + "\", text \""
116
							+ ((Keyword) adGroupCriterionResult.getCriterion()).getText()
117
							+ "\" and match type \""
9018 manish.sha 118
							+ ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType() 
119
							+ " was found.");
120
					BiddableAdGroupCriterion biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
121
					if(("Keyword").equalsIgnoreCase(adGroupCriterionResult.getCriterion().getCriterionType())){
9005 manish.sha 122
						Keyword keyword = (Keyword) adGroupCriterionResult.getCriterion();
9018 manish.sha 123
						if(biddableAdGroupCriterion.getBiddingStrategyConfiguration()!=null){
9441 manish.sha 124
							Bids[] bids = biddableAdGroupCriterion.getBiddingStrategyConfiguration()
125
							.getBids();
126
							CpcBid cpcBid = null;
127
							for(Bids b: bids){
128
								if("CpcBid".equalsIgnoreCase(b.getBidsType())){
129
									cpcBid = (CpcBid) b;
130
								}
131
							}
132
							if(cpcBid!=null){
9005 manish.sha 133
								Map<String,String> keywordMap = new HashMap<String,String>();
134
								keywordMap.put("AdgroupId", adGroupCriterionResult.getAdGroupId()+"");
135
								keywordMap.put("Text",keyword.getText());
136
								keywordMap.put("CriterionId", keyword.getId()+"");
137
								keywordMap.put("Type", keyword.getMatchType().getValue());
9441 manish.sha 138
								keywordMap.put("BidAmount", cpcBid.getBid().getMicroAmount()+"");
9005 manish.sha 139
								keywordDataMainList.add(keywordMap);
140
							}
9018 manish.sha 141
						}
9005 manish.sha 142
					}
9441 manish.sha 143
 
9005 manish.sha 144
				}
145
			} else {
146
				System.out.println("No ad group criteria were found.");
147
			}
148
 
149
			offset += PAGE_SIZE;
150
			selector.getPaging().setStartIndex(offset);
151
			morePages = offset < page.getTotalNumEntries();
152
		}
9441 manish.sha 153
 
9018 manish.sha 154
		for(Map<String,String> adgroupAdKeywordDataMap : keywordDataMainList){
155
			System.out.println(adgroupAdKeywordDataMap.get("AdgroupId")+" "
156
					+adgroupAdKeywordDataMap.get("Text")+" "
157
					+adgroupAdKeywordDataMap.get("CriterionId")+ " "
158
					+adgroupAdKeywordDataMap.get("Type")+" "
159
					+adgroupAdKeywordDataMap.get("BidAmount"));
160
		}
9005 manish.sha 161
		return keywordDataMainList;
162
	}
163
}