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