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.AdGroup;
24
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupPage;
25
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
26
import com.google.api.ads.adwords.axis.v201309.cm.Bid;
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.OrderBy;
30
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
31
import com.google.api.ads.adwords.axis.v201309.cm.Predicate;
32
import com.google.api.ads.adwords.axis.v201309.cm.PredicateOperator;
33
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
34
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
35
import com.google.api.ads.adwords.lib.client.AdWordsSession;
36
import com.google.api.ads.common.lib.auth.OfflineCredentials;
37
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
38
import com.google.api.client.auth.oauth2.Credential;
39
 
40
/**
41
 * This example gets all ad groups in a campaign. To add an ad group, run
42
 * AddAdGroup.java. To get campaigns, run GetCampaigns.java.
43
 *
44
 * Credentials and properties in {@code fromFile()} are pulled from the
45
 * "ads.properties" file. See README for more info.
46
 *
47
 * Tags: AdGroupService.get
48
 *
49
 * @author Kevin Winter
50
 */
51
public class GetAdGroups {
52
 
53
	private static final int PAGE_SIZE = 100;
9441 manish.sha 54
 
9018 manish.sha 55
	public static void main(String args[]){
56
		try {
57
			runExample(106590944L);
58
		} catch (Exception e) {
59
			e.printStackTrace();
60
		}
61
	}
9441 manish.sha 62
 
9005 manish.sha 63
	public static List<Map<String,String>> runExample(Long campaignId) throws Exception {
64
		Credential oAuth2Credential = new OfflineCredentials.Builder()
65
		.forApi(Api.ADWORDS)
66
		.fromFile()
67
		.build()
68
		.generateCredential();
69
 
70
		// Construct an AdWordsSession.
71
		AdWordsSession session = new AdWordsSession.Builder()
72
		.fromFile()
73
		.withOAuth2Credential(oAuth2Credential)
74
		.build();
75
 
76
		AdWordsServices adWordsServices = new AdWordsServices();
77
 
78
		// Get the AdGroupService.
79
		AdGroupServiceInterface adGroupService =
80
			adWordsServices.get(session, AdGroupServiceInterface.class);
81
 
82
		int offset = 0;
83
		boolean morePages = true;
84
 
85
		// Create selector.
86
		Selector selector = new Selector();
87
		selector.setFields(new String[] {"Id", "Name", "CampaignId", "CpcBid" , "Status"});
88
		selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
89
		selector.setPaging(new Paging(offset, PAGE_SIZE));
90
 
91
		// Create predicates.
92
		Predicate campaignIdPredicate =
93
			new Predicate("CampaignId", PredicateOperator.IN, new String[] {campaignId.toString()});
94
		selector.setPredicates(new Predicate[] {campaignIdPredicate});
95
		List<Map<String,String>> adgroupDataMainList = new ArrayList<Map<String,String>>();
96
		while (morePages) {
97
			// Get all ad groups.
98
			AdGroupPage page = adGroupService.get(selector);			
99
			// Display ad groups.
100
			if (page.getEntries() != null) {
101
				for (AdGroup adGroup : page.getEntries()) {
102
					System.out.println("Ad group with name \"" + adGroup.getName() + "\" and id \""
103
							+ adGroup.getId() + "\" was found.");
104
					if(adGroup.getBiddingStrategyConfiguration()!=null){
9441 manish.sha 105
						Bids[] bids = adGroup.getBiddingStrategyConfiguration()
106
						.getBids();
107
						CpcBid cpcBid = null;
108
						for(Bids b: bids){
109
							if("CpcBid".equalsIgnoreCase(b.getBidsType())){
110
								cpcBid = (CpcBid) b;
111
							}
112
						}
113
 
114
						if(cpcBid!=null){
9005 manish.sha 115
							Map<String,String> adgroupMap = new HashMap<String,String>();
116
							adgroupMap.put("CampaignId", adGroup.getCampaignId()+"");
117
							adgroupMap.put("AdgroupId", adGroup.getId()+"");
118
							adgroupMap.put("Name", adGroup.getName());
119
							adgroupMap.put("Status", adGroup.getStatus().getValue());
9441 manish.sha 120
							adgroupMap.put("BidAmount", cpcBid.getBid().getMicroAmount()+"");
9005 manish.sha 121
							adgroupMap.put("CatalogItemId", "0");
122
							adgroupDataMainList.add(adgroupMap);
123
						}						
124
					}
9441 manish.sha 125
 
9005 manish.sha 126
				}
127
			} else {
128
				System.out.println("No ad groups were found.");
129
			}
130
 
131
			offset += PAGE_SIZE;
132
			selector.getPaging().setStartIndex(offset);
133
			morePages = offset < page.getTotalNumEntries();
134
		}
135
		return adgroupDataMainList;
136
	}
137
}