Reduce the amount of time it takes for a Google AdWords Script to generate a

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.

Answer №1

My solution involved storing operations in an array and then looping through them, following the guidance provided 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();

This code snippet runs in approximately 8 seconds.


Despite this efficiency, I still ponder why it outperforms making 1000 individual service calls with operations[i].getResult(). Could Google's handling of these calls on their end contribute to this speed, possibly through optimization techniques like caching?

If anyone can shed some light on this, I would greatly appreciate it!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Statement after post is not yielding any result

Below is a javascript function that I'm struggling with: function loginsubmit() { var url = "../php/loginsubmit.php"; var data = ""; ajaxRequest(url, "POST",data , true, insertNewBody); } This function is responsible for sending an ajax ...

Dynamic Image Grid Created Using JavaScript Glitches

My dilemma involves using JQuery to create an HTML grid of images styled with Flex Boxes. The setup works perfectly on desktop, but when viewing it on mobile devices, the images begin to act strangely - jumping, overlapping, and even repeating themselves i ...

The function is invoked numerous times

My issue involves using jQuery to handle the mouseenter and mouseleave events. The problem arises when transitioning from one div to another, causing the events to trigger again. I am looking for a solution to only run these events once per mouse enter and ...

Is jQuery AJAX returning improperly formatted JSON when using the jsonp type?

$('#rn_s').keyup(function() { var rn = $('#rn_s').val(); if(rn.length == 9) { $.ajax({ url: 'http://routingnumbers.info/api/data.json?rn=' + rn, type: 'GET', dataT ...

How to efficiently pass props between components in NextJs

This is the project's file structure: components ├─homepage │ ├─index.jsx ├─location │ ├─index.jsx pages │ ├─location │ │ ├─[id].jsx │ ├─presentation │ │ ├─[id].jsx │ ├─_app.jsx │ ├─index.jsx ...

Automatically submitting forms without having to refresh the page

I have been searching for answers online, but none of them seem to help me. Additionally, I am struggling to grasp the concept of Ajax. I need everything to happen on a single page without any refreshing until the entire form is submitted. <form id=" ...

I am having trouble getting Stripe Elements to function properly within Laravel

I'm currently developing a subscription app using Laravel and trying to integrate Stripe Elements, but I'm encountering issues with getting it to function properly. Below is my form: <form method="POST" action="/subscribe" id="payment-form"& ...

Disable the use of componentWillUnmount in case of an incomplete form

I have implemented a form using redux-form and I am trying to set up a scenario where if any of the form's inputs have content and the user tries to navigate away from the page, a prompt should appear. My goal is to prevent the page from being unmoun ...

Is it possible to stop JSON.stringify from invoking the toJSON method?

JSON.stringify provides a way for objects to customize their serialization process by defining a function named toJSON. Here is an excerpt from the MDN documentation: toJSON() behavior If an object being converted to a string has a method called toJSON, ...

Incorporate the title of the article into the URL instead of using the ID

I have a collection of hyperlinks leading to various articles. Here is an example: <a ui-sref="post-view({ id: post._id })">...</a> When clicked, these links generate hrefs like this: href="/blog/546cb8af0c0ec394d7fbfdbf", effectively taking ...

The function initiates without delay upon meeting the specified condition

I am looking to trigger a function automatically upon certain dynamically changing conditions in my JavaScript code. I attempted using the document.body.onload method, but it did not work as expected. document.body.onload = myFunction() function myFunct ...

Tips for choosing a particular list item using jQuery: retrieve the attribute value of 'value' and showcase it on the webpage

I have a task that requires the following: Implement an event listener so that when you click on any list item, the value of its "value" attribute will be shown next to this line. In my HTML, I have an ordered list with 8 list items. The values range fro ...

Adding a half circle connector in HTML can be accomplished by using SVG (Scal

My task is to replicate the construction shown in this image: I have written the entire code but I am unsure how to include a half circle next to the full circle and connect it with a line connector. Here is my code: .ps-timeline-sec { position: rela ...

Differences between encoding URL variables in HREF and using JS window.location for onclick events

For some reason, this particular hyperlink is not functioning properly. I have a Javascript redirect (window.opener.location) where I pass several variables through the URL. The problem arises when these variables contain apostrophes. In PHP, I am using UR ...

How can you use yargs (npm package) to generate an error message when a command is not recognized?

Is it possible to have yargs with 2 commands? yargs .command('test-all','',handler) .command('test-file','',handler) .argv When the user inputs: node myapp.js other-command No error is thrown by yargs. What steps s ...

Tips for excluding certain parameters in the jslint unparam block

While developing my angular app, I encountered an issue with jslint flagging an unused parameter. Typically in angular, the "$scope" is required as the first parameter in your controller definition. In my case, I prefer using the "this" keyword instead of ...

Implementing dynamic props in Vue2 component by passing arbitrary named variables

Having recently delved into Vue, I am facing a challenge that has left me scratching my head after consulting the documentation: I am struggling to pass an arbitrarily named variable as a prop to a component instance. As per my understanding, props serve ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

Having trouble getting my angular form validation to function properly

Even though I disabled Bootstrap's validation while using Angular, the validation for every input field still doesn't work. It seems like I have everything set up correctly. My code looks like this below with no success on input validation: < ...

A guide on using Jest.js to test labels within Vue 3 Quasar components by utilizing a forEach loop

Within my Vue Quasar component, a badge is implemented as shown below: <q-badge :color="green" text-color="white" :label="`${value.toFixed(2)}%`" /> The corresponding script is structured like this: <scri ...