Script failed last month for unknown reasons

35 views
Skip to first unread message

Rachel Anthony

unread,
Jun 6, 2024, 8:03:34 PMJun 6
to Google Ads Scripts Forum
We have a script built to pause all campaigns in an account after the monthly spend reaches a certain threshold. It was tested and worked for a while, but mysteriously, last month, the scripts failed to pause our search campaigns and only paused Pmax campaigns. 

Weirdly enough, we recently rewrote this script to make sure pmax got paused, but it randomly stopped pausing the search campaigns this time around.

This is the script: 

var monthly_budget =XXXX;

function main() {
var spend=AdsApp.currentAccount().getStatsFor("THIS_MONTH").getCost();
if(spend>monthly_budget){
pauseAllCampaigns()
}
}

function pauseAllCampaigns() {
Logger.log('Monthly budget has been exceeded…pausing')
var campaignIterator = AdsApp.campaigns().get();
var campaignIterator = AdsApp.performanceMaxCampaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause()
}
}


Any idea what happened and how to fix it?

Nils Rooijmans

unread,
Jun 10, 2024, 2:19:37 AM (11 days ago) Jun 10
to Google Ads Scripts Forum
inside your pauseAllCampaigns() function,  you assign a new value to your campaignIterator before using it.

Try this instead:


function pauseAllCampaigns() {
Logger.log('Monthly budget has been exceeded…pausing')

// pause search and display campaigns
var campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause()
}

// pause PMax campaigns

var campaignIterator = AdsApp.performanceMaxCampaigns().get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause()
}
}

Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Jun 10, 2024, 3:17:58 AM (11 days ago) Jun 10
to adwords...@googlegroups.com

Hi,

I would suggest that you follow the solution provided by Nils and get back to us with the below details if you still face any issues.

  • Google Ads account ID/CID
  • Name of the affected script

You can share the requested details via Reply privately to the author option or a direct private reply to this email. 

This message is in relation to case "ref:!00D1U01174p.!5004Q02tJGRI:ref" (ADR-00239242)

Thanks,
 
Google Logo Google Ads Scripts Team


Reply all
Reply to author
Forward
0 new messages