מספנות

השימוש ב-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 עושה זאת באמצעות תבנית הבנייה הזו.

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

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