This is the legacy documentation for Google Ads scripts. Go to the current docs.

Flexible Budgets - Manager Account

MCC Flexible Budgets extends Flexible Budgets to run for multiple accounts under a single manager account. Flexible Budgets can dynamically adjust your campaign budget daily with a custom budget distribution scheme.

The script reads a spreadsheet for each specified account/campaign and corresponding budget (associated with start date and end date), finds the campaign, calculates the budget for the current day, sets it as the campaign's daily budget, and records the result in the spreadsheet. It won’t touch campaigns not specified in the spreadsheet.

How it works

The script works the same way the single account Flexible Budget script does. The only additional functionality is that it supports multiple accounts through the specified spreadsheet.

The first 2 columns specify the campaign for which to calculate a budget, the next 3 specify its budget information, and the last records the execution result.

The account ID should be an advertiser account, not a manager account. You can have multiple budgets for the same account/campaign, but please make sure you only have one active budget at a time, or else a newer budget calculation may overwrite an older one. If an account/campaign is not specified in the spreadsheet, the script will not set a flexible budget for it.

Iterating over all managed accounts

Initially the script runs in the manager account's context. It first invokes

var managerAccount = AdsApp.currentAccount();
to store the manager account, then invokes
var accountIterator = AdsManagerApp.accounts().get();
to get an iterator of all managed accounts under the manager account.

In each iteration, the script invokes;
to switch to the managed account's context so that it can perform operations on that account. After processing all the managed accounts, the script invokes;
to restore the manager account's context.

Testing budget strategies

The script includes testing code to simulate the effects of running for multiple days. This gives you a better idea of what will happen when the script is scheduled to run daily over a period of time.

By default, this script will simulate an even budget distribution of $500 spent over 10 days.

