Automate And Better Manage Your Budgets With Scripts

By Jacob Fairclough | @RealSecretJake | Senior Account Analyst at Hanapin Marketing

Today we’ll tackle a few basics of budget based scripts. Why budgets? Well, first of all, they impact every advertiser. In fact, it is one of the requirements for launching a campaign. Second, since budgets are relatively limited and fixed in their implementation it is a great way to get your feet wet in the scripts world.


Through my examples, you’ll see how to change a simple template and dramatically alter the functionality. This is not only efficient but one of the best ways to learn and increase your skill set. You’ll need to do some lifting on your own but with a little learning you should quickly find that you can fulfill most of your scripting needs on your own with no need for developers or external tools.


Getting Started: Accessing The Budget


Since budgets are a component of campaigns, you’ll start by accessing the campaigns in question. If you’ve ever looked at a script, this should look familiar to you.
Once you have the campaign selected you’ll then go one step further and select the budget. Below is a screenshot from the Google help material. As you can see, it starts by selecting campaigns, cycles through the campaigns, sets the budget to 100 and prints the budget with Logger.log().


Image of javascript
Google example


This is nice and simple but what if you want to do more?


Increasing The Budget For Limited Campaigns


Sometimes traffic sneaks up on you and campaigns with low budgets expend themselves before the end of the day. What if we want to automatically increase these budgets? It’s easy enough once you know the steps. This isn’t a trick question as the steps are exactly what you would do when changing the budget manually.


First, we’ll check if the daily spend is greater than the daily budget. I’m going to take a more conservative approach though. Rather than look at the previous day, I want to see if we are consistently limited by budget. I’ll take the last seven days of data and then calculate the average daily spend. If the average daily spend is greater than the daily budget, I’ll increase the budget by 15%.


Image of budget script
Review last 7 days


Once you take a look at the script, you’ll realize not too much has changed. We calculated the average daily spend and implemented it into our original script.


This is often where I see people’s eyes light up. You don’t and shouldn’t start from scratch every time. A good portion of the code is reusable, and you can adapt it to your own specific needs.


Cutting Budgets And Pausing Campaigns


Sometimes you have to toe the line with budgets. You need to maximize click volume, but there is enough traffic that you can blow the budget if you don’t keep a close eye on it.


Let’s take the same script we’ve used above and add additional logic. First, we need a way to calculate our total spend. Thankfully, unlike the dark ages where your only option was to sum up all your campaigns, we can now get account spend. We simply select the current account and get the stats for the month to date time period.


Now that we have a reference for the total spend, we run the script as before but if certain thresholds are reached, we start adjusting budgets.


We will use a simple method here and half the budgets once we reach a certain threshold. Specifically, once we hit 75% of the monthly budget we cut the budgets by half as a precautionary measure. This isn’t something I’d recommend, but it serves as a great example.


Image of budget script
Make adjustments when budget hits threshold


Pausing Campaigns At A Certain Spend Level


Now that we know how to calculate total account spend, it is just as easy to set periodic thresholds where we cut budgets throughout the month as we approach the limit, the opposite of what we did in the increasing budget example.


This isn’t always practical though. You will often have your top performers running all the time. It should be the poor to mediocre performers that get cut.


You’ll notice the template script had a line for “campaign name.” This can be a great way to filter if you have a consistent naming convention. On the other hand, I’d recommend labels as the more robust solution. The semi-permanence of labels makes them perfect for tagging parts of your account on a permanent or semi-permanent basis.


A labeling system allows you to hone in on a group of labels or ignore them completely. For instance, you may opt to leave you brand campaigns running but pause anything with a “generic” or “low ROAS” label.


Now we’ll take a look at a subsection of campaigns, defined by a specific label. Let’s imagine that we have a set monthly budget. Once we approach 75% of the total spend of that budget, we want to pause a group of test campaigns. These tests are new campaigns we are trying this month but ultimately aren’t important to overall performance.


The only thing we need is to calculate the percentage of spend and pause the test campaigns if needed.


For this example, we check actual spend to the limit. If the ratio exceeds 75% we pause the test campaigns to make room for the standard campaigns we’d like to keep running.


Image of script for potential pause
A script to pause the test campaigns


What’s Next?


One of the most challenging aspects of writing a post like this is applicability. Every account differs in performance and needs. This makes it hard to create a post that gives you exactly what you are looking for. I doubt that any of these scripts are a final solution, but I hope you’re encouraged to adapt them to your needs.


As shown in the examples, sometimes a few lines of code are all you need to drastically change the output. Any of the ideas above could even be combined into a mega script that manages budgets by checking spend across multiple categories and fine tuning as needed.