Skip to content

Instantly share code, notes, and snippets.

@g-ads-org
Last active March 4, 2019 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-ads-org/978c59bbfccfdde872e928b4f8c2cb5a to your computer and use it in GitHub Desktop.
Save g-ads-org/978c59bbfccfdde872e928b4f8c2cb5a to your computer and use it in GitHub Desktop.
/**
*
* Stable Daily Budget Script
*
* If scheduled hourly, this script will pause campaigns if they have
* spent more than their daily budget and reactivate them the next day.
*
* Version: 1.0
* Google AdWords Script maintained by Frederic Harnois
*
**/
function main() {
// Selects campaigns with a chosen label
// Insures only those campaigns are activated or paused
var campaignSelector = AdWordsApp
.campaigns()
// Define your own label here
// IMPORTANT NOTE: LABEL IS CASE SENSITIVE
// .withCondition("LabelNames CONTAINS_ANY ['Active']")
// Goes through the selected campaigns
var campaignIterator = campaignSelector.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
// Gets the campaign's budget
var budget = campaign.getBudget();
// Gets the campaign's spend over the current day
var stats = campaign.getStatsFor("TODAY");
var cost = stats.getCost()
Logger.log("Campaign name: " + campaign.getName());
Logger.log("Daily budget: " + budget.getAmount());
Logger.log("Today's spend: " + cost);
// Pauses the campaign if the spend exceeds the budget
if (cost > budget.getAmount()){
campaign.pause();
Logger.log("Today's campaign spend EXCEEDS the daily maximum!");
Logger.log("Campaign paused");
Logger.log("================");
}
// Activates or leaves the campaign on if the spend spend does not exceed the budget
else if (cost < budget.getAmount()){
campaign.enable();
Logger.log("Today's campaign spend DOES NOT exceed the daily maximum.");
Logger.log("Campaign remains enabled");
Logger.log("================");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment