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.v201302.basicoperations;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupCriterion;
19
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupCriterionOperation;
20
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupCriterionReturnValue;
21
import com.google.api.ads.adwords.axis.v201302.cm.AdGroupCriterionServiceInterface;
22
import com.google.api.ads.adwords.axis.v201302.cm.BiddableAdGroupCriterion;
23
import com.google.api.ads.adwords.axis.v201302.cm.BiddingStrategyConfiguration;
24
import com.google.api.ads.adwords.axis.v201302.cm.Bids;
25
import com.google.api.ads.adwords.axis.v201302.cm.CpcBid;
26
import com.google.api.ads.adwords.axis.v201302.cm.Keyword;
27
import com.google.api.ads.adwords.axis.v201302.cm.KeywordMatchType;
28
import com.google.api.ads.adwords.axis.v201302.cm.Money;
29
import com.google.api.ads.adwords.axis.v201302.cm.NegativeAdGroupCriterion;
30
import com.google.api.ads.adwords.axis.v201302.cm.Operator;
31
import com.google.api.ads.adwords.axis.v201302.cm.UserStatus;
32
import com.google.api.ads.adwords.lib.client.AdWordsSession;
33
import com.google.api.ads.common.lib.auth.OfflineCredentials;
34
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
35
import com.google.api.client.auth.oauth2.Credential;
36
 
37
/**
38
 * This example adds keywords to an ad group. To get ad groups, run
39
 * AddAdGroup.java
40
 *
41
 * Credentials and properties in {@code fromFile()} are pulled from the
42
 * "ads.properties" file. See README for more info.
43
 *
44
 * Tags: AdGroupCriterionService.mutate
45
 *
46
 * Category: adx-exclude
47
 *
48
 * @author Kevin Winter
49
 */
50
public class AddKeywords {
51
 
52
  public static void main(String[] args) 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();
61
 
62
    // Construct an AdWordsSession.
63
    AdWordsSession session = new AdWordsSession.Builder()
64
        .fromFile()
65
        .withOAuth2Credential(oAuth2Credential)
66
        .build();
67
 
68
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
69
 
70
    AdWordsServices adWordsServices = new AdWordsServices();
71
 
72
    runExample(adWordsServices, session, adGroupId);
73
  }
74
 
75
  public static void runExample(
76
      AdWordsServices adWordsServices, AdWordsSession session, long adGroupId) throws Exception {
77
    // Get the AdGroupCriterionService.
78
    AdGroupCriterionServiceInterface adGroupCriterionService =
79
        adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
80
 
81
    // Create keywords.
82
    Keyword keyword1 = new Keyword();
83
    keyword1.setText("mars");
84
    keyword1.setMatchType(KeywordMatchType.BROAD);
85
    Keyword keyword2 = new Keyword();
86
    keyword2.setText("pluto");
87
    keyword2.setMatchType(KeywordMatchType.EXACT);
88
 
89
    // Create biddable ad group criterion.
90
    BiddableAdGroupCriterion keywordBiddableAdGroupCriterion1 = new BiddableAdGroupCriterion();
91
    keywordBiddableAdGroupCriterion1.setAdGroupId(adGroupId);
92
    keywordBiddableAdGroupCriterion1.setCriterion(keyword1);
93
 
94
    // You can optionally provide these field(s).
95
    keywordBiddableAdGroupCriterion1.setUserStatus(UserStatus.PAUSED);
96
    keywordBiddableAdGroupCriterion1.setDestinationUrl("http://example.com/mars");
97
    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
98
    CpcBid bid = new CpcBid();
99
    bid.setBid(new Money(null, 10000000L));
100
    biddingStrategyConfiguration.setBids(new Bids[] {bid});
101
    keywordBiddableAdGroupCriterion1.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
102
 
103
    NegativeAdGroupCriterion keywordNegativeAdGroupCriterion2 = new NegativeAdGroupCriterion();
104
    keywordNegativeAdGroupCriterion2.setAdGroupId(adGroupId);
105
    keywordNegativeAdGroupCriterion2.setCriterion(keyword2);
106
 
107
    // Create operations.
108
    AdGroupCriterionOperation keywordAdGroupCriterionOperation1 = new AdGroupCriterionOperation();
109
    keywordAdGroupCriterionOperation1.setOperand(keywordBiddableAdGroupCriterion1);
110
    keywordAdGroupCriterionOperation1.setOperator(Operator.ADD);
111
    AdGroupCriterionOperation keywordAdGroupCriterionOperation2 = new AdGroupCriterionOperation();
112
    keywordAdGroupCriterionOperation2.setOperand(keywordNegativeAdGroupCriterion2);
113
    keywordAdGroupCriterionOperation2.setOperator(Operator.ADD);
114
 
115
    AdGroupCriterionOperation[] operations =
116
        new AdGroupCriterionOperation[] {keywordAdGroupCriterionOperation1,
117
            keywordAdGroupCriterionOperation2};
118
 
119
    // Add keywords.
120
    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations);
121
 
122
    // Display results.
123
    for (AdGroupCriterion adGroupCriterionResult : result.getValue()) {
124
      System.out.println("Keyword ad group criterion with ad group id \""
125
          + adGroupCriterionResult.getAdGroupId() + "\", criterion id \""
126
          + adGroupCriterionResult.getCriterion().getId() + "\", text \""
127
          + ((Keyword) adGroupCriterionResult.getCriterion()).getText() + "\" and match type \""
128
          + ((Keyword) adGroupCriterionResult.getCriterion()).getMatchType() + "\" was added.");
129
    }
130
  }
131
}