Ads Manager 스크립트

Google Ads 스크립트의 AdsManagerApp 클래스를 사용하면 관리자 계정에 연결된 계정을 관리할 수 있습니다. 계정마다 별도의 스크립트를 만드는 대신 단일 스크립트를 통해 모든 광고주 계정을 관리할 수 있습니다.

계정 목록 가져오기

accounts 메서드를 사용하여 관리자 계정에 속한 계정을 검색할 수 있습니다. 예를 들면 다음과 같습니다.

const accountSelector = AdsManagerApp.accounts()
    .withCondition('customer_client.descriptive_name = "My Account"');

const accountIterator = accountSelector.get();

가져올 수 있는 계정에는 몇 가지 제한사항이 있습니다.

  • 여러 수준의 계층 구조가 있는 경우 관리자 계정을 가져올 수 없습니다. 고객 계정만 선택할 수 있습니다.
  • 기본적으로 해지, 해지, 정지된 계정은 반환되지 않습니다. customer_client.status에 다른 필터를 지정하여 withCondition를 호출하여 이 동작을 재정의할 수 있습니다.

accounts 호출은 기본적으로 관리자 계정 계층 구조에 있는 모든 고객 계정 목록을 검색합니다. ManagedAccountSelector 클래스의 withLimit 메서드를 사용하여 스크립트가 검색하는 계정 수를 제한할 수 있습니다. 또 다른 방법은 withIds 메서드를 사용하여 고객 ID로 계정을 선택하는 것입니다.

// Hyphens in the account ID are optional.
const accountSelector = AdsManagerApp.accounts()
    .withIds(['123-456-7890', '234-567-8901', '345-678-9012']);

고객 계정 관리

클라이언트 계정을 가져온 후에는 반복자의 hasNextnext 메서드를 사용하여 계정 전체에서 반복할 수 있습니다. 실행 컨텍스트를 클라이언트 계정으로 전환하려면 select 메서드를 사용해야 합니다. 고객 계정을 선택한 후에는 다른 계정을 명시적으로 선택할 때까지 추가 API 호출이 고객 계정에 적용됩니다.

// Keep track of the manager account for future reference.
const managerAccount = AdsApp.currentAccount();

// Select your accounts
const accountIterator = AdsManagerApp.accounts()
// ... Write some logic here to select the accounts you want using
// withCondition or withIds

// Iterate through the list of accounts
for (const account of accountIterator) {
  // Select the client account.
  AdsManagerApp.select(account);

  // Select campaigns under the client account
  const campaignIterator = AdsApp.campaigns().get();

  // Operate on client account
  ...
}

여러 계정에서 동시에 작업

Google Ads 스크립트를 사용하면 ManagedAccountSelector 클래스의 executeInParallel 메서드를 사용하여 여러 클라이언트 계정에서 동시에 작업할 수 있습니다. executeInParallel 메서드에는 다음과 같은 서명이 있습니다.

function executeInParallel(functionName, optionalCallbackFunctionName, optionalInput);

executeInParallel 메서드는 ManagedAccountSelector와 일치하는 각 ManagedAccount에서 functionName로 지정된 함수를 실행합니다. 모든 계정이 처리되면 콜백 함수(optionalCallbackFunctionName로 지정된 경우)가 한 번 실행되며 추가 처리를 위한 인수로 ExecutionResult 객체 목록을 전달합니다. 일반적인 사용법은 다음과 같습니다.

function main() {
  const accountSelector = AdsManagerApp.accounts()
      .withLimit(50)
      .withCondition('customer_client.currency_code = "USD"');

  accountSelector.executeInParallel("processClientAccount", "afterProcessAllClientAccounts");
}

function processClientAccount() {
  const clientAccount = AdsApp.currentAccount();

  // Process your client account here.
  ...

  // optionally, return a result, as text.
  return "";
}

function afterProcessAllClientAccounts(results) {
  for (const result of results) {
    // Process the result further
    ...
  }
}

functionName로 지정된 함수는 선택적으로 문자열 인수 (optionalInput)를 수락할 수 있습니다. 이 매개변수를 사용하여 executeInParallel에서 호출하는 모든 병렬 메서드에 추가 매개변수를 전달할 수 있습니다.

function main() {
  const accountSelector = AdsManagerApp.accounts().withIds([1234567890, 3456787890]);
  const sharedParameter = "INSERT_SHARED_PARAMETER_HERE";
  accountSelector.executeInParallel("processClientAccount", null, sharedParameter);
}

function processClientAccount(sharedParameter) {
  // Process your client account here.
  ...
}

계정별 설정이 포함된 자바스크립트 구성 객체를 전달하려면 먼저 JSON.stringify 메서드를 사용하여 이 객체를 문자열로 변환할 수 있습니다.

function main() {
  ...
  const accountFlags = {
    '1234567890': {
       'label': 'Brand 1 campaigns',
     },
    '3456787890': {
       'label': 'Brand 2 campaigns',
     }
  };
  accountSelector.executeInParallel("processClientAccount", null,
      JSON.stringify(accountFlags));
  ...
}

function processClientAccount(sharedParameter) {
  const accountFlags = JSON.parse(sharedParameter);
  // Process your client account here.
  ...
}

functionName로 지정된 함수는 JSON.stringify를 통해 객체 대신 문자열을 반환할 수도 있습니다.

function processClientAccount() {
  ...
  const jsonObj = {value: 10, list: [1,2,3,4,5,6], name: "Joe Smith"};
  return JSON.stringify(jsonObj);
}

반환된 값은 ExecutionResult 객체 목록의 콜백 함수에 전달됩니다. 함수에서 JSON 문자열을 반환한 경우 JSON.parse 메서드를 사용하여 이 문자열을 자바스크립트 객체로 다시 변환할 수 있습니다.

function callbackFunctionName(results) {
  for (var i = 0; i < results.length; i++) {
    var resultObj = JSON.parse(results[i].getReturnValue());
  }
}

executeInParallel 메서드는 최대 50개의 accounts에서 작동하므로 스크립트가 검색할 계정 수를 제한하려면 자체 제한사항을 구현해야 합니다. ManagedAccountSelector 클래스의 withLimit 또는 withIds 메서드를 사용하여 스크립트가 검색하는 계정 수를 제한할 수 있습니다.

실행 시간 제한

Ads Manager 스크립트 실행 시간 제한에 대한 자세한 내용은 이 페이지를 참조하세요.