什麼是建築商?

您可以使用建置器來新增實體。 每個父物件(例如 Campaign)都包含用於取得用於新增子實體的建置器物件的方法。 例如,若要將廣告群組新增至行銷活動,您需要呼叫 Campaign 物件的方法 newAdGroupBuilder

建置器物件包含您用來設定實體屬性值的方法。 例如,若要指定關鍵字的每次點選成本,您會使用方法 withCpc 。 設定實體的所有屬性值之後,您可以呼叫 build 方法來建立實體。 建置程序是非同步程序,其中要求會與其他建置要求一起排入佇列,並批次處理。 批次處理的請求將在指令碼終止之前完成。

若要判斷建置要求是否成功,您可以查看記錄檔,或使用方法傳回的 build 作業物件。 例如, AdGroupBuilder 會傳回 AdGroupOperation。 您可以呼叫任何作業物件的方法 (isSuccessfulgetResultgetErrors) 來判斷指令碼是否成功建立實體。 但是呼叫這些方法時有效能考量, (請參閱 效能考量) 。

下列範例在概念上示範如何使用建置器和 operation 物件建立關鍵字。 您可能只應該在建立單一實體 (或幾個) 時使用此流程。

    // Gets the first ad group in the account.
    var adGroup = AdsApp.adGroups().get().next();

    // Use the 'with' methods to specify the keyword's property values.
    // The .build() method adds the build request to the build queue.
    var keywordOperation = adGroup.newKeywordBuilder()
        .withCpc(1.2)
        .withText("shirts")
        .withFinalUrl("https://www.contoso.com/shirts")
        .build();

    // Call isSuccessful() to determine if the build succeeded.
    // Calling any of the operation object's method processes the
    // build request immediately. 
    if (keywordOperation.isSuccessful()) {
        // You only need to call getResult if you need to access
        // the new keyword entity.
        var keyword = keywordOperation.getResult();
    } else {
        // Handle the errors.
        for (var error of keywordOperation.getErrors()) {
            Logger.log(`${error}\n`);
        }
    }

效能考量

為了改善效能,指令碼會批次處理建置要求。 如果您呼叫建置請求的作業方法,它會強制指令碼立即處理佇列的建置請求,從而抵消任何效能提升。 如果您要建立多個實體,請勿在用於建置實體的相同迴圈中執行作業方法。 這會導致效能不佳,因為一次只處理一個實體。 相反地,請建立作業陣列,並在建置迴圈之後處理它們。

執行此動作

    // An array to hold the operations, so you 
    // can process them after all the entities are queued.
    var operations = []; 

    // Create all the new entities.
    for (var i = 0; i < keywords.length; i++) {
        var keywordOperation = AdsApp.adGroups().get().next()
          .newKeywordBuilder()
          .withText(keywords[i])
          .build();
        operations.push(keywordOperation);
    }

    // Now call the operation method so the build requests
    // get processed in batches.
    for (var i = 0; i < operations.length; i++) {
        var newKeyword = operations[i].getResult();
    }

切莫這麼做

    for (var i = 0; i < keywords.length; i++) {
        var keywordOperation = AdsApp.adGroups().get().next()
          .newKeywordBuilder()
          .withText(keywords[i])
          .build();

        // Don't get results in the same loop that creates
        // the entity because Scripts then only processes one
        // entity at a time.
        var newKeyword = keywordOperation.getResult();
    }

以下是建築商名單。

後續步驟