Adding PMAX to this script

591 views
Skip to first unread message

Bronson Bagwell

unread,
Dec 20, 2022, 4:41:58 PM12/20/22
to Google Ads Scripts Forum
I have the following script that I found on this forum awhile back. It works by pausing off all campaigns when the total cost for the month exceeds a certain number, I noticed that the script doesn't pause off PMAX campaigns. I was wondering if anyone here could help me add that. Thanks

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

Martijn Kraan

unread,
Dec 20, 2022, 4:56:29 PM12/20/22
to Google Ads Scripts Forum
Hi,

I'm not from the official Google Ads Support Team.
However, I might be able to help you with your question:

pMax campaigns need to be fetched separately from Search campaigns (just as you would do with Shopping campaigns for example).
See this reference article for more information.

Good luck!

Gr, Martijn

Google Ads Scripts Forum Advisor

unread,
Dec 21, 2022, 2:11:08 AM12/21/22
to adwords...@googlegroups.com

Hi All,

 

@Martijn - Your assistance for this issue is greatly appreciated.

 

@Bronson - I would also like to echo Martijn's response. Kindly note that a performance max campaign has it's own selector and it is different from that of the CampaignSelector being used in the script you provided. With this, you may modify the campaignIterator variable to: AdsApp.performanceMaxCampaigns().get().

 

Let us know how it goes on your end.

 

Best regards,

 

 

Google Logo
Yasmin Gabrielle
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2hM8qw:ref

Bronson Bagwell

unread,
Dec 21, 2022, 10:07:37 AM12/21/22
to Google Ads Scripts Forum
Thank you so much for the reply, I'm not too familiar with coding google scripts. Could one of you be kind enough to show me how the new script would look with the "campaignIterator variable to: AdsApp.performanceMaxCampaigns().get()." With the goal of pulling both Pmax and other search campaigns in order to total the cost for the month and pause off all campaigns when it exceeds a certain amount?

Google Ads Scripts Forum Advisor

unread,
Dec 22, 2022, 1:59:05 AM12/22/22
to adwords...@googlegroups.com

Hello Bronson,

 

You could instead implement two scripts wherein one gets performance max campaigns by using it's own selector and another which is using a CampaignSelector. You would just need to make the changes I had previously stated for this script to get performance max campaigns, or retain it in order to get search campaigns (just change AdWordsApp to AdsApp).

 

However, if you'd like for these two functionalities included in one script, then you may check the code attached below. I would recommend testing this first in Preview Mode. Once satisfied with the output, you can start the live execution of the script or schedule it.

 

Let us know if we can assist you with anything else.

Budget changing script for PMAX and Search.txt

Bronson Bagwell

unread,
Dec 22, 2022, 9:30:31 AM12/22/22
to Google Ads Scripts Forum
The script isn't running as intended. I was hoping that the script would take the current total cost (Spend) of all campaigns combined in this case search and Pmax for the month. If that total exceeds $500 then it would pause all campaigns. This current script is running two different totals for pmax and search.

Google Ads Scripts Forum Advisor

unread,
Dec 23, 2022, 2:16:48 AM12/23/22
to adwords...@googlegroups.com

Hi Bronson,

 

Thank you for the clarification and for your insights.

 

I've made some adjustments to the script to fit the use-case. This should get the performance max and search campaigns, show their costs for the month and pause them if the combined cost exceeds the monthly budget you've set (the value is currently 500).

 

Let me know if it works for you. You may also make updates to the script to fit your liking.

Budget changing script for PMAX and Search.txt

Bronson Bagwell

unread,
Dec 24, 2022, 11:25:47 AM12/24/22
to Google Ads Scripts Forum
Hey, this script while does show and compare the total cost of search and Pmax campigns for the month combined against a set budget. It only pauses off pmax campaigns and not all campaigns when that combined total exceeds the budget set. For example, if my budget set for the month in the script is ($500) and Pmax cost for the month is $450 and the search campaigns are $100 the total is $550, therefore, I want the script to pause all campaigns in the account.

Google Ads Scripts Forum Advisor

unread,
Dec 27, 2022, 4:40:24 AM12/27/22
to adwords...@googlegroups.com

Hi Bronson,

 

Thank you for providing more context on this.

 

This script pauses all campaigns encompassing performance max and search campaigns, but kindly modify the logger in line 62 as it denotes that the retrieved campaigns are performance max. Please note that the selector just above it does retrieve non-performance max campaigns. Additionally, I would also suggest to modify line 83 and 84 to just `campaigns.pause()` in order to pause the campaigns pushed in the campaignList.

 

In the event that you would still encounter an issue on your end, kindly provide your Google Ads account ID or CID so we're able to further investigate.

Bronson Bagwell

unread,
Jan 3, 2023, 9:11:45 AM1/3/23
to Google Ads Scripts Forum
Do you mind providing me with a script with the following modifications completed? Thank you

Google Ads Scripts Forum Advisor

unread,
Jan 4, 2023, 5:21:41 AM1/4/23
to adwords...@googlegroups.com

Hi Bronson,

 

Thank you for your patience on this. Please check the file I had attached for the updated version. Kindly let us know if you need further assistance or have any clarifications.

Budget changing script for PMAX and Search.txt
Reply all
Reply to author
Forward
0 new messages