تُعدّ أدوات الإنشاء الطريقة العادية لإنشاء عناصر في نصوص "إعلانات Google" البرمجية. تتيح أدوات الإنشاء إنشاء عنصر في "إعلانات Google" بشكل متزامن أو غير متزامن. يمكنك أيضًا التحقّق مما إذا كانت العملية ناجحة أم لا، واتّخاذ الإجراءات المناسبة وفقًا لنتيجة العملية. يوضّح مقتطف الرمز البرمجي التالي كيفية إنشاء كلمة رئيسية باستخدام أداة إنشاء.
// 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" البرمجية نمط الإنشاء هذا.
اعتبارات الأداء
تنفّذ نصوص "إعلانات Google" البرمجية عملياتها بشكل غير متزامن تلقائيًا. يتيح ذلك للبرامج النصية تجميع عملياتك في دفعات وتحقيق أداء عالٍ. ومع ذلك، يؤدي استدعاء طرق
العملية، مثل
isSuccessful()
و
getResult()
،
إلى إجبار نصوص "إعلانات Google" البرمجية على إفراغ قائمة العمليات المعلّقة، وبالتالي قد يؤدي ذلك إلى
ضعف الأداء. بدلاً من ذلك، أنشئ صفيفًا لتضمين العمليات، ثم كرِّر هذا الصفيف لاسترداد النتائج.
أداء ضعيف | أداء جيد |
---|---|
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"); } |