Why is this simple script so slow?

347 views
Skip to first unread message

Dean Yates

unread,
Sep 29, 2016, 12:34:59 PM9/29/16
to AdWords Scripts Forum
Hi All, 

I modified this simple script by Russell Savage, it runs error free but it takes an unusual amount of time to execute, can anyone see any issues within the script that could be causing this?

function main() {
  // Let's start by getting all of the adGroups that are active
  var ag_iter = AdWordsApp.adGroups()
  .withCondition("Status = ENABLED")
  .get();
 
  // Then we will go through each one
  while (ag_iter.hasNext()) {
    var ag = ag_iter.next();
    var ad_iter = ag.ads()
      .withCondition("Status = ENABLED")
      .withCondition("CampaignName CONTAINS 'Bulgaria [Aas, Sno] MM'")
      .forDateRange("ALL_TIME")
      .orderBy("Ctr DESC")
      .get();
    var ad_array = new Array();
    while(ad_iter.hasNext()) {
      ad_array.push(ad_iter.next());
    }
    if(ad_array.length > 1) {
      for(var i = 1; i < ad_array.length; i++) {
        ad_array[i].pause(); //or .remove(); to delete them 
      }
    }
  }
}

Tyler Sidell (AdWords Scripts Team)

unread,
Sep 29, 2016, 12:47:29 PM9/29/16
to AdWords Scripts Forum
Hi Dean,

You could speed up the script by removing the ads selector from the while loop.  We do not encourage including selectors inside of tight loops.  You can take a look at our Best Practices for additional steps to take in order to speed up your script.  

Thanks,
Tyler Sidell
AdWords Scripts Team

AussieWeb Conversion

unread,
Sep 29, 2016, 10:23:27 PM9/29/16
to AdWords Scripts Forum
Hi Dean

Why is it pushing the ads into an array in the initial while loop and then cycling though the array to pause/delete the ads?
Why not just pause/delete the ads in the initial while loop?  It's unnecessary overhead to push them into the array.

Regards
Nigel

Trevor

unread,
Oct 3, 2016, 10:44:11 AM10/3/16
to AdWords Scripts Forum
Hi Dean,

You also don't need the first ad group selector. Within your ad iterator, you can simply using .withCondition("AdGroupStatus = ENABLED");

So if you wanted to speed it up I would get rid of the ad group selector as well as the while loop the ad iterator is nested in, and get rid of the for loop for the array. Add the above condition to the ad iterator, and have the while(ad_iter.hasNext()) loop do the pausing right there instead of pushing to the array.

That should speed things up considerably
Reply all
Reply to author
Forward
0 new messages