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

Storing values in an array when checkboxes are selected within an ng-repeat loop in AngularJS

I am facing a situation where I need to populate an array with values when a checkbox is checked within an ng-repeat iteration. <li ng-repeat="player in team.players"> <div class="row"> <div class="col-md-3 m-t-xs"> <inp ...

Error: The EJS compiler encountered a SyntaxError due to an unexpected token || in the show component file located at /var/www/html

I'm currently working on a project inspired by Colt Steele's YelpCamp creation during his Udemy Web Dev Bootcamp. Everything was going smoothly until I tried to refactor some code towards the end of the course using YouTube tutorials. Now, whenev ...

Full replacement of a cell within an HTML table

I have a scenario like the following: <table id="myTable"> <tr> <td class="1">cell 1</td> <td class="2">cell 2</td> </tr> <tr> <td class="3">cell 3</td> &l ...

Ensure the footer remains fixed while scrolling

For the past day, I've been tackling a challenge with my online shop project. On the specific item's page, I want to include a fixed [add to cart] button as a footer initially, which then becomes sticky when scrolling to a certain point. You can ...

Tips for saving and accessing Shopping Cart items using localstorage

As I develop a shopping cart for an e-commerce site, I aim to utilize browser localstorage to store the products. The functions that have been added to my code include: - addToCart(): triggered when the "Add to Cart" button is clicked. - addProduct(): ...

A peaceful WCF service initiates a client callback whenever a server update occurs

In the process of developing a WCF RESTFUL service on top of a c++ console application, I am confronted with an issue. The client accesses this c++ application through my restful wcf service using a browser. Every time there is an update received by my w ...

A function injected into a constructor of a class causes an undefined error

As I delve into learning about utilizing typescript for constructing API's, I have encountered a couple of challenges at the moment. Initially, I have developed a fairly straightforward PostController Class that has the ability to accept a use-case wh ...

Adjust the text size for groupBy and option labels in Material UI Autocomplete without altering the size of the input field

Currently, I am utilizing Material-UI and ReactJS to implement grouped autocomplete functionality. See the code snippet below: import * as React from "react"; import TextField from "@mui/material/TextField"; import Autocomplete from &q ...

The button is converting my text to a string instead of the integer format that I require

Hello everyone, I've been grappling with this button conundrum for the last 45 minutes, and I can't seem to find a solution. I have created three different buttons in my code snippet below. (html) <div class="action"> ...

Sending information to a Flask application using AJAX

Currently, I am working on transferring URLs from an extension to a Flask app. The extension is able to access the current URL of the website. I have set up an AJAX request to connect to Flask, and the connection is successful. However, when trying to send ...

Dealing with undefined properties in Javascript can cause errors

[ { dateTime: '2016-03-30 05:55:53', value: '4.0' }, { dateTime: '2016-03-30 05:55:55', value: '17.0' }, { dateTime: '2016-03-30 05:55:57', value: '17.0' }, { dateTime: '2016-03-30 06:0 ...

Creating a horizontal scroll effect using jQuery when the widths of the items are not

I am working on a jQuery gallery that showcases images in a horizontal layout. Below the images, there are "left" and "right" buttons which allow users to scroll through the pictures. There are many tutorials and plugins available for this type of function ...

Retrieving the class name using jQuery

In my HTML code, there is a div with three classes defined as: <div class = "a b c"> .. </div> My goal is to access only the class b using the .attr() method. Is this achievable? ...

Navigating with Vue.js using programmatic methods while passing props

I am working with a Vue component that includes a prop called 'title' like this: <script> export default { props: ['title'], data() { return { } } } </script> After completing a specific action, I need to pro ...

Access the value of a JSON property, return null if the key is not found, all in one line of JavaScript code

How about a fun analogy in Python: fruits_dict = {"banana": 4, "apple": 3} num_apples = fruits_dict.get("apple", None) num_oranges = fruits_dict.get("orange", None) print(num_apples, num_oranges) The result would be: 3 None If we switch gears to Jav ...

Using JavaScript with Selenium, you can easily clear the input field and send

Here is a question input field. When clicked on, the h3 element with the name will disappear and the input tag will appear for entry of a new name. <td style="width:92%;"> <h3 id="question_tex ...

Manipulating JSON Elements using JavaScript in HTML

My objective is to alternate the visibility between the "prev" and "ext" elements retrieved from the JSON File in the following manner: Here is the link to JS Fiddle The menu bar appears as follows: [ "prev_Person1" "Person1" "ext_Person1" ... "Person2" ...

Issues with the functionality of the submit button (html, js, php)

Seeking assistance with a submit button functionality issue - I have a simple submit button that allows visitors to enter their email address. Upon clicking the button, it should add their email to a CSV file and display a pop-up thank you message. The pr ...

Adding an interactive element to a predetermined collection

CSS <button class='btn' data-next="games" data-prev="games1" data-storage="128" data-graphcard="2" data-processor="i3">Click Here</button> JavaScript. let components = ["storage", "graphcard", "processor"]; const allBtnsToStore = d ...

What is the best way to access query string values using JavaScript?

Is it possible to retrieve query string values without using a plugin in jQuery? If the answer is yes, how can this be accomplished? If not, are there any plugins available that can help with this task? ...