Script to pause all campaigns based on daily budget total set

683 views
Skip to first unread message

George LCS

unread,
Oct 7, 2020, 8:24:15 PM10/7/20
to Google Ads Scripts Forum
Hi Guys, i got this monthly script from this forum, it works a treat.

Can someone help me create a similar script that looks at all campaigns total for the day, or daily budget?

So for the same day (today) if all the campaigns total spend is > XX, i want to then pause all active campaigns..

This is the script that works well for Monthly total 

>>>>>>>>>>>>

function main() {
  /***
    This script will auto-pause campaigns when the account exceeds its monthly budget.
    No more daily monitoring of account budgets!
  ***/
 
  Logger.log("**********************");
  Logger.log("Running BUDGET - Pause All Campaigns script...");
  Logger.log("**********************");
 
  // THIS AMOUNT WILL VARY CLIENT BY CLIENT
  // MAKE SURE IT IS CORRECT
  var monthlyBudget = 500;



  var totalCost = 0;
  var campaignsList = [];

 
  var campaignIterator = AdWordsApp.campaigns().get();

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

   
    // Save each current campaign into an array for ease of iteration
    campaignsList.push(campaign);
   
    // Use THIS_MONTH to get data for all days in the current month

    var stats = campaign.getStatsFor('THIS_MONTH');
    var campaignCost = stats.getCost();
    totalCost += campaignCost;
  }

 
  Logger.log("Account cost for this month: " + totalCost);
 
  // If totalCost of combined campaigns exceeds defined monthlyBudget, pause all!
  if (totalCost >= monthlyBudget){
    Logger.log("Monthly budget met or exceeded. Auto-pausing all campaigns!");

   
    for (var i = 0; i < campaignsList.length; i++) {
      var campaign = campaignsList[i];

      Logger.log("Pausing campaign: " + campaign.getName());
      campaign.pause();
    }
  } else {
    Logger.log("Total monthly cost currently under budget.");
    Logger.log("Monthly Cost: " + totalCost);
    Logger.log("Monthly Budget: " + monthlyBudget);
  }
}function main() {
  /***
    This script will auto-pause campaigns when the account exceeds its monthly budget.
    No more daily monitoring of account budgets!
  ***/
 
  Logger.log("**********************");
  Logger.log("Running BUDGET - Pause All Campaigns script...");
  Logger.log("**********************");
 
  // THIS AMOUNT WILL VARY CLIENT BY CLIENT
  // MAKE SURE IT IS CORRECT
  var monthlyBudget = 500;



  var totalCost = 0;
  var campaignsList = [];

 
  var campaignIterator = AdWordsApp.campaigns().get();

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

   
    // Save each current campaign into an array for ease of iteration
    campaignsList.push(campaign);
   
    // Use THIS_MONTH to get data for all days in the current month

    var stats = campaign.getStatsFor('THIS_MONTH');
    var campaignCost = stats.getCost();
    totalCost += campaignCost;
  }

 
  Logger.log("Account cost for this month: " + totalCost);
 
  // If totalCost of combined campaigns exceeds defined monthlyBudget, pause all!
  if (totalCost >= monthlyBudget){
    Logger.log("Monthly budget met or exceeded. Auto-pausing all campaigns!");

   
    for (var i = 0; i < campaignsList.length; i++) {
      var campaign = campaignsList[i];

      Logger.log("Pausing campaign: " + campaign.getName());
      campaign.pause();
    }
  } else {
    Logger.log("Total monthly cost currently under budget.");
    Logger.log("Monthly Cost: " + totalCost);
    Logger.log("Monthly Budget: " + monthlyBudget);
  }
}function main() {
  /***
    This script will auto-pause campaigns when the account exceeds its monthly budget.
    No more daily monitoring of account budgets!
  ***/
 
  Logger.log("**********************");
  Logger.log("Running BUDGET - Pause All Campaigns script...");
  Logger.log("**********************");
 
  // THIS AMOUNT WILL VARY CLIENT BY CLIENT
  // MAKE SURE IT IS CORRECT
  var monthlyBudget = 500;



  var totalCost = 0;
  var campaignsList = [];

 
  var campaignIterator = AdWordsApp.campaigns().get();

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

   
    // Save each current campaign into an array for ease of iteration
    campaignsList.push(campaign);
   
    // Use THIS_MONTH to get data for all days in the current month

    var stats = campaign.getStatsFor('THIS_MONTH');
    var campaignCost = stats.getCost();
    totalCost += campaignCost;
  }

 
  Logger.log("Account cost for this month: " + totalCost);
 
  // If totalCost of combined campaigns exceeds defined monthlyBudget, pause all!
  if (totalCost >= monthlyBudget){
    Logger.log("Monthly budget met or exceeded. Auto-pausing all campaigns!");

   
    for (var i = 0; i < campaignsList.length; i++) {
      var campaign = campaignsList[i];

      Logger.log("Pausing campaign: " + campaign.getName());
      campaign.pause();
    }
  } else {
    Logger.log("Total monthly cost currently under budget.");
    Logger.log("Monthly Cost: " + totalCost);
    Logger.log("Monthly Budget: " + monthlyBudget);
  }
}

>>>>>>>>>>>>>>>

ta
George

Google Ads Scripts Forum Advisor

unread,
Oct 8, 2020, 4:49:28 AM10/8/20
to adwords...@googlegroups.com
Hi George,

Thank you for reaching out to us. Allow me to assist you in this.

With regard to your concern, you need to do the following steps to recreate the script based on your use case.
  1. Create a variable named dailyBudget which will be used for comparing the cost of your campaigns in a daily basis
  2. Create a campaign iterator
  3. Inside the campaign iterator while loop, you need to get the cost of each campaigns. To do this, use the getStatsFor() method
  4. Create an if statement that will compare the dailyBudget and stats.getCost(), then pause the campaign if the condition has been met using pause() function
I've created a script following the steps above. Kindly test it on your end, then let me know how it goes.

function main() {

  
  Logger.log("**********************");
  Logger.log("Running BUDGET - Pause All Campaigns script...");
  Logger.log("**********************");
 
  // THIS AMOUNT WILL VARY CLIENT BY CLIENT
  // MAKE SURE IT IS CORRECT
  var dailyBudget = 0;

  
  var campaignIterator = AdWordsApp.campaigns().get();
  
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
   
    //Use TODAY to get data on the date today
    var stats = campaign.getStatsFor('TODAY');
    
    var campaignCost = stats.getCost();
    
    Logger.log("=================================");
    Logger.log("Campaign: " + campaign.getName());
    Logger.log("Cost: " + campaignCost);
    
    
    //COMPARE current campaignCost and dailyBuddget variable
    if (campaignCost >= dailyBudget){
      
      Logger.log("Daily budget reach! Pausing campaign");
      campaign.pause();
      
    }
    
    Logger.log("=================================");
    
  }
  
}


Regards,
Google Logo
Teejay Wennie Pimentel
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q267eW3:ref

George - Low Cost SEO

unread,
Oct 8, 2020, 8:06:29 PM10/8/20
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor

Hi Teejay,

 

Thank you for helping !

 

Almost..  Its pausing individual campaigns that are over $X.  For example I set X to be $5..  and it paused the remaketing campaign..  What I wanted it to do is when the campaign total for today is over X ($5) pause all campaigns.  See here for what I mean..

 

 

 

 

Regards

George

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/t8P4mqsO50M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/rHm2U000000000000000000000000000000000000000000000QHVKI400KO-BABUXQ4KWej4aCy8qJg%40sfdc.net.

image001.png

Google Ads Scripts Forum Advisor

unread,
Oct 9, 2020, 1:00:39 AM10/9/20
to adwords...@googlegroups.com
Hi George,

Thanks for getting back to us. I work with Teejay and allow me to assists you this time.

I've updated the script below based on the total campaigns' daily cost.
function main() {
  
  Logger.log("**********************");
  Logger.log("Running BUDGET - Pause All Campaigns script...");
  Logger.log("**********************");
 
  // THIS AMOUNT WILL VARY CLIENT BY CLIENT
  // MAKE SURE IT IS CORRECT
  var dailyBudget = 0;
  
  //This variable holds the total cost for all campaigns
  var totalCampaignCost = 0;
  
  var campaignIterator = AdWordsApp.campaigns().get();
  
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
   
    //Use TODAY to get data on the date today
    var stats = campaign.getStatsFor('TODAY');
    var campaignCost = stats.getCost();
    
    Logger.log("=================================");
    Logger.log("Campaign: " + campaign.getName());
    Logger.log("Cost: " + campaignCost);
    Logger.log("=================================");
    
    totalCampaignCost = totalCampaignCost + campaignCost;
  }
  
  Logger.log("Total Daily Campaign Cost: "+totalCampaignCost);  
  
  if(totalCampaignCost >= dailyBudget ){
    Logger.log("Daily budget reach! Pausing all campaigns");
    var campaignIterator = AdWordsApp.campaigns().get();
    while(campaignIterator.hasNext()){
      var campaign = campaignIterator.next();
      Logger.log("Pausing Campaign: " + campaign.getName());
      campaign.pause();
    }
  }
}
Hope this helps.

Regards,
Google Logo
Mark Kevin Albios
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q267eW3:ref

George - Low Cost SEO

unread,
Oct 9, 2020, 1:34:49 AM10/9/20
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor

Hi Mark,

 

You guys are champions! Yes this worked a treat..  Many thanks for your help..  See results below..

 

 

Kind Regards,
George Katsoudas  |  LCS Partner
Lead generation through website SEO & AdWords

W: 
lowcostseoplans.com
Office: 200 Barangaroo Ave, Sydney NSW 2000


LCS 


From: Google Ads Scripts Forum on behalf of adsscriptsforumadvisor <adwords...@googlegroups.com>
Sent: Friday, 9 October 2020 4:00 PM
To: adwords...@googlegroups.com
Subject: RE: Script to pause all campaigns based on daily budget total set

 

Hi George,

--

-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to a topic in the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/t8P4mqsO50M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

image001.png
image002.png
image003.png
Reply all
Reply to author
Forward
0 new messages