Google 広告 では、キャンペーンごとに 1 日の予算額を設定することができます。ただし、一部のマーケティング イニシアチブには固定費用が関連付けられます(「秋のセールに向けて 5,000 ドルを費やしたい」など)。入札戦略では、1 日の予算の使い方をある程度制御できますが、キャンペーン期間中の予算の消化方法を制御することはできません。
たとえば、秋のセールへの広告掲載に 5,000 ドルのみを費やし、10 日間広告を掲載する場合は、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);
}
スケジュール
ローカル タイムゾーンの深夜 12 時かその直後に毎日スクリプトが実行されるようスケジュールを設定して、翌日にできるだけ多くの予算が配分されるようにします。ただし、費用などの取得されたレポートデータは約 3 時間遅れる可能性があるため、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}`);
}