Script to Pause Multiple Campaigns when they reach a combined total monthly budget

4,508 views
Skip to first unread message

Cody Palmer

unread,
Jun 29, 2017, 12:47:29 AM6/29/17
to AdWords Scripts Forum

Hello,

 

For one of my accounts there are multiple locations inside the account.  Each location has 4 campaigns.  These campaigns are named based off the location.  I am wondering if there is a script to pause these 4 campaigns once the total cost for all of these campaigns combined equals a monthly budget.

 

For example, the Dallas location has 4 campaigns.  Each of these campaigns spend a different amount of money throughout the month but once they reach a combined total budget, I want to automatically turn these 4 campaigns off.

 

Campaign-Dallas 1

Campaign-Dallas 2

Campaign-Dallas 3

Campaign-Dallas 4

 

Pause all 4 campaigns automatically once they spend a combined total of $1,000 for the month.

 

I do not want to use shared budgets because you can only set a daily budget for the campaigns.  I want to set a monthly budget.

 

I do not want to use the automated rules because this only applies to the individual campaign total spend and does not combined the total for all 4 campaigns.


I was wondering if anyone has created a script for this? 

 

Thank you,

Joyce Lava (AdWords Scripts Team)

unread,
Jun 29, 2017, 3:10:03 AM6/29/17
to AdWords Scripts Forum
Hi Cody,

Please refer to the below sample script to help you get started in getting the combined cost of 4 campaigns and pause them when requirement is met.

function main() {
 
//update the value of monthlyBudget based on your requirement
 
var monthlyBudget = 1000;
 
var totalCost = 0;
 
var campaignsList = [];
 
 
var campaignIterator = AdWordsApp.campaigns()
     
.withCondition('Name IN ["INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE", "INSERT_CAMPAIGN_NAME_HERE"]')
     
.get();
 
 
while (campaignIterator.hasNext()) {
   
var campaign = campaignIterator.next();
     
//save in campaignsList the list of campaigns object.
    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;
 
}
 
 
//if totalCost of combined 4 campaigns is equal to defined monthlyBudget, pause the 4 campaigns
 
if (totalCost == monthlyBudget){
   
for (var i = 0; i < campaignsList.length; i++) {
     
var campaign = campaignsList[i];
     
Logger.log(campaign.getName())
      campaign
.pause();
   
}
 
}
}

You may use the Preview button so you can check if this is indeed what you want to achieve and let me know if this works for you. The script may be scheduled to run Daily so it checks the current total cost from first day of the month.

Regards,
Joyce Lava
AdWords Scripts Team

Cody Palmer

unread,
Jul 5, 2017, 9:25:00 PM7/5/17
to AdWords Scripts Forum
Thank you for helping me get started.

I changed the monthly budget and entered my 4 campaign names into the script to run a test.  I set the monthly budget at a cost under what the 4 campaigns have already spent.  When I did this and ran the script, it did not pause my 4 campaigns.

Any suggestions?

Joyce Lava (AdWords Scripts Team)

unread,
Jul 5, 2017, 11:03:43 PM7/5/17
to AdWords Scripts Forum
Hi Cody,

There can be a lot of factors why the campaigns are not being paused. One possible reason can be due to not meeting the criteria, that is, the combined cost of the 4 campaigns within "THIS_MONTH" dateRange is not equal to the monthlyBudget specified in the variable. So I could better check this, could you send to me (via reply privately to author option) your CID and the name of the script?

Thanks,

Thea Vega (AdWords Scripts Team)

unread,
Jan 11, 2018, 1:04:21 AM1/11/18
to AdWords Scripts Forum
Hi Daniel, 

We have received your private reply and for the benefit of all other developers browsing this forum, please see below for the provided updated script in this thread which included Loggers to identify if the if-condition is met or not. You may Preview it on your end and let me know the results.

