This is the legacy documentation for Google Ads scripts. Go to the current docs.

Video

Stay organized with collections Save and categorize content based on your preferences.

Retrieve all video campaigns

function getAllVideoCampaigns() {
  // AdsApp.videoCampaigns() will return all campaigns that are not
  // removed by default.
  var videoCampaigns = [];
  var videoCampaignIterator = AdsApp.videoCampaigns().get();
  Logger.log('Total campaigns found : ' +
      videoCampaignIterator.totalNumEntities());
  while (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    Logger.log(videoCampaign.getName());
    videoCampaigns.push(videoCampaign);
  }
  return videoCampaigns;
}

Retrieve a video campaign by its name

function getVideoCampaignByName() {
  var videoCampaignIterator = AdsApp.videoCampaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    Logger.log('Campaign Name: ' + videoCampaign.getName());
    Logger.log('Enabled: ' + videoCampaign.isEnabled());
    Logger.log('Bidding strategy: ' + videoCampaign.getBiddingStrategyType());
    Logger.log('Ad rotation: ' + videoCampaign.getAdRotationType());
    Logger.log('Start date: ' + formatDate(videoCampaign.getStartDate()));
    Logger.log('End date: ' + formatDate(videoCampaign.getEndDate()));
    return videoCampaign;
  }
  return null;
}

function formatDate(date) {
  function zeroPad(number) { return Utilities.formatString('%02d', number); }
  return (date == null) ? 'None' : zeroPad(date.year) + zeroPad(date.month) +
      zeroPad(date.day);
}

Retrieve a video campaign's stats

function getVideoCampaignStats() {
  var videoCampaignIterator = AdsApp.videoCampaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    // Fetch stats for the last month. See the DateRangeLiteral section at
    // https://developers.google.com/adwords/api/docs/guides/awql#formal_grammar
    // for a list of all supported pre-defined date ranges.
    // Note: Reports can also be used to fetch stats. See
    // https://developers.google.com/google-ads/scripts-legacy/docs/features/reports
    // for more information.
    var stats = videoCampaign.getStatsFor('LAST_MONTH');
    Logger.log(videoCampaign.getName() + ', ' + stats.getImpressions() +
        ' impressions, ' + stats.getViews() + ' views');
    return stats;
  }
  return null;
}

Pause a video campaign

function pauseVideoCampaign() {
  var videoCampaignIterator = AdsApp.videoCampaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    videoCampaign.pause();
  }
}

Add a video ad group

function addVideoAdGroup() {
  var videoCampaignIterator = AdsApp.videoCampaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    var videoAdGroupOperation = videoCampaign.newVideoAdGroupBuilder()
        .withName('INSERT_ADGROUP_NAME_HERE')
        // This can also be 'TRUE_VIEW_IN_DISPLAY'
        .withAdGroupType('TRUE_VIEW_IN_STREAM')
        .withCpv(1.2)
        .build();
  }
}

Update a video ad group

function updateAdGroup() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    videoAdGroup.bidding().setCpv(1.2);
    // update other properties as required here
  }
}

Retrieve all video ad groups

function getAllVideoAdGroups() {
  // AdsApp.videoAdGroups() will return all ad groups that are not removed by
  // default.
  var videoAdGroups = [];
  var videoAdGroupIterator = AdsApp.videoAdGroups().get();
  Logger.log('Total adGroups found : ' + videoAdGroupIterator.totalNumEntities());
  while (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    Logger.log('AdGroup Name: ' + videoAdGroup.getName() +
        ', AdGroup Type: ' + videoAdGroup.getAdGroupType());
    videoAdGroups.push(videoAdGroup);
  }
  return videoAdGroups;
}

Retrieve a video ad group by name

function getVideoAdGroupByName() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    Logger.log('AdGroup Name: ' + videoAdGroup.getName());
    Logger.log('AdGroup Type: ' + videoAdGroup.getAdGroupType());
    Logger.log('Enabled: ' + videoAdGroup.isEnabled());
    return videoAdGroup;
  }
  return null;
}

Retrieve a video ad group's stats

function getVideoAdGroupStats() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    // You can also request reports for pre-defined date ranges. See
    // https://developers.google.com/adwords/api/docs/guides/awql,
    // DateRangeLiteral section for possible values.
    var stats = videoAdGroup.getStatsFor('LAST_MONTH');
    Logger.log(videoAdGroup.getName() + ', ' + stats.getImpressions() + ', ' +
        stats.getViews());
    return stats;
  }
  return null;
}

Pause a video ad group

function pauseVideoAdGroup() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    videoAdGroup.pause();
    Logger.log('AdGroup with name: ' + videoAdGroup.getName() +
        ' has paused status: ' + videoAdGroup.isPaused());
  }
}

Retrieve any video for use in an ad

function getVideo() {
  // This will just get the first valid YouTube video in the account.
  // It demonstrates how to filter to see if a video is valid for video ads.
  var videos = AdsApp.adAssets().assets()
      .withCondition("Type = YOUTUBE_VIDEO")
      .get();
  var video = null;
  while (videos.hasNext()) {
    video = videos.next();
    // You have to use a YouTube video for True View ads, so only return if
    // the YouTubeVideoId exists.
    if(video.getYouTubeVideoId()) {
      return video;
    }
  }
  return null;
}

