Những người xây dựng

Trình tạo là cách tiêu chuẩn để tạo các thực thể trong tập lệnh Google Ads. Trình tạo cho phép bạn tạo thực thể Google Ads một cách đồng bộ hoặc không đồng bộ. Bạn cũng có thể kiểm tra xem thao tác có thành công hay không và thực hiện các hành động phù hợp tuỳ thuộc vào kết quả của thao tác đó. Đoạn mã sau đây cho biết cách tạo từ khoá bằng trình tạo.

// 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();
}

Bất kỳ thực thể nào được tạo bằng tập lệnh Google Ads đều được tạo bằng mẫu trình tạo này.

Xem xét hiệu suất

Theo mặc định, tập lệnh Google Ads thực thi các hoạt động của tập lệnh một cách không đồng bộ. Điều này cho phép tập lệnh nhóm các hoạt động thành các nhóm và đạt được hiệu suất cao. Tuy nhiên, việc gọi các phương thức Thao tác như isSuccessful()getResult() sẽ buộc tập lệnh Google Ads xoá danh sách hoạt động đang chờ xử lý, từ đó có thể dẫn đến hiệu suất kém. Thay vào đó, hãy tạo một mảng để lưu giữ các thao tác, sau đó lặp lại mảng đó để truy xuất kết quả.

Hiệu suất kém Hiệu suất tốt
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”);
}