Skip to content

Commit d49f190

Browse files
authored
Add AddDisplayUploadAd example (#302)
* Add AddDisplayUploadAd example
1 parent 49d35b3 commit d49f190

1 file changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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.advancedoperations;
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.v3.common.AdMediaBundleAsset;
22+
import com.google.ads.googleads.v3.common.DisplayUploadAdInfo;
23+
import com.google.ads.googleads.v3.common.MediaBundleAsset;
24+
import com.google.ads.googleads.v3.enums.AdGroupAdStatusEnum.AdGroupAdStatus;
25+
import com.google.ads.googleads.v3.enums.AssetTypeEnum.AssetType;
26+
import com.google.ads.googleads.v3.enums.DisplayUploadProductTypeEnum.DisplayUploadProductType;
27+
import com.google.ads.googleads.v3.errors.GoogleAdsError;
28+
import com.google.ads.googleads.v3.errors.GoogleAdsException;
29+
import com.google.ads.googleads.v3.resources.Ad;
30+
import com.google.ads.googleads.v3.resources.AdGroupAd;
31+
import com.google.ads.googleads.v3.resources.Asset;
32+
import com.google.ads.googleads.v3.services.AdGroupAdOperation;
33+
import com.google.ads.googleads.v3.services.AdGroupAdServiceClient;
34+
import com.google.ads.googleads.v3.services.AssetOperation;
35+
import com.google.ads.googleads.v3.services.AssetServiceClient;
36+
import com.google.ads.googleads.v3.services.MutateAdGroupAdsResponse;
37+
import com.google.ads.googleads.v3.services.MutateAssetsResponse;
38+
import com.google.ads.googleads.v3.utils.ResourceNames;
39+
import com.google.common.collect.ImmutableList;
40+
import com.google.common.io.ByteStreams;
41+
import com.google.protobuf.ByteString;
42+
import com.google.protobuf.BytesValue;
43+
import com.google.protobuf.StringValue;
44+
import java.io.FileNotFoundException;
45+
import java.io.IOException;
46+
import java.net.URL;
47+
48+
/** Adds a display upload ad to a given ad group. To get ad groups, run GetAdGroups.java. */
49+
public class AddDisplayUploadAd {
50+
51+
private static final String BUNDLE_URL = "https://goo.gl/9Y7qI2";
52+
53+
private static class AddDisplayUploadAdParams extends CodeSampleParams {
54+
55+
@Parameter(names = ArgumentNames.CUSTOMER_ID, required = true)
56+
private Long customerId;
57+
58+
@Parameter(names = ArgumentNames.AD_GROUP_ID, required = true)
59+
private Long adGroupId;
60+
}
61+
62+
public static void main(String[] args) throws IOException {
63+
AddDisplayUploadAdParams params = new AddDisplayUploadAdParams();
64+
if (!params.parseArguments(args)) {
65+
66+
// Either pass the required parameters for this example on the command line, or insert them
67+
// into the code here. See the parameter class definition above for descriptions.
68+
params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE");
69+
params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
70+
}
71+
72+
GoogleAdsClient googleAdsClient;
73+
try {
74+
googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build();
75+
} catch (FileNotFoundException fnfe) {
76+
System.err.printf(
77+
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe);
78+
return;
79+
} catch (IOException ioe) {
80+
System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe);
81+
return;
82+
}
83+
84+
try {
85+
new AddDisplayUploadAd().runExample(googleAdsClient, params.customerId, params.adGroupId);
86+
} catch (GoogleAdsException gae) {
87+
// GoogleAdsException is the base class for most exceptions thrown by an API request.
88+
// Instances of this exception have a message and a GoogleAdsFailure that contains a
89+
// collection of GoogleAdsErrors that indicate the underlying causes of the
90+
// GoogleAdsException.
91+
System.err.printf(
92+
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
93+
gae.getRequestId());
94+
int i = 0;
95+
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
96+
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
97+
}
98+
}
99+
}
100+
101+
/**
102+
* Runs the example.
103+
*
104+
* @param googleAdsClient the Google Ads API client.
105+
* @param customerId the client customer ID.
106+
* @param adGroupId the ad group ID.
107+
* @throws GoogleAdsException if an API request failed with one or more service errors.
108+
*/
109+
private void runExample(GoogleAdsClient googleAdsClient, long customerId, long adGroupId)
110+
throws IOException {
111+
// There are several types of display upload ads. For this example, we will create
112+
// an HTML5 upload ad, which requires a media bundle.
113+
// The DisplayUploadProductType field lists the available display upload types:
114+
// https://developers.google.com/google-ads/api/reference/rpc/v3/DisplayUploadAdInfo
115+
116+
// Creates a new media bundle asset and returns the resource name.
117+
String adAssetResourceName = createMediaBundleAsset(googleAdsClient, customerId);
118+
119+
// Creates a new display upload ad and associates it with the specified ad group.
120+
createDisplayUploadAdGroupAd(googleAdsClient, customerId, adGroupId, adAssetResourceName);
121+
}
122+
123+
/**
124+
* Creates a media bundle from the assets in a zip file. The zip file contains the HTML5
125+
* components.
126+
*
127+
* @param googleAdsClient the Google Ads API client.
128+
* @param customerId the client customer ID.
129+
* @return the resource name of the newly created media bundle.
130+
* @throws IOException if there is an error reading the media bundle.
131+
*/
132+
private String createMediaBundleAsset(GoogleAdsClient googleAdsClient, long customerId)
133+
throws IOException {
134+
// The HTML5 zip file contains all the HTML, CSS, and images needed for the
135+
// HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
136+
// Designer (https://www.google.com/webdesigner/).
137+
byte[] html5Zip = ByteStreams.toByteArray(new URL(BUNDLE_URL).openStream());
138+
139+
// Creates the media bundle asset.
140+
Asset asset =
141+
Asset.newBuilder()
142+
.setType(AssetType.MEDIA_BUNDLE)
143+
.setMediaBundleAsset(
144+
MediaBundleAsset.newBuilder()
145+
.setData(BytesValue.of(ByteString.copyFrom(html5Zip)))
146+
.build())
147+
.build();
148+
149+
// Creates the asset operation.
150+
AssetOperation operation = AssetOperation.newBuilder().setCreate(asset).build();
151+
152+
// Gets the AssetService.
153+
try (AssetServiceClient assetServiceClient =
154+
googleAdsClient.getLatestVersion().createAssetServiceClient()) {
155+
// Adds the asset to the client account.
156+
MutateAssetsResponse response =
157+
assetServiceClient.mutateAssets(Long.toString(customerId), ImmutableList.of(operation));
158+
// Displays and returns the resulting resource name.
159+
String uploadedAssetResourceName = response.getResults(0).getResourceName();
160+
System.out.printf(
161+
"Uploaded media bundle with resource name: '%s'.%n", uploadedAssetResourceName);
162+
return uploadedAssetResourceName;
163+
}
164+
}
165+
166+
/**
167+
* Creates a new HTML5 display upload ad and adds it to the specified ad group.
168+
*
169+
* @param googleAdsClient the Google Ads API client.
170+
* @param customerId the client customer ID.
171+
* @param adGroupId the ad group ID.
172+
* @param adAssetResourceName The ID of the ad group to which the new ad will be added.
173+
*/
174+
private void createDisplayUploadAdGroupAd(
175+
GoogleAdsClient googleAdsClient,
176+
long customerId,
177+
long adGroupId,
178+
String adAssetResourceName) {
179+
// Creates the ad with the required fields.
180+
Ad displayUploadAd =
181+
Ad.newBuilder()
182+
.setName(StringValue.of("Ad for HTML5"))
183+
.addFinalUrls(StringValue.of("http://example.com/html5"))
184+
// Exactly one ad data field must be included to specify the ad type. See
185+
// https://developers.google.com/google-ads/api/reference/rpc/v3/Ad for the full
186+
// list of available types.
187+
.setDisplayUploadAd(
188+
DisplayUploadAdInfo.newBuilder()
189+
.setDisplayUploadProductType(DisplayUploadProductType.HTML5_UPLOAD_AD)
190+
.setMediaBundle(
191+
AdMediaBundleAsset.newBuilder()
192+
.setAsset(StringValue.of(adAssetResourceName))
193+
.build())
194+
.build())
195+
.build();
196+
197+
// Creates an ad group ad for the new ad.
198+
AdGroupAd adGroupAd =
199+
AdGroupAd.newBuilder()
200+
.setAd(displayUploadAd)
201+
.setStatus(AdGroupAdStatus.PAUSED)
202+
.setAdGroup(StringValue.of(ResourceNames.adGroup(customerId, adGroupId)))
203+
.build();
204+
205+
// Creates the ad group ad operation.
206+
AdGroupAdOperation operation = AdGroupAdOperation.newBuilder().setCreate(adGroupAd).build();
207+
208+
// Creates the ad group ad service client.
209+
try (AdGroupAdServiceClient adGroupAdServiceClient =
210+
googleAdsClient.getLatestVersion().createAdGroupAdServiceClient()) {
211+
// Adds the ad group ad to the client account.
212+
MutateAdGroupAdsResponse response =
213+
adGroupAdServiceClient.mutateAdGroupAds(
214+
Long.toString(customerId), ImmutableList.of(operation));
215+
216+
// Displays the resulting ad group ad's resource name.
217+
System.out.printf(
218+
"Created new ad group ad with resource name: '%s'.%n",
219+
response.getResults(0).getResourceName());
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)