Skip to content

Instantly share code, notes, and snippets.

@g-ads-org
Last active August 18, 2020 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g-ads-org/90ca18af7cecaaca874ef689f451882f to your computer and use it in GitHub Desktop.
Save g-ads-org/90ca18af7cecaaca874ef689f451882f to your computer and use it in GitHub Desktop.
function main() {
var accountBudgetLimit = 80; // set your account budget limit here
var accountCampaignsCost = 0;
var Campaign_List = "['Women Sport Shoes', 'Men Sport Shoes']";
var Email_Address = "youremail@example.com";
var campaigns = AdWordsApp.campaigns()
.withCondition("Name IN " + Campaign_List)
.get();
while (campaigns.hasNext()) {
var campaign = campaigns.next();
var stats = campaign.getStatsFor("ALL_TIME"); // gets stats for ALL TIME, you may change the date range based on your requirment. Valid values: TODAY, YESTERDAY, LAST_7_DAYS, THIS_WEEK_SUN_TODAY, THIS_WEEK_MON_TODAY, LAST_WEEK, LAST_14_DAYS, LAST_30_DAYS, LAST_WEEK, LAST_BUSINESS_WEEK, LAST_WEEK_SUN_SAT, THIS_MONTH, LAST_MONTH, ALL_TIME
accountCampaignsCost += stats.getCost(); // gets the cost of the campaign
Logger.log(campaign.getName() + " = " + stats.getCost());
}
Logger.log("sum = " + accountCampaignsCost);
if (accountCampaignsCost >= accountBudgetLimit) { // If the total costs of the campaign has reached or exceeded the specified limit, pause the camapaigns
Logger.log("Budget has exceeded the limit!");
var campaignIterator = AdWordsApp.campaigns()
.withCondition("Status = ENABLED")
.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause();
Logger.log("Pausing campaign " + campaign.getName() + "...");
}
MailApp.sendEmail(Email_Address,
'ِYour Campaigns paused!',
'Your campaigns reached the specified budget and thus campaigns were stopped.');
} else if (accountCampaignsCost >= (accountBudgetLimit * 0.9)) { // Checks if the total cost of the campaigns has reached 90% of the specified limit
MailApp.sendEmail(Email_Address,
'Your advertising budget is running out!',
'More than 90% of the specified budget is spent on your account.'); // notifies you with an email when total cost of the campaigns has reached 90% of the specified limit.
} else {
Logger.log("Total spending of campaigns has not yet reached the account budget limit."); }
}
@starterfirm
Copy link

Thanks for sharing. Finally working (out of the box) code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment