Next up in our series of quick scripts we’re creating for free following requests is a simple script to pause your campaigns if they spend over a certain threshold of your budget, and then re-enable them the next day.
A few years ago Google changed the over-delivery threshold from 20% to 100%, and while this is fine for most advertisers there are often cases where this isn’t ideal. A mid-sized agency based in Denver asked us to build this for them, and since it was only a few lines of code – here it is an hour later!
The script only has two parameters. The over- delivery threshold, which is the most additional budget that can be spent – 20% means that a campaign with a budget of $100 will be paused once it’s spent $120. The second parameter is only needed if running the script in an MCC – and indicates the accounts within that MCC on which the script should run.
The script must be set up to run hourly in order to function properly. Any less than that, and it either won’t pause overspending campaigns, or it won’t re-enable them after they’ve been paused.
On that note, it’s also important to remember that the script can only run once an hour, so it’s not guaranteed to pause campaigns just as they high 20% – in fact, it’s likely they’ll be paused slightly after this threshold is reached.
The script knows which campaigns to enable the next morning based on labels, so please don’t remove the ‘Paused – Budget Overdelivery’ label that the script will apply – it’ll remove the label on its own once the new day starts and its time to re-enable the campaign.
As always, and as with any script, ensure you understand what it’s doing before applying it, and preview it before running to ensure the changes its making are as you’d expect.
/*
_ ___ _ _ _____ ___ _
/_\ / __| | | |_ _/ _ \ (_)___
/ _ \ (__| |_| | | || (_) || / _ \
/_/ \_\___|\___/ |_| \___(_)_\___/
*/
config = {
overdeliveryThreshold: '20%', // The maximum over budget the campaign can go before it gets paused
accounts: ['494-823-7580'] // The accounts in this MCC the script will run on, ignore if using on a single account
}
function main() {
try {
AdsManagerApp.accounts().withIds(config.accounts).executeInParallel('pauseOverspendingCampaigns');
} catch(e) {
pauseOverspendingCampaigns();
}
}
function pauseOverspendingCampaigns() {
var labelName = 'Paused - Budget Overdelivery';
// Unpause any campaigns that were previously paused
var date = Utilities.formatDate(new Date(), AdsApp.currentAccount().getTimeZone(), 'MMMM dd, yyyy HH:mm:ss Z');
var hour = parseFloat(date.split(' ')[3].substring(0, 2));
if (hour < 3) {
var campaigns = AdsApp.campaigns().withCondition('LabelNames CONTAINS_ANY ["' + labelName +'"]').get();
while (campaigns.hasNext()) {
var campaign = campaigns.next();
campaign.removeLabel(labelName);
campaign.enable();
}
}
// If the label already doesn't exist, create it
if (AdsApp.labels().withCondition('Name = "' + labelName + '"').get().totalNumEntities() === 0) AdsApp.createLabel(labelName);
// Iterate through campaigns and check for any that are overspending
var campaigns = AdsApp.campaigns().forDateRange('TODAY').withCondition('Cost > 0 AND Status = ENABLED').get();
while (campaigns.hasNext()) {
var campaign = campaigns.next();
var budget = campaign.getBudget().getAmount();
var cost = campaign.getStatsFor('TODAY').getCost();
var threshold = (parseFloat(config.overdeliveryThreshold.replace('%', '')) / 100) + 1;
var maxSpend = budget * threshold;
if (cost > maxSpend) {
campaign.applyLabel(labelName);
campaign.pause();
}
}
}