Infinite Loop Issue When Setting Target ROAS on All Campaigns in MCC Account

166 views
Skip to first unread message

Salvatore Aranzulla

unread,
Jul 29, 2024, 5:00:03 AM7/29/24
to Google Ads Scripts Forum

Good morning,

I am trying to run the following script on an MCC account to set the target ROAS to 100 in all campaigns of all managed accounts:

function main() {
  var accountIterator = MccApp.accounts().get();
 
  while (accountIterator.hasNext()) {
    var account = accountIterator.next();
    MccApp.select(account);
    setTargetRoas();
  }

  Logger.log("Target ROAS has been updated for all managed accounts.");
}

function setTargetRoas() {
  var TARGET_ROAS = 100; // 100% ROAS

  // Get all standard campaigns
  var campaignIterator = AdsApp.campaigns().get();
  while (campaignIterator.hasNext()) {
    var campaign = campaignIterator.next();
    var campaignName = campaign.getName();
    var biddingStrategy = campaign.bidding().getStrategyType();

    // Debug to check the bidding strategy
    Logger.log("Campaign: " + campaignName + ", Bidding Strategy: " + biddingStrategy);

    if (biddingStrategy == "MAXIMIZE_CONVERSION_VALUE") {
      // Set the target ROAS
      campaign.bidding().setTargetRoas(TARGET_ROAS);
      Logger.log("Target ROAS set to " + TARGET_ROAS + " for campaign: " + campaignName);
    }
  }

  // Get all Performance Max campaigns
  var pMaxCampaignIterator = AdsApp.performanceMaxCampaigns().get();
  while (pMaxCampaignIterator.hasNext()) {
    var pMaxCampaign = pMaxCampaignIterator.next();
    var pMaxCampaignName = pMaxCampaign.getName();
    var pMaxBiddingStrategy = pMaxCampaign.bidding().getStrategyType();

    // Debug to check the bidding strategy
    Logger.log("Performance Max Campaign: " + pMaxCampaignName + ", Bidding Strategy: " + pMaxBiddingStrategy);

    if (pMaxBiddingStrategy == "MAXIMIZE_CONVERSION_VALUE") {
      // Set the target ROAS
      pMaxCampaign.bidding().setTargetRoas(TARGET_ROAS);
      Logger.log("Target ROAS set to " + TARGET_ROAS + " for Performance Max campaign: " + pMaxCampaignName);
    }
  }
}

When I click on Preview to see what the script does, it ends up in an infinite loop. In the logs, I see that the target ROAS was changed in 3 campaigns:

28/07/2024 16:26:00 Campaign: XXXXXXX - dsa, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE 28/07/2024 16:26:00 Target ROAS set to 1 for campaign: XXXXXX - dsa 28/07/2024 16:26:01 Performance Max Campaign: XXXXXXX - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE 28/07/2024 16:26:01 Target ROAS set to 1 for Performance Max campaign: XXXXX - pmax 28/07/2024 16:26:02 Performance Max Campaign: XXXXXXX - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE

Actually, in the section with the changes made, I see that the changes have been correctly applied to all campaigns.

However, the script keeps running infinitely and logs show only 3 campaigns edit.

Is there a bug?

Thank you,
Salvatore

Google Ads Scripts Forum Advisor

unread,
Jul 29, 2024, 8:51:59 AM7/29/24
to adwords...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

We have tested the below code from our end, and the script is not running an infinite loop. In order to replicate your script from our end, share with us the following details:

  • Google Ads account ID or CID
  • Name of the affected script
This message is in relation to case "ref:!00D1U01174p.!5004Q02vEHEn:ref" (ADR-00259931)

Thanks,
 
Google Logo Google Ads Scripts Team


Salvatore Aranzulla

unread,
Jul 30, 2024, 4:39:18 AM7/30/24
to Google Ads Scripts Forum

Good morning,

Could you please test the script on my account 216-776-7109? The script is called "bug-infinite-loop". If you try to click the "Preview" button, you will see the following:

  • In the "Changes" tab, all campaigns are correctly updated as target ROAS.
  • In the "Logs" tab, all logs are shown except for the log "Target ROAS has been updated for all managed accounts", which marks the end of the process, and you will see that the execution continues indefinitely.

I am also attaching a video below:

https://drive.google.com/file/d/1DUMIB7NG3btRZr8S4DJktcHBPCPVA92l/view?usp=sharing

Thank you,
Salvatore

Salvatore Aranzulla

unread,
Aug 2, 2024, 3:16:09 AM8/2/24
to Google Ads Scripts Forum
Hi,

Do you have any updates on this? 

Nils Rooijmans

unread,
Aug 2, 2024, 4:04:23 AM8/2/24
to Google Ads Scripts Forum
to debug, try logging the number of campaigns retrieved via your selector:


  var pMaxCampaignIterator = AdsApp.performanceMaxCampaigns().get();
  console.log(``Nr of campaigns selected: {$pMaxCampaignIterator.totalNumEntities()});

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/

Salvatore Aranzulla

unread,
Aug 2, 2024, 10:13:27 AM8/2/24
to Google Ads Scripts Forum
Why this is marked as abuse? It has been marked as abuse.
Report not abuse

