用于管理共享预算的脚本示例

以下部分显示了针对 共享预算执行各种操作的脚本示例。

添加共享预算

若要添加共享预算,必须使用 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,请改用它。 使用 ID 获取实体可提供更好的性能。 使用 方法,而不是使用withConditionwithIds筛选器方法。 例如,withIds(['12345'])

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

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

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

获取共享预算的所有市场活动

若要获取共享预算的所有市场活动,请调用预算的 市场活动 方法。 只能从从 BudgetSelector 获取的预算对象调用此方法;如果预算的来源是市场活动的 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.
    }
}