キャンペーン

全てのキャンペーン情報を取得する
指定した全てのキャンペーン情報を取得する
キャンペーンを新規追加する
キャンペーンを配信停止にする
キャンペーン予算を更新する

全てのキャンペーン情報を取得する


function getAllCampaigns() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaigns = Search.CampaignService.get({
    accountId: accountId,
    numberResults: 10000,//Max campaigns you can get is 10000.
  }).rval;
  if (campaigns.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  for (let i = 0; i < campaigns.values.length; i++){   
    let campaign = campaigns.values[i].campaign;
    Logger.log('campaignId-> ' + campaign.campaignId + ', campaignName-> ' + campaign.campaignName);
  }
}

指定した全てのキャンペーン情報を取得する


function getCampaignById() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignIds = [1111111, 2222222];//Multiple selections are possible
  const campaigns = Search.CampaignService.get({
    accountId: accountId,
    campaignIds: campaignIds,
  }).rval;
  if (campaigns.totalNumEntries == 0) {
    Logger.log('Target ID does not exist.');
    return;
  }
  for (let i = 0; i < campaigns.values.length; i++){   
    let campaign = campaigns.values[i].campaign;
    Logger.log('campaignId-> ' + campaign.campaignId + ', campaignName-> ' + campaign.campaignName);
  }
}

キャンペーンを新規追加する


function addCampaign() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignArray = [
    {
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'CPC',
        },
      },
      budget: {
        amount: 1000,
      },
      campaignName: 'campaignNameAAA',
      userStatus: 'ACTIVE',
    },
    {
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CLICKS',
        },
      },
      budget: {
        amount: 1000,
      },
      campaignName: 'campaignNameBBB',
      userStatus: 'ACTIVE',
    },
    {
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CONVERSIONS',
          maximizeConversionsBiddingScheme: {
            targetCpa: 1000,//1-800000
          },
        },
      },
      budget: {
        amount: 1000,
      },
      campaignName: 'campaignNameCCC',
      userStatus: 'ACTIVE',
    },
    {
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CONVERSION_VALUE',
          maximizeConversionValueBiddingScheme: {
            targetRoas: 1.0,//0.01-1000.00(1-100000%)
          },
        },
      },
      budget: {
        amount: 1000,
      },
      campaignName: 'campaignNameDDD',
      userStatus: 'ACTIVE',
    },
    {
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'TARGET_IMPRESSION_SHARE',
          targetImpressionShareScheme: {
            location: 'ANYWHERE_ON_PAGE',
            bidCeiling: 1000,
            targetImpressionShare: 1.0,
          },
        },
      },
      budget: {
        amount: 1000,
      },
      campaignName: 'campaignNameEEE',
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const campaigns = Search.CampaignService.add({
    accountId: accountId,
    operand: campaignArray,
  }).rval;
  for (let i = 0; i < campaigns.values.length; i++) {
    if (campaigns.values[i].operationSucceeded) {
      let campaign = campaigns.values[i].campaign;
      Logger.log('biddingStrategyType-> '
        + campaign.biddingStrategyConfiguration.biddingScheme.biddingStrategyType
        + ', campaignId-> ' + campaign.campaignId
        + ', campaignName-> ' + campaign.campaignName + ' has been added.');
    } else {
      let errorList = campaigns.values[i].errors;
      errorList.forEach(function (element) {
        if (element.code == 'D0001' && element.details[0].requestKey.includes('campaignName')) {
          Logger.log('campaignName-> ' + campaignArray[i].campaignName
            + ' is duplicate.');
        } else {
          Logger.log('campaignName-> ' + campaignArray[i].campaignName
            + ' could not be added.');
        }
      });
    }
  }
}

キャンペーンを配信停止にする


function pauseCampaign() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignIds = [1111111, 2222222];//Empty when not specified
  let campaignArray = [];
  for (let i = 0; i < campaignIds.length; i++) {
    //New object to set
    let setCampaign = {
      accountId: accountId,
      campaignId: campaignIds[i],
      userStatus: 'PAUSED',
    };
    campaignArray.push(setCampaign);
  }
  const campaignsSet = Search.CampaignService.set({
    accountId: accountId,
    operand: campaignArray,
  }).rval;
  for (let i = 0; i < campaignsSet.values.length; i++) {
    if (campaignsSet.values[i].operationSucceeded) {
      let campaign = campaignsSet.values[i].campaign;
      Logger.log('campaignId-> ' + campaign.campaignId
        + ', campaignName-> ' + campaign.campaignName + ' has been paused.');
    } else {
      let errorList = campaignsSet.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 {
          Logger.log('campaignId-> ' + campaign.campaignId + ' could not be stopped.');
        }
      });
    }
  }
}

キャンペーン予算を更新する


function setCampaignBudget() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignIds = [1111111, 2222222];//Empty when not specified
  const budgetAmount = 1000;
  let campaignArray = [];
  for (let i = 0; i < campaignIds.length; i++) {
    //New object to set
    let setCampaign = {
      accountId: accountId,
      campaignId: campaignIds[i],
      budget: {
        amount: budgetAmount
      },
    };
    campaignArray.push(setCampaign);
  }
  const campaignsSet = Search.CampaignService.set({
    accountId: accountId,
    operand: campaignArray,
  }).rval;
  for (let i = 0; i < campaignsSet.values.length; i++) {
    if (campaignsSet.values[i].operationSucceeded) {
      let campaign = campaignsSet.values[i].campaign;
      Logger.log('campaignId-> ' + campaign.campaignId
        + ', campaignName-> ' + campaign.campaignName + ' has been paused.');
    } else {
      let errorList = campaignsSet.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 == '210307' && element.details[0].requestKey.includes('budget/amount')) {
          Logger.log('campaignId-> ' + campaignArray[i].campaignId + ' budget-> ' + element.details[0].requestValue +': Budget must be lower than ad group/keywords.');
        } else {
          Logger.log('campaignId-> ' + campaignArray[i].campaignId + ' could not be stopped.');
        }
      });
    }
  }
}