מספנות

ה-builders הם הדרך הרגילה ליצור ישויות בסקריפטים של Google Ads. באמצעות ה-builders אפשר ליצור ישות ב-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 נוצרת באמצעות תבנית ה-builder הזו.

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

כברירת מחדל, הסקריפטים של Google Ads מבצעים את הפעולות שלהם באופן אסינכרוני. כך אפשר לקבץ את הפעולות בסקריפטים בקבוצות ולשפר את הביצועים. עם זאת, קריאה ל-methods של 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);
}