function main() {
 
//update the value of monthlyBudget based on your requirement

 
var monthlyBudget = 100;


 
var totalCost = 0;
 
var campaignsList = [];
 
 
var campaignIterator = AdWordsApp.campaigns()

     
.withCondition('Name IN ["INSERT_CAMPAIGN_NAMES_HERE"]') //you may change this depending on your requirement

     
.get();
 
 
while (campaignIterator.hasNext()) {
   
var campaign = campaignIterator.next();
     
//save in campaignsList the list of campaigns object.
    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("Combined Cost for this month: " +totalCost);


 
 
//if totalCost of combined 4 campaigns is equal to defined monthlyBudget, pause the 4 campaigns
 
if (totalCost == monthlyBudget){
   
for (var i = 0; i < campaignsList.length; i++) {
     
var campaign = campaignsList[i];
     
Logger.log(campaign.getName())
      campaign
.pause();
   
}
 
}
}

Hope this helps.

Thanks,
Thea
AdWords Scripts Team

Daniel

unread,
Jan 11, 2018, 3:22:12 PM1/11/18
to AdWords Scripts Forum on behalf of Thea Vega (AdWords Scripts Team)
I ran the script and the logs said, "Combined Cost for this month: 2521.72."  I wonder why it showed it over budget but did not pause.

I set the budget variable to "100"
function main() {
  //update the value of monthlyBudget based on your requirement
  var monthlyBudget = 100;
  var totalCost = 0;
  var campaignsList = [];
  
  var campaignIterator = AdWordsApp.campaigns()
      .withCondition('Name IN ["InSynq_Search_Brand", "InSynq_Search_NonBrand_Competitor", "InSynq_Search_NonBrand_Drake Tax", "InSynq_Search_NonBrand_NEW QB", "InSynq_Search_NonBrand_ProSeries", "InSynq_Search_NonBrand_ProSystem fx Tax"]') //you may change this depending on your requirement

--
-- 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 "AdWords Scripts Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-scripts/UF_YditdtL8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/fce09bc0-8713-4839-a776-2c8c8caffce6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thea Vega (AdWords Scripts Team)

unread,
Jan 11, 2018, 10:53:44 PM1/11/18
to AdWords Scripts Forum
Hi Daniel,

The reason as to why your campaigns are not pausing is because your if-condition does not return true. That means that variable totalCost is not equal to your variable monthlyBudget. In your scenario, the values of totalCost and monthlyBudget are 2521.72 and 100 respectively thus not satisfying your if-condition. If you would want to pause your campaigns when totalCost is greater than or equal to monthlyBudget, please change the following in your code:

if (totalCost == monthlyBudget){ //line 27

to

if (totalCost >= monthlyBudget){ //line 27

Jessica Dee

unread,
Feb 9, 2018, 10:55:50 AM2/9/18
to AdWords Scripts Forum
Does this script work with Video campaigns? It does not seem to register any video campaigns when testing.

Thea Vega (AdWords Scripts Team)

unread,
Feb 11, 2018, 10:06:14 PM2/11/18
to AdWords Scripts Forum
Hi Jessica,

The script provided currently works for non-video campaigns (e.g. Search Network, Display Network, etc.) only. If you would require the script to fetch video campaigns, you may change:

var campaignIterator = AdWordsApp.campaigns()

to

var campaignIterator = AdWordsApp.videoCampaigns()

nicolas...@groupm.com

unread,
Jun 25, 2018, 6:13:57 AM6/25/18
to AdWords Scripts Forum
Thanks for this great scripts.

Could you please give support abaut video, display and universal app campaigns?
Many thanks


Anthony Madrigal

unread,
Jun 25, 2018, 4:25:49 PM6/25/18
to AdWords Scripts Forum
Hi,

As Thea mentioned, you will need to use AdWordsApp.videoCampaigns() for video campaigns and AdWordsApp.campaigns() for display campaigns. UAC are still not supported through AdWords Scripts.

Can you provide the full snippet for lines 10-14 of your script so I can see why you are running into issues?

Thanks,
Anthony
AdWords Scripts Team

Anthony

unread,
Jun 29, 2018, 10:10:07 AM6/29/18
to AdWords Scripts Forum
Joyce,

Thank you for this! Instead of using .withCondition, is there a way to call all campaigns? For some reason, I log an "One of the conditions in the query is invalid." error when I list all my campaigns using .withCondition.

Thanks,

Anthony

Anthony Madrigal

unread,
Jun 29, 2018, 10:35:00 AM6/29/18
to AdWords Scripts Forum
Hi Anthony,

If you want all campaigns, you do not need a withCondition. You can find an example here.

Cheers,
Anthony
AdWords Scripts Team

Rob Gillan

unread,
Jul 27, 2018, 5:23:47 AM7/27/18
to AdWords Scripts Forum
Here is a modified version of the above script that we use to pause all campaigns. I've also added more Logger output for debugging / routing checking. Thanks everyone for contributing!

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);
 
}
}

Nicolás Cicciarelli

unread,
Jul 31, 2018, 5:44:57 AM7/31/18
to AdWords Scripts Forum
Hi Joyce, this is almost exactly what I was looking for, I've seen a similar question in this thread, but I can't seem to find a way to add Video Campaigns. I don't want to use Video Campaigns instead of search I need this to work with both. I've been trying to get campaigns and videocampaigns inside the iterator but so far, had no luck, do you think you can point me in the right direction?

Thank you in advance!

Anthony Madrigal

unread,
Jul 31, 2018, 10:18:40 AM7/31/18
to AdWords Scripts Forum
Hi Nicolás,

You will need to separate iterators, one for video campaigns and one for search campaigns. You should be able to add the costs for both to your totalCost variable.

Let me know if you face any issues.

Cheers,
Anthony
AdWords Scripts Team

Divya Mahbubani

unread,
Aug 6, 2018, 8:26:47 AM8/6/18
to AdWords Scripts Forum
What if I wanted to edit the script to be YTD or past 90 days?

Also if I want it to be label = x

Thanks

Anthony Madrigal

unread,
Aug 6, 2018, 9:26:06 AM8/6/18
to AdWords Scripts Forum
Hi Divya,

I see you create a post regarding this issue. In order to track the issue better, we will further discuss the issue there.

Regards,
Anthony
AdWords Scripts Team

han...@digitalmarketingarm.com

unread,
Nov 28, 2018, 6:07:25 AM11/28/18
to AdWords Scripts Forum
Hi Anthony,

Is it possible to adjust this script to use THIS_DAY in some cases? There are occasionally days where we only want to spend a specific amount. 

Thank you,
Hannah

Anthony Madrigal

unread,
Nov 28, 2018, 10:07:26 AM11/28/18
to AdWords Scripts Forum
Hi Hannah,

If you only want your script to run on certain days, you can use the snippet below:
var date = new Date();
var day = date.getDay();
//Sunday = 0, Monday = 1, Tuesday = 2, etc
if(day == 0){
 
//rest of code
}

If your question is different, can you please create a new thread and provide more details.

Regards,
Anthony
AdWords Scripts Team

Andrea Dela Paz

unread,
Mar 26, 2019, 12:19:28 PM3/26/19
to Google Ads Scripts Forum
Hello Everyone,

The scripts here are really helpful. However, I can't seem to edit it to perform on a weekly basis. Hope I can get help here.

Thanks,
Andrea

googleadsscrip...@google.com

unread,
Mar 26, 2019, 1:30:32 PM3/26/19
to Andrea Dela Paz via Google Ads Scripts Forum, Google Ads Scripts Forum
Hi Andrea,

Can you describe the issues you are facing with your script? 

Regards,
Anthony
Google Ads Scripts Team


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
    http://googleadsdeveloper.blogspot.com/search/label/adwords_scripts
    https://developers.google.com/google-ads/scripts/community/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

--
-- 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 the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scripts+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/1a5dd642-7b91-4354-b3d4-e96d0707a34b%40googlegroups.com.

Antonio Castro

unread,
May 28, 2019, 7:15:40 AM5/28/19
to Google Ads Scripts Forum
Hi.
I starting to use this script and its awesome.
Thanks.

I wonder if there is a way to fetch the date of the campaign (start date to end date ) and define Max Budget so it can pause the campaign with ID X but for the defined period.
Let's say

Campaign Model X, Starts on 3 FEV to 6 FEV
Max Budget = 100 x 30
The script fetches the campaign duration timeframe instead of manually inserting and pauses if the campaign exceeds total budget

:) thanks

