Simple script that can be used to pause any Campaign or Campaigns that contain some string of text in their name when they surpass a budget cap in multiple accounts of an MCC. Similar to the Single Campaign Budget Cap script, but this can be used to aggregate spend for some set of campaigns (or all campaigns) across multiple accounts within an MCC and pause those campaigns if/when they surpass the allotted budget for the period.
Great for one-off campaigns for specific trips, shows, promotions, etc.
No Google Sheet needed here – just the script, and you can set this up in the account as many times as you'd like!
To get started, go to your Google Ads MCC, click Tools > Bulk Actions > Scripts
You'll be creating one new scripts. The instructions are below!
Create a new script and name it "<ACCOUNTS> <CAMPAIGNS> | Budget Cap Script" (or whatever you want)
Copy and paste the script below.
Change the ENTER_BUDGET_HERE to whatever you want the total spend of these campaigns to be for the selected date range. If you don't want to have this check the lifetime spend, you can quickly change the ALL_TIME in the script below to one of the following values:
TODAY, YESTERDAY, LAST_7_DAYS, THIS_WEEK_SUN_TODAY, LAST_WEEK, LAST_14_DAYS, LAST_30_DAYS, LAST_BUSINESS_WEEK, LAST_WEEK_SUN_SAT, THIS_MONTH, LAST_MONTH, ALL_TIME
Or enter custom start and end dates by entering dates in the format: .getStatsFor('YYYYMMDD', 'YYYYMMDD')
If you need this to only run on specific accounts, you can change ACCOUNT_NAME_1 and ACCOUNT_NAME_2 to whatever Account Names you need. You can also add more if needed. If you just want this to run on all accounts in an MCC, just delete the .withCondition so you end up with just: var accountIterator = MccApp.accounts().get();
Change the CAMPAIGN_NAME_FRAGMENT to some string of text that would apply to all the campaigns you want to pause when they hit the budget cap. Same note as above if you want to just get spend for all campaigns.
After changing the ENTER_BUDGET_HERE to your Campaign budget, updating the date ranges, and setting the Campaign and Account name options, you're all set! Hit Save and Run. Authorize anything needed and Run again.
Set the Frequency of this script to "Hourly"
It might be a good idea to disable this script after the campaign is over, since if you set up a Campaign with a similar name at any point in the future, it may be paused by this script prematurely.
This script is not set up to email you when the campaigns are paused. So just be sure that it's working properly!
function main() {
//***BEGIN CUSTOM VARIABLES***///
//update the value of customBudget based on your requirement
var customBudget = ENTER_BUDGET_HERE;
//you can use startDate and endDate if you want custom dates. but if you just want to have this work on a rolling monthly basis, you can change
//the line of code: campaignSelect.getStatsFor('20210101', endDate); to: campaignSelect.getStatsFor('THIS_MONTH'); to just always retrieve
//data for MtD.
var startDate = '20210101';
//if you're using custom start & end dates, you can manually set the end date by un-commenting the second 'var endDate' line and adding '//'
//in front of the next two variables. otherwise, the default below is to have the endDate equal to today's date. you can manually enter a
//future date if desired.
var now = new Date();
var endDate = Utilities.formatDate(now, "GMT-5", "yyyyMMdd");
//var endDate = '20211231';
//***END CUSTOM VARIABLES***///
var accountIterator = MccApp.accounts().withCondition("Name IN ['ACCOUNT_NAME_1', 'ACCOUNT_NAME_2']").get();
var campaignsList = [];
var totalCost = 0;
while (accountIterator.hasNext()) {
var account = accountIterator.next();
AdsManagerApp.select(account);
var campaignIterator = AdWordsApp.campaigns()
.withCondition("Name CONTAINS 'CAMPAIGN_NAME_FRAGMENT'")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
//save in campaignsList the list of campaigns object.
campaignsList.push(campaign);
}
}
for (var i = 0; i < campaignsList.length; i++) {
//use THIS_MONTH to get data for all days in the current month
var campaignSelect = campaignsList[i];
var stats = campaignSelect.getStatsFor(startDate, endDate);
var campaignCost = stats.getCost();
totalCost += campaignCost;
}
//if totalCost of combined campaigns is equal to defined customBudget, pause the campaigns. otherwise log no changes.
Logger.log('Total Spend To-Date: '+totalCost);
if (totalCost >= customBudget){
for (var i = 0; i < campaignsList.length; i++) {
var campaign = campaignsList[i];
Logger.log('Campaign "'+campaign.getName()+'" has been paused.')
campaign.pause();
}
}
else{
Logger.log('Spend of '+totalCost+' is less than alotted budget of '+customBudget+'. No changes have been made.');
}
}