Flexible Budgets - Single Account

ツールアイコン

Google 広告 では、キャンペーンごとに 1 日の予算額を設定することができます。ただし、一部のマーケティング イニシアチブには、固定費用が関連付けられています。たとえば、「秋のセールに向けて 5,000 ドルを費やしたい」などです。入札戦略では、1 日の予算の消化方法をある程度管理できますが、キャンペーン期間中の予算の消化方法は管理できません。

たとえば、秋のセールを 10 日間宣伝するために 5,000 ドルだけを費やしたい場合は、1 日の予算を 500 ドルに設定して予算全体を使い切ることができます。ただし、これは毎日全額を均等に使うことを前提としています。最後の数日間に予算の大部分を消化するように Google 広告に指示することはできません。

このスクリプトでは、独自の予算配分スキームを使用して、キャンペーンの予算を日単位で動的に調整することができます。

仕組み

予算設定方法のテスト

このスクリプトには、複数日にわたって実行した場合の効果をシミュレートするテストコードが含まれています。これにより、スクリプトを一定期間にわたって毎日実行するようにスケジュールした場合に何が起こるかをより正確に把握できます。

デフォルトでは、このスクリプトは 10 日間で $500 を均等に配分するシミュレーションを行います。

function main() {
  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  // setNewBudget(calculateBudgetEvenly, CAMPAIGN_NAME, TOTAL_BUDGET, START_DATE, END_DATE);
}

setNewBudget 関数呼び出しはコメントアウトされています。これは、テストコードのみが実行されることを示しています。この例の出力結果は次のようになります。

Day 1.0 of 10.0, new budget 50.0, cost so far 0.0
Day 2.0 of 10.0, new budget 50.0, cost so far 50.0
Day 3.0 of 10.0, new budget 50.0, cost so far 100.0
Day 4.0 of 10.0, new budget 50.0, cost so far 150.0
Day 5.0 of 10.0, new budget 50.0, cost so far 200.0
Day 6.0 of 10.0, new budget 50.0, cost so far 250.0
Day 7.0 of 10.0, new budget 50.0, cost so far 300.0
Day 8.0 of 10.0, new budget 50.0, cost so far 350.0
Day 9.0 of 10.0, new budget 50.0, cost so far 400.0
Day 10.0 of 10.0, new budget 50.0, cost so far 450.0
Day 11.0 of 10.0, new budget 0.0, cost so far 500.0

このスクリプトは、予算の消化が均等に配分されるよう、毎日新しい予算を計算します。割り当てられた予算の上限に達すると、予算はゼロに設定され、費用が停止されます。

使用する予算戦略を変更するには、使用する関数を変更するか、関数自体を変更します。このスクリプトには、calculateBudgetEvenly と calculateBudgetWeighted の 2 つの事前構築済み戦略が用意されています。重み付けテストの予算戦略を設定するには、次のように testBudgetStrategy を変更します。

testBudgetStrategy(calculateBudgetWeighted, 10, 500);

[プレビュー] をクリックして、ログの出力結果を確認します。この予算戦略では、期間の初めには予算を少なく割り当て、最後の数日間に多く割り当てます。

このテスト手法により、予算を算定する関数を変更してシミュレーションを行い、独自の予算配分方法を試すことができます。

予算を割り当てる

calculateBudgetWeighted 予算戦略は、次の関数によって実装されます。

function calculateBudgetWeighted(costSoFar, totalBudget, daysSoFar, totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / (2 * daysRemaining - 1) ;
  }
}

この関数は次の引数を取ります。

costSoFar
START_DATE から今日までのキャンペーンの費用。
totalBudget
START_DATE から END_DATE に割り当てられた費用。
daysSoFar
START_DATE から今日までの経過日数。
totalDays
START_DATE と END_DATE の間の合計日数。

関数は独自に記述できますが、これらの引数は必ず渡す必要があります。これらの値を使用すると、これまでに使用した金額と全体で使用する金額を比較して、予算全体のタイムライン内で現在の状況を判断できます。

特に、この予算戦略では、残りの予算(totalBudget - costSoFar)を計算し、残りの日数の 2 倍で割ります。これにより、キャンペーンの終了に向けて予算配分が重視されます。START_DATE 以降の費用を使用することで、設定した予算が完全に使い切られていない「スローな日」も考慮されます。

実際の予算

予算戦略に問題がなければ、このスクリプトを毎日実行するようにスケジュールする前に、いくつかの変更を行う必要があります。

まず、ファイルの最初に記述されている次の定数を変更します。

  • START_DATE: 予算戦略の開始日に設定します。現在または過去の日付で指定します。
  • END_DATE: この予算を使用して広告を掲載する最終日を設定します。
  • TOTAL_BUDGET: 消化したい予算の総額。この値は、アカウントの通貨単位で指定します。なお、スクリプトの実行がスケジュール設定された時間によっては、値が超過する場合もあります。
  • CAMPAIGN_NAME: 予算戦略を適用するキャンペーンの名前。

次に、テストを無効にし、実際に予算を変更するロジックを有効にします。

function main() {
  // testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  setNewBudget(calculateBudgetWeighted, CAMPAIGN_NAME, TOTAL_BUDGET, START_DATE, END_DATE);
}

