Script to fetch budget amounts

375 views
Skip to first unread message

Jake

unread,
Jun 29, 2018, 9:35:48 AM6/29/18
to AdWords Scripts Forum
Hello, 

I am trying to get today's and yesterday's budget amounts.

I would like to run a script on the MCC level (that can be filtered by labels) that pulls the account level
  1. Account Name
  2. Account Total active budget,
  3. And (if possible) if the account is using a shared budget then also export the Budget Name and Bid strategy type, or just a yes/no indication that it is using a shared budget.
And exports the information into a google sheet. Every time the script runs, I would like it to replace the current information in the same google sheet.

Likewise, I would like to pull similar information, but on the campaign level. So a second MCC script that would get:
  1. Account Name,
  2. Campaign Name (for all enabled)
  3. Campaign budget amount,
  4. Status (examples: Limited by budget, Eligible (Learning), Eligible)
  5. Budget name,
  6. Bid strategy type
And exports the information into a google sheet. Every time the script runs, I would like it to replace the current information in the same google sheet.

Lastly, is it possible to add a Total Active Daily Account Budget to the Account Summary Report?

Appreciate your time and feedback. Thanks.

Anthony Madrigal

unread,
Jun 29, 2018, 2:53:17 PM6/29/18
to AdWords Scripts Forum
Hi Jake,

You can learn about MCC scripts with the help of our guides. The Scripts documentation also has resources for budget operations and working with spreadsheets. The function, isExplicitlyShared, can tell you if a budget is shared.

The code below is an example of how to iterate through accounts and log the account budget total:

function main() {
 
 
var accountSelector = MccApp.accounts();
 
var accountIterator = accountSelector.get();

 
// Keep track of the MCC account for future reference.
 
var mccAccount = AdWordsApp.currentAccount();

 
// Iterate through the list of accounts
 
while (accountIterator.hasNext()) {
   
var account = accountIterator.next();
   
// Select the client account.
   
MccApp.select(account);
   
// Select campaigns under the client account
   
var campaignIterator = AdWordsApp.campaigns().get();
   
var accountBudget = 0;
   
while(campaignIterator.hasNext()) {
     
var campaign = campaignIterator.next();
     accountBudget
+= campaign.getBudget().getAmount();
 
   
}
 
Logger.log("The budget for account " + account.getName() + " is " + accountBudget);
 
}
 
// Switch back to MCC account
 
MccApp.select(mccAccount);
}


The above script can be retooled for your second set of questions
while(campaignIterator.hasNext()) {
   
 
var campaign = campaignIterator.next();
 
//For example:
 
var campaignName = campaign.getName(); //2
 
var campaignBudget = campaign.getBudget().getAmount(); //3
 
var campaignBudgetName = campaignBudget.getName(); //5
 
var bidStrategy = campaign.bidding().getStrategy().getType(); //6
}


Lastly, you can add the active daily budget to the account summary report. Please see the 'Extending the Report' section of this page.

Regards,
Anthony
AdWords Scripts Team

Jake

unread,
Jul 2, 2018, 4:49:23 PM7/2/18
to AdWords Scripts Forum
Hi Anthony, thanks for that.
I got it to work a little but I am still having trouble getting the campaignBudgetName and bidStrategy.
I have attached my script for you review. I have commented out the campaignBudgetName and bidStrategy because it breaks when I try to work with them.
Let me know if you have any ideas on how to get them to work.
Thanks
code01.txt
Capture01.PNG

Anthony Madrigal

unread,
Jul 3, 2018, 3:50:58 PM7/3/18
to AdWords Scripts Forum
Hi Jake,

I've updated your script so that it should handle scenarios in which campaigns don't have bid strategy (bidStrategy = null).
function main() {
 
 
var spreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/*******");
 
var sheet4 = spreadsheet.getSheets()[4];
  sheet4
.clear();
  sheet4
.setName("OPMC");
  sheet4
.appendRow(["Account", "Campaign", "Budget", "Cost Today", "Cost Yesterday", "Cost M2D"]);
 
 
var accountSelector = MccApp.accounts()
 
// .withIds(['123-456-7899']);
 
var accountIterator = accountSelector
 
//.withCondition("LabelNames CONTAINS 'Active'")

 
.get();
 
 
// Keep track of the MCC account for future reference.
 
var mccAccount = AdWordsApp.currentAccount();
 
 
// Iterate through the list of accounts
 
while (accountIterator.hasNext()) {
   
var account = accountIterator.next();
   
// Select the client account.
   
MccApp.select(account);
   
// Select campaigns under the client account
   
var campaignIterator = AdWordsApp.campaigns()

   
.withCondition("CampaignStatus = ENABLED")
   
.withCondition("Status = ENABLED")
   
//.withCondition("LabelNames CONTAINS_ANY ['Competitors', 'Brand', 'Discovery']")
   
//.withCondition("CampaignName CONTAINS_IGNORE_CASE ''")
   
//.withCondition("IsBudgetExplicitlyShared = false")
   
.get();
   
   
//var accountBudget = 0;

   
while(campaignIterator.hasNext()) {
     
var campaign = campaignIterator.next();

     
//accountBudget += campaign.getBudget().getAmount();

     
     
     
var campaignName = campaign.getName(); //2
     
var campaignBudget = campaign.getBudget().getAmount(); //3

     
var campaignBudgetName = campaign.getBudget().getName(); //5
     
var bidStrategy = campaign.bidding().getStrategy(); //6
     
if(bidStrategy != null)
        bidStrategy
= bidStrategy.getName();
     
var stats1 = campaign.getStatsFor('TODAY');
     
var stats2 = campaign.getStatsFor('YESTERDAY');
     
var stats3 = campaign.getStatsFor('THIS_MONTH');
      sheet4
.appendRow([account.getName(), campaign.getName(), campaign.getBudget().getAmount(), stats1.getCost(), stats2.getCost(), stats3.getCost() ]);

   
}
   
   
// Switch back to MCC account
   
MccApp.select(mccAccount);  
 
}
}

If you face any issues, please reply privately to author your CID and script name so I can take a look.

Cheers,
Anthony
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages