広告グループ

広告グループを新規追加する
広告グループを配信停止にする
広告グループの入札価格を変更する
アカウント配下の全ての広告グループ情報を取得
指定した全ての広告グループ情報を取得する

広告グループを新規追加する


function addAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupArray = [
    {
      accountId: accountId,
      adGroupName: 'adGroupAAA',
      campaignId: 11111111,
      device: ['DESKTOP'],
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      adGroupName: 'adGroupBBB',
      campaignId: 22222222,
      device: ['SMARTPHONE', 'TABLET'],
      deviceApp: ['APP'],
      deviceOs: ['NONE'],
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      adGroupName: 'adGroupCCC',
      campaignId: 33333333,
      device: ['NONE'],
      deviceApp: ['NONE'],
      deviceOs: ['NONE'],
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroups = Display.AdGroupService.add({
    accountId: accountId,
    operand: adGroupArray,
  }).rval;
  for (let i = 0; i < adGroups.values.length; i++){
    if (adGroups.values[i].operationSucceeded) {
      let adGroup = adGroups.values[i].adGroup;
      Logger.log('adGroupId-> ' + adGroup.adGroupId + ', adGroupName-> ' + adGroup.adGroupName
        + ' have been added as device-> ' + adGroup.device.join(', ') + ' to campaignId-> ' + adGroup.campaignId
        + ' campaignName-> ' + adGroup.campaignName);
    } else {
      let errorList = adGroups.values[i].errors;
      errorList.forEach(function (element) {
        if (element.code == 'I0001' && element.details[0].requestKey.includes('campaignId')) {
          Logger.log('campaignId-> ' + element.details[0].requestValue + ' does not exsit.');
        } else if (element.code == 'D0001' && element.details[0].requestKey.includes('adGroupName')) {
          Logger.log('adGroupName-> ' + element.details[0].requestValue + ' is duplicate.');
        } else {
          Logger.log('adGroupName-> ' + adGroupArray[i].adGroupName
            + ' could not to be added to campaignId-> ' + adGroupArray[i].campaignId);
        }
      });
    }
  }
}

広告グループを配信停止にする


function pauseAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222, 333333333];//Empty when not specified
  const adGroupsGet = Display.AdGroupService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
  }).rval;
  if (adGroupsGet.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  let adGroupsArray = [];
  for (let i = 0; i < adGroupsGet.values.length; i++) {
    let adGroup = adGroupsGet.values[i].adGroup;
    //Create new operand to set
    let setAdGroup = {
      accountId: accountId,
      adGroupId: adGroup.adGroupId,
      campaignId: adGroup.campaignId,
      userStatus: 'PAUSED'
    };
    adGroupsArray.push(setAdGroup);
  }
  const adGroupsSet = Display.AdGroupService.set({
    accountId: accountId,
    operand: adGroupsArray,
  }).rval;
  for (let i = 0; i < adGroupsSet.values.length; i++) {
    if (adGroupsSet.values[i].operationSucceeded) {
      let adGroup = adGroupsSet.values[i].adGroup;
      Logger.log('adGroupId-> ' + adGroup.adGroupId + ', adGroupName-> ' + adGroup.adGroupName + ' have paused.');
    } else {
      Logger.log('adGroupId-> ' + adGroupsArray[i].adGroupId + ' could not to be paused.');
    }
  }
}

広告グループの入札価格を変更する


function setAdGroupBid() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222, 333333333];//Empty when not specified
  const bid = 3000;
  const adGroupsGet = Display.AdGroupService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
  }).rval;
  if (adGroupsGet.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  let adGroupsArray = [];
  for (let i = 0; i < adGroupsGet.values.length; i++) {
    let adGroup = adGroupsGet.values[i].adGroup;
    let type = adGroup.biddingStrategyConfiguration.biddingScheme.campaignBiddingStrategyType;
    let bidScheme;
    switch (type) {
      case 'VCPM':
        bidScheme = {
          vcpmBiddingScheme: {
            vcpm: bid
          }
        };
        break;
      case 'CPC':
        bidScheme = {
          cpcBiddingScheme: {
            cpc: bid,
            enhancedCpcEnabled: 'FALSE'
          }
        };
        break;
      case 'CPV':
        bidScheme = {
          cpvBiddingScheme: {
            cpv: bid
          }
        };
        break;
      case 'MAXIMIZE_CONVERSIONS':
        bidScheme = {
          maximizeConversionsBiddingScheme: {
            targetCpa: bid
          }
        };
        break;
      default:
        continue;
    }
    bidScheme.campaignBiddingStrategyType = type;
    //Create new operand to set
    let setAdGroup = {
      accountId: accountId,
      campaignId: adGroup.campaignId,
      adGroupId: adGroup.adGroupId,
      biddingStrategyConfiguration: {
        biddingScheme: bidScheme
      }
    };
    adGroupsArray.push(setAdGroup);
  }
  if (adGroupsArray.length == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  const adGroupsSet = Display.AdGroupService.set({
    accountId: accountId,
    operand: adGroupsArray,
  }).rval;
  for (let i = 0; i < adGroupsSet.values.length; i++) {
    if (adGroupsSet.values[i].operationSucceeded) {
      let adGroup = adGroupsSet.values[i].adGroup;
      let type = adGroup.biddingStrategyConfiguration.biddingScheme.campaignBiddingStrategyType;
      let bidValue;
      switch (type) {
        case 'VCPM':
          bidValue = adGroup.biddingStrategyConfiguration.biddingScheme.vcpmBiddingScheme.vcpm;
          break;
        case 'CPC':
          bidValue = adGroup.biddingStrategyConfiguration.biddingScheme.cpcBiddingScheme.cpc;
          break;
        case 'CPV':
          bidValue = adGroup.biddingStrategyConfiguration.biddingScheme.cpvBiddingScheme.cpv;
          break;
        case 'MAXIMIZE_CONVERSIONS':
          bidValue = adGroup.biddingStrategyConfiguration.biddingScheme.maximizeConversionsBiddingScheme.targetCpa;
          break;
        default:
          break;
      }
      Logger.log('adGroupId-> ' + adGroup.adGroupId + ', adGroupName-> ' + adGroup.adGroupName
        + ' set as campaignBiddingStrategyType-> ' + type + ' to bid-> ' + bidValue);
    } else {
      Logger.log('adGroupId-> ' + adGroupsArray[i].adGroupId + ' could not to be set to bid-> ' + bid);
    }
  }
}

アカウント配下の全ての広告グループ情報を取得


function getAllAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroups = Display.AdGroupService.get({
    accountId: accountId,
  }).rval;
  if (adGroups.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  for (let i = 0; i < adGroups.values.length; i++){
    let adGroup = adGroups.values[i].adGroup;
    Logger.log('adGroupId-> ' + adGroup.adGroupId + ', adGroupName-> ' + adGroup.adGroupName
      + ', campaignId-> ' + adGroup.campaignId + ', campaignName-> ' + adGroup.campaignName);
  }
}

指定した全ての広告グループ情報を取得する


function getAllAdGroupById() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222, 333333333];//Multiple selections are possible
  const adGroups = Display.AdGroupService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
  }).rval;
  if (adGroups.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  for (let i = 0; i < adGroups.values.length; i++){
    let adGroup = adGroups.values[i].adGroup;
    Logger.log('adGroupId-> ' + adGroup.adGroupId + ', adGroupName-> ' + adGroup.adGroupName
      + ', campaignId-> ' + adGroup.campaignId + ', campaignName-> ' + adGroup.campaignName);
  }
}