Antonio Castro

unread,
May 28, 2019, 7:54:29 AM5/28/19
to Google Ads Scripts Forum
Maybe i was Unclear: i wish to use a data range like:

3 FEV to 7 FEV.
Is it possible?

Google Ads Scripts Forum Advisor Prod

unread,
May 28, 2019, 8:01:01 AM5/28/19
to adwords...@googlegroups.com

Hi Antonio,

You may use getStartDate() and getEndDate() method to get a campaign's period. With regard to budget, could you confirm what you meant by this "define Max Budget so it can pause the campaign with ID X but for the defined period."? If you meant that setting the amount of budget, you may use setAmount() method. Otherwise, so I can provide proper suggestion, could you provide further details?

Regards,
Hiroyuki
Google Ads Scripts Team



ref:_00D1U1174p._5001UAqovH:ref

Antonio Castro

unread,
May 28, 2019, 9:28:45 AM5/28/19
to Google Ads Scripts Forum
Hi,This is my modified scrip to ensure that a campaign with ID's defined in .withIds([ID1,ID2, ]) don't overspend.
Im also applying a LABEL to make sure that on the next month the campaing will be reativated ( is this ok, do you have any feedback)
It's working really nice.

now, I need a variation os this do apply to campaigns that run not on a montly base, but on a start and close date.


