// Get the AdGroupService.
AdGroupServiceInterface adGroupService =
adWordsServices.get(session, AdGroupServiceInterface.class);
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService =
adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Create experiment.
Experiment experiment = new Experiment();
experiment.setCampaignId(campaignId);
experiment.setName("Interplanetary Experiment #" + System.currentTimeMillis());
experiment.setQueryPercentage(10);
experiment.setStartDateTime(new DateTime().plusDays(1).toString("yyyyMMdd HHmmss Z"));
// You can optionally provide these field(s).
experiment.setEndDateTime(new DateTime().plusDays(30).toString("yyyyMMdd HHmmss Z"));
// Create operation.
ExperimentOperation experimentOperation = new ExperimentOperation();
experimentOperation.setOperand(experiment);
experimentOperation.setOperator(Operator.ADD);
// Add experiment.
ExperimentReturnValue result =
experimentService.mutate(new ExperimentOperation[] {experimentOperation});
for (Experiment experimentResult : result.getValue()) {
System.out.println("Experiment with name \"" + experimentResult.getName() + "\" and id \""
+ experimentResult.getId() + "\" was added.");
}
Long experimentId = result.getValue()[0].getId();
// Set ad group for the experiment.
AdGroup adGroup = new AdGroup();
adGroup.setId(adGroupId);
// Create experiment bid multiplier rule that will modify ad group bid for
// the experiment.
ManualCPCAdGroupExperimentBidMultipliers adGroupExperimentBidMultipliers =
new ManualCPCAdGroupExperimentBidMultipliers();
adGroupExperimentBidMultipliers.setMaxCpcMultiplier(new BidMultiplier(1.5, null));
// Set experiment data to the ad group.
AdGroupExperimentData adGroupExperimentData = new AdGroupExperimentData();
adGroupExperimentData.setExperimentId(experimentId);
adGroupExperimentData.setExperimentDeltaStatus(ExperimentDeltaStatus.MODIFIED);
adGroupExperimentData.setExperimentBidMultipliers(adGroupExperimentBidMultipliers);
adGroup.setExperimentData(adGroupExperimentData);
// Create operation.
AdGroupOperation adGroupOperation = new AdGroupOperation();
adGroupOperation.setOperand(adGroup);
adGroupOperation.setOperator(Operator.SET);
// Update ad group.
adGroup = adGroupService.mutate(new AdGroupOperation[] {adGroupOperation}).getValue()[0];
System.out.println("Ad group with name \"" + adGroup.getName() + "\" and id \""
+ adGroup.getId() + "\" was updated for the experiment.");
// Set ad group criterion for the experiment.
BiddableAdGroupCriterion adGroupCriterion = new BiddableAdGroupCriterion();
adGroupCriterion.setCriterion(new Criterion(criterionId, null, null));
adGroupCriterion.setAdGroupId(adGroupId);
// Create experiment bid multiplier rule that will modify ad group bid for
// the experiment.
ManualCPCAdGroupCriterionExperimentBidMultiplier adGroupCriterionExperimentBidMultiplier =
new ManualCPCAdGroupCriterionExperimentBidMultiplier();
adGroupCriterionExperimentBidMultiplier.setMaxCpcMultiplier(new BidMultiplier(1.5, null));
// Set experiment data to the ad group.
BiddableAdGroupCriterionExperimentData adGroupCriterionExperimentData =
new BiddableAdGroupCriterionExperimentData();
adGroupCriterionExperimentData.setExperimentId(experimentId);
adGroupCriterionExperimentData.setExperimentDeltaStatus(ExperimentDeltaStatus.MODIFIED);
adGroupCriterionExperimentData
.setExperimentBidMultiplier(adGroupCriterionExperimentBidMultiplier);
adGroupCriterion.setExperimentData(adGroupCriterionExperimentData);
// Create operation.
AdGroupCriterionOperation adGroupCriterionOperation = new AdGroupCriterionOperation();
adGroupCriterionOperation.setOperand(adGroupCriterion);
adGroupCriterionOperation.setOperator(Operator.SET);
// Update ad group criterion.
adGroupCriterion =
(BiddableAdGroupCriterion) adGroupCriterionService.mutate(
new AdGroupCriterionOperation[] {adGroupCriterionOperation}).getValue()[0];
System.out.println("Ad group criterion with ad group id \"" + adGroupCriterion.getAdGroupId()
+ "\" and criterion id \"" + adGroupCriterion.getCriterion().getId()
+ "\" was updated for the experiment.");