מספנות

בנאים הם הדרך הרגילה ליצור ישויות בסקריפטים של Google Ads. ה-builders מאפשרים ליצור ישות ב-Google Ads באופן סינכרוני או אסינכרוני. בנוסף, אפשר לבדוק אם הפעולה הצליחה או לא, ולבצע את הפעולות המתאימות בהתאם לתוצאה שלה. קטע הקוד הבא מראה איך ליצור מילת מפתח באמצעות builder.

// 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 עושה זאת באמצעות הדפוס הזה של ה-builder.

שיקולי ביצועים

כברירת מחדל, סקריפטים של 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”);
}