function main() {
  //update the value of monthlyBudget based on your requirement

  var monthlyBudget = 600;
  var totalCost = 0;
  var campaignsList = [];
  var labelName = "Tobedefined";
 
  AdWordsApp.createLabel(labelName, "");

  
  var campaignIterator = AdWordsApp.campaigns()

  .withCondition("Name CONTAINS_IGNORE_CASE 'Tobedefined'")
      .withIds([ID1,ID2, ])

  

      .get();
  
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
     //save in campaignsList the list of campaigns object.
    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("Combined Cost for this month: " +totalCost);


  
  //if totalCost of combined 4 campaigns is equal to defined monthlyBudget, pause the 4 campaigns
  if (totalCost >= monthlyBudget){
    for (var i = 0; i < campaignsList.length; i++) {
      var campaign = campaignsList[i];
      Logger.log(campaign.getName())
      campaign.applyLabel(labelName);
      campaign.pause();
    }
  }
}

Antonio Castro

unread,
May 28, 2019, 9:41:48 AM5/28/19
to Google Ads Scripts Forum
Yes, i need to set amount (fixed) for a determined timeframe.

Google Ads Scripts Forum Advisor Prod

unread,
May 29, 2019, 7:22:12 AM5/29/19
to adwords...@googlegroups.com

Hi Antonio,

> now, I need a variation os this do apply to campaigns that run not on a montly base, but on a start and close date.

Could you confirm if what you want to achieve is to check the spending (cost) for only the period between campaign's start date and end date? If yes, you may get start and end date by getStartDate() and getEndDate(), then use getStatsFor(dateFrom, dateTo) to stats for the specified custom date range. 

> Yes, i need to set amount (fixed) for a determined timeframe.

Could you confirm if what you meant is to set amount everyday during the period between the start date and end date?

Antonio Castro

unread,
May 29, 2019, 7:48:51 AM5/29/19
to Google Ads Scripts Forum
Hi.
Yes
I need to 

  • check the spending (cost) for only the campaign period
  • set the global maximum cost amount
  • Pause if equals or exceeds.

    Can you provide a sample script or modification of mine?

Divya Mahbubani

unread,
May 30, 2019, 12:16:00 AM5/30/19
to Google Ads Scripts Forum
i'm getting an error
ReferenceError: "spreadsheet" is not defined. (file Code.gs, line 903)

CID
889-023-8112 

Can you please assist?

Essentially i just want an export that lists account, date, spend, revenue, clicks for the current month

Google Ads Scripts Forum Advisor Prod

unread,
May 30, 2019, 1:52:26 AM5/30/19
to adwords...@googlegroups.com

Hi Antonio,

Thank you for your confirmation.

You may refer to the sample script below.

Getting the total cost only for campaign period------------------------------

 var campaignIterator = AdWordsApp.campaigns()
 .withCondition("Name CONTAINS_IGNORE_CASE 'Tobedefined'")
 .withIds([ID1,ID2, ])
 .get();  

 while (campaignIterator.hasNext()) {
  var campaign = campaignIterator.next();
  //save in campaignsList the list of campaigns object.
  campaignsList.push(campaign);
  //use THIS_MONTH to get data for all days in the current month   

  var stats = campaign.getStatsFor(campaign.getStartDate(), campaign.getEndDate());


  var campaignCost = stats.getCost();
  totalCost += campaignCost;
 }


------------------------------------------------------------------------------------------

With regard to set amount of budget, you may refer to this sample.

Please let me know if you have further clarifications.



Regards,
Hiroyuki
Google Ads Scripts Team



ref:_00D1U1174p._5001UAqovH:ref

Google Ads Scripts Forum Advisor Prod

unread,
May 30, 2019, 1:53:29 AM5/30/19
to adwords...@googlegroups.com

Hi Divya,

Could you confirm if the email address which authorized the script has access to the spreadsheet? If no, you would need to give an access to the email address.

Since this is an old thread and the topic here is about managing campaigns by total monthly budget, could you create a new thread with the details for better tracking?



Regards,
Hiroyuki
Google Ads Scripts Team



ref:_00D1U1174p._5001UAqovH:ref
Reply all
Reply to author
Forward
0 new messages