Thank you so much for the suggestion. I have modified:

  // Get all Performance Max campaigns
  var pMaxCampaignIterator = AdsApp.performanceMaxCampaigns().get();
  Logger.log('Nr of Pmax campaigns selected: ' + pMaxCampaignIterator.totalNumEntities());

  while (pMaxCampaignIterator.hasNext()) {
    var pMaxCampaign = pMaxCampaignIterator.next();
    var pMaxCampaignName = pMaxCampaign.getName();
    var pMaxBiddingStrategy = pMaxCampaign.bidding().getStrategyType();

    // Debug to check the bidding strategy
    Logger.log("Performance Max Campaign: " + pMaxCampaignName + ", Bidding Strategy: " + pMaxBiddingStrategy);

    if (pMaxBiddingStrategy == "MAXIMIZE_CONVERSION_VALUE") {
      // Set the target ROAS
      pMaxCampaign.bidding().setTargetRoas(TARGET_ROAS);
      Logger.log("Target ROAS set to " + TARGET_ROAS + " for Performance Max campaign: " + pMaxCampaignName);
    }
  }

What I see on the last log is:

02/08/2024 14:21:16 Nr of non-Pmax campaigns selected: 1
02/08/2024 14:21:16 Campaign: xxxxx.com - dsa, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:16 Target ROAS set to 100 for campaign: ilmiglioracquisto.com - dsa
02/08/2024 14:21:17 Nr of Pmax campaigns selected: 1
02/08/2024 14:21:18 Performance Max Campaign: xxxx.com - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:18 Target ROAS set to 100 for Performance Max campaign: xxxxx.com - pmax
02/08/2024 14:21:18 Nr of non-Pmax campaigns selected: 0
02/08/2024 14:21:22 Nr of Pmax campaigns selected: 1
02/08/2024 14:21:22 Performance Max Campaign: xxxxx.fr - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:22 Target ROAS set to 100 for Performance Max campaign: xxxxxx.fr - pmax
02/08/2024 14:21:22 Nr of non-Pmax campaigns selected: 0
02/08/2024 14:21:23 Nr of Pmax campaigns selected: 1

Does this mean that the Google Ads Script bug might be in pMaxCampaignIterator.hasNext() which selects a campaign?

The strange thing is that if I open the Changes tab, I see all the campaigns correctly modified as ROAS, while the script runs indefinitely.

Do you have any suggestions?

Thank you, Salvatore

Salvatore Aranzulla

unread,
Aug 2, 2024, 10:13:31 AM8/2/24
to Google Ads Scripts Forum
Why this is marked as abuse? It has been marked as abuse.
Report not abuse

Thank you so much for the suggestion. I have modified:


// Get all Performance Max campaigns
var pMaxCampaignIterator = AdsApp.performanceMaxCampaigns().get();
Logger.log('Nr of Pmax campaigns selected: ' + pMaxCampaignIterator.totalNumEntities());
while (pMaxCampaignIterator.hasNext()) {
    var pMaxCampaign = pMaxCampaignIterator.next();
    var pMaxCampaignName = pMaxCampaign.getName();
    var pMaxBiddingStrategy = pMaxCampaign.bidding().getStrategyType();

    // Debug to check the bidding strategy
    Logger.log("Performance Max Campaign: " + pMaxCampaignName + ", Bidding Strategy: " + pMaxBiddingStrategy);

    if (pMaxBiddingStrategy == "MAXIMIZE_CONVERSION_VALUE") {
        // Set the target ROAS
        pMaxCampaign.bidding().setTargetRoas(TARGET_ROAS);
        Logger.log("Target ROAS set to " + TARGET_ROAS + " for Performance Max campaign: " + pMaxCampaignName);
    }
}

What I see on the last log is:

02/08/2024 14:21:16 Nr of non-Pmax campaigns selected: 1
02/08/2024 14:21:16 Campaign: xxxxx.com - dsa, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:16 Target ROAS set to 100 for campaign: ilmiglioracquisto.com - dsa
02/08/2024 14:21:17 Nr of Pmax campaigns selected: 1
02/08/2024 14:21:18 Performance Max Campaign: xxxx.com - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:18 Target ROAS set to 100 for Performance Max campaign: xxxxx.com - pmax
02/08/2024 14:21:18 Nr of non-Pmax campaigns selected: 0
02/08/2024 14:21:22 Nr of Pmax campaigns selected: 1
02/08/2024 14:21:22 Performance Max Campaign: xxxxx.fr - pmax, Bidding Strategy: MAXIMIZE_CONVERSION_VALUE
02/08/2024 14:21:22 Target ROAS set to 100 for Performance Max campaign: xxxxxx.fr - pmax
02/08/2024 14:21:22 Nr of non-Pmax campaigns selected: 0
02/08/2024 14:21:23 Nr of Pmax campaigns selected: 1


Does this mean that the Google Ads Script bug might be in pMaxCampaignIterator.hasNext() which selects a campaign?

The strange thing is that if I open the Changes tab, I see all the campaigns correctly modified as ROAS, while the script runs indefinitely:

https://drive.google.com/file/d/1DUMIB7NG3btRZr8S4DJktcHBPCPVA92l/view?usp=sharing

Do you have any suggestions?

Thank you, 

Salvatore

Shikata

unread,
Jan 31, 2025, 5:00:50 AM1/31/25
to Google Ads Scripts Forum

Hi, I am experiencing the same issue. 

The preview keeps loading indefinitely and never completes (mm:ss ...showing the elapsed time).

Has this issue been resolved?


2024年8月2日金曜日 17:04:23 UTC+9 Nils Rooijmans:

Google Ads Scripts Forum

unread,
Feb 3, 2025, 6:03:18 PM2/3/25
to Google Ads Scripts Forum

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

In order to assist you further, kindly provide us with the following details:
  • Google Ads account ID/CID
  • Name of the affected script
  • Shareable spreadsheet link if you are using any in the script. You may refer to this article to share a file publicly

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

Thanks,
Google Ads Scripts support team
Reply all
Reply to author
Forward
0 new messages