Subversion Repositories SmartDukaan

Rev

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 com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201309.cm.AdGroup;
19
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupReturnValue;
21
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupStatus;
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.CriterionTypeGroup;
27
import com.google.api.ads.adwords.axis.v201309.cm.Money;
28
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
29
import com.google.api.ads.adwords.axis.v201309.cm.TargetingSetting;
30
import com.google.api.ads.adwords.axis.v201309.cm.TargetingSettingDetail;
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 adds ad groups to a campaign. To get campaigns, run
38
 * GetCampaigns.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: AdGroupService.mutate
44
 *
45
 * Category: adx-exclude
46
 *
47
 * @author Kevin Winter
48
 */
49
public class AddAdGroups {
50
 
51
 
52
	public static Long runExample(long campaignId, String name, String status, Long bidAmount) throws Exception {
53
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
54
		// and can be used in place of a service account.
55
		Credential oAuth2Credential = new OfflineCredentials.Builder()
56
		.forApi(Api.ADWORDS)
57
		.fromFile()
58
		.build()
59
		.generateCredential();
60
 
61
		// Construct an AdWordsSession.
62
		AdWordsSession session = new AdWordsSession.Builder()
63
		.fromFile()
64
		.withOAuth2Credential(oAuth2Credential)
65
		.build();
66
 
67
		AdWordsServices adWordsServices = new AdWordsServices();
68
		// Get the AdGroupService.
69
		AdGroupServiceInterface adGroupService =
70
			adWordsServices.get(session, AdGroupServiceInterface.class);
71
 
72
		// Create ad group.
73
		AdGroup adGroup = new AdGroup();
74
		adGroup.setName(name);
75
		adGroup.setStatus(AdGroupStatus.fromString(status));
76
		adGroup.setCampaignId(campaignId);
77
		//Setting[] setting = new Setting[1];
78
 
79
 
80
		// Optional settings.
81
 
82
		// Targeting restriction settings - these settings only affect serving
83
		// for the Display Network.
84
		TargetingSetting targeting = new TargetingSetting();
85
		TargetingSettingDetail placements = new TargetingSettingDetail();
86
		placements.setCriterionTypeGroup(CriterionTypeGroup.PLACEMENT);
87
		placements.setTargetAll(Boolean.TRUE);
88
		TargetingSettingDetail verticals = new TargetingSettingDetail();
89
		verticals.setCriterionTypeGroup(CriterionTypeGroup.VERTICAL);
90
		verticals.setTargetAll(Boolean.FALSE);
91
		targeting.setDetails(new TargetingSettingDetail[]{placements, verticals});
92
 
93
		// Create ad group bid.
94
		BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
95
		CpcBid bid = new CpcBid();
96
		bid.setBid(new Money(null, bidAmount));
97
 
98
		// You can optionally provide this field.
99
		//bid.setContentBid(new Money(null, 20000000L));
100
 
101
		biddingStrategyConfiguration.setBids(new Bids[] {bid});
102
		adGroup.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
103
 
104
		// Add as many additional ad groups as you need.
105
		/*AdGroup adGroup2 = new AdGroup();
106
		adGroup2.setName("Earth to Venus Cruises #" + System.currentTimeMillis());
107
		adGroup2.setStatus(AdGroupStatus.ENABLED);
108
		adGroup2.setCampaignId(campaignId);
109
 
110
 
111
		BiddingStrategyConfiguration biddingStrategyConfiguration2 = new BiddingStrategyConfiguration();
112
		CpcBid bid2 = new CpcBid();
113
		bid2.setBid(new Money(null, 10000000L));
114
		biddingStrategyConfiguration2.setBids(new Bids[] {bid2});
115
		adGroup2.setBiddingStrategyConfiguration(biddingStrategyConfiguration2);*/
116
 
117
		// Create operations.
118
		AdGroupOperation operation = new AdGroupOperation();
119
		operation.setOperand(adGroup);
120
		operation.setOperator(Operator.ADD);
121
		/*AdGroupOperation operation2 = new AdGroupOperation();
122
		operation2.setOperand(adGroup2);
123
		operation2.setOperator(Operator.ADD);*/
124
 
125
		AdGroupOperation[] operations = new AdGroupOperation[] {operation};
126
 
127
		// Add ad groups.
128
		AdGroupReturnValue result = adGroupService.mutate(operations);
129
 
130
		Long adGroupId = 0l;
131
		// Display new ad groups.
132
		for (AdGroup adGroupResult : result.getValue()) {
133
			System.out.println("Ad group with name \"" + adGroupResult.getName() + "\" and id \""
134
					+ adGroupResult.getId() + "\" was added.");
135
			adGroupId= adGroupResult.getId();
136
		}
137
		return adGroupId;
138
	}
139
}