השימוש ב-Builders הוא הדרך הרגילה ליצור ישויות בסקריפטים של 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 משתמשת בתבנית ה-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"); } |