Skip to content

Commit bc7c6cf

Browse files
Merge pull request #887 from googleads/experimentsExample
Add experiments examples
1 parent d46ae50 commit bc7c6cf

3 files changed

Lines changed: 367 additions & 31 deletions

File tree

google-ads-examples/src/main/java/com/google/ads/googleads/examples/campaignmanagement/CreateExperiment.java renamed to google-ads-examples/src/main/java/com/google/ads/googleads/examples/experiments/CreateSearchCustomExperiment.java

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package com.google.ads.googleads.examples.campaignmanagement;
15+
package com.google.ads.googleads.examples.experiments;
1616

1717
import static com.google.ads.googleads.examples.utils.CodeSampleHelper.getPrintableDateTime;
1818

@@ -47,12 +47,19 @@
4747
import java.util.List;
4848

4949
/**
50-
* This example creates a new experiment, experiment arms, and demonstrates how to modify the draft
51-
* campaign as well as begin the experiment.
50+
* Creates a standard, system-managed campaign experiment of type SEARCH_CUSTOM.
51+
*
52+
* <p>Sets up the experiment, configures its control and treatment arms (where the treatment arm
53+
* automatically generates a draft campaign), modifies the system-generated draft campaign, and
54+
* schedule the experiment.
55+
*
56+
* <p>Note: This standard draft-based workflow applies only to experiment types that use
57+
* system-generated treatment campaign copies, and excludes intra-campaign or asset-optimization
58+
* experiments.
5259
*/
53-
public class CreateExperiment {
60+
public class CreateSearchCustomExperiment {
5461

55-
private static class CreateExperimentParams extends CodeSampleParams {
62+
private static class CreateSearchCustomExperimentParams extends CodeSampleParams {
5663

5764
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
5865
private Long customerId;
@@ -62,7 +69,7 @@ private static class CreateExperimentParams extends CodeSampleParams {
6269
}
6370

6471
public static void main(String[] args) {
65-
CreateExperimentParams params = new CreateExperimentParams();
72+
CreateSearchCustomExperimentParams params = new CreateSearchCustomExperimentParams();
6673
if (!params.parseArguments(args)) {
6774
throw new IllegalArgumentException("Invalid or missing command line arguments");
6875
}
@@ -80,7 +87,8 @@ public static void main(String[] args) {
8087
}
8188

8289
try {
83-
new CreateExperiment().runExample(googleAdsClient, params.customerId, params.baseCampaignId);
90+
new CreateSearchCustomExperiment()
91+
.runExample(googleAdsClient, params.customerId, params.baseCampaignId);
8492
} catch (GoogleAdsException gae) {
8593
// GoogleAdsException is the base class for most exceptions thrown by an API request.
8694
// Instances of this exception have a message and a GoogleAdsFailure that contains a
@@ -119,9 +127,7 @@ private void runExample(GoogleAdsClient googleAdsClient, long customerId, long b
119127
}
120128
}
121129

122-
/**
123-
* Creates a campaign experiment.
124-
*/
130+
/** Creates a campaign experiment. */
125131
// [START create_experiment_1]
126132
private String createExperimentResource(GoogleAdsClient googleAdsClient, long customerId) {
127133
ExperimentOperation operation =
@@ -130,6 +136,9 @@ private String createExperimentResource(GoogleAdsClient googleAdsClient, long cu
130136
Experiment.newBuilder()
131137
// Name must be unique.
132138
.setName("Example Experiment #" + getPrintableDateTime())
139+
// We specify SEARCH_CUSTOM to create a standard search campaign experiment.
140+
// This type uses a standard draft-based workflow where the system automatically
141+
// creates a draft/in-design campaign for the treatment arm.
133142
.setType(ExperimentType.SEARCH_CUSTOM)
134143
.setSuffix("[experiment]")
135144
.setStatus(ExperimentStatus.SETUP)
@@ -146,11 +155,10 @@ private String createExperimentResource(GoogleAdsClient googleAdsClient, long cu
146155
return experiment;
147156
}
148157
}
158+
149159
// [END create_experiment_1]
150160

151-
/**
152-
* Creates control and experiment arms for the experiment.
153-
*/
161+
/** Creates control and experiment arms for the experiment. */
154162
// [START create_experiment_2]
155163
private String createExperimentArms(
156164
GoogleAdsClient googleAdsClient, long customerId, long campaignId, String experiment) {
@@ -170,8 +178,8 @@ private String createExperimentArms(
170178
operations.add(
171179
ExperimentArmOperation.newBuilder()
172180
.setCreate(
173-
// The non-"control" arm, also called a "treatment" arm, will automatically
174-
// generate draft campaigns that you can modify before starting the experiment.
181+
// In standard campaign experiments, creating the treatment arm automatically
182+
// generates a draft campaign that you can modify before starting the experiment.
175183
ExperimentArm.newBuilder()
176184
.setControl(false)
177185
.setExperiment(experiment)
@@ -183,13 +191,14 @@ private String createExperimentArms(
183191
try (ExperimentArmServiceClient experimentArmServiceClient =
184192
googleAdsClient.getLatestVersion().createExperimentArmServiceClient()) {
185193
// Constructs the mutate request.
186-
MutateExperimentArmsRequest mutateRequest = MutateExperimentArmsRequest.newBuilder()
187-
.setCustomerId(Long.toString(customerId))
188-
.addAllOperations(operations)
189-
// We want to fetch the draft campaign IDs from the treatment arm, so the easiest way to do
190-
// that is to have the response return the newly created entities.
191-
.setResponseContentType(ResponseContentType.MUTABLE_RESOURCE)
192-
.build();
194+
MutateExperimentArmsRequest mutateRequest =
195+
MutateExperimentArmsRequest.newBuilder()
196+
.setCustomerId(Long.toString(customerId))
197+
.addAllOperations(operations)
198+
// We want to fetch the draft campaign IDs from the treatment arm, so the easiest way
199+
// to do that is to have the response return the newly created entities.
200+
.setResponseContentType(ResponseContentType.MUTABLE_RESOURCE)
201+
.build();
193202

194203
// Sends the mutate request.
195204
MutateExperimentArmsResponse response =
@@ -200,22 +209,21 @@ private String createExperimentArms(
200209
// treatment arm, you can always filter the query in the next section with
201210
// `experiment_arm.control = false`.
202211
MutateExperimentArmResult controlArmResult = response.getResults(0);
203-
MutateExperimentArmResult treatmentArmResult = response.getResults(
204-
response.getResultsCount() - 1);
212+
MutateExperimentArmResult treatmentArmResult =
213+
response.getResults(response.getResultsCount() - 1);
205214

206-
System.out.printf("Created control arm with resource name '%s'%n",
207-
controlArmResult.getResourceName());
208-
System.out.printf("Created treatment arm with resource name '%s'%n",
209-
treatmentArmResult.getResourceName());
215+
System.out.printf(
216+
"Created control arm with resource name '%s'%n", controlArmResult.getResourceName());
217+
System.out.printf(
218+
"Created treatment arm with resource name '%s'%n", treatmentArmResult.getResourceName());
210219

211220
return treatmentArmResult.getExperimentArm().getInDesignCampaigns(0);
212221
}
213222
}
223+
214224
// [END create_experiment_2]
215225

216-
/**
217-
* Modifies the draft campaign.
218-
*/
226+
/** Modifies the draft campaign. */
219227
// [START create_experiment_4]
220228
private void modifyDraftCampaign(
221229
GoogleAdsClient googleAdsClient, long customerId, String draftCampaign) {

0 commit comments

Comments
 (0)