Video

Retrieve all video campaigns

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

Retrieve a video campaign by its name

function getVideoCampaignByName() {
  var videoCampaignIterator = AdWordsApp.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()));
  }
}
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 = AdWordsApp.videoCampaigns()
      .withCondition('Name = "INSERT_CAMPAIGN_NAME_HERE"')
      .get();
  if (videoCampaignIterator.hasNext()) {
    var videoCampaign = videoCampaignIterator.next();
    // You can also request reports for pre-defined date ranges. See
    // https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_campaign#getStatsFor_1,
    // DateRangeLiteral section for possible values.
    var stats = videoCampaign.getStatsFor('LAST_MONTH');
    Logger.log(videoCampaign.getName() + ', ' + stats.getImpressions() +
        ' impressions, ' + stats.getViews() + ' views');
  }
}

Pause a video campaign

function pauseVideoCampaign() {
  var videoCampaignIterator = AdWordsApp.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 = AdWordsApp.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 = AdWordsApp.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() {
  // AdWordsApp.videoAdGroups() will return all ad groups that are not removed by
  // default.
  var videoAdGroupIterator = AdWordsApp.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());
  }
}

Retrieve a video ad group by name

function getVideoAdGroupByName() {
  var videoAdGroupIterator = AdWordsApp.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());
  }
}

Retrieve a video ad group's stats

function getVideoAdGroupStats() {
  var videoAdGroupIterator = AdWordsApp.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());
  }
}

Pause a video ad group

function pauseVideoAdGroup() {
  var videoAdGroupIterator = AdWordsApp.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 = AdWordsApp.adMedia().media()
      .withCondition("Type = 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 = AdWordsApp.adMedia().media()
      .withCondition("Type = 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:
  //
  // AdWordsApp.videoAdGroups()
  //     .withCondition('Name = "INSERT_ADGROUP_NAME_HERE"')
  //     .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"')
  var videoAdGroupIterator = AdWordsApp.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 = AdWordsApp.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 = AdWordsApp.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 videoAdGroupIterator = AdWordsApp.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);
    }
  }
}

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 a video ad's stats

function getVideoAdStats() {
  var videoAdGroupIterator = AdWordsApp.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());
    }
  }
}

发送以下问题的反馈:

此网页
AdWords Scripts
AdWords Scripts
需要帮助?请访问我们的支持页面