Skip to content

Commit 6cf5b71

Browse files
authored
Add SetBidModifier example (#232)
* Add SetBidModifier example * Rename example and add criterion param * Add bidModifier param
1 parent 0a01694 commit 6cf5b71

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2020 Google LLC
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+
// https://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 com.google.ads.googleads.examples.campaignmanagement;
16+
17+
import com.beust.jcommander.Parameter;
18+
import com.google.ads.googleads.examples.utils.ArgumentNames;
19+
import com.google.ads.googleads.examples.utils.CodeSampleParams;
20+
import com.google.ads.googleads.lib.GoogleAdsClient;
21+
import com.google.ads.googleads.lib.utils.FieldMasks;
22+
import com.google.ads.googleads.v2.common.DeviceInfo;
23+
import com.google.ads.googleads.v2.enums.DeviceEnum.Device;
24+
import com.google.ads.googleads.v2.services.CampaignCriterionOperation;
25+
import com.google.ads.googleads.v2.errors.GoogleAdsError;
26+
import com.google.ads.googleads.v2.errors.GoogleAdsException;
27+
import com.google.ads.googleads.v2.resources.CampaignCriterion;
28+
import com.google.ads.googleads.v2.services.CampaignCriterionServiceClient;
29+
import com.google.ads.googleads.v2.services.MutateCampaignCriteriaResponse;
30+
import com.google.ads.googleads.v2.services.MutateCampaignCriterionResult;
31+
import com.google.ads.googleads.v2.utils.ResourceNames;
32+
import com.google.common.collect.ImmutableList;
33+
import com.google.protobuf.FloatValue;
34+
import java.io.FileNotFoundException;
35+
import java.io.IOException;
36+
37+
/** Updates a campaign criterion with a new bid modifier. */
38+
public class UpdateCampaignCriterionBidModifier {
39+
40+
private static class UpdateCampaignCriterionBidModifierParams extends CodeSampleParams {
41+
42+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
43+
private Long customerId;
44+
45+
@Parameter(names = ArgumentNames.CAMPAIGN_ID, required = true)
46+
private Long campaignId;
47+
48+
@Parameter(names = ArgumentNames.CRITERION_ID, required = true)
49+
private Long criterionId;
50+
51+
/** Specify the bid modifier value here or the default specified below will be used. */
52+
@Parameter(names = ArgumentNames.BID_MODIFIER)
53+
private Float bidModifier = 1.5f;
54+
}
55+
56+
public static void main(String[] args) throws IOException {
57+
UpdateCampaignCriterionBidModifierParams params =
58+
new UpdateCampaignCriterionBidModifierParams();
59+
if (!params.parseArguments(args)) {
60+
61+
// Either pass the required parameters for this example on the command line, or insert them
62+
// into the code here. See the parameter class definition above for descriptions.
63+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
64+
params.campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
65+
params.criterionId = Long.parseLong("INSERT_CRITERION_ID_HERE");
66+
// Optional: To use a different bid modifier value from the default (1.5f), uncomment
67+
// the line below and insert the desired bid modifier value.
68+
// params.bidModifier = Float.parseFloat("INSERT_BID_MODIFIER_VALUE_HERE");
69+
}
70+
71+
GoogleAdsClient googleAdsClient;
72+
try {
73+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
74+
} catch (FileNotFoundException fnfe) {
75+
System.err.printf(
76+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
77+
return;
78+
} catch (IOException ioe) {
79+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
80+
return;
81+
}
82+
83+
try {
84+
new UpdateCampaignCriterionBidModifier().runExample(
85+
googleAdsClient,
86+
params.customerId,
87+
params.campaignId,
88+
params.criterionId,
89+
params.bidModifier);
90+
} catch (GoogleAdsException gae) {
91+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
92+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
93+
// collection of GoogleAdsErrors that indicate the underlying causes of the
94+
// GoogleAdsException.
95+
System.err.printf(
96+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
97+
gae.getRequestId());
98+
int i = 0;
99+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
100+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
101+
}
102+
}
103+
}
104+
105+
/**
106+
* Runs the example.
107+
*
108+
* @param googleAdsClient the Google Ads API client.
109+
* @param customerId the client customer ID.
110+
* @param campaignId the campaign ID.
111+
* @param criterionId the ID of the criterion to be updated.
112+
* @param bidModifier the bid modifier for the campaign criterion.
113+
* @throws GoogleAdsException if an API request failed with one or more service errors.
114+
*/
115+
private void runExample(
116+
GoogleAdsClient googleAdsClient,
117+
long customerId,
118+
long campaignId,
119+
long criterionId,
120+
float bidModifier) {
121+
// Creates the criterion resource name.
122+
String criterionResourceName =
123+
ResourceNames.campaignCriterion(customerId, campaignId, criterionId);
124+
125+
// Creates the CampaignCriterion.
126+
CampaignCriterion campaignCriterion = CampaignCriterion.newBuilder()
127+
.setResourceName(criterionResourceName)
128+
.setBidModifier(FloatValue.of(bidModifier))
129+
.setDevice(DeviceInfo.newBuilder().setType(Device.MOBILE).build())
130+
.build();
131+
132+
// Creates the operation.
133+
CampaignCriterionOperation operation =
134+
CampaignCriterionOperation.newBuilder()
135+
.setUpdate(campaignCriterion)
136+
.setUpdateMask(FieldMasks.allSetFieldsOf(campaignCriterion))
137+
.build();
138+
139+
// Creates the CampaignCriterionServiceClient.
140+
try (CampaignCriterionServiceClient campaignCriterionServiceClient =
141+
googleAdsClient.getLatestVersion().createCampaignCriterionServiceClient()) {
142+
// Adds the CampaignCriterion.
143+
MutateCampaignCriteriaResponse response =
144+
campaignCriterionServiceClient.mutateCampaignCriteria(
145+
Long.toString(customerId), ImmutableList.of(operation));
146+
for (MutateCampaignCriterionResult result : response.getResultsList()) {
147+
System.out.printf("Campaign criterion with resource name '%s' was modified.%n",
148+
result.getResourceName());
149+
}
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)