Script - Pause/enable adgroups within a campaign depending on daily spend

1,177 views
Skip to first unread message

Etienne Lozach

unread,
Jan 30, 2018, 3:52:46 PM1/30/18
to AdWords Scripts Forum
Hi guys,

I would like to have some more control on my ad group spend  within the same campaign (mainly, if one adgroup is working very well, i would like to avoid it spending all my daily budget, leaving the other adgroups without any budget).

Now, i know that you can pause/enable ad groups via rules, but i would like to run an hourly script to have a better control on this.

Anyone could help me setting up a script that could:

- target a specific campaign
- pause/enable ad groups depending on a specific daily budget i would set

thank you in advance for your help!

Best,

Etienne


Thea Vega (AdWords Scripts Team)

unread,
Jan 30, 2018, 11:02:35 PM1/30/18
to AdWords Scripts Forum
Hi Etienne,

Based on my understanding, you would require for a script that would pause an adGroup on a specific campaign if it reached the daily budget you have set and vice versa. If so, please see below sample script based on your requirements to help you get started.

function main() {
 
//update the value of dailyBudget based on your requirement
 
var dailyBudget = 100;
 
 
var adGroupIterator = AdWordsApp.adGroups()
 
.withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"') //you may change this depending on your requirement
 
.get();
 
 
while (adGroupIterator.hasNext()) {
   
var adGroup = adGroupIterator.next();
   
   
Logger.log("AdGroup Name : %s", adGroup.getName());
   
   
//use TODAY to get data for today
   
var adGroupCost = adGroup.getStatsFor('TODAY').getCost();
   
Logger.log("AdGroup Cost: %s", adGroupCost);
   
   
//if adGroupCost reached the defined dailyBudget, pause the current adGroup
   
if (adGroupCost >= dailyBudget){
      adGroup
.pause();
   
}
   
else {
   
//do something else
   
}
 
}
}

I would suggest for you to Preview the script first to see if it fits your requirements.

Thanks,
Thea
AdWords Scripts Team

Etienne Lozach

unread,
Jan 31, 2018, 4:55:27 AM1/31/18
to AdWords Scripts Forum
Hi Thea,

Thank your quick answer :)

Just explain it better: If i run a £10000 campaign with 10 adgroups, i want to ensure that my budget will be properly spent between those adgroups by pausing the most performing ones. So in this example i would setup a rule that pause any adgroup once they reach £1000 a day. (10 000 budget campaign / 10 adgroups = 1000 daily budget at ad group level).

As i have just a basic javascript knowledge, i just want to doublecheck with you:

var dailyBudget = 100 => is this the daily budget i am willing to allocate at my ad group level before pausing it? so in my example i would set this up to 1000?
Logger.log("AdGroup Name : %s", adGroup.getName()); => Do i need to use a specific ad group name or can i leave this blank so it will target every ad groups withing my campaign?

Finally, if an adgroup is paused, i need it to be automatically enabled the next day. So i guess i need so more code on this section as it will actually just pause things but not unpause it?


    //if adGroupCost reached the defined dailyBudget, pause the current adGroup
    
if (adGroupCost >= dailyBudget){
      adGroup
.pause();
    
}
    
else {
    
//do something else
    
}

I understand that the adgroup is going to be paused once the DailyBudget is reached, but dont we need to add an else (if adgroup is paused and daily budget is not reached, then enable the adgroup)?

Best,

Etienne

Thea Vega (AdWords Scripts Team)

unread,
Feb 1, 2018, 1:38:49 AM2/1/18
to AdWords Scripts Forum
Hi Etienne,

