Script to pause all campaigns based on daily budget total and apply a label

89 views
Skip to first unread message

Jeremy G.

unread,
Jan 10, 2023, 2:13:53 PM1/10/23
to Google Ads Scripts Forum
Hi All, 

I've been using this script below to set a daily account budget and pause all campaigns after the limit has been reached. It works great, but I would like to modify it to also apply a label to each campaign that gets paused by the script. Could I have some assistance with editing this script?

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 = 9000;
 
  //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();
    }
  }
}

Martijn Kraan

unread,
Jan 10, 2023, 3:36:49 PM1/10/23
to Google Ads Scripts Forum
Hi Jemery,

I'm not from the official Google Ads Scripts support team. However, I might be able to help you.

In this article you can find documentation on how to apply a label to a campaign.
If I'm correct, it could be as easy as adding the following line of code after the "campaign.pause()" line:

campaign.applyLabel('High performing campaign');

Where "High performing campaign" is the name of your label.
To test this, I would suggest you create the label (manually) in the UI first.
Then preview the script and check if all works well.

while(campaignIterator.hasNext()){
  var campaign = campaignIterator.next();
  Logger.log("Pausing Campaign: " + campaign.getName());
  campaign.pause();
  campaign.applyLabel('High performing campaign');
}


Good luck!

Gr, Martijn

Google Ads Scripts Forum Advisor

unread,
Jan 11, 2023, 12:52:14 AM1/11/23
to adwords...@googlegroups.com

Hello,

I’m James from the Google Ads Scripts Team. Thank you for reaching out to us.

I concur with the given context and sample by Martijn. You just need to use the appyLabel method to include labels to your campaigns. As described with the given sample implementation of Martijn, you just need to implement the appyLabel method after the pause method in order to meet your desired result.

@Martijn - thank you for sharing your insights here.

Let me know if you have any questions.

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2hbvCj:ref

Jeremy G.

unread,
Jan 11, 2023, 1:31:59 PM1/11/23
to Google Ads Scripts Forum
Thank you Martijn and James for your responses! I've added this to the script and it does apply the desired label, but the only issue is it is applying the label to every campaign in the account. I would like this to only apply to the label to the campaigns that were enabled and now paused by the script.

Also, this made me think of an additional function. Is there a way for me to make an exception to what campaigns get paused? For instance, if my overall account daily spend surpasses $5000, I would like to pause all enabled campaigns except for my brand campaigns (which contain 'Brand') in their name.

Google Ads Scripts Forum Advisor

unread,
Jan 12, 2023, 12:31:13 AM1/12/23
to adwords...@googlegroups.com

Hello Jeremy,

Thank you for your response.

The label has been applied to all of your campaigns because you didn’t specify a filter or condition within your campaign selector. Since you’re looking to apply labels to your enabled campaigns, then you need to incorporate the withCondition method along with campaign.status field to your campaign selector. See sample implementation below.

var campaignIterator = Ads App.campaigns()
.withCondition("campaign.status = ENABLED")
.get();

On the other hand, yes it is possible to make exceptions to what campaigns get paused. You just need to include an additional filter or condition in your campaign selector. As for the given sample use case, you may try to use the sample code below. 

var campaignIterator = Ads App.campaigns()
.withCondition("campaign.status = ENABLED")
.withCondition("campaign.name NOT REGEXP_MATCH '.(?!)*Brand.*'")
.get();


Could you please try it on your end, then let me know how it goes?

Regards,

Google Ads Scripts Forum Advisor

unread,
Jan 12, 2023, 12:38:13 AM1/12/23
to adwords...@googlegroups.com

Jeremy G.

unread,
Jan 12, 2023, 6:45:02 PM1/12/23
to Google Ads Scripts Forum
Hi James,

Thank you very much for your helpful insight. I added the new conditions and they are excluding my brand campaigns now. The only thing is that now the Brand campaigns are also excluded from being counted as part of the daily spend instead of excluded only from being paused. What I would like is that when all enabled campaigns (Brand included) surpass my total budget limit, I would like to pause everything except for Brand.

Google Ads Scripts Forum Advisor

unread,
Jan 13, 2023, 1:18:26 AM1/13/23
to adwords...@googlegroups.com

Hello Jeremy,

Thank you for your clarification. You may try to incorporate the given code below within your script to pause all the enabled campaigns on your end, except for campaigns that contains `Brand` on its name.

function main() {
  var campaignIterator = AdsApp.campaigns()
  .withCondition("campaign.status = ENABLED")
  .get();

  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var campaignName = campaign.getName();
 
    if(!campaignName.toLowerCase().includes("brand")){
      Logger.log("Pausing Campaign: " + campaignName));
      campaign.pause()
    }
  }
}


Let me know how it goes on your end.

Regards,

Jeremy G.

unread,
Jan 13, 2023, 11:37:01 AM1/13/23
to Google Ads Scripts Forum
Perfect! Everything is working as I would like it to. Thank you very much for your time and assistance James. Cheers!

Morgan Ser

unread,
Jun 14, 2024, 5:11:01 AM (7 days ago) Jun 14
to Google Ads Scripts Forum
Hi there,

Hope all is well. 

Could I check with the Google ads Script support team whether we are able to modify the script to achieve the following objective:

Context: There are YouTube and Search campaigns in a Google Ads account. Google Search campaigns have big fluctuation throughout the week based on the search demand. Account budget limit has been set in the account billing setting. We adjusted the pacing as needed but towards the end of the month, it is challenging to control the daily spend of the search campaigns and they would spend more than intended.

Google ads script's goal:
- To check for several campaigns and pause them when their combined total ad spend for the day exceeds a certain amount; then reactivate them the following day.

Thanks in advance if we can get the team's help to modify the Google ads script for us.

Regards,
Morgan
Reply all
Reply to author
Forward
0 new messages