campaign.getBudget().getAmount() // instead of this you should rather fetch the cost for some period
var CUTOFF_COST = 1125;
var CUTOFF_LABEL = "Total Cost Cutoff";
function main() {
if (AdWordsApp.currentAccount().getStatsFor("THIS_MONTH").getCost() > CUTOFF_COST) {
pauseCampaign();
pauseVideoCampaign();
}
}
function pauseCampaign() {
var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get().next();
var campaignIterator = label.campaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
Logger.log(campaign.getName());
campaign.pause();
}
}
function pauseVideoCampaign() {
var videoCampaignIterator = AdWordsApp.videoCampaigns()
.withCondition("LabelNames CONTAINS_ANY ['" + CUTOFF_LABEL +"']")
.get();
while (videoCampaignIterator.hasNext()) {
var videoCampaign = videoCampaignIterator.next();
Logger.log(videoCampaign.getName());
videoCampaign.pause();
/*
Pause search and regular display campaigns when cost exceed a predefined threshold.Send email notification when campaigns are paused.
Schedule script to run hourly.
*** Provided "as is" Use at your own peril. No warranty of any kind You are welcome to use and share this code in it's entirety, including the warning above
sigurd
*/
var settings = { timePeriod: 'LAST_30_DAYS', // more date range options here http://tiny.cc/10d6hz threshold: 500, // change to whatever amount notify: 'em...@example.com', // Optional. Get notifications when script pauses campaigns notifyCc: 'anothe...@example.com', // Optional. Also notify this email }
function main() { var account = AdsApp.currentAccount(); var cost = account.getStatsFor(settings.timePeriod).getCost(); Logger.log('Threshold is set to: ' + settings.threshold + ', cost so far: ' + Math.round(cost) + ' during: ' + settings.timePeriod + '\n\n*\n\n') if (cost > settings.threshold) { var campaignNames = []; var accountName = account.getName(); var accountId = account.getCustomerId() var campaignIter = AdsApp.campaigns().get(); // this will not fetch e.g. shopping and app campaigns while (campaignIter.hasNext()) { var campaign = campaignIter.next(); var campaignName = campaign.getName(); campaignNames.push(campaignName); var campaignCost = campaign.getStatsFor(settings.timePeriod).getCost(); campaign.pause() Logger.log(campaignName + ' Paused. Campaign cost: ' + campaignCost) } } else if (cost <= settings.threshold){ Logger.log('Cost is below threshold so won\'t do anything this time. There\'re still ' + Math.round(settings.threshold-cost) + ' left to spend') } else {Logger.log('This shouldn\'t happen .. check your dates and threshold variables')} if(campaignNames.length > 0 && typeof(settings.notify) === 'string' && settings.notify.indexOf('@') !== -1){ MailApp.sendEmail(settings.notify,campaignNames.length + ' campaigns in account: ' + accountId + ' have been paused by a script', 'Hi,\n\nA Google Ads script running in the Google Ads account "' + accountName + '" has paused ' + campaignNames.length + ' campaigns because costs have exceeded the threshold.\n\nCost threshold is set to: ' + settings.threshold + ' but the account has accrued costs of: ' + Math.round(cost) + '\n\n*\n\nThese campaigns are now paused:\n\n' + campaignNames.join('\n'),{cc:settings.cc} ) Logger.log('\n\n*\n\nEmail sent to: ' + settings.notify + ', cc: ' + settings.cc) }}