Make Budget Control Script Video and Shopping compatible

221 views
Skip to first unread message

jochen...@gmail.com

unread,
Dec 9, 2020, 12:54:50 AM12/9/20
to Google Ads Scripts Forum
Hello there, 
I have been using the below script for some years now as a safety net to prevent accidental overspending in my Google Ads accounts. It is working well for Search campaigns, however it is not checking/pausing any of my video or shopping campaigns in case of overspending. Since my javascript knowledge is very limited I was wondering if anyone could help me (and protentially others in the same boat) out and adjust the script so it is also working for shopping and video campaigns.

Cheers,
Jochen

function main() {
    var accountBudgetLimit = 0; // set your account budget limit here
    var accountCampaignsCost = 0;

    var campaigns = AdWordsApp.campaigns().get(); // gets all the campaigns of the account. You may add conditions to select campaigns using withCondtion()

    /*
     *
     *
     * This While-loop sums the costs garnered by all
     * the campaigns of the account for the specified date range
     *
     */
    while (campaigns.hasNext()) {
        var campaign = campaigns.next();
        var stats = campaign.getStatsFor("THIS_MONTH"); // gets stats for today, you may change the date range based on your requirment
        accountCampaignsCost += stats.getCost(); // gets the cost of the campaign
    }

    if (accountCampaignsCost >= accountBudgetLimit) { // If the total costs of the campaign has reached or exceeded the specified limit, pause the camapaigns
        Logger.log("Budget has exceeded the limit!");
        var campaigns = AdWordsApp.campaigns().get();

        while (campaigns.hasNext()) {
            var campaign = campaigns.next();
            Logger.log("Pausing campaign " + campaign.getName() + "...");
            campaign.pause(); // pauses the campaign      
          
        }
      
        MailApp.sendEmail('info(A)website.com', // specify email address here
            'Google Ads OVERSPENDING ALERT: ACCOUNT NAME monthly budget exceeded & all campaigns paused',
            'The total cost of your campaigns has reached the specified limit.'); // notifies you with an email when total cost of the campaigns has reached he specified limit. You may change the email message based on your preference.     
      
      
    }
    else {
        Logger.log("Total spending of campaigns has not yet reached the account budget limit.");
    }
}

Google Ads Scripts Forum Advisor

unread,
Dec 9, 2020, 1:39:58 AM12/9/20
to adwords...@googlegroups.com

Hi,

 

Thanks for reaching out and allow me to assist you in this. The CampaignSelector only allows you to fetch Search and Display campaigns. With that said, there are separate campaign selectors that you should you use to check/pause video and shopping campaigns which is the VideoCampaignSelector and the ShoppingCampaignSelector.

 

Let me know if you have other questions.

 

Thanks,

Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q28oWY1:ref

jochen...@gmail.com

unread,
Dec 9, 2020, 1:50:10 AM12/9/20
to Google Ads Scripts Forum
Thanks for your speedy reply. Any chance you can make those changes to the script for me? I had tried that but gave up after some time of getting errors while testing.

Google Ads Scripts Forum Advisor

unread,
Dec 9, 2020, 4:03:44 AM12/9/20
to adwords...@googlegroups.com

Hi there,

 

Sure. I have tweaked your script and added a selector array to loop through the three campaign selectors. You would just need to identify in which condition you would like to receive the email notification. Please see below code for your reference. Let me know if this would work on your end.

 

var accountBudgetLimit = 0; // set your account budget limit here
  var accountCampaignsCost = 0;
  
  // gets all the campaigns of the account. You may add conditions to select campaigns using withCondtion()
  var selectors = [AdsApp.campaigns(), AdsApp.videoCampaigns(), AdsApp.shoppingCampaigns()];
  
  for(var i = 0; i < selectors.length; i ++) {
    var campaigns = selectors[i].get();
    
    /*
     * This While-loop sums the costs garnered by all
     * the campaigns of the account for the specified date range
     *
    */
    while (campaigns.hasNext()) {
      var campaign = campaigns.next();
      var stats = campaign.getStatsFor("THIS_MONTH"); // gets stats for today, you may change the date range based on your requirment
      accountCampaignsCost += stats.getCost(); // gets the cost of the campaign
 
      if (accountCampaignsCost >= accountBudgetLimit) { // If the total costs of the campaign has reached or exceeded the specified limit, pause the camapaigns
          Logger.log("Campaign " + campaign.getName() + " has exceeded budget limit. Pausing campaign...");
          campaign.pause(); // pauses the campaign      
      }
      else {
         Logger.log("Total spending of campaigns has not yet reached the account budget limit.");
      }
    }
  
    // MailApp.sendEmail('info(A)website.com', // specify email address here
    //    'Google Ads OVERSPENDING ALERT: ACCOUNT NAME monthly budget exceeded & all campaigns paused',
    //    'The total cost of your campaigns has reached the specified limit.'); // notifies you with an email when total cost of the campaigns has reached he specified limit. You may change the email message based on your preference.     
  }

jochen...@gmail.com

unread,
Dec 9, 2020, 5:46:19 AM12/9/20
to Google Ads Scripts Forum
Thank you very much for your help. This is wonderful!

jochen...@gmail.com

unread,
Dec 9, 2020, 5:32:03 PM12/9/20
to Google Ads Scripts Forum
Hey there again. I went ahead and implemented the above sripts now for one of my accounts. However, when I run the script and the budget is exceeded it does not pause all campaigns. Is there any way you could help me troubleshoot this? Cheers, Jochen

Google Ads Scripts Forum Advisor

unread,
Dec 9, 2020, 9:47:23 PM12/9/20
to adwords...@googlegroups.com

Hello,

 

Kindly share with us your CID and the script ID/Name via Reply privately to author or Reply to author option instead, so we could check on our end. If those options aren't working on your end, you can send those requested details on this email (googleadsscr...@google.com) and include the link of this thread so we could keep track of this existing conversation.

Google Ads Scripts Forum Advisor

unread,
Dec 13, 2020, 10:05:55 PM12/13/20
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for providing the requested details. However, it seems that the script is working as expected and does pause the campaigns based on your condition. Please see screenshot for your reference. Are there any thrown error messages at your end when running the script?

3n9WJputjkBLTfB.png

Jochen Setzer

unread,
Dec 13, 2020, 10:30:45 PM12/13/20
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Thanks for double checking Harry. It does seem to work at my end now too and I will roll it out to all my accounts.

Cheers for all your help.

Regards,
Jochen


--
-- 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/b_0Nlu00rbc/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/VaULO000000000000000000000000000000000000000000000QLB79J00r-8HfNhbTTiEPq5Mlu1lpQ%40sfdc.net.

Google Ads Scripts Forum Advisor

unread,
Dec 13, 2020, 10:47:29 PM12/13/20
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for confirming. Kindly let me know if you still have any questions or concerns.

jochen...@gmail.com

unread,
Jan 1, 2021, 8:36:27 AM1/1/21
to Google Ads Scripts Forum
Hello there,
sorry, but I have to come this again. After the script has been running for a few weeks in my accounts I have noticed, that only selected campaigns are paused in cases where the overall account budget has been exceeded. This let to overspending in some of my accounts. Could you please help in troubleshooting while not all campaigns where paused? I have attached a screenshot of the script history. I have provided the CID in the past. Please let me know if I should resend it.
Cheers,
Jochen

busch global ads script error.jpg

Google Ads Scripts Forum Advisor

unread,
Jan 3, 2021, 9:18:30 PM1/3/21
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for coming back. You may add a condition to your CampaignSelector by using the withCondition method to filter campaigns that are enabled. You may update line 9 with the following:

 

var campaigns = selectors[i].withCondition("Status = ENABLED").get();

 

Kindly try this at your end then let me know how it goes.

Jochen Setzer

unread,
Jan 4, 2021, 2:17:14 AM1/4/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Thank you very much for your help. This worked when I just tested it. However, even the previous script works when I test it. It just seemed to fail in a live environment. I'll update the scripts and watch the accounts closely this month. I will let you know if it works smoothly this time.
Cheers, 
Jochen



--
-- 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/b_0Nlu00rbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

Jochen Setzer

unread,
Feb 7, 2021, 8:29:55 PM2/7/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Hello there,
I am just following up on my latest email. In January the script did not work as intended again. Only a few campaigns were paused and the account overspent again. Could you please investigate account 321-743-3789 "Busch AG (Switzerland) - Switzerland"? 

Please let me know if you need any further information.

Regards,
Jochen


Google Ads Scripts Forum Advisor

unread,
Feb 7, 2021, 11:09:08 PM2/7/21
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for coming back. I have made a couple of changes that should help us monitor why the script would not be able to pause the campaigns as intended. I have also changed the way we set value you to the accountCampaignsCost variable instead of summing up the cost every time, we should check individual cost since we there are multiple campaigns. Kindly see attached for your reference. Note that I have run tests and you may have received emails from the script so please disregard. Let me know how it goes this time, then we'll go from there.

script.txt

Jochen Setzer

unread,
Feb 7, 2021, 11:12:02 PM2/7/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Hi Harry,
thanks for looking into this so quickly. One question: Did you change the script in the account already or should I do this?
Regards,

Jochen


--
-- 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/b_0Nlu00rbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

Google Ads Scripts Forum Advisor

unread,
Feb 8, 2021, 12:57:00 AM2/8/21
to adwords...@googlegroups.com

Hi Jochen,

 

We can't really make changes for you. You may choose to update the script on your end based on my suggestions. Let me know how it goes.

Jochen Setzer

unread,
Feb 9, 2021, 6:23:38 PM2/9/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Hi Harry,
thanks for your response. I have implemented the altered script now but it already failed at the first test.

I have changed "accountBudgetLimit" to 70. Since the campaign had already spent 73 EUR this month this should have paused all campaigns. However, it did not pause any.

Is the script summing up all the campaigns costs to determine if they should be paused? This is how it is intended to work.

It would be great if you could check this again.

As you might be aware overspending my client's accounts is very bad for my reputation as a Google Ads partner. It would be great if you could help me to get this sorted out.

Thanks a lot.

Regards,
Jochen



--
-- 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/b_0Nlu00rbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

Google Ads Scripts Forum Advisor

unread,
Feb 9, 2021, 10:13:33 PM2/9/21
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for your patience on this. Can I interest you in looking at the cost of the Account level instead of summing up the cost of each campaign? I assume that you would like to pause all campaigns based on account's cost spent as a whole, regardless if a campaign or two has not exceeded its budget. I have revised the script in this regard. Please see attached. Let me know how it goes.

script.txt

Jochen Setzer

unread,
Feb 11, 2021, 1:29:00 AM2/11/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Hi Harry,

thanks a lot for your ongoing support. Really appreciate it.

Does looking at the account level cost also include "Video and Shopping campaigns"? I was under the impression that it does not. It would be great if you could confirm this. 

Thanks a lot.
Regards,
Jochen

--
-- 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/b_0Nlu00rbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

Google Ads Scripts Forum Advisor

unread,
Feb 11, 2021, 3:47:24 AM2/11/21
to adwords...@googlegroups.com

Hi Jochen,

 

Thanks for bringing that up. Upon checking with the team, this would consider all costs spent for the whole account. Let me suggest another revision to your script. I think it would be best if we iterate through all of the campaigns' cost before having them paused instead. Would you agree? Please see attached and let me know your thoughts.

script.txt

Jochen Setzer

unread,
Feb 15, 2021, 1:41:30 AM2/15/21
to Google Ads Scripts Forum on behalf of adsscriptsforumadvisor
Hi Harry,
thanks for getting back to me. 
This script variation looks great and I have implemented it for one of the accounts for testing purposes. I will roll it out to all accounts if it proofs to work as intended.
I'll keep you posted.

Thanks again and have a great week.
Regards,
Jochen



--
-- 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/b_0Nlu00rbc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-scrip...@googlegroups.com.

Boren Probitasindo

unread,
Dec 7, 2021, 3:26:55 AM12/7/21
to Google Ads Scripts Forum
Hi Jochen and Google Ads Script Team, 

I've been reading this thread and I've implemented the script and it works for me on the account level. I'm thankful for that. 

What I need next is to have the script working in campaign level, for ex: campaign A & B, their monthly cost in total is above 500, then campaign A & B are paused. Campaign C and D will not be affected with this script. For campaign C and D, I will use other scripts to pause them as their monthly budgets are different from each other. I've been using this script for Search campaign but it doesn't work for video campaigns. I've tried to modify it by changing the campaigniterator to videocampaigniterator but it didn't work. Would you please help to modify it?

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

  var monthlyBudget = 500;


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

      .withCondition('Name IN ["Campaign A", "Campaign B"]') //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();
    }
  }
}

And if you have another script where in 1 script I can have rules with 2 different settings, let's say total cost campaign A + B = 500, campaign A + B are paused (C+ D are not paused), total cost campaign C + D = 700, campaign C + D are paused (A + B are not paused), that will be great! With the above script, that can only be done by implementing 2 scripts.

Thank you!
Best regards,
Boren

Google Ads Scripts Forum Advisor

unread,
Dec 8, 2021, 9:37:26 PM12/8/21
to adwords...@googlegroups.com
Hi Boren,

Thanks for reaching out. Harry here and allow me to assist you.

To include or select video campaigns, you would have to use its dedicated selector - AdsApp.​VideoCampaignSelector to do so. Please see video and shopping campaigns limit for information in this regard. You can update your script to replace the AdWordsApp.campaigns() to AdsApp.​VideoCampaignSelector / AdWordsApp.​VideoCampaignSelector. On the other hand, you would have to make a custom script for your other requirement where you need to be specific with the campaigns you mean and don't mean to pause based off the monthly budget thresholds. I'd suggest have two separate selectors where you'll select campaign A and B in the first selector while you can select campaigns C and D for the second selector then depend on the results from iterating through each selector before managing the campaigns. 

Let me know if you need anything else.

Boren Probitasindo

unread,
Dec 13, 2021, 2:42:45 AM12/13/21
to Google Ads Scripts Forum
Hi Harry,

Thank you for your answer. I have tried but to no success unfortunately. These are 2 modified scripts I've tried:


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

  var monthlyBudget = 190



  var totalCost = 0;
  var campaignsList = [];
 
  var videoCampaignIterator = AdsApp.videoCampaignSelector

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

      .get();
 
  while (videoCampaignIterator.hasNext()) {
    var campaign = videoCampaignIterator.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();
    }
  }
}

and


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

  var monthlyBudget = 190



  var totalCost = 0;
  var campaignsList = [];
 
  var CampaignIterator = AdsApp.videoCampaignSelector()

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

      .get();
 
  while (CampaignIterator.hasNext()) {
    var campaign = videoCampaignIterator.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();
    }
  }
}

I would appreciate more of your help..

Thank you.
Best regards,
Boren

Google Ads Scripts Forum Advisor

unread,
Dec 13, 2021, 4:55:51 AM12/13/21
to adwords...@googlegroups.com
Hi Boren,

Thanks for coming back. Would you be able to specify how the script is not working? Did the script not log any video campaigns data? Please provide screenshots if possible and I would also appreciate it if you can provide your Google Ads account ID and the names of your scripts.

Looking forward to your reply.

Boren Probitasindo

unread,
Dec 13, 2021, 5:43:52 AM12/13/21
to Google Ads Scripts Forum
Hi Harry,

Thank you for your reply. The script log said this:

error.PNG

How can I send the Google CID to you? Posting it here in public seems a bit risky, and I cannot use reply to the author button.

Thank you.
Boren

Google Ads Scripts Forum Advisor

unread,
Dec 13, 2021, 10:11:48 PM12/13/21
to adwords...@googlegroups.com
Hi Boren,

Thanks for providing the screenshot. Would you mind replacing 'AdsApp.videoCampaignSelector()' to 'AdsApp.videoCampaigns()' instead and let me know it that would resolve the issue? If not, you can provide further details through our email (googleadsscr...@google.com) instead.

Boren Probitasindo

unread,
Dec 14, 2021, 2:44:33 AM12/14/21
to Google Ads Scripts Forum
Hi Harry,

Ah, this one works! Yeay! Thank you very much for your support!

successful 1.PNG

successful 2.PNG


Thank you!
Best regards,
Boren
Reply all
Reply to author
Forward
0 new messages