Search in sources :

Example 1 with MediaServiceInterface

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

the class GetAllImagesAndVideos 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.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException {
    // Get the MediaService.
    MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
    int offset = 0;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(MediaField.MediaId, MediaField.Width, MediaField.Height, MediaField.MimeType).orderAscBy(MediaField.MediaId).offset(offset).limit(PAGE_SIZE).in(MediaField.Type, "IMAGE", "VIDEO").build();
    MediaPage page = null;
    do {
        // Get all images.
        page = mediaService.get(selector);
        // Display images.
        if (page != null && page.getEntries() != null) {
            for (Media media : page.getEntries()) {
                Map<MediaSize, Dimensions> dimensions = Maps.toMap(media.getDimensions());
                System.out.printf("Media with ID %d, dimensions %s, and MIME type '%s' was found.%n", media.getMediaId(), toString(dimensions.get(MediaSize.FULL)), media.getMediaType());
            }
        } else {
            System.out.println("No images/videos were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());
}
Also used : MediaSize(com.google.api.ads.adwords.axis.v201809.cm.MediaSize) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) MediaPage(com.google.api.ads.adwords.axis.v201809.cm.MediaPage) 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) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 2 with MediaServiceInterface

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

the class UploadImage 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 image.
    Image image = new Image();
    image.setData(com.google.api.ads.common.lib.utils.Media.getMediaDataFromUrl("https://goo.gl/3b9Wfh"));
    image.setType(MediaMediaType.IMAGE);
    Media[] media = new Media[] { image };
    // Upload image.
    Media[] result = mediaService.upload(media);
    // Display images.
    image = (Image) result[0];
    Map<MediaSize, Dimensions> dimensions = Maps.toMap(image.getDimensions());
    System.out.printf("Image with ID %d, dimensions %dx%d, and MIME type '%s' was " + "uploaded.%n", image.getMediaId(), dimensions.get(MediaSize.FULL).getWidth(), dimensions.get(MediaSize.FULL).getHeight(), image.getMediaType());
}
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) Image(com.google.api.ads.adwords.axis.v201809.cm.Image)

Example 3 with MediaServiceInterface

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

the class AddResponsiveDisplayAd method createDynamicDisplayAdSettings.

private static DynamicSettings createDynamicDisplayAdSettings(MediaServiceInterface mediaService) throws IOException {
    long logoImageMediaId = uploadImage(mediaService, "https://goo.gl/dEvQeF");
    Image logo = new Image();
    logo.setMediaId(logoImageMediaId);
    DynamicSettings dynamicSettings = new DynamicSettings();
    dynamicSettings.setLandscapeLogoImage(logo);
    dynamicSettings.setPricePrefix("as low as");
    dynamicSettings.setPromoText("Free shipping!");
    return dynamicSettings;
}
Also used : Image(com.google.api.ads.adwords.axis.v201809.cm.Image) DynamicSettings(com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings)

Example 4 with MediaServiceInterface

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

the class AddResponsiveDisplayAd 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 uploading an image failed.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
    // Get the MediaService.
    MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create a responsive display ad.
    ResponsiveDisplayAd responsiveDisplayAd = new ResponsiveDisplayAd();
    // This ad format does not allow the creation of an image using the
    // Image.data field. An image must first be created using the MediaService,
    // and Image.mediaId must be populated when creating the ad.
    long marketingImageMediaId = uploadImage(mediaService, "https://goo.gl/3b9Wfh");
    Image marketingImage = new Image();
    marketingImage.setMediaId(marketingImageMediaId);
    responsiveDisplayAd.setMarketingImage(marketingImage);
    responsiveDisplayAd.setShortHeadline("Travel");
    responsiveDisplayAd.setLongHeadline("Travel the World");
    responsiveDisplayAd.setDescription("Take to the air!");
    responsiveDisplayAd.setBusinessName("Interplanetary Cruises");
    responsiveDisplayAd.setFinalUrls(new String[] { "http://www.example.com/" });
    // Optional: Create a square marketing image using MediaService, and set it
    // to the ad.
    long squareMarketingImageMediaId = uploadImage(mediaService, "https://goo.gl/mtt54n");
    Image squareMarketingImage = new Image();
    squareMarketingImage.setMediaId(squareMarketingImageMediaId);
    responsiveDisplayAd.setSquareMarketingImage(squareMarketingImage);
    // Optional: set call to action text.
    responsiveDisplayAd.setCallToActionText("Shop Now");
    // Optional: Set dynamic display ad settings, composed of landscape logo
    // image, promotion text, and price prefix.
    DynamicSettings dynamicDisplayAdSettings = createDynamicDisplayAdSettings(mediaService);
    responsiveDisplayAd.setDynamicDisplayAdSettings(dynamicDisplayAdSettings);
    // Whitelisted accounts only: Set color settings using hexadecimal values.
    // Set allowFlexibleColor to false if you want your ads to render by always
    // using your colors strictly.
    /*
    responsiveDisplayAd.setMainColor("#0000ff");
    responsiveDisplayAd.setAccentColor("#ffff00");
    responsiveDisplayAd.setAllowFlexibleColor(false);
    */
    // Whitelisted accounts only: Set the format setting that the ad will be
    // served in.
    /*
    responsiveDisplayAd.setFormatSetting(
        com.google.api.ads.adwords.axis.v201809.cm.DisplayAdFormatSetting.NON_NATIVE);
    */
    // Create ad group ad for the responsive display ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(responsiveDisplayAd);
    // Optional: set the status.
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create the operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
    // Display ads.
    Arrays.stream(result.getValue()).map(adGroupAdResult -> (ResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Responsive display ad with ID %d and short headline '%s' was added.%n", newAd.getId(), newAd.getShortHeadline()));
}
Also used : ResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveDisplayAd) Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) ResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveDisplayAd) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdStatus(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) MediaMediaType(com.google.api.ads.adwords.axis.v201809.cm.MediaMediaType) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) IOException(java.io.IOException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) Media(com.google.api.ads.adwords.axis.v201809.cm.Media) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) DynamicSettings(com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) Image(com.google.api.ads.adwords.axis.v201809.cm.Image) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) Image(com.google.api.ads.adwords.axis.v201809.cm.Image) DynamicSettings(com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings)

Example 5 with MediaServiceInterface

use of com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface 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)

Aggregations

Image (com.google.api.ads.adwords.axis.v201809.cm.Image)6 MediaServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface)6 Media (com.google.api.ads.adwords.axis.v201809.cm.Media)5 Dimensions (com.google.api.ads.adwords.axis.v201809.cm.Dimensions)3 MediaSize (com.google.api.ads.adwords.axis.v201809.cm.MediaSize)3 DynamicSettings (com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings)2 Parameter (com.beust.jcommander.Parameter)1 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)1 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)1 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)1 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)1 AdGroupAdStatus (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 MediaBundle (com.google.api.ads.adwords.axis.v201809.cm.MediaBundle)1 MediaMediaType (com.google.api.ads.adwords.axis.v201809.cm.MediaMediaType)1 MediaPage (com.google.api.ads.adwords.axis.v201809.cm.MediaPage)1 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)1