Subversion Repositories SmartDukaan

Rev

Rev 9227 | 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 com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterion;
19
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionReturnValue;
21
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
23
import com.google.api.ads.adwords.axis.v201309.cm.BiddingStrategyConfiguration;
24
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
25
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
26
import com.google.api.ads.adwords.axis.v201309.cm.Criterion;
9227 manish.sha 27
import com.google.api.ads.adwords.axis.v201309.cm.Keyword;
28
import com.google.api.ads.adwords.axis.v201309.cm.KeywordMatchType;
9005 manish.sha 29
import com.google.api.ads.adwords.axis.v201309.cm.Money;
30
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
31
import com.google.api.ads.adwords.lib.client.AdWordsSession;
32
import com.google.api.ads.common.lib.auth.OfflineCredentials;
33
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
34
import com.google.api.client.auth.oauth2.Credential;
35
 
36
/**
37
 * This example updates the bid of a keyword. To add a keyword, run
38
 * AddKeywords.java.
39
 *
40
 * Credentials and properties in {@code fromFile()} are pulled from the
41
 * "ads.properties" file. See README for more info.
42
 *
43
 * Tags: AdGroupCriterionService.mutate
44
 *
45
 * Category: adx-exclude
46
 *
47
 * @author Kevin Winter
48
 */
49
public class UpdateKeyword {
50
 
9227 manish.sha 51
	public static Long runExample(Long adGroupId, Long keywordId, String text, Long bidAmount, String type)
52
	throws Exception {
53
 
54
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
55
		// and can be used in place of a service account.
56
		Credential oAuth2Credential = new OfflineCredentials.Builder()
57
		.forApi(Api.ADWORDS)
58
		.fromFile()
59
		.build()
60
		.generateCredential();
9005 manish.sha 61
 
9227 manish.sha 62
		// Construct an AdWordsSession.
63
		AdWordsSession session = new AdWordsSession.Builder()
64
		.fromFile()
65
		.withOAuth2Credential(oAuth2Credential)
66
		.build();
9005 manish.sha 67
 
9227 manish.sha 68
		AdWordsServices adWordsServices = new AdWordsServices();
69
 
70
		// Get the AdGroupCriterionService.
71
		AdGroupCriterionServiceInterface adGroupCriterionService =
72
			adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
9005 manish.sha 73
 
9227 manish.sha 74
		Keyword keyword = new Keyword();
9441 manish.sha 75
		if(text!=null && !("").equalsIgnoreCase(text)){
9227 manish.sha 76
			keyword.setText(text);
77
		}
9441 manish.sha 78
		if(type!=null && !("").equalsIgnoreCase(type)){
9227 manish.sha 79
			keyword.setMatchType(KeywordMatchType.fromString(type));
80
		}
81
		keyword.setId(keywordId);
82
		// Create ad group criterion with updated bid.
83
		/*Criterion criterion = new Criterion();
84
		criterion.setId(keywordId);*/
85
 
86
		BiddableAdGroupCriterion biddableAdGroupCriterion = new BiddableAdGroupCriterion();
87
		biddableAdGroupCriterion.setAdGroupId(adGroupId);
88
		biddableAdGroupCriterion.setCriterion(keyword);
89
 
90
		if(bidAmount > -1l){
91
			// Create bids.
92
			BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
93
			CpcBid bid = new CpcBid();
94
			bid.setBid(new Money(null, bidAmount));
95
			biddingStrategyConfiguration.setBids(new Bids[] {bid});
96
			biddableAdGroupCriterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
97
		}
98
		// Create operations.
99
		AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
100
		operation.setOperand(biddableAdGroupCriterion);
101
		operation.setOperator(Operator.SET);
9005 manish.sha 102
 
9227 manish.sha 103
		AdGroupCriterionOperation[] operations = new AdGroupCriterionOperation[] {operation};
104
 
105
		if((text!=null && ("").equalsIgnoreCase(text)) || (type!=null && ("").equalsIgnoreCase(type)) || (bidAmount > -1l)){
106
			// Update ad group criteria.
107
			AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
108
 
109
			Long criterionId = 0l;
110
			// Display ad group criteria.
111
			for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
112
				if (adGroupCriterionResult instanceof BiddableAdGroupCriterion) {
113
					biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterionResult;
9441 manish.sha 114
					Bids[] bids = biddableAdGroupCriterion.getBiddingStrategyConfiguration()
115
									.getBids();
116
					CpcBid cpcBid = null;
117
					for(Bids b: bids){
118
						if("CpcBid".equalsIgnoreCase(b.getBidsType())){
119
							cpcBid = (CpcBid) b;
120
						}
121
					}
9227 manish.sha 122
					System.out.println(
123
							"Ad group criterion with ad group id \""
124
							+ biddableAdGroupCriterion.getAdGroupId() + "\", criterion id \""
125
							+ biddableAdGroupCriterion.getCriterion().getId() + "\", type \""
126
							+ biddableAdGroupCriterion.getCriterion().getCriterionType()
127
							+ "\", and bid \""
9441 manish.sha 128
							+ cpcBid!= null ? cpcBid.getBid().getMicroAmount() : " "+ "\" was updated.");
9227 manish.sha 129
					criterionId = biddableAdGroupCriterion.getCriterion().getId();
130
				}
131
			}
132
			return criterionId;
133
		}
134
 
135
		return 0l;
136
	}
9005 manish.sha 137
}