ผู้สร้าง

Builder เป็นวิธีมาตรฐานในการสร้างเอนทิตีในสคริปต์ Google Ads เครื่องมือสร้างช่วยให้คุณสร้างเอนทิตี Google Ads ได้ทั้งแบบซิงโครนัสและแบบอะซิงโครนัส นอกจากนี้ คุณยังตรวจสอบได้ว่าการดำเนินการสำเร็จหรือไม่ และดำเนินการที่เหมาะสมตามผลลัพธ์ของการดำเนินการ ข้อมูลโค้ดต่อไปนี้ แสดงวิธีสร้างคีย์เวิร์ดโดยใช้เครื่องมือสร้าง

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

เอนทิตีที่สร้างได้โดยใช้สคริปต์ Google Ads จะสร้างโดยใช้รูปแบบบิลเดอร์นี้

ข้อพิจารณาด้านประสิทธิภาพ

โดยค่าเริ่มต้น สคริปต์ Google Ads จะดำเนินการแบบไม่พร้อมกัน ซึ่งจะช่วยให้สคริปต์จัดกลุ่มการดำเนินการเป็นชุด และมีประสิทธิภาพสูง อย่างไรก็ตาม การเรียกใช้เมธอด Operation เช่น isSuccessful() และ getResult() จะบังคับให้สคริปต์ Google Ads ล้างรายการการดำเนินการที่รอดำเนินการ ซึ่งอาจส่งผลให้ประสิทธิภาพไม่ดี แต่ให้สร้างอาร์เรย์เพื่อเก็บการดำเนินการ แล้ววนซ้ำ ในอาร์เรย์นั้นเพื่อดึงข้อมูลผลลัพธ์

ประสิทธิภาพแย่ ประสิทธิภาพดี
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");
}