Please see below for answers regarding your inquiries:
    • var dailyBudget = 100 => is this the daily budget i am willing to allocate at my ad group level before pausing it? so in my example i would set this up to 1000?
      • Yes, you are correct. You may indicate here the value of your daily budget you are willing to allocate to your adgroups.
    • Logger.log("AdGroup Name : %s", adGroup.getName()); => Do i need to use a specific ad group name or can i leave this blank so it will target every ad groups withing my campaign?
      • This code line is a Logger that will output the adgroup name the script is currently processing. You may leave this line of code as is to help you identify which are the adgroups have been processed by the script.
    • Finally, if an adgroup is paused, i need it to be automatically enabled the next day. So i guess i need so more code on this section as it will actually just pause things but not unpause it? I understand that the adgroup is going to be paused once the DailyBudget is reached, but dont we need to add an else (if adgroup is paused and daily budget is not reached, then enable the adgroup)?
      • Yes, the current script only pauses those adgroups that have reached your daily budget. With this, please see below script and take note of the bolded code lines as these are the ones that needs to be updated on your script based on your additional requirements.
    function main() {
     
    //update the value of dailyBudget based on your requirement

     
    var dailyBudget = 1000;

     
     
    var adGroupIterator = AdWordsApp.adGroups()
     
    .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"') //you may change this depending on your requirement
     
    .get();
     
     
    while (adGroupIterator.hasNext()) {
       
    var adGroup = adGroupIterator.next();
       
       
    Logger.log("AdGroup Name : %s", adGroup.getName());
       
       
    //use TODAY to get data for today
       
    var adGroupCost = adGroup.getStatsFor('TODAY').getCost();
       
    Logger.log("AdGroup Cost: %s", adGroupCost);

       
       
    //if adGroupCost reached the defined dailyBudget, pause the current adgroup

       
    if (adGroupCost >= dailyBudget){
          adGroup
    .pause();
       
    }
        //if adGroupCost is less than the defined budget
       
    else if(adGroupCost < dailyBudget){
          adGroup
    .enable();
       
    }

     
    }
    }
    Please take note of the following:
    • Assuming that you will schedule the script to run Hourly, and you will set a fresh budget daily (the else-if condition will then be satisfied), then the adgroups will be enabled for the next day.  
    • I would suggest for you to Preview the script above to see if it is within your requirements.
    Hope these help. Let me know if you need further clarifications.

    Etienne Lozach

    unread,
    Feb 2, 2018, 8:20:57 AM2/2/18
    to AdWords Scripts Forum
    Hi Thea,

    That's brilliant!

    Thank you :)

    One last question and will stop annoying you.

    If i want to have some more control on the campaign(s) i am targeting with this script, is there a way to

    Replace the code below


    .withCondition('CampaignName = "INSERT_CAMPAIGN_NAME_HERE"') 
    //you may change this depending on your requirement 

    by the code below

    var campaignNameDoesNotContain = [];
    // Use this if you want to exclude some campaigns.
    // For example ["Display"] would ignore any campaigns with 'Display' in the name,
    // while ["Display","Competitors"] would ignore any campaigns with 'display' or
    // 'competitors' in the name. Case insensitive.
    // Leave as [] to not exclude any campaigns.

    var campaignNameContains = [];
    // Use this if you only want to look at some campaigns.
    // For example ["Brand"] would only look at campaigns with 'Brand' in the name,
    // while ["Brand","Generic"] would only look at campaigns with 'brand' or 'generic'
    // in the name. Case insensitive.
    // Leave as [] to include all campaigns.

    var ignorePausedCampaigns = true;
    // Set this to true to only look at currently active campaigns.
    // Set to false to include campaigns that are currently paused.


    I think you will understand what i mean. This would help me to target several campaigns at the same time and to exclude some of them.

    Best,

    Etienne

    Thea Vega (AdWords Scripts Team)

    unread,
    Feb 4, 2018, 11:13:23 PM2/4/18
    to AdWords Scripts Forum
    Hi Etienne,

    Feel free to reach out to us if you have questions or inquiries related to AdWords Scripts.

    You may use the code lines you have provided. However, those are only variable initialization (i.e. campaignNameDoesNotContain, campaignNameContains, ignorePausedCampaigns) hence you may have to add additional logic to the original script for it to use those variables. 

    Also, the script I have provided earlier fetches a campaign with a certain name. If you would require to fetch or exclude campaigns with certain words, please see below answers to your inquiries.
    • to exclude campaigns with 'SAMPLE' in the name:
      • initialize variable
    //you may change 'SAMPLE' to any word based on your requirements and campaigns with that word will be ignored    
    var campaignNameDoesNotContain = ["SAMPLE"];
      • change your current .withCondition with:
    .withCondition('CampaignName DOES_NOT_CONTAIN '+campaignNameDoesNotContain)
    • fetch campaigns with 'SAMPLE' in the name
      • initialize variable
    //you may change 'SAMPLE' to any word based on your requirements and campaigns with that word will be the only ones fetched   
    var campaignNameContains = ["SAMPLE"];
      • change your current .withCondition with:
    .withCondition('CampaignName CONTAINS '+campaignNameContains)
    • get enabled campaigns only
      • initialize variable
    //you may change the status to PAUSED or REMOVED depending on your requirement 
    var ignorePausedCampaigns = 'ENABLED';
      • add another .withCondition. See sample code line below.
    .withCondition('CampaignStatus = '+ignorePausedCampaigns)
     
    Please note that the word SAMPLE above should be changed to any word you would require to use for excluding or including campaigns in your selector. Also, I would suggest for you to read this documentation for more information regarding the code lines above.

    In the future, please create a new forum thread for new topics for better tracking and we'll gladly assist you.

    Hope these help.

    Etienne Lozach

    unread,
    Feb 8, 2018, 6:16:08 AM2/8/18
    to AdWords Scripts Forum
    Hello Thea,

    so to doublecheck if i understood well:

    If i want to run this script for every campaigns containing the word "Thea", ignoring paused campaigns, and limit the ad groups daily budget to £100 per ad group.

    My final code would looks like:


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

      var dailyBudget = 100;
      var campaignNameContains = ["Thea"];
      var ignorePausedCampaigns = 'ENABLED';

      
      var adGroupIterator = AdWordsApp.adGroups()
      .withCondition('CampaignName CONTAINS '+campaignNameContains)
      .withCondition('CampaignStatus = '+ignorePausedCampaigns)
      .get();
      
      while (adGroupIterator.hasNext()) {
        var adGroup = adGroupIterator.next();
        
        Logger.log("AdGroup Name : %s", adGroup.getName());
        
        //use TODAY to get data for today
        var adGroupCost = adGroup.getStatsFor('TODAY').getCost();
        Logger.log("AdGroup Cost: %s", adGroupCost);

        
        //if adGroupCost reached the defined dailyBudget, pause the current adgroup

        if (adGroupCost >= dailyBudget){
          adGroup.pause();
        }
        //if adGroupCost is less than the defined budget
        else if(adGroupCost < dailyBudget){ 
          adGroup.enable();
        }
      }
    }

    Am i right?

    Again, thank you a lot for your help! Much appreciated!

    Etienne

    Etienne Lozach

    unread,
    Feb 8, 2018, 6:48:31 AM2/8/18
    to AdWords Scripts Forum
    After running a preview, it seems that the last code version i used is working properly.

    Below the final version i intend to use for my campaigns ( i added a var ignorePausedCampaigns + withCondition ignorePausedCampaigns , so that i am sure to only target my live campaigns if i need to).

    Can you just confirm that you do not spot any non-sense in my final script version?

    So in this case the script would run for every campaigns containing the word "Thea", ignoring paused and removed campaigns, and limit the ad groups daily budget to £100 per ad group.

    thank you,

    Etienne

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


     
    var dailyBudget = 100;
     
    var campaignNameContains = ["Thea"];
     
    var ignorePausedCampaigns = 'ENABLED';

     
    var ignorePausedCampaigns = 'ENABLED';
     
    var ignoreRemovedCampaigns = 'ENABLED';





     
     
    var adGroupIterator = AdWordsApp.adGroups()
     
    .withCondition('CampaignName CONTAINS '+campaignNameContains)
     
    .withCondition('CampaignStatus = '+ignorePausedCampaigns)

     
    .withCondition('CampaignStatus = '+ignorePausedCampaigns)
     
    .withCondition('CampaignStatus = '+ignoreRemovedCampaigns)

    Thea Vega (AdWords Scripts Team)

    unread,
    Feb 8, 2018, 10:04:58 PM2/8/18
    to AdWords Scripts Forum
    Hi Etienne,

    I checked the final code you have attached and here are my findings:
    • If you would want to ignore paused and removed campaigns, you may just have .withCondition(CampaignStatus = '+ignorePausedCampaigns) as this will fetch only ENABLED campaigns.
    • You may remove the following: 
      • line 7 on your script (var ignorePausedCampaigns) as it was initialized twice on lines 7 and 9 respectively.
      • line 10 on your script (var ignoreRemovedCampaigns) as it contains the same value as var ignorePausedCampaigns and will not be used on your code.
    Aside from those, your script seems to work well. Let me know if you run into issues after applying the suggestions above.

    Etienne Lozach

    unread,
    Feb 9, 2018, 7:42:10 AM2/9/18
    to AdWords Scripts Forum
    Hi thea,

    Oh, thank you for this additional feedback, i would not have spotted this problem by myself!

    I got rid of the elements including any ignoreRemovedCampaigns information as you suggested :)

    I got what i need and its working perfectly.

    Thank you again for your time, i wish you a great weekend,

    Etienne

    ashis...@performics.convonix.com

    unread,
    May 2, 2019, 4:59:59 PM5/2/19
    to Google Ads Scripts Forum
    Hi,

    We want to run the same script at MCC level where we want to pause all the ad groups that have exceeded their daily budget, and all ad groups have a different daily budget.

    can you please help us with that.

    googleadsscrip...@google.com

    unread,
    May 2, 2019, 11:06:14 PM5/2/19
    to Google Ads Scripts Forum
    Hi,

    If you want to implement a script at MCC level, you may refer to this guide and these samples. If you have different daily budget for each adGroup, I would suggest that you work with Spreadsheet Service. You may store the necessary information (ex. adGroup ID, daily budget) in a spreadsheet, and check each adGroup's cost and pause it. You may also refer to these samples on how to access to a spreadsheet from Google Ads scripts.

    Since this is an old thread, feel free to open a new thread so we can track this on our end better.

    Regards,
    Hiroyuki
    Google Ads Scripts Team

    =~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
    Also find us on our blog and discussion group:
        https://ads-developers.googleblog.com/search/label/google_ads_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/9cfdf1e2-21de-40d0-9723-6b3801b441fd%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    Reply all
    Reply to author
    Forward
    0 new messages