ผู้สร้าง

เครื่องมือสร้างเป็นวิธีมาตรฐานในการสร้างเอนทิตีในสคริปต์ 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 จะดำเนินการแบบอะซิงโครนัส ซึ่งช่วยให้สคริปต์จัดกลุ่มการดำเนินการเป็นชุดๆ และมีประสิทธิภาพสูง อย่างไรก็ตาม การเรียกใช้เมธอด การดำเนินการ เช่น 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");
}