Back to Blog

A Simple & Customizable Google Ads Script To Prevent Your Account from Overspending

July 30, 2019

By Christine Zirnheld
No Comments

Google Ads does not make budget management an easy task. As we all know (and roll our eyes at), Google allows campaigns to spend up to 2X their daily “budget”. This creates a need for advertisers to frequently check on daily campaign spend, particularly when they have a very limited budget, launch a new campaign, or make significant changes.

The Problem

While searching for a solution for overspending, we came across this Google Ads scripts forum where an account budget-management script was posted by an unknown user. This script turns all campaigns off if the account spends more than the specified maximum budget in the last 30 days. One issue with this was that we wanted to monitor spend daily (a lot can happen in a day!), which was an easy adjustment to make to the script. However, the script came with a couple of other challenges that were not so simple to fix. First, after implementing the script as we found it online, we noticed that it does not shut down video campaigns. Second, it does not leave any room for flexibility when it comes to campaign structure. If the account overspends, all campaigns will shut down and we have some campaigns that are so important that we simply cannot afford to turn them off.

The Solution

We turned to our development team to adjust the script we found online, allowing us to include video campaigns and exclude select campaigns to ensure their ads run all day, every day. After making these changes, we use this script as the center of a standard, automated budget management system that ensures our Google Ads’ account spend remains under control.

This strategy does have its limitations (which we will dig into later), but since setting this practice into place, we sleep much better at night knowing that if our account has overspent the daily maximum budget that we’ve specified, our campaigns will automatically pause shortly after. This is a 3 part strategy that utilizes a Google Ad’s label, script, and automated rule to keep spend in check while allowing select campaigns to remain unaffected. 

How it works:

The free daily script we’re providing in this post will check your total account spend every hour. If the account has spent more than the maximum amount you’ve set, the script will pause all active campaigns (unless you specify that certain campaigns remain active, we’ll get to that soon).

Oh great, now you’ll have to check all of your accounts every day to see which campaigns were paused the day before and re-enable them, right? 

Wrong! Using an automated rule, we can have Google Ads do that work for us. 

The automated rule will run at midnight every day to check and see if any of the campaigns labeled “active” are paused. If they are, it will re-enable these campaigns for you. 

Keep reading for a step-by-step, fool-proof guide to setting up this strategy. 

Step 1: Leveraging Labels

The most important thing to note when using this Google Ads budget-management strategy is that it is entirely dependent on consistent and up-to-date campaign labels. 

Utilizing the script as-is, you’ll have to create one, or perhaps two, new labels in your account. 

Mandatory:  “active” label for all enabled campaigns. This is vital for the automated rule to work properly. 

Optional: “nopause” label for any active campaigns that you do not want the script to make changes to. 

  • For example, maybe you always want your ads to show up for branded searches. In this case, you would label every campaign with branded keywords “nopause”.

As you’re creating new campaigns or pausing seasonal or under-performing campaigns, it’s absolutely vital that you keep labels accurate and up-to-date. If you pause a campaign manually and forget to remove the “active” label, the automated rule will re-enable that campaign when it runs. 

Step 2: Setting Up the Script

The rest of this process will all take place in the “Bulk Rules” section of Google Ads, where you can set up and manage Google Ads’ scripts, automated rules, and uploads.

To enable this script in your account, follow these steps:

1.Under the “Tools & Settings” menu in Google Ads, select “Scripts” under the “Bulk Actions” category. 

2. Click the blue plus sign on the top left side of your screen to add a new script. 

3. Enter a name for your script in the top left corner of the window. We named ours “Pause Campaigns When Max Daily Spend is Reached”.

4. Copy and paste this free script into the editor area:

var MAX_TOTALS = 350;
var TODAYS_SPEND = 0;

function main() {
    processTodaysSpend();
    processCampaignPause();
}

function processTodaysSpend() {
    //Get Ad Spend
    TODAYS_SPEND += getTodaysCosts(AdsApp.campaigns().get());
    //Get Video Spend
    TODAYS_SPEND += getTodaysCosts(AdsApp.videoCampaigns().get());
}

