広告グループ

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

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


function addAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupArray = [
    {
      adGroupName: 'adGroupNameAAA',
      biddingStrategyConfiguration: {
        biddingScheme: {
          campaignBiddingStrategyType: 'CPC',
          cpcBiddingScheme: {
            cpc: 1000
          },
        },
      },
      campaignId: 1111111,
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const adGroups = Search.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
        + ', bidMaxCpc-> ' + adGroup.biddingStrategyConfiguration.biddingScheme.cpcBiddingScheme.cpc + ' has been added.');
    } 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 be stopped.');
        }
      });
    }
  }
}

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


function pauseAdGroup() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222];//Empty when not specified
  const adGroupsGet = Search.AdGroupService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
    userStatuses: ['ACTIVE'],
  }).rval;
  if (adGroupsGet.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  let adGroupArray = [];
  for (let i = 0; i < adGroupsGet.values.length; i++) {
    let adGroup = adGroupsGet.values[i].adGroup;
    //New object to set
    const setAdGroup = {
      campaignId: adGroup.campaignId,
      adGroupId: adGroup.adGroupId,
      userStatus: 'PAUSED',
    }
    adGroupArray.push(setAdGroup);
  }
  const adGroupsSet = Search.AdGroupService.set({
    accountId: accountId,
    operand: adGroupArray,
  }).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 + ' has been paused.');
    } else {
      Logger.log('adGroupId-> ' + adGroupIds[i] + ' could not be stopped.');
    }
  }
}

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


function setAdGroupBid(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222];//Empty when not specified
  const bidMaxCpc = 500;
  const adGroupsGet = Search.AdGroupService.get({
    accountId: accountId,
    adGroupIds: adGroupIds,
  }).rval;
  if (adGroupsGet.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  let adGroupArray = [];
  for (let i = 0; i < adGroupsGet.values.length; i++) {
    let adGroup = adGroupsGet.values[i].adGroup;
    // create new object to set
    let setAdGroup = {
      accountId: accountId,
      campaignId: adGroup.campaignId,
      adGroupId: adGroup.adGroupId,
      biddingStrategyConfiguration: {
        biddingScheme: {
          campaignBiddingStrategyType: 'CPC',
          cpcBiddingScheme: {
            cpc: bidMaxCpc
          },
        },
      },
    };
    adGroupArray.push(setAdGroup);
  }
  const adGroupsSet = Search.AdGroupService.set({
    accountId: accountId,
    operand: adGroupArray,
  }).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
        + ' has been changed to budgetAmount-> ' + adGroup.biddingStrategyConfiguration.biddingScheme.cpcBiddingScheme.cpc);
    } else {
      Logger.log('adGroupName-> ' + adGroupsGet.values[i].adGroup.adGroupName 
        + ' budget could not be changed.');
    }
  }
}

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


function getAllAdGroup(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroups = Search.AdGroupService.get({
    accountId: accountId,
    numberResults: 10000,//Max adGroups you can get is 10000.
  }).rval;
  if (adGroups.totalNumEntries == 0) {
    Logger.log('Ad group 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);
  }
}

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


function getAllAdGroupById(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const adGroupIds = [111111111, 222222222];//Multiple selections are possible
  const adGroups = Search.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);
  }
}