Search in sources :

Example 6 with Media

use of com.google.api.ads.adwords.axis.v201809.cm.Media 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());
    }
}
Also used : TemplateElementField(com.google.api.ads.adwords.axis.v201809.cm.TemplateElementField) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) TemplateAd(com.google.api.ads.adwords.axis.v201809.cm.TemplateAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) TemplateElement(com.google.api.ads.adwords.axis.v201809.cm.TemplateElement) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) Dimensions(com.google.api.ads.adwords.axis.v201809.cm.Dimensions) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) MediaBundle(com.google.api.ads.adwords.axis.v201809.cm.MediaBundle)

Example 7 with Media

use of com.google.api.ads.adwords.axis.v201809.cm.Media in project googleads-java-lib by googleads.

the class AddResponsiveDisplayAd method uploadImage.

/**
 * Uploads the image from the specified {@code url} via {@code MediaService}.
 *
 * @return the {@code mediaId} of the uploaded image.
 */
private static long uploadImage(MediaServiceInterface mediaService, String url) throws IOException {
    // Create image.
    Image image = new Image();
    image.setType(MediaMediaType.IMAGE);
    image.setData(com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl(url));
    // Upload image.
    Image uploadedImage = (Image) mediaService.upload(new Media[] { image })[0];
    return uploadedImage.getMediaId();
}
Also used : Image(com.google.api.ads.adwords.axis.v201809.cm.Image)

Example 8 with Media

use of com.google.api.ads.adwords.axis.v201809.cm.Media in project googleads-java-lib by googleads.

the class UploadMediaBundle method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @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) throws IOException {
    // Get the MediaService.
    MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
    // Create HTML5 media.
    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.
    MediaBundle mediaBundle = new MediaBundle();
    mediaBundle.setData(html5Zip);
    mediaBundle.setType(MediaMediaType.MEDIA_BUNDLE);
    // Upload HTML5 zip.
    mediaBundle = (MediaBundle) mediaService.upload(new Media[] { mediaBundle })[0];
    // Display HTML5 zip.
    Map<MediaSize, Dimensions> dimensions = Maps.toMap(mediaBundle.getDimensions());
    System.out.printf("HTML5 media with ID %d, dimensions '%dx%d', and MIME type '%s' " + "was uploaded.%n", mediaBundle.getMediaId(), dimensions.get(MediaSize.FULL).getWidth(), dimensions.get(MediaSize.FULL).getHeight(), mediaBundle.getMimeType());
}
Also used : MediaSize(com.google.api.ads.adwords.axis.v201809.cm.MediaSize) MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) Media(com.google.api.ads.adwords.axis.v201809.cm.Media) Dimensions(com.google.api.ads.adwords.axis.v201809.cm.Dimensions) MediaBundle(com.google.api.ads.adwords.axis.v201809.cm.MediaBundle)

Example 9 with Media

use of com.google.api.ads.adwords.axis.v201809.cm.Media in project googleads-java-lib by googleads.

the class AddGmailAd method uploadImage.

/**
 * Uploads an image.
 */
private static long uploadImage(AdWordsServicesInterface adWordsServices, AdWordsSession session, String url) throws IOException {
    MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
    // Create image.
    Image image = new Image();
    image.setData(com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl(url));
    image.setType(MediaMediaType.IMAGE);
    Media[] media = new Media[] { image };
    // Upload image.
    Media[] result = mediaService.upload(media);
    return result[0].getMediaId();
}
Also used : MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) Media(com.google.api.ads.adwords.axis.v201809.cm.Media) Image(com.google.api.ads.adwords.axis.v201809.cm.Image)

Example 10 with Media

use of com.google.api.ads.adwords.axis.v201809.cm.Media in project googleads-java-lib by googleads.

the class AddShoppingDynamicRemarketingCampaign method uploadImage.

/**
 * Uploads the image from the specified {@code url}.
 *
 * @return the {@code Image} that was uploaded.
 * @throws IOException if the image cannot be loaded.
 */
private static Image uploadImage(AdWordsServicesInterface services, AdWordsSession session, String url) throws IOException {
    Image image = new Image();
    image.setType(MediaMediaType.IMAGE);
    image.setData(com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl(url));
    MediaServiceInterface mediaService = services.get(session, MediaServiceInterface.class);
    return (Image) mediaService.upload(new Media[] { image })[0];
}
Also used : MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) Image(com.google.api.ads.adwords.axis.v201809.cm.Image)

Aggregations

Image (com.google.api.ads.adwords.axis.v201809.cm.Image)5 MediaServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface)5 Dimensions (com.google.api.ads.adwords.axis.v201809.cm.Dimensions)4 Media (com.google.api.ads.adwords.axis.v201809.cm.Media)4 MediaSize (com.google.api.ads.adwords.axis.v201809.cm.MediaSize)3 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)2 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)2 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)2 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)2 MediaBundle (com.google.api.ads.adwords.axis.v201809.cm.MediaBundle)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 Asset (com.google.api.ads.adwords.axis.v201809.cm.Asset)1 AssetOperation (com.google.api.ads.adwords.axis.v201809.cm.AssetOperation)1 AssetReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AssetReturnValue)1 AssetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AssetServiceInterface)1 CampaignCriterion (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion)1 CampaignCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation)1 CampaignCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue)1 CampaignCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)1 GmailAd (com.google.api.ads.adwords.axis.v201809.cm.GmailAd)1