function processCampaignPause() {

    if (TODAYS_SPEND > MAX_TOTALS) {
  
        var adsSelector = AdsApp.campaigns()
        .withCondition("LabelNames CONTAINS_NONE ['nopause']")
        .get();
      
        var videoSelector = AdsApp.videoCampaigns()
        .withCondition("LabelNames CONTAINS_NONE ['nopause']")
        .get();
  
        //Pause Ads
        pauseCampaigns(adsSelector);
        //Pause Videos
        pauseCampaigns(videoSelector);
    }
}

function getTodaysCosts(selector) {
    var totals = 0;

    while(selector.hasNext()) {
        var campaign = selector.next();
        var stats = campaign.getStatsFor("TODAY");
        totals += stats.getCost();
    }
      return totals;
}

function pauseCampaigns(selector) {

    while(selector.hasNext()) {
        var campaign = selector.next();
        campaign.pause();
    } 
}

5. Set your account-wide daily maximum budget

  • You will set this in the first line of javascript. Our script has a pre-filled $500 limit.

6. On the bottom right-hand corner of your screen, hit “Save”, and then “Run”. You’ll see a pop-up asking you whether or not you want to preview the script before running. Select “Run Without Preview”.

7. You’ll then see a yellow banner at the bottom of the Google Ad’s window asking you to authorize the script to run, select “Authorize” and follow the steps for authorization.     

8. Select “Scripts” on the left side of your screen to return to the general scripts page. 

9. From this page, you’ll see all of the scripts in the account and can specify how often your scripts will run. Under the “frequency” column for the script you just created, select “hourly”.*

*This script only runs every hour, so it’s still possible to slightly overspend the maximum spend that you set. If your daily spend limit of $500 is hit at 3:15 but the script does not run until 4:00, your ads will continue to run for that 45 minutes.

 Step 3: Regarding the Rule

Now it’s time to set up an automated rule to re-enable all active campaigns the following day.

1.From the bulk actions page, select “Rules”.

2. Press the blue plus sign on the top left side of your screen to add a new rule, then select “campaign” rules.

3. Choose “Enable Campaigns” as your type of rule.

4. Apply to all “enabled or paused campaigns”.

5. Add the condition “Label contains all ‘active’”

6. Set the frequency as daily at 12:00 A.M. using data from the same day. 

7. Now, choose whether or not you want an email notification when the rule runs (we recommend you do! If this rule makes changes to your account, then your account overspent the previous day. This probably warrants some investigation to see if something is wrong). 

8. Finally, name your rule. We went with “Daily Re-Enable”.

And there you have it! A fool-proof daily budget management strategy to keep Google Ads from overspending. If you are more advanced in Google Ads or have access to a development team, there is endless opportunity to manipulate this script so it makes sense for your account. We have found that this script is necessary for all of our accounts. For clients with a limited budget, it allows us to pace it evenly throughout the month and not run out of money towards the end. For clients with larger budgets, it’s a safety net in case a campaign budget is entered incorrectly or there is an error in campaign targeting. At the very least, if you’re utilizing this strategy, you’ll be able to relax knowing that whether it’s due to limited budget, human error, changes to Google Ads’ policy, or an act of God, your campaigns will automatically pause within an hour of reaching the daily limit you set in this script. 

Let us know in the comments if you make any edits to the script to make it work for your Google Ad’s campaigns. Want more practical strategies to optimize your PPC campaigns? Listen to our podcast to hear about what works, what doesn’t, and what has us shaking our heads this week. 

Christine Zirnheld

Christine Zirnheld

Christine is a Digital Marketer at Cypress North, working in paid and organic search marketing as well as digital content creation. She loves the challenge of digital marketing and learning new techniques each day. Christine is eager to continue growing her knowledge base in the field of digital marketing, focusing on analytics and data to discover new insights and best practices. She prides herself in her keen attention to detail, and will stop at nothing to assist clients in surpassing their goals.

See Christine's Most Recent Posts

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search our blog

Start A Project

Categories

What's next?

Well...you might like one of these

Article

[SOLVED] Using an in-memory repository....

.NET Core Data Protection One of the main benefits of...

Read article

Article

Country Ham, Hip Hop & Halloween:...

For your convenience, seasonally-themed puns have been...

Read article

Article

Marketing O’Clock: Google My...

Hey, do you know what time it is? It's...

Read article