빌더

빌더는 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 스크립트를 사용하여 만들 수 있는 모든 항목은 이 빌더 패턴을 사용하여 만들 수 있습니다.

성능에 대한 고려사항

기본적으로 Google Ads 스크립트는 작업을 비동기식으로 실행합니다. 이를 통해 스크립트에서 작업을 배치로 그룹화하여 고성능을 달성할 수 있습니다. 그러나 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”);
}