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.campaignmanagement;
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.AdGroupCriterionOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupCriterionServiceInterface;
21
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupExperimentData;
22
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupOperation;
23
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
24
import com.google.api.ads.adwords.axis.v201309.cm.BidMultiplier;
25
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterion;
26
import com.google.api.ads.adwords.axis.v201309.cm.BiddableAdGroupCriterionExperimentData;
27
import com.google.api.ads.adwords.axis.v201309.cm.Criterion;
28
import com.google.api.ads.adwords.axis.v201309.cm.Experiment;
29
import com.google.api.ads.adwords.axis.v201309.cm.ExperimentDeltaStatus;
30
import com.google.api.ads.adwords.axis.v201309.cm.ExperimentOperation;
31
import com.google.api.ads.adwords.axis.v201309.cm.ExperimentReturnValue;
32
import com.google.api.ads.adwords.axis.v201309.cm.ExperimentServiceInterface;
33
import com.google.api.ads.adwords.axis.v201309.cm.ManualCPCAdGroupCriterionExperimentBidMultiplier;
34
import com.google.api.ads.adwords.axis.v201309.cm.ManualCPCAdGroupExperimentBidMultipliers;
35
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
36
import com.google.api.ads.adwords.lib.client.AdWordsSession;
37
import com.google.api.ads.common.lib.auth.OfflineCredentials;
38
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
39
import com.google.api.client.auth.oauth2.Credential;
40
 
41
import org.joda.time.DateTime;
42
 
43
/**
44
 * This example creates an experiment using a query percentage of 10, which
45
 * defines what fraction of auctions should go to the control split (90%) vs.
46
 * the experiment split (10%), then adds experimental bid changes for criteria
47
 * and ad groups. To get campaigns, run GetAllCampaigns.java. To get ad groups,
48
 * run GetAllAdGroups.java. To get criteria, run GetAllAdGroupCriteria.java.
49
 *
50
 * Credentials and properties in {@code fromFile()} are pulled from the
51
 * "ads.properties" file. See README for more info.
52
 *
53
 * Tags: ExperimentService.mutate
54
 *
55
 * Category: adx-exclude
56
 *
57
 * @author Kevin Winter
58
 */
59
public class AddExperiment {
60
 
61
  public static void main(String[] args) throws Exception {
62
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
63
    // and can be used in place of a service account.
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
    // Replace with valid values of your account.
77
    long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
78
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
79
    long criterionId = Long.parseLong("INSERT_CRITERION_ID_HERE");
80
 
81
    AdWordsServices adWordsServices = new AdWordsServices();
82
 
83
    runExample(adWordsServices, session, campaignId, adGroupId, criterionId);
84
  }
85
 
86
  public static void runExample(
87
      AdWordsServices adWordsServices, AdWordsSession session, Long campaignId, Long adGroupId,
88
      Long criterionId) throws Exception {
89
    // Get the ExperimentService.
90
    ExperimentServiceInterface experimentService =
91
        adWordsServices.get(session, ExperimentServiceInterface.class);
92
 
93
    // Get the AdGroupService.
94
    AdGroupServiceInterface adGroupService =
95
        adWordsServices.get(session, AdGroupServiceInterface.class);
96
 
97
    // Get the AdGroupCriterionService.
98
    AdGroupCriterionServiceInterface adGroupCriterionService =
99
        adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
100
 
101
    // Create experiment.
102
    Experiment experiment = new Experiment();
103
    experiment.setCampaignId(campaignId);
104
    experiment.setName("Interplanetary Experiment #" + System.currentTimeMillis());
105
    experiment.setQueryPercentage(10);
106
    experiment.setStartDateTime(new DateTime().plusDays(1).toString("yyyyMMdd HHmmss Z"));
107
 
108
    // You can optionally provide these field(s).
109
    experiment.setEndDateTime(new DateTime().plusDays(30).toString("yyyyMMdd HHmmss Z"));
110
 
111
    // Create operation.
112
    ExperimentOperation experimentOperation = new ExperimentOperation();
113
    experimentOperation.setOperand(experiment);
114
    experimentOperation.setOperator(Operator.ADD);
115
 
116
    // Add experiment.
117
    ExperimentReturnValue result =
118
        experimentService.mutate(new ExperimentOperation[] {experimentOperation});
119
 
120
    for (Experiment experimentResult : result.getValue()) {
121
      System.out.println("Experiment with name \"" + experimentResult.getName() + "\" and id \""
122
          + experimentResult.getId() + "\" was added.");
123
    }
124
 
125
    Long experimentId = result.getValue()[0].getId();
126
 
127
    // Set ad group for the experiment.
128
    AdGroup adGroup = new AdGroup();
129
    adGroup.setId(adGroupId);
130
 
131
    // Create experiment bid multiplier rule that will modify ad group bid for
132
    // the experiment.
133
    ManualCPCAdGroupExperimentBidMultipliers adGroupExperimentBidMultipliers =
134
        new ManualCPCAdGroupExperimentBidMultipliers();
135
    adGroupExperimentBidMultipliers.setMaxCpcMultiplier(new BidMultiplier(1.5, null));
136
 
137
    // Set experiment data to the ad group.
138
    AdGroupExperimentData adGroupExperimentData = new AdGroupExperimentData();
139
    adGroupExperimentData.setExperimentId(experimentId);
140
    adGroupExperimentData.setExperimentDeltaStatus(ExperimentDeltaStatus.MODIFIED);
141
    adGroupExperimentData.setExperimentBidMultipliers(adGroupExperimentBidMultipliers);
142
    adGroup.setExperimentData(adGroupExperimentData);
143
 
144
    // Create operation.
145
    AdGroupOperation adGroupOperation = new AdGroupOperation();
146
    adGroupOperation.setOperand(adGroup);
147
    adGroupOperation.setOperator(Operator.SET);
148
 
149
    // Update ad group.
150
    adGroup = adGroupService.mutate(new AdGroupOperation[] {adGroupOperation}).getValue()[0];
151
 
152
    System.out.println("Ad group with name \"" + adGroup.getName() + "\" and id \""
153
        + adGroup.getId() + "\" was updated for the experiment.");
154
 
155
    // Set ad group criterion for the experiment.
156
    BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion();
157
    adGroupCriterion.setCriterion(new Criterion(criterionId, null, null));
158
    adGroupCriterion.setAdGroupId(adGroupId);
159
 
160
    // Create experiment bid multiplier rule that will modify ad group bid for
161
    // the experiment.
162
    ManualCPCAdGroupCriterionExperimentBidMultiplier adGroupCriterionExperimentBidMultiplier =
163
        new ManualCPCAdGroupCriterionExperimentBidMultiplier();
164
    adGroupCriterionExperimentBidMultiplier.setMaxCpcMultiplier(new BidMultiplier(1.5, null));
165
 
166
    // Set experiment data to the ad group.
167
    BiddableAdGroupCriterionExperimentData adGroupCriterionExperimentData =
168
        new BiddableAdGroupCriterionExperimentData();
169
    adGroupCriterionExperimentData.setExperimentId(experimentId);
170
    adGroupCriterionExperimentData.setExperimentDeltaStatus(ExperimentDeltaStatus.MODIFIED);
171
    adGroupCriterionExperimentData
172
        .setExperimentBidMultiplier(adGroupCriterionExperimentBidMultiplier);
173
    adGroupCriterion.setExperimentData(adGroupCriterionExperimentData);
174
 
175
    // Create operation.
176
    AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation();
177
    adGroupCriterionOperation.setOperand(adGroupCriterion);
178
    adGroupCriterionOperation.setOperator(Operator.SET);
179
 
180
    // Update ad group criterion.
181
    adGroupCriterion =
182
        (BiddableAdGroupCriterion) adGroupCriterionService.mutate(
183
            new AdGroupCriterionOperation[] {adGroupCriterionOperation}).getValue()[0];
184
 
185
    System.out.println("Ad group criterion with ad group id \"" + adGroupCriterion.getAdGroupId()
186
        + "\" and criterion id \"" + adGroupCriterion.getCriterion().getId()
187
        + "\" was updated for the experiment.");
188
  }
189
}