Ad

Add a text ad
Set a text ad
Add an image ad
Set an image ad
Add a responsive display ad
Set a responsive display ad
Pause ads in an ad group
Get all ads
Get all ads by id
Get all ad in an ad group

function addTextAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'TEXT_AD',
        mainMediaFormat:'NONE',
        textAd: {
          description: 'descriptionAAA',
          headline: 'headlineAAA',
        },
        displayUrlLevel: 'DOMAIN',
        finalUrl: 'http://xxxxx.co.jp',
      },
      adGroupId: 111111111,
      adName: 'adNameAAA',
      campaignId: 22222222,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.add({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++) {
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName 
        + ' be added as headline-> ' + adGroupAd.ad.textAd.headline + ', description-> ' + adGroupAd.ad.textAd.description 
        + ' to adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName);
    } else {
      Logger.log('adName-> ' + adGroupAdArray[i].adName
        + ' could not to be added as headline-> ' + adGroupAdArray[i].ad.textAd.headline
        + ', description-> ' + adGroupAdArray[i].ad.textAd.description + ' to adGroupId-> ' + adGroupAdArray[i].adGroupId);
    }
  } 
}

function setTextAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'TEXT_AD',
        mainMediaFormat:'NONE',
        textAd: {
          description: 'New descriptionAAA',
          description2: 'New descriptionBBB',
          headline: 'New headlineAAA',
        },
        displayUrlLevel: 'FIRST_LEVEL',
        finalUrl: 'http://yyyyy.co.jp/aaaaa/bbbbb',
      },
      adId: 3333333333,
      adGroupId: 111111111,
      campaignId: 22222222,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.set({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++) {
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName + ', adType-> ' + adGroupAd.ad.adType + ' has been updated.');
    } else {
      Logger.log('adId-> ' + adGroupAdArray[i].adId + ' could not be updated.');
    }
  } 
}

function addImageAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'BANNER_AD',
        mainMediaFormat: "IMAGE",
        bannerAd: null,//Required even if null
        displayUrlLevel: 'DOMAIN',
        finalUrl: 'http://xxxxx.co.jp'
      },
      adGroupId: 111111111,
      adName: 'adNameAAA',
      campaignId: 22222222,
      mediaId: 3333333,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.add({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++){
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adName-> ' + adGroupAd.adName + ', adId-> ' + adGroupAd.adId + ', mediaId-> ' + adGroupAd.mediaId
        + ' be added to adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName);
    } else {
      Logger.log('adName-> ' + adGroupAdArray[i].adName 
        + ' could not to be added to adGroupId-> ' + adGroupAdArray[i].adGroupId);
    }
  } 
}

function setImageAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'BANNER_AD',
        mainMediaFormat: 'IMAGE',
        displayUrlLevel: 'FIRST_LEVEL',
        finalUrl: 'http://xxxxx.co.jp/aaaaa/bbbbb',
      },
      adId: 3333333333,
      adGroupId: 111111111,
      campaignId: 22222222,
      mediaId: 4444444,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.set({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++){
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName + ', adType-> ' + adGroupAd.ad.adType + ' has been updated.');
    } else {
      Logger.log('adId-> ' + adGroupAdArray[i].adId + ' could not be updated.');
    }
  } 
}

function addResponsiveDisplayAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'RESPONSIVE_AD',
        mainMediaFormat: 'IMAGE',
        responsiveAd: {
          buttonText: 'MORE_DETAIL',//https://ads-developers.yahoo.co.jp/ja/ads-script/product-guide/reference/enums/display.AdGroupAdServiceButtonText.html
          principal: 'principalAAA',
          asset: {
            mediaAssets: [
              { mediaId: 1111111 },
              { mediaId: 2222222 },
              { mediaId: 3333333 },
            ],
            headlineAssets: [
              { text: "headlineAsset1" },
              { text: "headlineAsset2" },
              { text: "headlineAsset3" },
            ],
            descriptionAssets: [
              { text: "descriptionAsset1" },
              { text: "descriptionAsset2" },
              { text: "descriptionAsset3" },
            ]
          }
        },
        displayUrlLevel: 'DOMAIN',
        finalUrl: 'http://xxxxx.co.jp'
      },
      campaignId: 11111111,
      adGroupId: 222222222,
      adName: 'adNameAAA',
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.add({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++) {
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adName-> ' + adGroupAd.adName + ', adId-> ' + adGroupAd.adId + ', mediaId-> ' + adGroupAd.mediaId
        + ' be added to adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName);
    } else {
      Logger.log('adName-> ' + adGroupAdArray[i].adName
        + ' could not to be added to adGroupId-> ' + adGroupAdArray[i].adGroupId);
    }
  }
}

function setResponsiveDisplayAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      accountId: accountId,
      ad: {
        adType: 'RESPONSIVE_AD',
        mainMediaFormat: 'IMAGE',
        responsiveAd: {
          buttonText: 'FOR_MORE_INFO',//https://ads-developers.yahoo.co.jp/ja/ads-script/product-guide/reference/enums/display.AdGroupAdServiceButtonText.html
          principal: 'principalBBB',
          asset: {
            mediaAssets: [
              { mediaId: 1111111 },
            ],
            headlineAssets: [
              { text: "New headlineAsset" },
              { text: "New headlineAsset2" },
            ],
            descriptionAssets: [
              { text: "New descriptionAsset" },
              { text: "New descriptionAsset2" },
            ]
          }
        },
        displayUrlLevel: 'FIRST_LEVEL',
        finalUrl: 'http://yyyyy.co.jp/aaaaaaa/bbbbbbb'
      },
      campaignId: 11111111,
      adGroupId: 222222222,
      adId: 3333333333,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.set({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++) {
    if (adGroupAds.values[i].operationSucceeded) {
      let adGroupAd = adGroupAds.values[i].adGroupAd;
      Logger.log('adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName + ', adType-> ' + adGroupAd.ad.adType + ' has been updated.');
    } else {
      Logger.log('adId-> ' + adGroupAdArray[i].adId + ' could not be updated.');
    }
  }
}

Pause ads in an ad group


function pauseAdsInAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222];//Multiple selections are possible
  const adGroupAdsGet = Display.AdGroupAdService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
  }).rval;
  if (adGroupAdsGet.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  let adGroupAdArray = [];
  for (let i = 0; i < adGroupAdsGet.values.length; i++){
    let adGroupAd = adGroupAdsGet.values[i].adGroupAd;
    let setAd = {
      accountId: accountId,
      campaignId: adGroupAd.campaignId,
      adGroupId: adGroupAd.adGroupId,
      adId: adGroupAd.adId,
      userStatus: 'PAUSED'
    };
    adGroupAdArray.push(setAd);
  }
  const adGroupAdsSet = Display.AdGroupAdService.set({
    accountId: accountId,
    operand: adGroupAdArray,
  }).rval;
  for (let i = 0; i < adGroupAdsSet.values.length; i++){
    if (adGroupAdsSet.values[i].operationSucceeded) {
      let adGroupAd = adGroupAdsSet.values[i].adGroupAd;
      Logger.log('adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName
        + ', adName-> ' + adGroupAd.adName + ', adId-> ' + adGroupAd.adId + ' have been stopped.');
    } else {
      let adGroupAd = adGroupAdsGet.values[i].adGroupAd;
      Logger.log('adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName
        + ', adName-> ' + adGroupAd.adName + ', adId-> ' + adGroupAd.adId + ' could not to be stopped.');
    }
  }
}

Get all ads


function getAllAds() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAds = Display.AdGroupAdService.get({
    accountId: accountId,
    numberResults: 10000,
  }).rval;
  if (adGroupAds.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  for (let i = 0; i < adGroupAds.values.length; i++){
    let adGroupAd = adGroupAds.values[i].adGroupAd;
    Logger.log('adType-> ' + adGroupAd.ad.adType + ', adStyle-> ' + adGroupAd.adStyle
      + ', adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName
      + ', mediaId-> ' + adGroupAd.mediaId);
  }
}

Get all ads by id


function getAllAdsById() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adIds = [1111111111, 2222222222];//Multiple selections are possible
  const adGroupAds = Display.AdGroupAdService.get({
    accountId: accountId,
    adIds: adIds,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++){
    let adGroupAd = adGroupAds.values[i].adGroupAd;
    Logger.log('adType-> ' + adGroupAd.ad.adType + ', adStyle-> ' + adGroupAd.adStyle
      + ', adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName
      + ', mediaId-> ' + adGroupAd.mediaId);
  }
}

Get all ad in an ad group


function getAdInAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222, 333333333];//Empty when not specified
  const adGroupAds = Display.AdGroupAdService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
    numberResults: 10000,
  }).rval;
  for (let i = 0; i < adGroupAds.values.length; i++) {
    let adGroupAd = adGroupAds.values[i].adGroupAd;
    let logText = 'adType-> ' + adGroupAd.ad.adType + 'mainMediaFormat-> ' + adGroupAd.ad.mainMediaFormat
      + ', adGroupId-> ' + adGroupAd.adGroupId + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId + ', adName-> ' + adGroupAd.adName;
    switch (adGroupAd.ad.adType) {
      case 'TEXT_AD':
      case 'DYNAMIC_DISPLAY_AD':
        //mediaIdなし
        break;
      case 'RESPONSIVE_AD':
        if (adGroupAd.ad.mainMediaFormat == 'VIDEO') {
          logText += ', mediaId-> ' + adGroupAd.mediaId;//動画の場合は入る
        }
        break;
      case 'BANNER_AD':
      case 'CAROUSEL_AD':
        logText += ', mediaId-> ' + adGroupAd.mediaId;
        break;
    }
    Logger.log(logText);
  }
}