This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. Finally working (out of the box) code!