var CLOSEPER = 0.8; // enter what you consider "close" to daily budget, e.g. 0.8 = 80% of budget
function main(campList) {
//get all the accounts in the MCC
var accounts = MccApp.accounts().get();
//iterate through the accounts & run the campaign checker
while (accounts.hasNext()) {
var thisAccount = accounts.next();
var accountName = thisAccount.getName();
MccApp.select(thisAccount);
checkCampaigns(accountName);
}
sendEmail(RECEMAIL, campList, accountName);
}
function checkCampaigns(accountName) {
var campaigns = AdWordsApp.campaigns()
.withCondition('Status = ENABLED')
.get();
//initialise the email body as empty
var campList = "";
while (campaigns.hasNext()) {
var thisCampaign = campaigns.next();
var stats = thisCampaign.getStatsFor("YESTERDAY");
var budget = thisCampaign.getBudget();
var spend = stats.getCost();
var name = thisCampaign.getName();
if (spend > (budget * CLOSEPER)) {
Logger.log("Campaign: " + name + " Budget: " + budget + " Spend: " + spend);
campList += "Campaign: " + name + " Budget: " + budget + " Spend: " + spend + "\n";
}
}
if (campList.length != 0) { //only if there are limited Campaigns, send email
var campList = "These Campaigns are close to their Daily Budget \n";
}
return campList;
}
function sendEmail(email, body, accountName) {
var subject = "Account '" + accountName + "' - Campaigns close to Budget";
Logger.log("Sending email, subject: " + subject);
//remove the comment "//" below when testing completed
MailApp.sendEmail(email, subject, body);
}function main() {
var accountIterator = MccApp.accounts().get();
var ssUrl = 'REPLACE_WITH_SPREADSHEET_URL';
var spreadsheet = SpreadsheetApp.openByUrl(ssUrl);
var mccAccount = AdWordsApp.currentAccount();
var i=0;
while (accountIterator.hasNext()) {
var account = accountIterator.next();
// Switch to the account you want to process.
MccApp.select(account);
var report = AdWordsApp.report("Select CampaignName, Clicks, Impressions, Ctr, AverageCpc, Cost, ConversionRate, SearchImpressionShare, SearchBudgetLostImpressionShare, Conversions from CAMPAIGN_PERFORMANCE_REPORT during LAST_MONTH");
var sheet = spreadsheet.getActiveSheet();
//Name sheet by account name
if(i == 0){
spreadsheet.renameActiveSheet("Report "+account.getName());
}
else if(i != 0 && !spreadsheet.getSheetByName("Report "+account.getName())){
spreadsheet.insertSheet("Report "+account.getName());
}
else if(i != 0 && spreadsheet.getSheetByName("Report "+account.getName())){
spreadsheet.setActiveSheet(spreadsheet.getSheets()[i]);
}
report.exportToSheet(spreadsheet.getActiveSheet());
i++;
var range = sheet.getRange("A2:G30");
range.sort(7);
}
MailApp.sendEmail('EMAIL_ADDRESS', 'Budget Report', ssUrl);
}Hi Tyler,
Thank you so much for your time to make this script.
I think we missed one thing here. I dont want the report for all the accounts. I only need reports for accounts which are limited by budget( we have about more than 300 hundred accounts in adwords i dont think it is possible to add 300 + sheets on to a sheet). the script that i got to find the accounts which are limited by budget was given in my very first post on this thread. please have a look. Also i need only the details of enabled campaigns.
Thanks
Sreekanth P R
var report = AdWordsApp.report("Select CampaignName, Clicks, Impressions, Ctr, AverageCpc, Cost, CampaignStatus, ConversionRate, SearchImpressionShare, SearchBudgetLostImpressionShare, Conversions from CAMPAIGN_PERFORMANCE_REPORT where Cost > 100 AND CampaignStatus = ENABLED during LAST_MONTH");