Auto Budget Google Ads

function main() {

var monthlyAccountBudget = 500; //Set in Dollars
var accountName = AdWordsApp.currentAccount().getName();
var campaignIterator = AdWordsApp.campaigns().withCondition("LabelNames CONTAINS_ANY ['live']").get();
var totalCost = 0;

while (campaignIterator.hasNext() ) {
// VARIABLE LIST
var campaign = campaignIterator.next();
var campaignStatus = campaign.isEnabled();
var campaignName = campaignIterator.getName();
var today = new Date();
var monthDay = today.getDate();

// GET INDIVIDUAL CAMPAIGN STATS
totalCost += campaign.getStatsFor("THIS_MONTH").getCost();
Logger.log(campaign.getName() +
"; daily budget=" + campaign.getBudget() +
"; cost this month=" + campaign.getStatsFor("THIS_MONTH").getCost());
Logger.log("total cost=" + totalCost + "; monthly account budget=" + monthlyAccountBudget);

// IS THE CAMPAIGN UNDER BUDGET & PAUSED?
if ( totalCost < monthlyAccountBudget && campaignStatus===false ){

// IS IT THE FIRST OF THE MONTH (US SERVER TIME)
if(monthDay == 1.0) {
Logger.log(campaignName + ' is enabled for new month');
campaign.enable();
}
else {
Logger.log('Not the 1st of month')
}

// ALERT MANAGER OF CAMPAIGN BEING ENABLED
MailApp.sendEmail({
to: '[email protected]',
subject: accountName + ':'+campaignName+' has been paused',
htmlBody: accountName + " Has reached 98% of it\'s monthly budget.",
});

// IS THE CAMPAIGN ALMOST OVER BUDGET?
}else if (totalCost >= monthlyAccountBudget){
Logger.log("Monthly budget reached.");

// IF THE CAMPAIGN IS ENABLED, THEN PAUSE IT
if ( campaignStatus===true ){
campaign.pause();
Logger.log(campaignName + " has been paused.");
}

// ALERT MANAGER OF CAMPAIGN BEING PAUSED
MailApp.sendEmail({
to: '[email protected]',
subject: accountName + ':'+campaignName+' has been paused',
htmlBody: accountName + " Has reached 98% of it\'s monthly budget.",
});
}
}
}

Want a Fast Server Like Mine?

GET VULTR!