Google Ads Script to Enforce AdGroup Daily Budget

I signed up a dental client not long ago with an old Google Ads campaign that had been running for a few years. As we go through the optimization of the account, we notice there where a few AdGroups that were converting ok but sometimes these were eating up the entire campaign’s budget. And we wanted the other AdGroups in the same campaign to get budget throughout the day.

We had a few options we discussed to optimize these AdGroups.

One option was to pull out all the AdGroups that were eating up the budget and place them in a separate Campaign to control the daily budget. However, this means we lose the keyword and ad history of the AdGroup. Basically throwing away years of accumulated quality score history which in this particular case had good history.

Another option was to lower the bids of the AdGroup so that it won’t get much traffic. We tried this a little bit but the conversion rate wasn’t as good and the leads weren’t as good either. We returned the bids of the AdGroups and it’s keywords to it’s original amounts and the quality leads came back.

So another option that was discussed was to try limiting the budget of the AdGroups on a daily basis and maintain the bids. So that we can continue to get the leads as well as allow the other AdGroups to get clicks and leads as well pending a more permanent restructuring of the campaign or account.

This lead to the development of this simple script that will allow the Google Ads Manager to set a daily limit for a particular AdGroup, if it reaches the spend, it will turn off the AdGroup for that day and when the day restarts, the script will turn it back on.

This is a super short script that will help you manage daily budgets for AdGroups.

function main(){
  processAdGroup();
}
var agLabel = {
  'AGDaily=25':25,
  'AGDaily=15':15
};

function processAdGroup(){
  var agLabelsArr = Object.keys(agLabel);
  Logger.log(agLabelsArr.join("','"));
  for(agLabelStr in agLabel){
    var adGroupSelector = AdsApp
      .adGroups()
      .withCondition("LabelNames CONTAINS_ANY ['" + agLabelStr + "']");
    var adGroupIterator = adGroupSelector.get();
    while (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      var stats = adGroup.getStatsFor('TODAY');
      if(stats.getCost()>agLabel[agLabelStr]){
        Logger.log(adGroup.getName() + 
                   '(' + agLabelStr + 
                   ') over daily budget - pausing!');
        adGroup.pause();
      }else{
        Logger.log(adGroup.getName() + 
                   '(' + agLabelStr + 
                   ') still under daily budget - enabling!');
        adGroup.enable();
      }
    }
  }
}

The idea of the script is for us to tag the AdGroups using labels with what daily budget limits we want to enforce.

On line 5 and 6, these are the labels we’ll be using to attach to AdGroups, it basically means Ad Group Daily budget of 25(Label: AGDaily=25) and Ad Group Daily budget of 15(Label: AGDaily=15) you can add as many as you need. The number beside the label is the actual number the script will use for comparing the total spend of the AdGroup.

Explanation of the Google Ads Script Execution

The script will load all the AdGroups attached to each label. Remember: if you add the label on line 5 and 6 you need to make sure you create the label inside your Google Ads account otherwise the script will fail.

It first loads up the list of labels and loops through it. As it loops through it, it will load AdGroups associated to the label and compare the AdGroups total cost for the day, if it exceeds it will pause the AdGroup. If it hasn’t exceeded it will enable the AdGroup. Enabling it everything becomes useful when the account is pass midnight. The stats will return the day’s cost as $0 and it will enable the AdGroup.

One thing to note, you would probably want to schedule this script hourly so it’ll continue to work throughout the day.

Now the amount is actually applied to one AdGroup, meaning if you attach the label AGLabel=25 to 3 AdGroups, each AdGroup needs to exceed $25 before it gets paused. It will not sum up the total and compare it to the limit. BUT the next script will…

Google Ads Script to Enforce Daily Budget Limit on a Group of AdGroups

This script essential functions the same way as the original script above but this time it treats the label as a grouping mechanism. It will sum up the AdGroups with the same label and see if it exceeds the limit, if it does. It will pause the group of AdGroups. The opposite is also true, past midnight if the total spend of the AdGroups is below the limit the script will reenable the AdGroups so it can run again.

function main(){
  processAdGroup();
}
var agLabel = {
  'AGDaily=25':25,
  'AGDaily=15':15
};

function processAdGroup(){
  var agLabelsArr = Object.keys(agLabel);
  Logger.log(agLabelsArr.join("','"));
  for(agLabelStr in agLabel){
    var adGroupSelector = AdsApp
      .adGroups()
      .withCondition("LabelNames CONTAINS_ANY ['" + agLabelStr + "']");
    var adGroupIterator = adGroupSelector.get();
    var totalSpend = 0;
    var adGroups = [];
    while (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      var stats = adGroup.getStatsFor('TODAY');
      totalSpend += stats.getCost();
      adGroups.push(adGroup);
    }
    if(totalSpend>agLabel[agLabelStr]){
      for(i=0;i<adGroups.length;i++){
        Logger.log(adGroups[i].getName() + 
                   '(' + agLabelStr + 
                   ') over daily budget - pausing!');
        adGroups[i].pause();
      }
    }else{
      for(i=0;i<adGroups.length;i++){
        Logger.log(adGroups[i].getName() + 
                   '(' + agLabelStr + 
                   ') still under daily budget - enabling!');
        adGroups[i].enable();
      }
    }
  }
}