管理共用預算的指令碼範例

下列各節顯示針對 共用預算執行各種動作的指令碼範例。

新增共用型預算

若要新增共用預算,您必須使用 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();
    }
}

依識別碼取得共用預算

如果您有權存取共用預算的 ID,請改用它。 使用 ID 獲取實體可提供更好的效能。 請使用withIds方法,而不是使用withCondition篩選方法。 例如,withIds(['12345'])

function main() {
    var sharedBudgetId = '12345';

    var iterator = AdsApp.budgets()
        .withIds([sharedBudgetId])
        .get();

    while (iterator.hasNext()) {
        var budget = iterator.next();
    }
}

取得共用預算的所有廣告活動

若要取得所有共用預算的廣告活動,請呼叫 budget's campaigns 方法。 您只能從從 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.
    }
}