0
votes

In Google Script it is recommended to store operations in an array, then calling the methods once all the operations has been constructed, to minimize response time every time a service is called.

For example in 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

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
}

Executes in almost 6 minutes.

In AdWords, an AdGroup can have several Ads, and in order to create an Ad there must be a corresponding AdGroup.

I have an external API which returns about 15 000 entities in batches of 1000 each, for which I am developing a Google Script that convert these into AdGroups with corresponding Ads. Obviously, to make that many individual calls to their services results in a timeout error. So I would like to break those down into 15 calls, with an array of 1000 entities each.


Now, I would like to know if there is possible to add Ads into the array of operations that is called by the method getResults(), or if the AdGroup object needs to be instantiated before I can create an Ad for it?

Because if not I see no other option than to make an individual call to the AdWords services for every entity that I want to turn into an Ad, but then the response time would be very high, and a different structure is required.

1

1 Answers

1
votes

I solved it by pushing the operations into an array then iterating through it as suggested here.

Script C

var adGroupBuilder = campaign.newAdGroupBuilder(); 
var operations = [];

for (i = 0; i < 1000; i++) {
  var adGroupOperation = adGroupBuilder
    .withName(i.toString())
    .build();
  operations.push(adGroupOperation);
}

for (var i = 0; i < operations.length; i++) {
  var adGroup = operations[i].getResult();
  var adOperation = adGroup.newTextAdBuilder()
    .withHeadline("headline of ad")
    .withDescription1("first line of ad description")
    .withDescription2("second line of ad description")
    .withDisplayUrl("www.example.com")
    .withDestinationUrl("http://www.example.com")
    .build();
}

var ad = adOperation.getResult();

Which executes in around 8 seconds.


However, I am still not so sure as of why that would be faster as there are still 1000 individual calls to their services with this badboy: operations[i].getResult(). I would assume it has something to do with how Google handles the calls on their end, perhaps through catching?

If anyone could share some light on this it would be appreciated!