Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adhummer/c73c92358487bd59d9631f7ca5ea0ad2 to your computer and use it in GitHub Desktop.
Save adhummer/c73c92358487bd59d9631f7ca5ea0ad2 to your computer and use it in GitHub Desktop.
function main() {
var date_range = 'THIS_MONTH';
//Change this variable to whatever your account level monthly budget is to be.
//If you have a hard limit of total Cost in a month of $1,000.00
//then you would change the value to 1000.
var total_monthly_budget = '1000';
var currentAccount = AdWordsApp.currentAccount();
var stats = currentAccount.getStatsFor(date_range);
var accountName = currentAccount.getName();
Logger.log('Cost this month to date: $' + stats.getCost());
Logger.log('Total Monthly Budget: $' + total_monthly_budget);
if(currentAccount.getStatsFor(date_range).getCost() >= total_monthly_budget)
{
var campaignIterator = AdWordsApp.campaigns()
.withCondition('Status = "ACTIVE"')
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause(); Logger.log("Campaigns Paused.");
}
//Change the email address to the email address that you want to get the email alert at.
//Separate multiple emails with a comma.
MailApp.sendEmail("sr@stevenruesch.de",
currentAccount.getName() +"Monthly Budget Met Campaigns Paused", currentAccount.getName() + " campaigns have been paused due to reaching the monthly budget. Affected campaigns will be restarted at the beginning of the next month. This is an auto generated email from the Adwords Script in the "
+ currentAccount.getName() + " account.");
}
if(currentAccount.getStatsFor(date_range).getCost() < total_monthly_budget)
{
var campaignIterator = AdWordsApp.campaigns()
.withCondition('LabelNames CONTAINS_ANY ["campaign#HardCap"]')
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.enable(); Logger.log("Campaign Enabled");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment