Ad Media

Some ad types, such as ImageAd and ResponsiveDisplayAd, contain images and other media elements. This guide describes how to upload and query media using AdWords scripts.

Uploading images

Images can be uploaded using the ImageBuilder class, which takes a name and image data. The data is provided as a Blob data interchange object, which can be created by services such as the Drive service and URL fetch service.

The following snippet shows how to upload an image from an external URL:

var imageUrl = "http://www.example.com/example.png";
var imageBlob = UrlFetchApp.fetch(imageUrl).getBlob();
var mediaOperation = AdWordsApp.adMedia().newImageBuilder()
    .withName("IMAGE_NAME")
    .withData(imageBlob)
    .build();

Alternatively, the image blob may be loaded from Google Drive:

var imageFileId = "IMAGE_FILE_ID";
var imageBlob = DriveApp.getFileById(imageFileId).getBlob();
var mediaOperation = AdWordsApp.adMedia().newImageBuilder()
    .withName("IMAGE_NAME")
    .withData(imageBlob)
    .build();

Uploading media bundles

Media bundles are ZIP archives that contain HTML5 assets, which can be used to create HTML5 ads. For more information on the file structure, see the format documentation. Upload media bundles using the MediaBundleBuilder class, which takes a name and file data. As with images, the data is provided as a Blob data interchange object.

The following snippet shows how to upload a media bundle from an external URL:

var mediaBundleUrl = "http://www.example.com/example.zip";
var mediaBundleBlob = UrlFetchApp.fetch(mediaBundleUrl).getBlob();
var mediaOperation = AdWordsApp.adMedia().newMediaBundleBuilder()
    .withName("bundle name")
    .withData(mediaBundleBlob)
    .build();

Querying media

Media of every type can be queried in AdWords scripts using a MediaSelector. Use the withCondition() predicate to filter media by name, type, and other fields. For example, the following snippet finds all images on an account:

var mediaIterator = AdWordsApp.adMedia().Media()
    .withCondition("Type = IMAGE")
    .get();
while (mediaIterator.hasNext()) {
  var image = adIterator.next();
}

Creating ads with media

See our code examples page for complete examples of creating supported ads with attached media.

Send feedback about...

AdWords Scripts
AdWords Scripts
Need help? Visit our support page.