Retrieve a specific video for use in an ad

function getVideoByYouTubeId() {
  // You can filter on the YouTubeVideoId if you already have that video in
  // your account to fetch the exact one you want right away.
  var videos = AdsApp.adAssets().assets()
      .withCondition("Type = YOUTUBE_VIDEO AND YouTubeVideoId = 'ABCDEFGHIJK'")
      .get();
  if (videos.hasNext()) {
    return videos.next();
  }
  return null;
}

Add an in-stream video ad

function addInStreamVideoAd() {
  // If you have multiple adGroups with the same name, this snippet will
  // pick an arbitrary matching ad group each time. In such cases, just
  // filter on the campaign name as well:
  //
  // AdsApp.videoAdGroups()
  //     .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
  //     .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"')
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  var video = getVideo(); // Defined above
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    videoAdGroup.newVideoAd().inStreamAdBuilder()
        .withAdName("In Stream Ad")
        .withDisplayUrl("http://www.example.com")
        .withFinalUrl("http://www.example.com")
        .withVideo(video)
        .build();
  }
}

Add video discovery ad

function addVideoDiscoveryAd() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  var video = getVideo(); // Defined above
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    videoAdGroup.newVideoAd().videoDiscoveryAdBuilder()
        .withAdName("Video Discovery Ad")
        .withDescription1("Description line 1")
        .withDescription2("Description line 2")
        .withHeadline("Headline")
        .withThumbnail("THUMBNAIL1")
        .withDestinationPage("WATCH")
        .withVideo(video)
        .build();
  }
}

Pause video ads in video ad group

function pauseVideoAdsInVideoAdGroup() {
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    var videoAdsIterator = videoAdGroup.videoAds().get();
    while (videoAdsIterator.hasNext()) {
      var videoAd = videoAdsIterator.next();
      videoAd.pause();
    }
  }
}

Retrieve video ads in video ad group

function getInStreamAdsInVideoAdGroup() {
  var videoAds = [];
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    var videoAdsIterator = videoAdGroup.videoAds()
        .withCondition('Type="TRUE_VIEW_IN_STREAM_VIDEO_AD"').get();
    while (videoAdsIterator.hasNext()) {
      var videoAd = videoAdsIterator.next();
      logVideoAd(videoAd);
      videoAds.push(videoAd);
    }
  }
  return videoAds;
}

function logVideoAd(videoAd) {
  // Note that not all fields are populated for both video ad types.
  Logger.log('Video ID : ' + videoAd.getVideoId());
  Logger.log('Headline : ' + videoAd.getHeadline());
  Logger.log('Line1 : ' + videoAd.getDescription1());
  Logger.log('Line2 : ' + videoAd.getDescription2());
  Logger.log('Final URL : ' + videoAd.urls().getFinalUrl());
  Logger.log('Display URL : ' + videoAd.getDisplayUrl());
  Logger.log('Destination Page : ' + videoAd.getDestinationPage());
  Logger.log('Approval Status : ' + videoAd.getApprovalStatus());
  Logger.log('Enabled : ' + videoAd.isEnabled());
}

Retrieve ad stats from a video ad group

function getVideoAdGroupAdStats() {
  var statsList = [];
  var videoAdGroupIterator = AdsApp.videoAdGroups()
      .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
      .get();
  if (videoAdGroupIterator.hasNext()) {
    var videoAdGroup = videoAdGroupIterator.next();
    var videoAdsIterator = videoAdGroup.videoAds().get();
    while (videoAdsIterator.hasNext()) {
      var videoAd = videoAdsIterator.next();
      // You can also request reports for pre-defined date ranges. See
      // https://developers.google.com/adwords/api/docs/guides/awql,
      // DateRangeLiteral section for possible values.
      var stats = videoAd.getStatsFor('LAST_MONTH');
      Logger.log(adGroup.getName() + ', ' +
          stats.getViews() + ', ' + stats.getImpressions());
      statsList.push(stats);
    }
  }
  return statsList;
}

Add in-market audience to a video ad group

function addInMarketAudienceToVideoAdGroup() {
  var ag = AdsApp.videoAdGroups()
      .withCondition('CampaignStatus != REMOVED')
      .get()
      .next();

  Logger.log(
      'AdGroup ID %s Campaign ID %s', ag.getId().toString(),
      ag.getVideoCampaign().getId().toString());

  // Get the audience ID from the list here:
  // https://developers.google.com/adwords/api/docs/appendix/codes-formats#in-market-categories

  var audience = ag.videoTargeting()
      .newAudienceBuilder()
      .withAudienceId(80428)
      .withAudienceType('USER_INTEREST')
      .build();

  Logger.log('Added Audience ID %s', audience.getResult().getId().toString());

  var audiences = ag.videoTargeting().audiences().get();
  while (audiences.hasNext()) {
    var aud = audiences.next();
    Logger.log('Retrieved Audience ID %s', aud.getId().toString());
  }
}