use of com.google.api.ads.adwords.axis.v201809.cm.TemplateElement in project googleads-java-lib by googleads.
the class AddHtml5Ad method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group where the ad will be created.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws IOException if unable to get media data from the URL.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create the template ad.
TemplateAd html5Ad = new TemplateAd();
html5Ad.setName("Ad for HTML5");
html5Ad.setTemplateId(419L);
html5Ad.setFinalUrls(new String[] { "http://example.com/html5" });
html5Ad.setDisplayUrl("example.com/html5");
Dimensions dimensions = new Dimensions();
dimensions.setWidth(300);
dimensions.setHeight(250);
html5Ad.setDimensions(dimensions);
// The HTML5 zip file contains all the HTML, CSS, and images needed for the
// HTML5 ad. For help on creating an HTML5 zip file, check out Google Web
// Designer (https://www.google.com/webdesigner/).
byte[] html5Zip = com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl("https://goo.gl/9Y7qI2");
// Create a media bundle containing the zip file with all the HTML5 components.
// NOTE: You may also upload an HTML5 zip using MediaService.upload()
// and simply set the mediaId field below. See UploadMediaBundle.java for an example.
MediaBundle mediaBundle = new MediaBundle();
mediaBundle.setData(html5Zip);
mediaBundle.setEntryPoint("carousel/index.html");
mediaBundle.setType(MediaMediaType.MEDIA_BUNDLE);
// Create the template elements for the ad. You can refer to
// https://developers.google.com/adwords/api/docs/appendix/templateads
// for the list of available template fields.
TemplateElementField media = new TemplateElementField();
media.setName("Custom_layout");
media.setFieldMedia(mediaBundle);
media.setType(TemplateElementFieldType.MEDIA_BUNDLE);
TemplateElementField layout = new TemplateElementField();
layout.setName("layout");
layout.setFieldText("Custom");
layout.setType(TemplateElementFieldType.ENUM);
TemplateElement adData = new TemplateElement();
adData.setUniqueName("adData");
adData.setFields(new TemplateElementField[] { media, layout });
html5Ad.setTemplateElements(new TemplateElement[] { adData });
// Create the AdGroupAd.
AdGroupAd html5AdGroupAd = new AdGroupAd();
html5AdGroupAd.setAdGroupId(adGroupId);
html5AdGroupAd.setAd(html5Ad);
// Optional: Set the status.
html5AdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperator(Operator.ADD);
operation.setOperand(html5AdGroupAd);
// Create the ads.
AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { operation });
for (AdGroupAd adGroupAd : result.getValue()) {
System.out.printf("New HTML5 ad with ID %d and display url '%s' was created.%n", adGroupAd.getAd().getId(), adGroupAd.getAd().getDisplayUrl());
}
}
Aggregations