Google Ads 可任您設定每個廣告活動的每日預算金額。不過,有些行銷計畫會產生固定費用,例如「我想在秋季特賣前花費 $5,000 美元」。出價策略可讓您在一定程度上控制每日預算的使用方式,但無法控制廣告活動期間的預算消耗方式。
舉例來說,如果我們只想花 $5,000 美元宣傳秋季特賣,且宣傳期為 10 天,則可將每日預算設為 $500 美元,用完所有預算。不過,這項假設的前提是,我們每天都會花費整筆金額,而且希望平均分配。您無法要求 Google Ads 在最後幾天內花費大部分預算。
這項指令碼會透過自訂預算分配計畫,動態調整廣告活動每日預算。
運作方式
測試預算策略
指令碼包含一些測試程式碼,可模擬執行多天的效果。這樣一來,您就能更清楚瞭解,如果排定指令碼每天執行一段時間,可能會發生什麼情況。
根據預設,這個指令碼會模擬 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。如要設定加權測試預算策略,請按照下列方式變更 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到今天經過的天數。 totalDaysSTART_DATE到END_DATE之間的天數總和。
You can write your own function as long as it takes these arguments. 您可以加入這些引數,自行撰寫函式。您可以根據這些值,比較目前已支出的金額與整體預算,並判斷整個預算的時間軸進度。
具體來說,這項預算策略會計算剩餘預算 (totalBudget - costSoFar),然後除以剩餘天數的兩倍。這會使預算分配偏向廣告活動結束時。使用自 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);
}
排程
建議安排這項指令碼在當地時間每天午夜左右執行,盡可能調節次日預算。不過請注意,擷取的報表資料 (例如費用) 可能會延遲約 3 小時,因此如果指令碼排定在午夜後執行,costSoFar 參數可能會參照昨天的總計。
設定
按一下按鈕,在 Google Ads 帳戶中建立指令碼。
儲存指令碼,然後按一下 [預覽] 按鈕。根據預設,這個指令碼會模擬 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}`);
}