Builders

Standardmäßig werden Entitäten in Google Ads-Skripts mit Generatoren erstellt. Mit Generatoren können Sie eine Google Ads-Entität synchron oder asynchron erstellen. Sie können auch prüfen, ob der Vorgang erfolgreich war oder nicht, und je nach Ergebnis entsprechende Maßnahmen ergreifen. Das folgende Code-Snippet zeigt, wie Sie ein Keyword mit einem Builder erstellen.

// Retrieve your ad group.
let adGroup = AdsApp.adGroups().get().next();

// Create a keyword operation.
let keywordOperation = adGroup.newKeywordBuilder()
    .withCpc(1.2)
    .withText("shoes")
    .withFinalUrl("http://www.example.com/shoes")
    .build();

// Optional: examine the outcome. The call to isSuccessful()
// will block until the operation completes.
if (keywordOperation.isSuccessful()) {
  // Get the result.
  let keyword = keywordOperation.getResult();
} else {
  // Handle the errors.
  let errors = keywordOperation.getErrors();
}

Jedes Element, das mit Google Ads-Skripts erstellt werden kann, verwendet dieses Builder-Muster.

Hinweise zur Leistung

Standardmäßig werden Google Ads-Skripts asynchron ausgeführt. Auf diese Weise können Skripts Ihre Vorgänge als Batches gruppieren und eine hohe Leistung erzielen. Wenn Sie jedoch Operation-Methoden wie isSuccessful() und getResult() aufrufen, wird durch Google Ads-Skripts erzwungen, dass die Liste der ausstehenden Vorgänge geleert wird. Das kann zu Leistungseinbußen führen. Erstellen Sie stattdessen ein Array für die Operationen und durchlaufen Sie dieses Array, um die Ergebnisse abzurufen.

Schlechte Leistung Gute Leistung
for (let i = 0; i < keywords.length; i++)
  let keywordOperation = adGroup
    .newKeywordBuilder()
    .withText(keywords[i])
    .build();

  // Bad: retrieving the result in the same
  // loop that creates the operation
  // leads to poor performance.
  let newKeyword =
      keywordOperation.getResult();
  newKeyword.applyLabel("New keywords”);
}
// Create an array to hold the operations
let operations = [];

for (let i = 0; i < keywords.length; i++) {
  let keywordOperation = adGroup
    .newKeywordBuilder()
    .withText(keywords[i])
    .build();
  operations.push(keywordOperation);
}

// Process the operations separately. Allows
// Google Ads scripts to group operations into
// batches.
for (let i = 0; i < operations.length; i++) {
  let newKeyword = operations[i].getResult();
  newKeyword.applyLabel("New keywords”);
}