Ad

Add a responsive search ad
Set a responsive search ad
Pause ads in an ad group
Get all ads
Get all ads by id
Get responsive search ad in an ad group

function addResponsiveSearchAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      ad: {
        adType: 'RESPONSIVE_SEARCH_AD',
        finalUrl: 'http://xxxxxx.co.jp',
        responsiveSearchAd: {
          headlines: [//3 or more and less than 15
            {
              text: 'First headline',
              pinnedField: "HEADLINE1",
              activeStatus: "ACTIVE",
            },
            {
              text: 'Second headline',
              pinnedField: "UNSPECIFIED",
              activeStatus: "ACTIVE",
            },
            {
              text: 'Third headline',
              pinnedField: "UNSPECIFIED",
              activeStatus: "ACTIVE",
            }
          ],
          descriptions: [//2 or more and less than 4
            {
              text: 'First description',
              pinnedField: "DESCRIPTION1",
              activeStatus: "ACTIVE",
            },
            {
              text: 'Second description',
              pinnedField: "UNSPECIFIED",
              activeStatus: "ACTIVE",
            }
          ],
        },
      },
      adGroupId: 1111111111,
      adName: 'adNameAAA',
      campaignId: 2222222,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Search.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
        + ', adType-> ' + adGroupAd.ad.adType + ' has been added.');
    } else {
      let errorList = adGroupAds.values[i].errors;
      errorList.forEach(function (element) {
        if (element.code == 'I0001' && element.details[0].requestKey.includes('campaignId')) {
          let text = 'campaignId-> ' + element.details[0].requestValue;
          if (element.details[1].requestKey.includes('adGroupId')) {
            text += ' adGroupId-> ' + element.details[1].requestValue;
          }
          Logger.log(text + ' does not exsit.');
        } else if (element.code == 'D0001' && element.details[0].requestKey.includes('adName')) {
          Logger.log('adName-> ' + element.details[0].requestValue + ' is duplicate.');
        } else {
          Logger.log('adName-> ' + adGroupAdArray[i].adName + ' could not be added.');
        }
      });
    }
  }
}

function setResponsiveSearchAd() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAdArray = [
    {
      ad: {
        adType: 'RESPONSIVE_SEARCH_AD',
        finalUrl: 'http://xxxxxx.co.jp',
        responsiveSearchAd: {
          headlines: [//3 or more and less than 15
            {
              text: 'New headline text1',
              pinnedField: "HEADLINE1",
            },
            {
              text: 'New headline text2',
              pinnedField: "UNSPECIFIED",
            },
            {
              text: 'New headline text3',
              pinnedField: "UNSPECIFIED",
            }
          ],
          descriptions: [//2 or more and less than 4
            {
              text: 'New description text1',
              pinnedField: "DESCRIPTION1",
            },
            {
              text: 'New description text2',
              pinnedField: "UNSPECIFIED",
            }
          ],
        },
      },
      adId: 3333333333,
      adGroupId: 1111111111,
      campaignId: 2222222,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroupAds = Search.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];//Empty when not specified
  const adGroupAdsGet = Search.AdGroupAdService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
    userStatuses: ['ACTIVE'],
  }).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;
    //Create new operand to set
    let adGroupAdSet = {
      accountId: accountId,
      campaignId: adGroupAd.campaignId,
      adGroupId: adGroupAd.adGroupId,
      adId: adGroupAd.adId,
      userStatus : 'PAUSED'
    };
    adGroupAdArray.push(adGroupAdSet);
  }
  const adGroupAdsSet = Search.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('adId-> ' + adGroupAd.adId 
        + ', adName-> ' + adGroupAd.adName
        + ', adGroupId-> ' + adGroupAd.adGroupId 
        + ', adGroupName-> ' + adGroupAd.adGroupName + ' has been paused.');
    } else {
      let adGroupAd = adGroupAdsGet.values[i].adGroupAd;
      Logger.log('adName-> ' + adGroupAd.adName 
        + ', adGroupName-> ' + adGroupAd.adGroupName + ' could not be stopped.');
    }
  }
}

Get all ads


function getAllAds(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupAds = Search.AdGroupAdService.get({
    accountId: accountId,
    numberResults: 10000,//Max ads you can get is 10000.
  }).rval;
  if (adGroupAds.totalNumEntries == 0) {
    Logger.log('Ad does not exist.');
    return;
  }
  for (let i = 0; i < adGroupAds.values.length; i++){
    let adGroupAd = adGroupAds.values[i].adGroupAd;
    Logger.log('adGroupId-> ' + adGroupAd.adGroupId 
      + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId 
      + ', adName-> ' + adGroupAd.adName);
  }
}

Get all ads by id


function getAllAdsById(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adIds = [1111111111, 2222222222];//Multiple selections are possible
  const adGroupAds = Search.AdGroupAdService.get({
    accountId: accountId,
    adIds: adIds,
  }).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('adGroupId-> ' + adGroupAd.adGroupId 
      + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId 
      + ', adName-> ' + adGroupAd.adName);
  }
}

function getResponsiveSearchAdInAdGroup(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222];//Multiple selections are possible
  const adGroupAds = Search.AdGroupAdService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
    adTypes: ['RESPONSIVE_SEARCH_AD'],
  }).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('adGroupId-> ' + adGroupAd.adGroupId  
      + ', adGroupName-> ' + adGroupAd.adGroupName
      + ', adId-> ' + adGroupAd.adId + ', adName-> ' 
      + adGroupAd.adName + ', finalUrl-> ' + adGroupAd.ad.finalUrl);
    let responsiveSearchAd = adGroupAd.ad.responsiveSearchAd;
    let headlines = [];
    let descriptions = [];
    for (let j = 0; j < responsiveSearchAd.headlines.length; j++){
      headlines.push('headline' + (j + 1) + '-> ' 
        + responsiveSearchAd.headlines[j].text);
    }
    Logger.log('***' + headlines.join(', '));
    for (let j = 0; j < responsiveSearchAd.descriptions.length; j++){
      descriptions.push('description' + (j + 1) + '-> ' 
        + responsiveSearchAd.descriptions[j].text);
    }
    Logger.log('***' + descriptions.join(', '));
  }
}