A Google Ads Script for Managing Client’s Budget

Dino Kukic

Aug 22, 2019

Full script: here 🙂

I don’t know what’s up with that, but within Google Ads, you can’t set a monthly budget. And, I never saw this as a problem, until I started running ads for clients and figured that I had to log in the account every day just to check how much was spent and adjust the budget so that I wouldn’t have to turn it off 10 days before the end of the month.

The problem is, you always agree on a monthly spend a client would like to run ads with and since they give you their trust that you won’t spend more, it’s important to keep an eye on it. For few clients it’s not a problem, but sometimes it can just be overwhelming and you can spend that time more wisely anyways. So, while working in the agency I developed a script that allowed me to be on top of it at all times, while working on something else. Here are the basic functionalities I wanted to have:

  1. Run through the account every morning,
  2. Check how much it was spent so far during this month
  3. Subtract that from the monthly budget
  4. Divide the result by the number of remaining days during the month
  5. Check whether this differs from the current daily budget
  6. If so adjust the daily budget to the result
  7. Send me an email with the info

For the start, it’s important to understand that it’s tailored to my way of doing things and the most important being that I run all campaigns under a Shared Budget. Meaning, even if there would be only one campaign, I would still use a shared budget just in case that I need to run another campaign within that same scope and overall, for a better overview.

First and foremost we select the budgets:

var budgetSelector = AdWordsApp.budgets()
	.withCondition("IsBudgetExplicitlyShared = true")
	.withIds([919911729, 971813687, 1371349908, 1371415178, 1384776128, 919910286, 1358991353, 1375633913])
	.forDateRange("THIS_MONTH");

Now, we set up the variables such as your email address (required or email sending purposes) and the budgets in respect to the order of the IDs above.

var emailAddress = "your@email.com";
var monthlyBudget = [700, 300, 500, 700, 500, 500, 700, 200];

Next we get the budgets names and the total spend for this month:

var budgetNames = [];
var totalSpend = [];
var currentBudget = [];

var budgetIterator = budgetSelector.get();
var budgetIDs = [];
  
while (budgetIterator.hasNext()) {
	var budget = budgetIterator.next();
	var budgetName = budget.getName();
  	budgetNames.push(budgetName);
  	currentBudget.push(budget.getAmount());
  	totalSpend.push(budget.getStatsFor("THIS_MONTH").getCost());
  	budgetIDs.push(budget.getId());
}

After that, we must calculate the number of days remaining within the current month:

function daysInThisMonth() {
  	var now = new Date();
  	return new Date(now.getFullYear(), now.getMonth()+1, 0).getDate();
	}

//Get current date
var now = new Date();
var daysSinceStartOfMonth = now.getDate();
//Logger.log(daysSinceStartOfMonth);

//Get the remaining days during this month i.e how many days we want ads to still run
var daysRemaining = daysInThisMonth() - daysSinceStartOfMonth;

Based on that, we’ll calculate the budget adjustments if required:

var budgetAdjustment = [];
for (var i = 0; i < currentBudget.length; i++) {
	if (monthlyBudget[i] > totalSpend[i]) {
		budgetAdjustment.push((monthlyBudget[i] - totalSpend[i]) / daysRemaining);
    } else {
    budgetAdjustment.push(0);
    }
}

Once that’s done, we want to check the differences and, since it’s not really necessary to adjust the budget if that change is really small, we change it only if the difference is bigger than 1.

var budgetStatus = [];
for (var i = 0; i < currentBudget.length; i++) {
	if (budgetAdjustment[i] < currentBudget[i] - 1 || budgetAdjustment[i] > currentBudget[i] + 1) {
		while (budgetIterator.hasNext()) {
			var budgetToSet = budgetIterator.next();
			if (budgetIDs === budgetToSet.getId()) {
				budgetToSet.setAmount(budgetAdjustment);
			}
		}
		budgetStatus.push('Budget Adjusted to: ' + budgetAdjustment[i].toFixed(2));
	}
	else budgetStatus.push('Budget is OK at: ' + currentBudget[i]);
}

Okay, operation performed and all we need to do now is create the body of the email and send it to your email address:

var adjustments = "";
var totalThisMonth = 0;
function mailBody() {
	for (var i = 0; i < budgetAdjustment.length; i++) {
    	adjustments = adjustments + "<strong>" + budgetNames[i] + "</strong>: " + "Spent this month: <strong>" + totalSpend[i] + "</strong>; To reach your monthly budget of " + monthlyBudget[i] + ", adjust it to <strong>" + budgetAdjustment[i] + ' ' + budgetStatus[i] + "</strong><br><br>";
    	totalThisMonth = totalThisMonth + totalSpend[i];
    } 
}
mailBody();

MailApp.sendEmail({
    to: emailAddress,
    subject: 'My Client Google Ads Adjustments',
    htmlBody: "Hey, <br><br> Here's the daily summary of campaigns for <em>My Client</em>:<br><br>" + adjustments + "Additionally, the total spend so far this month is <strong>" + totalThisMonth + "</strong><br><br>Cheers,",
  });

}

To copy the entire script go to this link and if you come across any issues, let me know in the comments.