キャンペーン

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

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


function getAllCampaigns() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaigns = Display.CampaignService.get({
    accountId: accountId,
  }).rval;
  if (campaigns.totalNumEntries == 0) {
    Logger.log('Campaign 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 = [11111111, 22222222, 33333333];//Multiple selections are possible
  const campaigns = Display.CampaignService.get({
    accountId: accountId,
    campaignIds: campaignIds,
  }).rval;
  if (campaigns.totalNumEntries == 0) {
    Logger.log('Campaign 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 = [
    {
      accountId: accountId,
      budget: {
        amount: 1000,
      },
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CONVERSIONS',
        }
      },
      campaignGoal: 'CONVERSION',
      campaignName: 'campaignAAA',
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      budget: {
        amount: 2000,
      },
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CLICKS',
        }
      },
      campaignGoal: 'WEBSITE_TRAFFIC',
      campaignName: 'campaignBBB',
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      budget: {
        amount: 3000,
      },
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_VIDEO_VIEWS',
        }
      },
      campaignGoal: 'VIDEO_VIEW',
      campaignName: 'campaignCCC',
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      budget: {
        amount: 4000,
      },
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'CPC',
          cpcBiddingScheme: {
            cpc: 1000,
            enhancedCpcEnabled: 'FALSE'
          },
        }
      },
      campaignGoal: 'WEBSITE_TRAFFIC',
      campaignName: 'campaignDDD',
      userStatus: 'ACTIVE',
    },
    {
      accountId: accountId,
      budget: {
        amount: 5000,
      },
      biddingStrategyConfiguration: {
        biddingScheme: {
          biddingStrategyType: 'MAXIMIZE_CONVERSIONS',
          maximizeConversionsBiddingScheme: { 
            targetCpa: 1000
          }
        }
      },
      campaignGoal: 'CONVERSION',
      campaignName: 'campaignEEE',
      userStatus: 'ACTIVE',
    }
  ];//Multiple selections are possible
  const campaigns = Display.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('campaignId-> ' + campaign.campaignId
        + ', campaignName-> ' + campaign.campaignName
        + ' be added as amount -> ' + campaign.budget.amount
        + ', campaignType -> ' + campaign.biddingStrategyConfiguration.biddingScheme.biddingStrategyType
        + ', campaignGoal -> ' + campaign.campaignGoal);
    } else {
      let errorList = campaigns.values[i].errors;
      errorList.forEach(function (element) {
        if (element.code == 'D0001' && element.details[0].requestKey.includes('campaignName')) {
          Logger.log('campaignName-> ' + element.details[0].requestValue
            + ' is duplicate.');
        } else {
          Logger.log('campaignName-> ' + campaignArray[i].campaignName
            + ' could not to added as amount-> ' + campaignArray[i].budget.amount
            + ', campaignType-> ' + campaignArray[i].biddingStrategyConfiguration.biddingScheme.biddingStrategyType
            + ', campaignGoal-> ' + campaignArray[i].campaignGoal);
        }
      });
     }
  }
}

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


function pauseCampaign() {
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignIds = [11111111, 22222222, 33333333];//Empty when not specified  
  let campaignsArray = [];
  for (let i = 0; i < campaignIds.length; i++) {
    //New object to set
    let setCampaign = {
      accountId: accountId,
      campaignId: campaignIds[i],
      userStatus: 'PAUSED',
    };
    campaignsArray.push(setCampaign);
  }
  const campaignsSet = Display.CampaignService.set({
    accountId: accountId,
    operand: campaignsArray,
  }).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 + ' have been stopped.');
    } 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-> ' + campaignsArray[i].campaignId + ' could not to be stopped.');
        }
      });
    }
  }
}

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


function setCampaignBudget(){
  const accountId = AdsUtilities.getCurrentAccountId();
  const campaignIds = [11111111, 22222222, 33333333];//Empty when not specified
  let campaignsArray = [];
  for (let i = 0; i < campaignIds.length; i++) {
    //New object to set
    let setCampaign = {
      accountId: accountId,
      campaignId: campaignIds[i],
      budget: {
        amount: 3000
      },
    };
    campaignsArray.push(setCampaign);
  }
  const campaignsSet = Display.CampaignService.set({
    accountId: accountId,
    operand: campaignsArray,
  }).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
        + ' set amount-> ' + campaign.budget.amount);
    } 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-> ' + campaignsArray[i].campaignId
            + ' could not set amount-> ' + campaignsArray[i].budget.amount);
        }
      });
    }
  }
}