スケジュール

ローカル タイムゾーンの深夜 0 時かその直後に毎日スクリプトが実行されるようスケジュールを設定して、翌日にできるだけ多くの予算が配分されるようにします。ただし、取得したレポートデータ(費用など)は 3 時間ほど遅れる可能性があるため、深夜 0 時以降に実行されるようにスケジュール設定されたスクリプトでは、costSoFar パラメータが前日の合計を参照する可能性があります。

セットアップ

  • ボタンをクリックして、Google 広告アカウントでスクリプトを作成します。

    スクリプト テンプレートをインストールする

  • スクリプトを保存して、[プレビュー] ボタンをクリックします。このスクリプトは(デフォルトで)、10 日間で 500 ドルの予算戦略をシミュレートします。ロガーの出力には、シミュレートされている日、その日に割り当てられた予算、これまでに費やされた合計金額が反映されます。

ソースコード

// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @name Flexible Budgets
 *
 * @overview The Flexible budgets script dynamically adjusts campaign budget for
 *     an advertiser account with a custom budget distribution scheme on a daily
 *     basis. See
 *     https://developers.google.com/google-ads/scripts/docs/solutions/flexible-budgets
 *     for more details.
 *
 * @author Google Ads Scripts Team [adwords-scripts@googlegroups.com]
 *
 * @version 2.1
 *
 * @changelog
 * - version 2.1
 *   - Split into info, config, and code.
 * - version 2.0
 *   - Updated to use new Google Ads scripts features.
 * - version 1.0.3
 *   - Add support for video and shopping campaigns.
 * - version 1.0.2
 *   - Use setAmount on the budget instead of campaign.setBudget.
 * - version 1.0.1
 *   - Improvements to time zone handling.
 * - version 1.0
 *   - Released initial version.
 */

/**
 * Configuration to be used for the Flexible Budgets script.
 */

CONFIG = {
  'total_budget': 500,
  'campaign_name': 'Special Promotion',
  'start_date': 'November 1, 2021 0:00:00 -0500',
  'end_date': 'December 1, 2021 0:00:00 -0500'
};

const TOTAL_BUDGET = CONFIG.total_budget;
const CAMPAIGN_NAME = CONFIG.campaign_name;
const START_DATE = new Date(CONFIG.start_date);
const END_DATE = new Date(CONFIG.end_date);

function main() {
  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
//  setNewBudget(calculateBudgetEvenly, CAMPAIGN_NAME, TOTAL_BUDGET,
//      START_DATE, END_DATE);
}

function setNewBudget(budgetFunction, campaignName, totalBudget, start, end) {
  const today = new Date();
  if (today < start) {
    console.log('Not ready to set budget yet');
    return;
  }
  const campaign = getCampaign(campaignName);
  const costSoFar = campaign.getStatsFor(
        getDateStringInTimeZone('yyyyMMdd', start),
        getDateStringInTimeZone('yyyyMMdd', end)).getCost();
  const daysSoFar = datediff(start, today);
  const totalDays = datediff(start, end);
  const newBudget = budgetFunction(costSoFar, totalBudget, daysSoFar,
                                   totalDays);
  campaign.getBudget().setAmount(newBudget);
}

function calculateBudgetEvenly(costSoFar, totalBudget, daysSoFar, totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / daysRemaining;
  }
}

function calculateBudgetWeighted(costSoFar, totalBudget, daysSoFar,
    totalDays) {
  const daysRemaining = totalDays - daysSoFar;
  const budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / (2 * daysRemaining - 1);
  }
}

function testBudgetStrategy(budgetFunc, totalDays, totalBudget) {
  let daysSoFar = 0;
  let costSoFar = 0;
  while (daysSoFar <= totalDays + 2) {
    const newBudget = budgetFunc(costSoFar, totalBudget, daysSoFar, totalDays);
    console.log(`Day ${daysSoFar + 1} of ${totalDays}, new budget ` +
                `${newBudget}, cost so far ${costSoFar}`);
    costSoFar += newBudget;
    daysSoFar += 1;
  }
}

/**
 * Returns number of days between two dates, rounded up to nearest whole day.
 */
function datediff(from, to) {
  const millisPerDay = 1000 * 60 * 60 * 24;
  return Math.ceil((to - from) / millisPerDay);
}

function getDateStringInTimeZone(format, date, timeZone) {
  date = date || new Date();
  timeZone = timeZone || AdsApp.currentAccount().getTimeZone();
  return Utilities.formatDate(date, timeZone, format);
}

/**
 * Finds a campaign by name, whether it is a regular, video, or shopping
 * campaign, by trying all in sequence until it finds one.
 *
 * @param {string} campaignName The campaign name to find.
 * @return {Object} The campaign found, or null if none was found.
 */
function getCampaign(campaignName) {
  const selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(),
      AdsApp.shoppingCampaigns()];
  for (const selector of selectors) {
    const campaignIter = selector
        .withCondition(`CampaignName = "${campaignName}"`)
        .get();
    if (campaignIter.hasNext()) {
      return campaignIter.next();
    }
  }
  throw new Error(`Could not find specified campaign: ${campaignName}`);
}