function main() {
  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
//  setNewBudget(calculateBudgetEvenly, campaignName, totalBudget, startDate, endDate);

The setNewBudget function call is commented out, which means we'll only run the testing code. Here is the output from the example:

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

Each day we calculate a new budget to make sure we're spending the budget evenly each day. After we exceed the initial budget allotment, we set the budget to zero to halt spending.

You can change the budget strategy used by changing which function is used, or modifying the function itself. The script comes with two pre-built strategies: calculateBudgetEvenly and calculateBudgetWeighted; we've just tested the former—update the testBudgetStrategy line to use the latter:

testBudgetStrategy(calculateBudgetWeighted, 10, 500);

Click Preview and check the logger output. Notice that this budget strategy allocates less budget early in the period and more during the last few days.

You can use this test method to simulate changes to the budget calculation functions and try your own approach to distributing a budget.

Allocating a budget

Let's look more closely at the calculateBudgetWeighted budget strategy:

function calculateBudgetWeighted(costSoFar, totalBudget, daysSoFar, totalDays) {
  var daysRemaining = totalDays - daysSoFar;
  var budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / (2 * daysRemaining - 1) ;

This function takes the following arguments:

  • costSoFar - how much has this campaign accrued in cost from the startDate to today.
  • totalBudget - how much we want to spend from startDate to endDate.
  • daysSoFar - how many days have elapsed from startDate to today.
  • totalDays - the total number of days between startDate and endDate.

You can write your own function as long as it takes these arguments. Using these values, you can compare how much money you've spent so far against how much to spend overall and determine where you currently are within the timeline for the entire budget.

In particular, this budget strategy figures out how much budget remains (totalBudget - costSoFar) and divides that by twice the number of days remaining. This weighs the budget distribution towards the end of the campaign. By using the cost since startDate, it also takes into account "slow days" where you don't spend the entire budget you set.

Budgeting for real

Once you're happy with your budget strategy, you'll need to make a few changes before you can schedule this script to run daily.

First, update the spreadsheet to specify account, campaign, budget, start date, end date -- one row for each campaign budget.

  • Account ID - the account ID (of format xxx-xxx-xxxx) of the campaign to apply the budget strategy to.
  • Campaign Name - the name of the campaign to apply the budget strategy to.
  • Start Date - the start date of your budget strategy - should be the current date or a day in the past.
  • End Date - the last day you want to advertise with this budget.
  • Total Budget - total amount you're trying to spend. This value is in account currency and may be exceeded depending on when the script is scheduled to run.

Next, disable the test and enable the logic to actually change the budget:

function main() {
//  testBudgetStrategy(calculateBudgetEvenly, 10, 500);
  setNewBudget(calculateBudgetWeighted, campaignName, totalBudget, startDate, endDate);

The result for each campaign will be recorded in the Execution Result column.


Schedule this script to run daily, at or shortly after midnight in the local timezone so as to direct as much as possible the upcoming day's budget. Note, however, that retrieved reports data such as cost could be delayed by about 3 hours, so the costSoFar parameter may be referencing yesterday's total for a script that is scheduled to run after midnight.


  • Set up a spreadsheet-based script with the source code below. Use the MCC Flexible Budgets template spreadsheet.
  • Update the SPREADSHEET_URL in the code to reflect your spreadsheet's URL.
  • Schedule the script Daily.

Source code

// 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
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

 * @name MCC Flexible Budgets
 * @overview The MCC Flexible Budgets script dynamically adjusts campaign budget
 *     daily for accounts under an MCC account with a custom budget distribution
 *     scheme. See
 *     for more details.
 * @author Google Ads Scripts Team []
 * @version 1.0.4
 * @changelog
 * - version 1.0.4
 *   - Add support for video and shopping campaigns.
 * - version 1.0.3
 *   - Added validation for external spreadsheet setup.
 * - version 1.0.2
 *   - Fix a minor bug in variable naming.
 *   - Use setAmount on the budget instead of campaign.setBudget.
 * - version 1.0.1
 *   - Improvements to time zone handling.
 * - version 1.0
 *   - Released initial version.


// Please fix the following variables if you need to reformat the spreadsheet
// column numbers of each config column. Column A in your spreadsheet has
// column number of 1, B has number of 2, etc.
var COLUMN = {
  accountId: 2,
  campaignName: 3,
  startDate: 4,
  endDate: 5,
  totalBudget: 6,
  results: 7

// Actual config (without header and margin) starts from this row

function main() {
  // Uncomment the following function to test your budget strategy function
  // testBudgetStrategy(calculateBudgetEvenly, 10, 500);

// Core logic for calculating and setting campaign daily budget
function setNewBudget(budgetFunc) {
  Logger.log('Using spreadsheet - %s.', SPREADSHEET_URL);
  var spreadsheet = validateAndGetSpreadsheet(SPREADSHEET_URL);
  var sheet = spreadsheet.getSheets()[0];

  var endRow = sheet.getLastRow();

  var mccAccount = AdsApp.currentAccount();
  sheet.getRange(2, 6, 1, 2).setValue(mccAccount.getCustomerId());

  for (var i = CONFIG_START_ROW; i <= endRow; i++) {
    Logger.log('Processing row %s', i);

    var accountId = sheet.getRange(i, COLUMN.accountId).getValue();
    var campaignName = sheet.getRange(i, COLUMN.campaignName).getValue();
    var startDate = new Date(sheet.getRange(i, COLUMN.startDate).getValue());
    var endDate = new Date(sheet.getRange(i, COLUMN.endDate).getValue());
    var totalBudget = sheet.getRange(i, COLUMN.totalBudget).getValue();
    var resultCell = sheet.getRange(i, COLUMN.results);

    var accountIter = AdsManagerApp.accounts().withIds([accountId]).get();
    if (!accountIter.hasNext()) {
      resultCell.setValue('Unknown account');
    var account =;;

    var campaign = getCampaign(campaignName);
    if (!campaign) {
      resultCell.setValue('Unknown campaign');

    var today = new Date();
    if (today < startDate) {
      resultCell.setValue('Budget not started yet');
    if (today > endDate) {
      resultCell.setValue('Budget already finished');

  var costSoFar = campaign.getStatsFor(
      getDateStringInTimeZone('yyyyMMdd', startDate),
      getDateStringInTimeZone('yyyyMMdd', endDate)).getCost();
    var daysSoFar = datediff(startDate, today);
    var totalDays = datediff(startDate, endDate);
    var newBudget = budgetFunc(costSoFar, totalBudget, daysSoFar, totalDays);
    Logger.log('AccountId=%s, CampaignName=%s, StartDate=%s, EndDate=%s, ' +
               'CostSoFar=%s, DaysSoFar=%s, TotalDays=%s, NewBudget=%s',
               accountId, campaignName, startDate, endDate,
               costSoFar, daysSoFar, totalDays, newBudget);
    resultCell.setValue('Set today\'s budget to ' + newBudget);

  // update "Last execution" timestamp
  sheet.getRange(1, 3).setValue(today);;

// One calculation logic that distributes remaining budget evenly
function calculateBudgetEvenly(costSoFar, totalBudget, daysSoFar, totalDays) {
  var daysRemaining = totalDays - daysSoFar;
  var budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / daysRemaining;

// One calculation logic that distributes remaining budget in a weighted manner
function calculateBudgetWeighted(costSoFar, totalBudget, daysSoFar, totalDays) {
  var daysRemaining = totalDays - daysSoFar;
  var budgetRemaining = totalBudget - costSoFar;
  if (daysRemaining <= 0) {
    return budgetRemaining;
  } else {
    return budgetRemaining / (2 * daysRemaining - 1);

// Test function to verify budget calculation logic
function testBudgetStrategy(budgetFunc, totalDays, totalBudget) {
  var daysSoFar = 0;
  var costSoFar = 0;
  while (daysSoFar <= totalDays + 2) {
    var newBudget = budgetFunc(costSoFar, totalBudget, daysSoFar, totalDays);
    Logger.log('Day %s of %s, new budget %s, cost so far %s',
               daysSoFar + 1, totalDays, newBudget, costSoFar);
    costSoFar += newBudget;
    daysSoFar += 1;

// Return number of days between two dates, rounded up to nearest whole day.
function datediff(from, to) {
  var millisPerDay = 1000 * 60 * 60 * 24;
  return Math.ceil((to - from) / millisPerDay);

// Produces a formatted string representing a given date in a given time zone.
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) {
  var selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(),
  for(var i = 0; i < selectors.length; i++) {
    var campaignIter = selectors[i].
        withCondition('CampaignName = "' + campaignName + '"').
    if (campaignIter.hasNext()) {
  return null;

 * Validates the provided spreadsheet URL to make sure that it's set up
 * properly. Throws a descriptive error message if validation fails.
 * @param {string} spreadsheeturl The URL of the spreadsheet to open.
 * @return {Spreadsheet} The spreadsheet object itself, fetched from the URL.
 * @throws {Error} If the spreadsheet URL hasn't been set
function validateAndGetSpreadsheet(spreadsheeturl) {
  if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
    throw new Error('Please specify a valid Spreadsheet URL. You can find' +
        ' a link to a template in the associated guide for this script.');
  return SpreadsheetApp.openByUrl(spreadsheeturl);