October 1, 2017
I had found myself struggling with automated scripts that are supposed to balance my spend – they rarely worked for me because of factors like lower spend during the weekend, holidays, or simple CPC fluctuations. So all I wanted is a report on how much my accounts had spent and I can then input my budgets myself.
This script compiles the budgets from all of the accounts under your MCC, and tells you how much each of your campaigns has spent within each of the accounts. From here, you can sum up the total account spend, or separate it out by campaign to figure out what you should be spending.
What you can change:
Account Labels for Accounts to Include:
.withCondition("LabelNames CONTAINS_IGNORE_CASE 'AccountName'")
var excludeCampaignNameContains = "used"
The script looks at your campaign budgets, and inputs them into your Google Spreadsheet daily, and you can make decisions on how to spend further. This script doesn't actually make any adjustments, but it lays out all of your accounts' MTD Spend in one neat spreadsheet.
The data this script provides is:
function main() {
var SPREADSHEET_URL = "YOUR_SPREADSHEET_URL"
var mccAccount = AdWordsApp.currentAccount();
Logger.log('Current MCC Account Selected');
// Select your accounts using account-level Labels. Enter in your label instead of '2'
var accountIterator = MccApp.accounts()
.withCondition("LabelNames CONTAINS_IGNORE_CASE 'YOUR_LABEL_NAME'")
.orderBy("Name ASC")
.get();
// Date calculations for dateBegin and dateEnd
var today = new Date();
var dd = today.getDate();
var yd = today.getDate()-1;
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(yd<10) {
yd = '0'+yd;
}
if(mm<10) {
mm = '0'+mm;
}
var dateBegin = yyyy + mm + '01';
var dateEnd = yyyy + mm + yd;
//Select which campaigns to include. Leave blank to include all campaigns.
var excludeCampaignNameContains = "used";
Logger.log('Accounts Selected');
// Iterate through the list of accounts
for (i=0; accountIterator.hasNext(); i++) {
Logger.log('Account Iterator Has Next');
var account = accountIterator.next();
var accountName = account.getName() ? account.getName() : '--';
Logger.log('%s,%s,%s,%s', account.getCustomerId(), accountName,
account.getTimeZone(), account.getCurrencyCode());
Logger.log('Iterating...');
// Select the client account.
MccApp.select(account);
Logger.log('Client Account Selected');
// Let's consider the campaigns for this month
var campaignIterator = AdWordsApp.campaigns()
.withCondition("CampaignStatus = ENABLED")
.withCondition('Name DOES_NOT_CONTAIN_IGNORE_CASE "' + excludeCampaignNameContains + '"')
.withCondition('Name DOES_NOT_CONTAIN_IGNORE_CASE "' + excludeCampaignNameContains2 + '"')
.withCondition('Name DOES_NOT_CONTAIN_IGNORE_CASE "' + excludeCampaignNameContains3 + '"')
.orderBy("Name ASC")
.forDateRange("THIS_MONTH")
.get();
// Access Spreadsheet
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1ZxzCK8WdAY9-ckLBx857bBXDG6EGp8ShQsPaUadtuZY/edit#gid=624668883';
// Get the default sheet.
var sheet = spreadsheet.getActiveSheet();
// Write header row.
sheet.getRange("A2").setValue("Campaign");
sheet.getRange("B2").setValue("Cost");
sheet.getRange("C2").setValue("Clicks");
sheet.getRange("D2").setValue("Budget");
if (i == 0) {
for (var row = 3; campaignIterator.hasNext(); row ++) {
var campaign = campaignIterator.next();
var stats = campaign.getStatsFor(dateBegin, dateEnd);
var budget = campaign.getBudget();
sheet.getRange("A1").setValue(account.getName());
sheet.getRange("A" + row).setValue(campaign.getName());
sheet.getRange("B" + row).setValue(stats.getCost());
sheet.getRange("C" + row).setValue(stats.getClicks());
sheet.getRange("D" + row).setValue(budget.getAmount());
}}
else
{
// Write body of report.
for (var row = (i*10)+1; campaignIterator.hasNext(); row ++) {
var campaign = campaignIterator.next();
var stats = campaign.getStatsFor(dateBegin, dateEnd);
var budget = campaign.getBudget();
sheet.getRange("A" + (i * 10)).setValue(account.getName());
sheet.getRange("A" + row).setValue(campaign.getName());
sheet.getRange("B" + row).setValue(stats.getCost());
sheet.getRange("C" + row).setValue(stats.getClicks());
sheet.getRange("D" + row).setValue(budget.getAmount());
}}
Utilities.sleep(200);
Logger.log("Report ready! Visit the following URL to see it:");
Logger.log("https://docs.google.com/spreadsheet/ccc?key=" + spreadsheet.getId());
}}