According to Google Script best practices, it is recommended to store operations in an array and then call the methods once all the operations have been constructed. This helps minimize response time each time a service is called.
For example, let's consider AdWords:
Script A
var adGroupBuilder = campaign.newAdGroupBuilder();
for (i = 0; i < 1000; i++) {
var adGroupOperation = adGroupBuilder
.withName(i.toString())
.build();
}
var adGroups = adGroupOperation.getResult(); // method call
This script executes in less than a second.
Script B
var adGroupBuilder = campaign.newAdGroupBuilder();
for (i = 0; i < 1000; i++) {
var adGroupOperation = adGroupBuilder
.withName(i.toString())
.build();
var adGroups = adGroupOperation.getResult(); // method call
}
This script executes in almost 6 minutes.
In AdWords, an AdGroup
can have multiple Ads
, and a corresponding AdGroup
must be created for each Ad
.
I am working on a Google Script that needs to convert about 15,000 entities from an external API into AdGroups
with matching Ads
. Making individual calls to the services for each entity results in a timeout error. Hence, I plan to make 15 calls with an array of 1000 entities each.
I want to know if it is possible to include Ads
in the array of operations called by the getResults()
method, or if the AdGroup
object has to be instantiated before creating an Ad
for it?
If not, I might have no choice but to make separate calls to AdWords services for each entity I want to turn into an Ad
, significantly increasing response time and requiring a different approach.