管理共用預算的腳本範例
下列各節示範針對 共用預算執行各種動作的腳本範例。
新增共用預算
若要新增共用預算,您必須使用 Microsoft Advertising Web 應用程式。 如需詳細資訊,請參閱如何?跨多個活動共用預算?
建立行銷活動與共享預算的關聯
若要將行銷活動與共享預算建立關聯,您必須使用 Microsoft Advertising Web 應用程式。 如需詳細資訊,請參閱如何?跨多個活動共用預算?
取得所有共用預算
若要取得帳戶中的所有共用預算,請先呼叫 AdsApp 物件的 budgets
方法來取得 選取器。 然後,呼叫選取器的 get
方法,以取得您用來逐一查看共用預算清單的 反覆運算器 。 由於此範例未指定任何篩選準則,因此選取器會傳回帳戶中的所有共用預算。 若要判斷反覆運算器中的共用預算數目,請呼叫反覆運算器的 totalNumEntities
方法。
注意事項
共用預算不包含未共用 (個別行銷活動) 預算。
function main() {
// Gets all shared budgets in the account.
var iterator = AdsApp.budgets().get();
// Iterates through the list of shared budgets and logs
// each budgets's name and amount.
while (iterator.hasNext()) {
var budget = iterator.next();
}
}
依名稱取得共用預算
若要依名稱取得共用預算,請先呼叫 AdsApp 物件的 budgets
方法來取得選取 器。 選取器包含一些您用來篩選預算清單的篩選方法。
withCondition
使用 方法,依名稱篩選預算。 例如,若要篩選特定名稱的清單,請使用: withCondition("BudgetName = '<budgetnamegoeshere>'")
。 若要依部分名稱篩選清單,請使用: withCondition("BudgetName CONTAINS_IGNORE_CASE '<partialnamegoeshere>'")
。 請注意,運算元和運算子會區分大小寫。
接下來,呼叫選取器的 get
方法以取得 反覆運算器。
function main() {
// Partial name of the shared budget to get.
var budgetName = 'PARTIAL NAME GOES HERE';
// Get the budgets that contain the partial name.
var iterator = AdsApp.budgets()
.withCondition(`BudgetName CONTAINS_IGNORE_CASE '${budgetName}'`)
.get();
// Iterates through the list of shared budgets and logs
// each budget's name and amount.
while (iterator.hasNext()) {
var budget = iterator.next();
}
}
依識別碼取得共用預算
如果您可以存取共用預算的識別碼,請改用它。 使用識別碼取得實體可提供更好的效能。 請使用 方法, withIds
而不是使用 withCondition
篩選方法。 例如,withIds(['12345'])
。
function main() {
var sharedBudgetId = '12345';
var iterator = AdsApp.budgets()
.withIds([sharedBudgetId])
.get();
while (iterator.hasNext()) {
var budget = iterator.next();
}
}
取得所有共用預算的活動
若要取得共用預算的所有活動,請呼叫預算的 行銷活動 方法。 您只能從BudgetSelector取得的Budget物件呼叫這個方法;如果預算的來源是行銷活動的getBudget方法,您就無法呼叫它。
function main() {
var sharedBudgetId = '12345';
var budgets = AdsApp.budgets()
.withIds([sharedBudgetId])
.get();
while (budgets.hasNext()) {
var budget = budgets.next();
var campaigns = budget.campaigns().get();
while (campaigns.hasNext()) {
var campaign = campaigns.next();
}
}
}
取得共用預算的效能資料
若要取得共用預算的效能計量,請呼叫預算的 getStats 方法。 當您取得共用預算清單時,您必須指定所要計量資料的日期範圍。 您可以使用預先定義的常值來指定日期範圍,例如LAST_MONTH或 TODAY,或是開始和結束日期。 若要指定日期範圍,請在選取預算時使用其中一 forDateRange
種方法 (請參閱 BudgetSelector) 。
如需您可以存取的計量清單,請參閱 Stats 物件。 計量是共用預算之所有行銷活動的匯總。
function main() {
var sharedBudgetId = '12345';
// Get the shared budget. You need to specify the date range of the
// performance data you want to get.
var budgets = AdsApp.budgets()
.forDateRange('LAST_WEEK')
.withIds([sharedBudgetId])
.get();
// If the budget is found, log some metrics.
while (budgets.hasNext()) {
var budget = budgets.next();
var metrics = budget.getStats(); // Gets the performance metrics.
}
}