Subversion Repositories SmartDukaan

Rev

Rev 9005 | Go to most recent revision | 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;
57
 
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();
82
 
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});
102
 
103
		List<Map<String, String>> keywordDataMainList = new ArrayList<Map<String,String>>();
104
 
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){
124
							if(("CpcBid").equalsIgnoreCase(biddableAdGroupCriterion.getBiddingStrategyConfiguration().getBids(0).getBidsType())){
125
								CpcBid bid = (CpcBid)biddableAdGroupCriterion.getBiddingStrategyConfiguration().getBids(0);
9005 manish.sha 126
								Map<String,String> keywordMap = new HashMap<String,String>();
127
								keywordMap.put("AdgroupId", adGroupCriterionResult.getAdGroupId()+"");
128
								keywordMap.put("Text",keyword.getText());
129
								keywordMap.put("CriterionId", keyword.getId()+"");
130
								keywordMap.put("Type", keyword.getMatchType().getValue());
9018 manish.sha 131
								keywordMap.put("BidAmount", bid.getBid().getMicroAmount()+"");
9005 manish.sha 132
								keywordDataMainList.add(keywordMap);
133
							}
9018 manish.sha 134
						}
9005 manish.sha 135
					}
9018 manish.sha 136
 
9005 manish.sha 137
				}
138
			} else {
139
				System.out.println("No ad group criteria were found.");
140
			}
141
 
142
			offset += PAGE_SIZE;
143
			selector.getPaging().setStartIndex(offset);
144
			morePages = offset < page.getTotalNumEntries();
145
		}
9018 manish.sha 146
 
147
		for(Map<String,String> adgroupAdKeywordDataMap : keywordDataMainList){
148
			System.out.println(adgroupAdKeywordDataMap.get("AdgroupId")+" "
149
					+adgroupAdKeywordDataMap.get("Text")+" "
150
					+adgroupAdKeywordDataMap.get("CriterionId")+ " "
151
					+adgroupAdKeywordDataMap.get("Type")+" "
152
					+adgroupAdKeywordDataMap.get("BidAmount"));
153
		}
9005 manish.sha 154
		return keywordDataMainList;
155
	}
156
}