Google Ads Script – Monthly Budget Target Email Alert

Do you manage accounts that have a monthly budget target?

Many of us do.

And many of the questions that land in my inbox these days are about old Google Ads scripts for budget management that don’t work anymore.

Here’s the issue:
Most of the old scripts use campaign selectors to aggregate spend over the different campaigns in the account. These don’t work for the new campaign types!

Local, Discovery and Performance Max campaign cost data is not accessible via campaign selectors.

This means that, unknowingly to you, your account may be overspending your monthly budget.  Or even worse, your client’s budget. (Can you hear the phone ringing already?)

You could go into your account every day and manually check the spend of course. You could even do that every hour…ughh…

But wait! It doesn’t have to be that way.

You can still sleep comfortably, knowing that your ad spend is within range, without wasting your time on endless checks.

If you want to make sure your account is not overspending its budget, here’s a script you can use.

You should schedule it to run hourly (see instructions).

NOTE: 
You still need to manually pause the campaigns once you receive the alert. Book a call if you want to learn how to automate that part as well.

 

INSTRUCTIONS:

  1. See the script code below. Install the script in your account.
    Don’t worry if you have never done this before. You do not need any coding skills. It is as simple as copy-paste. Simply follow these instructions on how to set up and schedule google ads scripts.
  2. Add your the total monthly budget for your account (line 16)
  3. Add your email address to the script (line 17)
  4. Authorize and Preview 
  5. Schedule to run hourly

 


/*** 
*
* Monthly Budget Target - Email Alert
*
* This script compares this month's ad spend to your monthly budget target
* In case the ad spend is greater than your budget target an alert is sent via email
*
* @author: Nils Rooijmans
*
* Version 1.0
*
* contact nils@nilsrooijmans.com for questions and a High Performance MCC version of the script for your manager account
*/


var MONTHLY_BUDGET = 1000; // set your total monthly account budget target
var EMAIL_ADDRESS = "youremail@address.com"; // insert your email address between the quotes
var EMAIL_SUBJECT = "[GAds Script Alert] Monthly budget depleted for account: ";
var EMAIL_BODY = 
    "\n\n"+
    "You probably want to pause some campaigns.\n" +
    "---\n" + 
    "This email is generated by a copy of the free Google Ads Script: \"Monthly Budget Target - Email Alert\", (C) Nils Rooijmans \n" +
    "For more FREE Google Ads Scripts to improve your results and make your working day feel like a breeze, visit https://nilsrooijmans.com \n" + 
    "---\n";


function main() {

  var query =
      "SELECT " +
      "metrics.cost_micros " +
      "FROM customer "+
      "WHERE segments.date DURING THIS_MONTH";

  try {
    var result = AdsApp.search(query);

    while (result.hasNext()) {
      var row = result.next();
      var adSpend = convertMicrosToCurrency(row.metrics.costMicros, true);
      Logger.log("Total Adspend: "+adSpend);
    }
  } catch (e) {
   Logger.log("### ERROR: "+e);
  }

  if (adSpend>MONTHLY_BUDGET) {
    Logger.log("Total spend this month: "+adSpend+"\n ---> You probably want to pause some campaigns.");
    MailApp.sendEmail(EMAIL_ADDRESS,EMAIL_SUBJECT+AdsApp.currentAccount().getName(),"Total spend this month: "+adSpend+EMAIL_BODY );
  }
}


function convertMicrosToCurrency(micros, rounded) {
  if (rounded == true) {
    return Number(Math.round(micros/1000000+'e2')+'e-2'); // returns currency value with 2 decimals
  } else if (rounded == false)  {
    return micros/1000000; // returns currency value without any rounding
  }
}

 

NOTE: 
You still need to manually pause the campaigns once you receive the alert. Book a call if you want to learn how to automate that part as well.